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
100
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.
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_size ⇒ Object (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_disconnect ⇒ Object (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 |
#uri ⇒ Object (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
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: #{$!.}") 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 |
#disconnect(force = nil) ⇒ Object
Disconnects from the crazyradio
171 172 173 174 175 176 177 178 179 |
# File 'lib/crubyflie/driver/radio_driver.rb', line 171 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
269 270 271 |
# File 'lib/crubyflie/driver/radio_driver.rb', line 269 def get_status return Crazyradio.status() end |
#receive_packet(non_block = true) ⇒ CRTPPacket?
Fetch a packet from the incoming queue
209 210 211 212 213 214 215 |
# File 'lib/crubyflie/driver/radio_driver.rb', line 209 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
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/crubyflie/driver/radio_driver.rb', line 229 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("Error scanning interface: #{$!}") 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
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/crubyflie/driver/radio_driver.rb', line 184 def send_packet(packet) return if !@crazyradio if (s = @out_queue.size) >= @out_queue_max_size if @retry_forever m = "Outgoing queue full (#{s} elements) but retry_forever " m << "enabled: clearing queue" logger.warn(m) @out_queue.clear() else m = "Reached maximum #{s} elements in outgoing queue" @callbacks[:link_error_cb].call(m) # Force shutdown, otherwise we will hit this error again # and go into stack overflow # (tecnnically, the link_error_cb should have made sure # we are disconnected, but it does not hurt to re-do it) disconnect(true) end end @out_queue << packet if !@shutdown_thread end |