Class: Crubyflie::RadioDriver

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

Overview

This layer takes care of connecting to the crazyradio and managing the incoming and outgoing queues. This is done by spawing a thread. It also provides the interface to scan for available crazyflies to which connect

Constant Summary collapse

CALLBACKS =

Currently used callbacks that can be passed to connect()

[:link_quality_cb,
:link_error_cb]
OUT_QUEUE_MAX_SIZE =

Default size for the outgoing queue

50
RETRIES_BEFORE_DISCONNECT =

Default number of retries before disconnecting

20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRadioDriver

Initialize the driver. Creates new empty queues.



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/crubyflie/driver/radio_driver.rb', line 76

def initialize()
    @uri = nil
    @in_queue = Queue.new()
    @out_queue = Queue.new()
    Thread.abort_on_exception = true
    @radio_thread = nil
    @callbacks = {}
    @crazyradio = nil
    @out_queue_max_size = nil
    @retries_before_disconnect = nil
    @shutdown_thread = false
end

Instance Attribute Details

#out_queue_max_sizeObject (readonly)

Returns the value of attribute out_queue_max_size.



73
74
75
# File 'lib/crubyflie/driver/radio_driver.rb', line 73

def out_queue_max_size
  @out_queue_max_size
end

#retries_before_disconnectObject (readonly)

Returns the value of attribute retries_before_disconnect.



73
74
75
# File 'lib/crubyflie/driver/radio_driver.rb', line 73

def retries_before_disconnect
  @retries_before_disconnect
end

#uriObject (readonly)

Returns the value of attribute uri.



72
73
74
# File 'lib/crubyflie/driver/radio_driver.rb', line 72

def uri
  @uri
end

Instance Method Details

#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
# 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

    # @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

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

end

#disconnect(force = nil) ⇒ Object

Disconnects from the crazyradio

Parameters:

  • force (TrueClass, FalseClass) (defaults to: nil)

    . Kill the thread right away, or wait for out_queue to empty



153
154
155
156
157
158
159
160
161
# File 'lib/crubyflie/driver/radio_driver.rb', line 153

def disconnect(force=nil)
    kill_radio_thread(force)
    @in_queue.clear()
    @out_queue.clear()

    return if !@crazyradio
    @crazyradio.close()
    @crazyradio = nil
end

#get_statusObject

Get status from the crazyradio. @see Crazyradio#status



241
242
243
# File 'lib/crubyflie/driver/radio_driver.rb', line 241

def get_status
    return Crazyradio.status()
end

#receive_packet(non_block = true) ⇒ CRTPPacket?

Fetch a packet from the incoming queue

Returns:

  • (CRTPPacket, nil)

    a packet from the queue, or nil when there is none



180
181
182
183
184
185
186
# File 'lib/crubyflie/driver/radio_driver.rb', line 180

def receive_packet(non_block=true)
    begin
        return @in_queue.pop(non_block)
    rescue ThreadError
        return nil
    end
end

#scan_interfaceArray

List available Crazyflies

Returns:

  • (Array)

    List of radio URIs where a crazyflie was found

Raises:

  • (OpenLink)

    if the Crazyradio is connected already



200
201
202
203
204
205
206
207
208
209
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
236
237
# File 'lib/crubyflie/driver/radio_driver.rb', line 200

def scan_interface
    raise OpenLink.new("Cannot scan when link is open") if @crazyradio
    begin
        @crazyradio = Crazyradio.factory()
        results = {}
        @crazyradio[:arc] = 1
        @crazyradio[:data_rate] = Crazyradio::DR_250KPS
        results["250K"] = scan_radio_channels()
        @crazyradio[:data_rate] = Crazyradio::DR_1MPS
        results["1M"]   = scan_radio_channels()
        @crazyradio[:data_rate] = Crazyradio::DR_2MPS
        results["2M"]   = scan_radio_channels()

        uris = []
        results.each do |rate, channels|
            channels.each do |ch|
                uris << "radio://0/#{ch}/#{rate}"
            end
        end
        return uris
    rescue USBDongleException
        raise
    rescue Exception
        retries ||= 0
        logger.error("Unknown error scanning interface: #{$!}")
        @crazyradio.reopen()
        retries += 1
        if retries < 2
            logger.error("Retrying")
            sleep 0.5
            retry
        end
        return []
    ensure
        @crazyradio.close() if @crazyradio
        @crazyradio = nil
    end
end

#send_packet(packet) ⇒ Object

Place a packet in the outgoing queue When not connected it will do nothing

Parameters:



166
167
168
169
170
171
172
173
174
175
# File 'lib/crubyflie/driver/radio_driver.rb', line 166

def send_packet(packet)
    return if !@crazyradio
    if (s = @out_queue.size) >= @out_queue_max_size
        m = "Reached #{s} elements in outgoing queue"
        @callbacks[:link_error_cb].call(m)
        disconnect()
    end

    @out_queue << packet if !@shutdown_thread
end