Method: Crubyflie::RadioDriver#connect

Defined in:
lib/crubyflie/driver/radio_driver.rb

#connect(uri_s, callbacks = {}, opts = {}) ⇒ Object

Connect to a Crazyflie in the specified URI

Parameters:

  • uri_s (String)

    a radio uri like radio://<dongle>/<ch>/<rate>

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

    blocks to call (see CALLBACKS contant values)

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

    options. Currently supported :retries_before_disconnect (defaults to 20) and :out_queue_max_size (defaults to 50)

Raises:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/crubyflie/driver/radio_driver.rb', line 99

def connect(uri_s, callbacks={}, opts={})
    # Check if apparently there is an open link

    if @crazyradio
        m = "Active link to #{@uri.to_s}. Disconnect first"
        raise OpenLink.new(m)
    end

    # Parse URI to initialize Crazyradio
    # @todo: better control input. It defaults to 0
    @uri = CrubyflieURI.new(uri_s)
    dongle_number = @uri.dongle.to_i
    channel = @uri.channel.to_i
    rate = @uri.rate
    address = @uri.address

    if address
        begin
            # The official driver does this. Takes address as decimal
            # number, calculate the binary and pack it as 5 byte.
            hex_addr = address.to_i.to_s(16)
            bin_addr = hex_addr.scan(/../).map { |x| x.hex }.pack('C*')
            address = bin_addr.unpack('CCCCC')
        rescue
            raise InvalidURIException.new("Address not valid: #{$!.message}")
        end
    end

    # @todo this should be taken care of in crazyradio
    case rate
    when "250K"
        rate = CrazyradioConstants::DR_250KPS
    when "1M"
        rate = CrazyradioConstants::DR_1MPS
    when "2M"
        rate = CrazyradioConstants::DR_2MPS
    else
        raise InvalidURIException.new("Bad radio rate")
    end

    # Fill in the callbacks Hash
    CALLBACKS.each do |cb|
        if passed_cb = callbacks[cb]
            @callbacks[cb] = passed_cb
        else
            raise CallbackMissing.new("Callback #{cb} mandatory")
        end
    end

    @retries_before_disconnect = opts[:retries_before_disconnect] ||
        RETRIES_BEFORE_DISCONNECT
    @out_queue_max_size = opts[:out_queue_max_size] ||
        OUT_QUEUE_MAX_SIZE

    @retry_forever = @retries_before_disconnect < 0
    m = "Radio driver will retry forever and never disconnect"
    logger.warn(m) if @retry_forever

    # Initialize Crazyradio and run thread
    cradio_opts = {
        :channel => channel,
        :data_rate => rate,
        :address => address
    }
    @crazyradio = Crazyradio.factory(cradio_opts)
    start_radio_thread()

end