Class: Crubyflie::RadioDriver
- Inherits:
-
Object
- Object
- Crubyflie::RadioDriver
- 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
-
#out_queue_max_size ⇒ Object
readonly
Returns the value of attribute out_queue_max_size.
-
#retries_before_disconnect ⇒ Object
readonly
Returns the value of attribute retries_before_disconnect.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#connect(uri_s, callbacks = {}, opts = {}) ⇒ Object
Connect to a Crazyflie in the specified URI.
-
#disconnect(force = nil) ⇒ Object
Disconnects from the crazyradio.
-
#get_status ⇒ Object
Get status from the crazyradio.
-
#initialize ⇒ RadioDriver
constructor
Initialize the driver.
-
#receive_packet(non_block = true) ⇒ CRTPPacket?
Fetch a packet from the incoming queue.
-
#scan_interface ⇒ Array
List available Crazyflies.
-
#send_packet(packet) ⇒ Object
Place a packet in the outgoing queue When not connected it will do nothing.
Constructor Details
#initialize ⇒ RadioDriver
Initialize the driver. Creates new empty queues.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/crubyflie/driver/radio_driver.rb', line 46 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_size ⇒ Object (readonly)
Returns the value of attribute out_queue_max_size.
43 44 45 |
# File 'lib/crubyflie/driver/radio_driver.rb', line 43 def out_queue_max_size @out_queue_max_size end |
#retries_before_disconnect ⇒ Object (readonly)
Returns the value of attribute retries_before_disconnect.
43 44 45 |
# File 'lib/crubyflie/driver/radio_driver.rb', line 43 def retries_before_disconnect @retries_before_disconnect end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
42 43 44 |
# File 'lib/crubyflie/driver/radio_driver.rb', line 42 def uri @uri end |
Instance Method Details
#connect(uri_s, callbacks = {}, opts = {}) ⇒ Object
Connect to a Crazyflie in the specified URI
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/crubyflie/driver/radio_driver.rb', line 69 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 = URI(uri_s) dongle_number = @uri.host.to_i channel, rate = @uri.path.split('/')[1..-1] # remove leading / channel = channel.to_i # @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 InvalidURIType.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
123 124 125 126 127 128 129 130 131 |
# File 'lib/crubyflie/driver/radio_driver.rb', line 123 def disconnect(force=nil) kill_radio_thread(force) @in_queue.clear() @out_queue.clear() return if !@crazyradio @crazyradio.close() @crazyradio = nil end |
#get_status ⇒ Object
Get status from the crazyradio. @see Crazyradio#status
211 212 213 |
# File 'lib/crubyflie/driver/radio_driver.rb', line 211 def get_status return Crazyradio.status() end |
#receive_packet(non_block = true) ⇒ CRTPPacket?
Fetch a packet from the incoming queue
150 151 152 153 154 155 156 |
# File 'lib/crubyflie/driver/radio_driver.rb', line 150 def receive_packet(non_block=true) begin return @in_queue.pop(non_block) rescue ThreadError return nil end end |
#scan_interface ⇒ Array
List available Crazyflies
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/crubyflie/driver/radio_driver.rb', line 170 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
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/crubyflie/driver/radio_driver.rb', line 136 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 |