class Crubyflie::Crazyradio

Driver for the USB crazyradio dongle

Constants

DEFAULT_SETTINGS

Default settings for Crazyradio

Attributes

dev_handle[R]
device[R]
handle[R]

Public Class Methods

factory(settings={}) click to toggle source

Creates a Crazyradio object with the first USB dongle found @param settings [Hash] Crazyradio settings. @see DEFAULT_SETTINGS @return [Crazyradio] a Crazyradio @raise [USBDongleException] when no USB dongle is found

# File lib/crubyflie/crazyradio/crazyradio.rb, line 161
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_devices() click to toggle source

List crazyradio dongles

# File lib/crubyflie/crazyradio/crazyradio.rb, line 168
def self.find_devices
    usb = LIBUSB::Context.new
    usb.devices(:idVendor  => CRAZYRADIO_VENDOR_ID,
                :idProduct => CRAZYRADIO_PRODUCT_ID)
end
new(device=nil, settings={}) click to toggle source

Initialize a crazyradio @param device [LIBUSB::Device] A crazyradio USB device @param settings [Hash] Crazyradio settings. @see DEFAULT_SETTINGS @raise [USBDongleException] when something goes wrong

# File lib/crubyflie/crazyradio/crazyradio.rb, line 93
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
status() click to toggle source

Return some information as string @return [String] Dongle information

# File lib/crubyflie/crazyradio/crazyradio.rb, line 117
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

Public Instance Methods

[](setting) click to toggle source

Get a crazyradio setting @param setting [Symbol] a valid Crazyradio setting name @return [Integer] the value

# File lib/crubyflie/crazyradio/crazyradio.rb, line 203
def [](setting)
    return @settings[setting]
end
[]=(setting, value) click to toggle source

Set a crazyradio setting @param setting [Symbol] a valid Crazyradio setting name @param value [Object] the setting value

# File lib/crubyflie/crazyradio/crazyradio.rb, line 195
def []=(setting, value)
    @settings[setting] = value
    apply_settings(setting)
end
apply_settings(setting=nil) click to toggle source

Applies the indicated setting or all settings if not specified @param setting [Symbol] a valid crazyradio setting name

# File lib/crubyflie/crazyradio/crazyradio.rb, line 209
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
close() click to toggle source

Release interface, reset device and close the handle

# File lib/crubyflie/crazyradio/crazyradio.rb, line 126
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_scan() click to toggle source

Determines if the dongle has hardware scanning. @return [nil] defaults to nil to mitigate a dongle bug

# File lib/crubyflie/crazyradio/crazyradio.rb, line 136
def has_fw_scan
    # it seems there is a bug on fw scan
    nil
end
reopen() click to toggle source

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

# File lib/crubyflie/crazyradio/crazyradio.rb, line 107
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]) click to toggle source

Scans channels for crazyflies

# File lib/crubyflie/crazyradio/crazyradio.rb, line 142
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) click to toggle source

Send a data packet and reads the response into an Ack @param [Array] data to be sent

# File lib/crubyflie/crazyradio/crazyradio.rb, line 176
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

Private Instance Methods

get_vendor_setup(request, value, index, dataIn=0) click to toggle source
# File lib/crubyflie/crazyradio/crazyradio.rb, line 248
def get_vendor_setup(request, value, index, dataIn=0)
    args = {
        # Why this mask?
        :bmRequestType        => LIBUSB::REQUEST_TYPE_VENDOR | 0x80,
        :bRequest             => request,
        :wValue               => value,
        :wIndex               => index,
        :dataIn               => dataIn
    }
    return @handle.control_transfer(args).unpack('C*')
end
send_vendor_setup(request, value, index=0, dataOut=[]) click to toggle source
# File lib/crubyflie/crazyradio/crazyradio.rb, line 236
def send_vendor_setup(request, value, index=0, dataOut=[])
    args = {
        :bmRequestType        => LIBUSB::REQUEST_TYPE_VENDOR,
        :bRequest             => request,
        :wValue               => value,
        :wIndex               => index,
        :dataOut              => dataOut.pack('C*')
    }
    @handle.control_transfer(args)
end
set_address(addr) click to toggle source
# File lib/crubyflie/crazyradio/crazyradio.rb, line 266
def set_address(addr)
    if addr.size != 5
        raise USBDongleException.new("Address needs 5 bytes")
    end
    send_vendor_setup(SET_RADIO_ADDRESS, 0, 0, addr)
end
set_arc(arc) click to toggle source
# File lib/crubyflie/crazyradio/crazyradio.rb, line 284
def set_arc(arc)
    send_vendor_setup(SET_RADIO_ARC, arc)
end
set_ard_bytes(nbytes) click to toggle source
# File lib/crubyflie/crazyradio/crazyradio.rb, line 289
def set_ard_bytes(nbytes)
    # masking this way converts 32 to 0xA0 for example
    send_vendor_setup(SET_RADIO_ARD, 0x80 | nbytes)
end
set_channel(channel) click to toggle source
# File lib/crubyflie/crazyradio/crazyradio.rb, line 261
def set_channel(channel)
    send_vendor_setup(SET_RADIO_CHANNEL, channel)
end
set_cont_carrier(active) click to toggle source
# File lib/crubyflie/crazyradio/crazyradio.rb, line 295
def set_cont_carrier(active)
    send_vendor_setup(SET_CONT_CARRIER, active ? 1 : 0)
end
set_data_rate(datarate) click to toggle source
# File lib/crubyflie/crazyradio/crazyradio.rb, line 274
def set_data_rate(datarate)
    send_vendor_setup(SET_DATA_RATE, datarate)
end
set_power(power) click to toggle source
# File lib/crubyflie/crazyradio/crazyradio.rb, line 279
def set_power(power)
    send_vendor_setup(SET_RADIO_POWER, power)
end