crazyflie-zmq 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 72ac0134ded573f08008637f80c872a6a034de84016efc57c9195245d51be191
4
- data.tar.gz: 1559e33582d8abf856f93e75b6e57510877e3eab544a710e4de6b28b1f3e22c5
3
+ metadata.gz: becf8bb70389291899c7fc7981149452d71a3eaef0a3219dfc5a2ddc54d80de7
4
+ data.tar.gz: 42919fa2747ac3573a8a6bb616401d7268fe363b9e9e1144d6e8171e1513351c
5
5
  SHA512:
6
- metadata.gz: 8892dd565405dba9078ad8961bb602b9fcf1f84dda6b4b03f8c7c386b2a79700ac4e3ad641b290563a2852ade6467a42f78704d7b4964083f26e5b8a01ec3f22
7
- data.tar.gz: 11fdc478146ab16903c9eacf58905a0b51fae8860612bd7a1da3fb9ecd8543cf235a80b734cd2fcdbc3f4d0c091d8d31cb718a1fe35ebd0030ee930297655cac
6
+ metadata.gz: f21262a12bc94fc5516cdbb275801a4a6ac1d4a60f19d06e8235e4daf6a5aded83137673d61695d5734e52d850ca4bed583e4961d8c41efc4f2bbe267cb5069a
7
+ data.tar.gz: 1d42be892edecaf4b7a66a33a85b9a20bec5d7f1b086aa96296b1c9effb23923448b771efa8963a8d3fc956b2de8eaaabd81ead658eb1c57184b40c841cc482a
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.unshift File.expand_path("../lib", __FILE__)
3
- require "crazyflie-zmq"
3
+ require "crazyflie-zmq/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "crazyflie-zmq"
data/lib/crazyflie-zmq.rb CHANGED
@@ -5,6 +5,8 @@ require 'json'
5
5
  require 'csv'
6
6
  require 'ffi-rzmq'
7
7
 
8
+ require_relative 'crazyflie-zmq/version'
9
+
8
10
 
9
11
  # Allow control of a {https://www.bitcraze.io/crazyflie-2/ Crazyflie}
10
12
  # drone using the ZMQ protocol.
@@ -16,13 +18,24 @@ require 'ffi-rzmq'
16
18
  #
17
19
  # crazyflie-clients-python/bin/cfzmq --url tcp://* -d
18
20
  #
21
+ # Small demonstration:
22
+ #
23
+ # $cf = CrazyflieZMQ.new('tcp://_ip_address_') # Connect to ZMQ sockets
24
+ # $cf.connect('radio://0/80/1M/E7E7E7E7E7') # Connect to drone
25
+ # $cf.log_create(:range, # Create log block
26
+ # 'ranging.distance0', 'ranging.distance1', 'ranging.distance2')
27
+ # $cf['posCtlPid','xKp']= 1.0 # Parameter (group, name notation)
28
+ # $cf['posCtlPid.yKp' ]= 1.0 # Paramerer (name dotted notation)
29
+ # $cf.log_start(:range, file: :csv) # Log to csv generated file
30
+ # sleep(120) # Wait 2 minutes
31
+ # $cf.log_stop(:range) # Stop logging of :range
32
+ # $cf.disconnect # Disconnect from drone
33
+ #
19
34
  # @see https://wiki.bitcraze.io/doc:crazyflie:client:cfzmq:index
20
35
  # @see https://wiki.bitcraze.io/doc:crazyflie:dev:arch:logparam
21
36
  # @see https://wiki.bitcraze.io/projects:crazyflie:firmware:comm_protocol
22
37
  # @see https://wiki.bitcraze.io/doc:crazyflie:crtp:log
23
38
  class CrazyflieZMQ
24
- VERSION = "0.1.1" # CrazyflieZMQ version
25
-
26
39
  # Standard error for {CrazyflieZMQ}
27
40
  class Error < StandardError ; end
28
41
 
@@ -38,7 +51,7 @@ class CrazyflieZMQ
38
51
 
39
52
 
40
53
 
41
- # Create a CrazyflieZMQ instance
54
+ # Create a {CrazyflieZMQ} instance
42
55
  #
43
56
  # @param url [String] the url to connect to the ZMQ socket
44
57
  def initialize(url, log: nil)
@@ -49,9 +62,9 @@ class CrazyflieZMQ
49
62
  @log_count = {}
50
63
  @log_blocks = nil
51
64
 
52
- @param = {}
53
- @log = {}
54
- @connected = nil
65
+ @param = {}
66
+ @log = {}
67
+ @connected = nil
55
68
 
56
69
  @ctx = ZMQ::Context.create(1)
57
70
 
