Class: C0f::CANPacket

Inherits:
Object
  • Object
show all
Defined in:
lib/c0f/can_packet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCANPacket

Returns a new instance of CANPacket.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/c0f/can_packet.rb', line 4

def initialize
  @id = 0
  @dlc = 0
  @data = Array.new
  @sent = nil
  # Statistical items
  @count = 0
  @interval = 0
  @sent_history = []
  @deltas = []
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



3
4
5
# File 'lib/c0f/can_packet.rb', line 3

def count
  @count
end

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/c0f/can_packet.rb', line 3

def data
  @data
end

#deltasObject

Returns the value of attribute deltas.



3
4
5
# File 'lib/c0f/can_packet.rb', line 3

def deltas
  @deltas
end

#dlcObject

Returns the value of attribute dlc.



3
4
5
# File 'lib/c0f/can_packet.rb', line 3

def dlc
  @dlc
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/c0f/can_packet.rb', line 3

def id
  @id
end

#intervalObject

Returns the value of attribute interval.



3
4
5
# File 'lib/c0f/can_packet.rb', line 3

def interval
  @interval
end

#sentObject

Returns the value of attribute sent.



3
4
5
# File 'lib/c0f/can_packet.rb', line 3

def sent
  @sent
end

#sent_historyObject

Returns the value of attribute sent_history.



3
4
5
# File 'lib/c0f/can_packet.rb', line 3

def sent_history
  @sent_history
end

Instance Method Details

#avg_deltaFloat

Returns Average delta between sent intevals.

Returns:

  • (Float)

    Average delta between sent intevals



52
53
54
55
# File 'lib/c0f/can_packet.rb', line 52

def avg_delta
  return 0.0 if @deltas.size == 0
  @deltas.inject{ |sum, el| sum + el }.to_f / @deltas.size
end

#parse(line) ⇒ Boolean

Parses CANDump line into CANPacket

Parameters:

  • line (String)

    CANDump line

Returns:

  • (Boolean)

    true if successful



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/c0f/can_packet.rb', line 19

def parse(line)
         # (1398128223.815980) can0 13A#0000000000000028
  if line=~/\((\d+\.\d+)\) [s]?[l|v]?can\d+ (\w+)#(\w+)/ then
    @sent = $1.to_f
    @id = $2
    @data = $3.scan(/../).map(&:hex)
    @dlc = @data.size
    @count = 1
	return true
          # (1421010803.828887)  slcan0  128   [3]  A0 00 03
  elsif line=~/\((\d+\.\d+)\)\s+[s]?[l|v]?can\d+\s+(\w+)\s+\[(\d+)\]  (.+)/ then
    @sent = $1.to_f
    @id = $2
    @dlc = $3.to_i
    @data = $4.gsub(' ','')
    @data = @data.scan(/../).map(&:hex)
    @count = 1
    return true
  end
  false
end

#update(pkt) ⇒ Object

Updates stats when the ID matches for a packet

Parameters:



43
44
45
46
47
48
49
# File 'lib/c0f/can_packet.rb', line 43

def update(pkt)
  return if not pkt.id == @id
  @count += 1
  @sent_history.push @sent
  @deltas.push  (pkt.sent - @sent)
  @sent = pkt.sent
end