Class: Crubyflie::CRTPPacket

Inherits:
Object
  • Object
show all
Defined in:
lib/crubyflie/driver/crtp_packet.rb

Overview

A data packet. Raw packet data is sent to the USB driver Some related docs: wiki.bitcraze.se/ projects:crazyflie:firmware:comm_protocol#serial_port

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header = 0, payload = []) ⇒ CRTPPacket

Initialize a package with a header and data

Parameters:

  • header (Integer) (defaults to: 0)

    represents an 8 bit header

  • payload (Array) (defaults to: [])

    @see #set_data



92
93
94
95
96
# File 'lib/crubyflie/driver/crtp_packet.rb', line 92

def initialize(header=0, payload=[])
    modify_header(header)
    @data = payload || []
    @size = data.size #+ 1 # header. Bytes
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



88
89
90
# File 'lib/crubyflie/driver/crtp_packet.rb', line 88

def channel
  @channel
end

#dataObject

Returns the value of attribute data.



88
89
90
# File 'lib/crubyflie/driver/crtp_packet.rb', line 88

def data
  @data
end

#headerObject (readonly)

Returns the value of attribute header.



88
89
90
# File 'lib/crubyflie/driver/crtp_packet.rb', line 88

def header
  @header
end

#portObject (readonly)

Returns the value of attribute port.



88
89
90
# File 'lib/crubyflie/driver/crtp_packet.rb', line 88

def port
  @port
end

#sizeObject (readonly)

Returns the value of attribute size.



88
89
90
# File 'lib/crubyflie/driver/crtp_packet.rb', line 88

def size
  @size
end

Class Method Details

.unpack(data) ⇒ Object

Creates a packet from a raw data array



127
128
129
130
131
132
# File 'lib/crubyflie/driver/crtp_packet.rb', line 127

def self.unpack(data)
    return CRTPPacket.new() if !data.is_a?(Array) || data.empty?()
    header = data[0]
    data = data[1..-1]
    CRTPPacket.new(header, data)
end

Instance Method Details

#data_repackString

Pack the data of the packet into unsigned chars when needed

Returns:

  • (String)

    binary data



142
143
144
# File 'lib/crubyflie/driver/crtp_packet.rb', line 142

def data_repack
    return @data.pack('C*')
end

#modify_header(header = nil, port = nil, channel = nil) ⇒ Object

Modify the full header, or the channel or the port

Parameters:

  • header (Integer) (defaults to: nil)

    a new full header. Prevails over the rest

  • port (Integer) (defaults to: nil)

    a new port (4 bits)

  • channel (Integer) (defaults to: nil)

    a new channel (2 bits)



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/crubyflie/driver/crtp_packet.rb', line 109

def modify_header(header=nil, port=nil, channel=nil)
    if header
        @header = header
        @channel = header & 0b11 # lowest 2 bits of header
        @port = (header >> 4) & 0b1111    # bits 4-7
        return
    end
    if channel
        @channel = channel & 0b11 # 2 bits
        @header = (@header & 0b11111100) | @channel
    end
    if port
        @port = (port & 0b1111)  # 4 bits
        @header = (@header & 0b00001111) | @port << 4
    end
end

#packArray

Concat the header and the data and return it

Returns:

  • (Array)

    header concatenated with data



136
137
138
# File 'lib/crubyflie/driver/crtp_packet.rb', line 136

def pack
    [@header].concat(@data)
end