Class: Crubyflie::Crazyradio

Inherits:
Object
  • Object
show all
Includes:
CrazyradioConstants
Defined in:
lib/crubyflie/crazyradio/crazyradio.rb

Overview

Driver for the USB crazyradio dongle

Constant Summary collapse

DEFAULT_SETTINGS =

Default settings for Crazyradio

{
    :data_rate      => DR_2MPS,
    :channel        => 2,
    :cont_carrier   => false,
    :address        => [0xE7] * 5, #5 times 0xE7
    :power          => P_0DBM,
    :arc            => 3,
    :ard_bytes      => 32 # 32
}

Constants included from CrazyradioConstants

Crubyflie::CrazyradioConstants::ACK_ENABLE, Crubyflie::CrazyradioConstants::CRAZYRADIO_PRODUCT_ID, Crubyflie::CrazyradioConstants::CRAZYRADIO_VENDOR_ID, Crubyflie::CrazyradioConstants::DEFAULT_CHANNEL, Crubyflie::CrazyradioConstants::DR_1MPS, Crubyflie::CrazyradioConstants::DR_250KPS, Crubyflie::CrazyradioConstants::DR_2MPS, Crubyflie::CrazyradioConstants::LAUNCH_BOOTLOADER, Crubyflie::CrazyradioConstants::P_0DBM, Crubyflie::CrazyradioConstants::P_M12DBM, Crubyflie::CrazyradioConstants::P_M18DBM, Crubyflie::CrazyradioConstants::P_M6DBM, Crubyflie::CrazyradioConstants::SCANN_CHANNELS, Crubyflie::CrazyradioConstants::SET_CONT_CARRIER, Crubyflie::CrazyradioConstants::SET_DATA_RATE, Crubyflie::CrazyradioConstants::SET_RADIO_ADDRESS, Crubyflie::CrazyradioConstants::SET_RADIO_ARC, Crubyflie::CrazyradioConstants::SET_RADIO_ARD, Crubyflie::CrazyradioConstants::SET_RADIO_CHANNEL, Crubyflie::CrazyradioConstants::SET_RADIO_POWER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device = nil, settings = {}) ⇒ Crazyradio

Initialize a crazyradio

Parameters:

  • device (LIBUSB::Device) (defaults to: nil)

    A crazyradio USB device

  • settings (Hash) (defaults to: {})

    Crazyradio settings. @see #DEFAULT_SETTINGS

Raises:



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/crubyflie/crazyradio/crazyradio.rb', line 94

def initialize(device=nil, settings={})
    if device.nil? || !device.is_a?(LIBUSB::Device)
        raise USBDongleException.new("Wrong USB device")
    end

    @device = device
    reopen()
    @settings = DEFAULT_SETTINGS
    @settings.update(settings)
    apply_settings()
end

Instance Attribute Details

#dev_handleObject (readonly)

Returns the value of attribute dev_handle.



89
90
91
# File 'lib/crubyflie/crazyradio/crazyradio.rb', line 89

def dev_handle
  @dev_handle
end

#deviceObject (readonly)

Returns the value of attribute device.



89
90
91
# File 'lib/crubyflie/crazyradio/crazyradio.rb', line 89

def device
  @device
end

#handleObject (readonly)

Returns the value of attribute handle.



89
90
91
# File 'lib/crubyflie/crazyradio/crazyradio.rb', line 89

def handle
  @handle
end

Class Method Details

.factory(settings = {}) ⇒ Crazyradio

Creates a Crazyradio object with the first USB dongle found

Parameters:

  • settings (Hash) (defaults to: {})

    Crazyradio settings. @see #DEFAULT_SETTINGS

Returns:

Raises:



162
163
164
165
166
# File 'lib/crubyflie/crazyradio/crazyradio.rb', line 162

def self.factory(settings={})
    devs = Crazyradio.find_devices()
    raise USBDongleException.new("No dongles found") if devs.empty?()
    return Crazyradio.new(devs.first, settings)
end

.find_devicesObject

List crazyradio dongles



169
170
171
172
173
# File 'lib/crubyflie/crazyradio/crazyradio.rb', line 169