@@ -69,7 +82,7 @@ class CrazyflieZMQ
69
82
  loop {
70
83
  data = ''
71
84
  @param_sock.recv_string(data)
72
- resp = JSON.parse(data)
85
+ resp = JSON.parse(_json_fix(data))
73
86
  version = resp.delete('version' )
74
87
  name = resp.delete('name' )
75
88
  value = resp.delete('value' )
@@ -89,7 +102,7 @@ class CrazyflieZMQ
89
102
  loop {
90
103
  data = ''
91
104
  @log_sock.recv_string(data)
92
- resp = JSON.parse(data)
105
+ resp = JSON.parse(_json_fix(data))
93
106
  version = resp.delete('version' )
94
107
  event = resp.delete('event' ).to_sym
95
108
  name = resp.delete('name' ).to_sym
@@ -105,8 +118,9 @@ class CrazyflieZMQ
105
118
 
106
119
 
107
120
  # Returns the list of available crazyflie
121
+ # @return [Array<Hash{String=>String}>] list of available interfaces
108
122
  def scan()
109
- _request(cmd: :scan)
123
+ _request(cmd: :scan).dig('interfaces')
110
124
  end
111
125
 
112
126
 
@@ -117,23 +131,23 @@ class CrazyflieZMQ
117
131
  # @return [self]
118
132
  def connect(uri, log_blocks: nil)
119
133
  toc = _request(cmd: :connect, uri: uri)
120
- @param = toc['param'] || {}
121
- @log = toc['log' ] || {}
122
- @connected = Time.now.freeze
123
-
124
- if @log_blocks = log_blocks
125
- @log_blocks.each {|key, data|
126
- variables, period =
127
- case data
128
- when Hash then [ data[:variables], data[:period] ]
129
- when Array then [ data ]
130
- when String then [ [ data ] ]
131
- end
132
-
133
- next if variables.nil? || variables.empty?
134
+ @param = toc['param'] || {}
135
+ @log = toc['log' ] || {}
136
+ @connected = Time.now.freeze
137
+ @log_blocks = log_blocks
138
+
139
+ @log_blocks&.each {|key, data|
140
+ variables, period =
141
+ case data
142
+ when Hash then [ data[:variables], data[:period] ]
143
+ when Array then [ data ]
144
+ when String then [ [ data ] ]
145
+ end
146
+
147
+ if !variables.nil? && !variables.empty?
134
148
  self.log_create(key, *variables, period: period || 100)
135
- }
136
- end
149
+ end
150
+ }
137
151
 
138
152
  self
139
153
  end
@@ -166,10 +180,11 @@ class CrazyflieZMQ
166
180
 
167
181
 
168
182
  # Create a log block
183
+ #
169
184
  # @note logging is usually done through the crazyflie radio link
170
- # so you are limited in the number of variable that you
185
+ # so you are limited in the number of variables that you
171
186
  # can log at the same time as well as the minimal logging
172
- # period that you can use
187
+ # period that you can use.
173
188
  #
174
189
  # @param name [Symbole,String] log block name
175
190
  # @param variables [Array<String>] name of the variable to logs
@@ -191,7 +206,7 @@ class CrazyflieZMQ
191
206
  #
192
207
  # @param name [Symbol, String] name of the log block to start
193
208
  # @param file [String, :csv, nil] name or type of file where to automatically log data
194
- # @param block block called on each new log
209
+ # @yield log data
195
210
  # @return [self]
196
211
  def log_start(name, file: nil, &block)
197
212
  count = (@log_count[name] || 0) + 1
@@ -272,7 +287,7 @@ class CrazyflieZMQ
272
287
  # Get a parameter value from the crazyflie
273
288
  #
274
289
  # If a parameter group is not specified it is possible
275
- # to use a +'.'+ in the parameter name to indicate it:
290
+ # to use a +.+ in the parameter name to indicate it:
276
291
  # +"group.name"+
277
292
  #
278
293
  # @param group [String,nil] group on which the parameter belongs
@@ -285,6 +300,12 @@ class CrazyflieZMQ
285
300
 
286
301
  private
287
302
 
303
+ def _json_fix(str)
304
+ str.gsub(/(?<=:)
305
+ (?:[\+\-]?Infinity|NaN)
306
+ (?=,|\})/x, 'null')
307
+ end
308
+
288
309
  def _zmq_ok!(rc, msg = nil)
289
310
  if !ZMQ::Util.resultcode_ok?(rc)
290
311
  raise ZMQError, msg
@@ -296,7 +317,7 @@ class CrazyflieZMQ
296
317
  resp = ''
297
318
  @client_sock.send_string(data.to_json)
298
319
  @client_sock.recv_string(resp)
299
- resp = JSON.parse(resp)
320
+ resp = JSON.parse(_json_fix(resp))
300
321
  version = resp.delete('version')
301
322
  status = resp.delete('status')
302
323
  unless status.nil? || status.zero?
@@ -0,0 +1,3 @@
1
+ class CrazyflieZMQ
2
+ VERSION = "0.1.2" # CrazyflieZMQ version
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crazyflie-zmq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephane D'Alu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-10 00:00:00.000000000 Z
11
+ date: 2018-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi-rzmq
@@ -77,6 +77,7 @@ files:
77
77
  - LICENSE
78
78
  - crazyflie-zmq.gemspec
79
79
  - lib/crazyflie-zmq.rb
80
+ - lib/crazyflie-zmq/version.rb
80
81
  homepage: http://github.com/sdalu/crazyflie-zmq
81
82
  licenses:
82
83
  - Apache-2.0