def self.find_devices
    usb = LIBUSB::Context.new
    usb.devices(:idVendor  => CRAZYRADIO_VENDOR_ID,
                :idProduct => CRAZYRADIO_PRODUCT_ID)
end

.statusString

Return some information as string

Returns:

  • (String)

    Dongle information



118
119
120
121
122
123
124
# File 'lib/crubyflie/crazyradio/crazyradio.rb', line 118

def self.status
    cr = Crazyradio.factory()
    serial = cr.device.serial_number
    manufacturer = cr.device.manufacturer
    cr.close()
    return "Found #{serial} USB dongle from #{manufacturer}"
end

Instance Method Details

#[](setting) ⇒ Integer

Get a crazyradio setting

Parameters:

  • setting (Symbol)

    a valid Crazyradio setting name

Returns:

  • (Integer)

    the value



204
205
206
# File 'lib/crubyflie/crazyradio/crazyradio.rb', line 204

def [](setting)
    return @settings[setting]
end

#[]=(setting, value) ⇒ Object

Set a crazyradio setting

Parameters:

  • setting (Symbol)

    a valid Crazyradio setting name

  • value (Object)

    the setting value



196
197
198
199
# File 'lib/crubyflie/crazyradio/crazyradio.rb', line 196

def []=(setting, value)
    @settings[setting] = value
    apply_settings(setting)
end

#apply_settings(setting = nil) ⇒ Object

Applies the indicated setting or all settings if not specified

Parameters:

  • setting (Symbol) (defaults to: nil)

    a valid crazyradio setting name



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/crubyflie/crazyradio/crazyradio.rb', line 210

def apply_settings(setting=nil)
    to_apply = setting.nil? ? @settings.keys() : [setting]
    to_apply.each do |setting|
        value = @settings[setting]
        next if value.nil?

        case setting
        when :data_rate
            set_data_rate(value)
        when :channel
            set_channel(value)
        when :arc
            set_arc(value)
        when :cont_carrier
            set_cont_carrier(value)
        when :address
            set_address(value)
        when :power
            set_power(value)
        when :ard_bytes
            set_ard_bytes(value)
        else
            @settings.delete(setting)
        end
    end
end

#closeObject

Release interface, reset device and close the handle



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

def close
    @handle.release_interface(0) if @handle
    @handle.reset_device() if @handle
    # WARNING: This hangs badly and randomly!!!
    # @handle.close() if @handle
    @handle = nil
end

#has_fw_scannil

Determines if the dongle has hardware scanning.

Returns:

  • (nil)

    defaults to nil to mitigate a dongle bug



137
138
139
140
# File 'lib/crubyflie/crazyradio/crazyradio.rb', line 137

def has_fw_scan
    # it seems there is a bug on fw scan
    nil
end

#reopenObject

Initializes the device and the USB handle If they are open, it releases the resources first



108
109
110
111
112
113
114
# File 'lib/crubyflie/crazyradio/crazyradio.rb', line 108

def reopen
    close()
    @handle = @device.open()
    # USB configuration 0 means unconfigured state
    @handle.configuration = 1 # hardcoded
    @handle.claim_interface(0) # hardcoded
end

#scan_channels(start, stop, packet = [0xFF]) ⇒ Object

Scans channels for crazyflies



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/crubyflie/crazyradio/crazyradio.rb', line 143

def scan_channels(start, stop, packet=[0xFF])
    if has_fw_scan()
        send_vendor_setup(SCANN_CHANNELS, start, stop, packet)
        return get_vendor_setup(SCANN_CHANNELS, 0, 0, 64)
    end

    result = []
    (start..stop).each do |ch|
        self[:channel] = ch
        status = send_packet(packet)
        result << ch if status && status.ack
    end
    return result
end

#send_packet(data) ⇒ Object

Send a data packet and reads the response into an Ack

Parameters:

  • data (Array)

    to be sent



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/crubyflie/crazyradio/crazyradio.rb', line 177

def send_packet(data)
    out_args = {
        :endpoint => 1,
        :dataOut => data.pack('C*')
    }
    @handle.bulk_transfer(out_args)
    in_args = {
        :endpoint => 0x81,
        :dataIn => 64
    }
    response = @handle.bulk_transfer(in_args)

    return nil unless response
    return RadioAck.from_raw(response, @settings[:arc])
end