29
29
from neobot .serial_connector import SerialConnector
30
30
from neobot .linker import Linker
31
31
32
+ START_BYTES = 'CDAB'
33
+
32
34
33
35
class NeosocoConnectionChecker (object ):
34
36
def __init__ (self , neobot ):
@@ -77,22 +79,12 @@ def __init__(self, index):
77
79
self ._thread = None
78
80
self ._thread_lock = threading .Lock ()
79
81
80
- self ._left_wheel = 0
81
- self ._right_wheel = 0
82
+ self ._output1 = 0
83
+ self ._output2 = 0
84
+ self ._output3 = 0
85
+ self ._mla = 0
86
+ self ._mra = 0
82
87
self ._buzzer = 0
83
- self ._output_a = 0
84
- self ._output_b = 0
85
- self ._topology = 0
86
- self ._left_led = 0
87
- self ._right_led = 0
88
- self ._note = 0
89
- self ._line_tracer_mode = 0
90
- self ._line_tracer_speed = 5
91
- self ._io_mode_a = 0
92
- self ._io_mode_b = 0
93
- self ._config_proximity = 2
94
- self ._config_gravity = 0
95
- self ._config_band_width = 3
96
88
97
89
self ._topology_written = False
98
90
self ._left_led_written = False
@@ -125,36 +117,10 @@ def _set_model_code(self, code):
125
117
def _create_model (self ):
126
118
from neobot .neosoco import Neosoco
127
119
dict = self ._device_dict = {}
128
- dict [Neosoco .LEFT_WHEEL ] = self ._left_wheel_device = self ._add_device (Neosoco .LEFT_WHEEL , "LeftWheel" , DeviceType .EFFECTOR , DataType .INTEGER , 1 , - 100 , 100 , 0 )
129
- dict [Neosoco .RIGHT_WHEEL ] = self ._right_wheel_device = self ._add_device (Neosoco .RIGHT_WHEEL , "RightWheel" , DeviceType .EFFECTOR , DataType .INTEGER , 1 , - 100 , 100 , 0 )
130
- dict [Neosoco .BUZZER ] = self ._buzzer_device = self ._add_device (Neosoco .BUZZER , "Buzzer" , DeviceType .EFFECTOR , DataType .FLOAT , 1 , 0 , 167772.15 , 0.0 )
131
- dict [Neosoco .OUTPUT_A ] = self ._output_a_device = self ._add_device (Neosoco .OUTPUT_A , "OutputA" , DeviceType .EFFECTOR , DataType .INTEGER , 1 , 0 , 255 , 0 )
132
- dict [Neosoco .OUTPUT_B ] = self ._output_b_device = self ._add_device (Neosoco .OUTPUT_B , "OutputB" , DeviceType .EFFECTOR , DataType .INTEGER , 1 , 0 , 255 , 0 )
133
- dict [Neosoco .TOPOLOGY ] = self ._topology_device = self ._add_device (Neosoco .TOPOLOGY , "Topology" , DeviceType .COMMAND , DataType .INTEGER , 1 , 0 , 15 , 0 )
134
- dict [Neosoco .LEFT_LED ] = self ._left_led_device = self ._add_device (Neosoco .LEFT_LED , "LeftLed" , DeviceType .COMMAND , DataType .INTEGER , 1 , 0 , 7 , 0 )
135
- dict [Neosoco .RIGHT_LED ] = self ._right_led_device = self ._add_device (Neosoco .RIGHT_LED , "RightLed" , DeviceType .COMMAND , DataType .INTEGER , 1 , 0 , 7 , 0 )
136
- dict [Neosoco .NOTE ] = self ._note_device = self ._add_device (Neosoco .NOTE , "Note" , DeviceType .COMMAND , DataType .INTEGER , 1 , 0 , 88 , 0 )
137
- dict [Neosoco .LINE_TRACER_MODE ] = self ._line_tracer_mode_device = self ._add_device (Neosoco .LINE_TRACER_MODE , "LineTracerMode" , DeviceType .COMMAND , DataType .INTEGER , 1 , 0 , 15 , 0 )
138
- dict [Neosoco .LINE_TRACER_SPEED ] = self ._line_tracer_speed_device = self ._add_device (Neosoco .LINE_TRACER_SPEED , "LineTracerSpeed" , DeviceType .COMMAND , DataType .INTEGER , 1 , 1 , 8 , 5 )
139
- dict [Neosoco .IO_MODE_A ] = self ._io_mode_a_device = self ._add_device (Neosoco .IO_MODE_A , "IoModeA" , DeviceType .COMMAND , DataType .INTEGER , 1 , 0 , 15 , 0 )
140
- dict [Neosoco .IO_MODE_B ] = self ._io_mode_b_device = self ._add_device (Neosoco .IO_MODE_B , "IoModeB" , DeviceType .COMMAND , DataType .INTEGER , 1 , 0 , 15 , 0 )
141
- dict [Neosoco .CONFIG_PROXIMITY ] = self ._config_proximity_device = self ._add_device (Neosoco .CONFIG_PROXIMITY , "ConfigProximity" , DeviceType .COMMAND , DataType .INTEGER , 1 , 1 , 7 , 2 )
142
- dict [Neosoco .CONFIG_GRAVITY ] = self ._config_gravity_device = self ._add_device (Neosoco .CONFIG_GRAVITY , "ConfigGravity" , DeviceType .COMMAND , DataType .INTEGER , 1 , 0 , 3 , 0 )
143
- dict [Neosoco .CONFIG_BAND_WIDTH ] = self ._config_band_width_device = self ._add_device (Neosoco .CONFIG_BAND_WIDTH , "ConfigBandWidth" , DeviceType .COMMAND , DataType .INTEGER , 1 , 1 , 8 , 3 )
144
- dict [Neosoco .SIGNAL_STRENGTH ] = self ._signal_strength_device = self ._add_device (Neosoco .SIGNAL_STRENGTH , "SignalStrength" , DeviceType .SENSOR , DataType .INTEGER , 1 , - 128 , 0 , 0 )
145
- dict [Neosoco .LEFT_PROXIMITY ] = self ._left_proximity_device = self ._add_device (Neosoco .LEFT_PROXIMITY , "LeftProximity" , DeviceType .SENSOR , DataType .INTEGER , 1 , 0 , 255 , 0 )
146
- dict [Neosoco .RIGHT_PROXIMITY ] = self ._right_proximity_device = self ._add_device (Neosoco .RIGHT_PROXIMITY , "RightProximity" , DeviceType .SENSOR , DataType .INTEGER , 1 , 0 , 255 , 0 )
147
- dict [Neosoco .LEFT_FLOOR ] = self ._left_floor_device = self ._add_device (Neosoco .LEFT_FLOOR , "LeftFloor" , DeviceType .SENSOR , DataType .INTEGER , 1 , 0 , 255 , 0 )
148
- dict [Neosoco .RIGHT_FLOOR ] = self ._right_floor_device = self ._add_device (Neosoco .RIGHT_FLOOR , "RightFloor" , DeviceType .SENSOR , DataType .INTEGER , 1 , 0 , 255 , 0 )
149
- dict [Neosoco .ACCELERATION ] = self ._acceleration_device = self ._add_device (Neosoco .ACCELERATION , "Acceleration" , DeviceType .SENSOR , DataType .INTEGER , 3 , - 32768 , 32767 , 0 )
150
- dict [Neosoco .LIGHT ] = self ._light_device = self ._add_device (Neosoco .LIGHT , "Light" , DeviceType .SENSOR , DataType .INTEGER , 1 , 0 , 65535 , 0 )
151
- dict [Neosoco .TEMPERATURE ] = self ._temperature_device = self ._add_device (Neosoco .TEMPERATURE , "Temperature" , DeviceType .SENSOR , DataType .INTEGER , 1 , - 40 , 88 , 0 )
152
- dict [Neosoco .INPUT_A ] = self ._input_a_device = self ._add_device (Neosoco .INPUT_A , "InputA" , DeviceType .SENSOR , DataType .INTEGER , 1 , 0 , 255 , 0 )
153
- dict [Neosoco .INPUT_B ] = self ._input_b_device = self ._add_device (Neosoco .INPUT_B , "InputB" , DeviceType .SENSOR , DataType .INTEGER , 1 , 0 , 255 , 0 )
154
- dict [Neosoco .LINE_TRACER_STATE ] = self ._line_tracer_state_device = self ._add_device (Neosoco .LINE_TRACER_STATE , "LineTracerState" , DeviceType .EVENT , DataType .INTEGER , 1 , 0 , 255 , 0 )
155
- dict [Neosoco .TILT ] = self ._tilt_device = self ._add_device (Neosoco .TILT , "Tilt" , DeviceType .EVENT , DataType .INTEGER , 1 , - 3 , 3 , 0 )
156
- dict [Neosoco .BATTERY_STATE ] = self ._battery_state_device = self ._add_device (Neosoco .BATTERY_STATE , "BatteryState" , DeviceType .EVENT , DataType .INTEGER , 1 , 0 , 2 , 2 )
157
-
120
+ dict [Neosoco .OUTPUT_1 ] = self ._output_1_device = self ._add_device (Neosoco .OUTPUT_1 , "Output1" , DeviceType .EFFECTOR , DataType .INTEGER , 1 , 0 , 255 , 0 )
121
+ dict [Neosoco .OUTPUT_2 ] = self ._output_2_device = self ._add_device (Neosoco .OUTPUT_2 , "Output2" , DeviceType .EFFECTOR , DataType .INTEGER , 1 , 0 , 255 , 0 )
122
+ dict [Neosoco .OUTPUT_3 ] = self ._output_3_device = self ._add_device (Neosoco .OUTPUT_3 , "Output3" , DeviceType .EFFECTOR , DataType .INTEGER , 1 , 0 , 255 , 0 )
123
+
158
124
def find_device_by_id (self , device_id ):
159
125
return self ._device_dict .get (device_id )
160
126
@@ -247,44 +213,9 @@ def _reset(self):
247
213
248
214
def _request_motoring_data (self ):
249
215
with self ._thread_lock :
250
- self ._left_wheel = self ._left_wheel_device .read ()
251
- self ._right_wheel = self ._right_wheel_device .read ()
252
- self ._buzzer = self ._buzzer_device .read ()
253
- self ._output_a = self ._output_a_device .read ()
254
- self ._output_b = self ._output_b_device .read ()
255
- if self ._topology_device ._is_written ():
256
- self ._topology = self ._topology_device .read ()
257
- self ._topology_written = True
258
- if self ._left_led_device ._is_written ():
259
- self ._left_led = self ._left_led_device .read ()
260
- self ._left_led_written = True
261
- if self ._right_led_device ._is_written ():
262
- self ._right_led = self ._right_led_device .read ()
263
- self ._right_led_written = True
264
- if self ._note_device ._is_written ():
265
- self ._note = self ._note_device .read ()
266
- self ._note_written = True
267
- if self ._line_tracer_mode_device ._is_written ():
268
- self ._line_tracer_mode = self ._line_tracer_mode_device .read ()
269
- self ._line_tracer_mode_written = True
270
- if self ._line_tracer_speed_device ._is_written ():
271
- self ._line_tracer_speed = self ._line_tracer_speed_device .read ()
272
- self ._line_tracer_speed_written = True
273
- if self ._io_mode_a_device ._is_written ():
274
- self ._io_mode_a = self ._io_mode_a_device .read ()
275
- self ._io_mode_a_written = True
276
- if self ._io_mode_b_device ._is_written ():
277
- self ._io_mode_b = self ._io_mode_b_device .read ()
278
- self ._io_mode_b_written = True
279
- if self ._config_proximity_device ._is_written ():
280
- self ._config_proximity = self ._config_proximity_device .read ()
281
- self ._config_proximity_written = True
282
- if self ._config_gravity_device ._is_written ():
283
- self ._config_gravity = self ._config_gravity_device .read ()
284
- self ._config_gravity_written = True
285
- if self ._config_band_width_device ._is_written ():
286
- self ._config_band_width = self ._config_band_width_device .read ()
287
- self ._config_band_width_written = True
216
+ self ._output_1 = self ._output_1_device .read ()
217
+ self ._output_2 = self ._output_2_device .read ()
218
+ self ._output_3 = self ._output_3_device .read ()
288
219
self ._clear_written ()
289
220
290
221
def _color_to_rgb (self , color ):
@@ -306,81 +237,16 @@ def _speed_to_gain(self, speed):
306
237
def _encode_motoring_packet (self , address ):
307
238
result = ""
308
239
with self ._thread_lock :
309
- if self ._model_code == 0x0E :
310
- result += "10"
311
- result += self ._to_hex (self ._left_wheel )
312
- result += self ._to_hex (self ._right_wheel )
313
- rgb = self ._color_to_rgb (self ._left_led )
314
- result += self ._to_hex (rgb [0 ])
315
- result += self ._to_hex (rgb [1 ])
316
- result += self ._to_hex (rgb [2 ])
317
- rgb = self ._color_to_rgb (self ._right_led )
318
- result += self ._to_hex (rgb [0 ])
319
- result += self ._to_hex (rgb [1 ])
320
- result += self ._to_hex (rgb [2 ])
321
- result += "000000"
322
- temp = self ._line_tracer_mode & 0x0f
323
- if temp > 7 : temp += 1
324
- if self ._line_tracer_mode_written :
325
- self ._line_tracer_count = 0
326
- if temp > 0 :
327
- self ._line_tracer_flag = (self ._line_tracer_flag % 15 ) + 1
328
- self ._line_tracer_event = 1
329
- else :
330
- self ._line_tracer_event = 0
331
- self ._line_tracer_mode_written = False
332
- temp |= (self ._line_tracer_flag & 0x0f ) << 4
333
- result += self ._to_hex (temp )
334
- temp = (self ._line_tracer_speed & 0x0f ) << 4
335
- temp |= self ._speed_to_gain (self ._line_tracer_speed ) & 0x0f
336
- result += self ._to_hex (temp )
337
- temp = (self ._config_proximity & 0x07 ) << 5
338
- temp |= (self ._config_band_width & 0x07 ) << 2
339
- temp |= self ._config_gravity & 0x03
340
- result += self ._to_hex (temp )
341
- temp = (self ._io_mode_a & 0x0f ) << 4
342
- temp |= self ._io_mode_b & 0x0f
343
- result += self ._to_hex (temp )
344
- result += self ._to_hex (self ._output_a )
345
- result += self ._to_hex (self ._output_b )
346
- if self ._note > 0 :
347
- result += "01"
348
- result += self ._to_hex (self ._note )
349
- else :
350
- temp = self ._buzzer
351
- if temp > 6500 : temp = 6500
352
- result += self ._to_hex2 (Util .round (temp * 10 ) + 512 )
353
- else :
354
- result += self ._to_hex (self ._topology & 0x0f )
355
- result += "0010"
356
- result += self ._to_hex (self ._left_wheel )
357
- result += self ._to_hex (self ._right_wheel )
358
- result += self ._to_hex (self ._left_led )
359
- result += self ._to_hex (self ._right_led )
360
- result += self ._to_hex3 (Util .round (self ._buzzer * 100 ))
361
- result += self ._to_hex (self ._note )
362
- if self ._line_tracer_mode_written :
363
- if self ._line_tracer_mode > 0 :
364
- self ._line_tracer_flag ^= 0x80
365
- self ._line_tracer_event = 1
366
- self ._line_tracer_mode_written = False
367
- temp = (self ._line_tracer_mode & 0x0f ) << 3
368
- temp |= (self ._line_tracer_speed - 1 ) & 0x07
369
- temp |= self ._line_tracer_flag & 0x80
370
- result += self ._to_hex (temp )
371
- result += self ._to_hex (self ._config_proximity )
372
- temp = (self ._config_gravity & 0x0f ) << 4
373
- temp |= self ._config_band_width & 0x0f
374
- result += self ._to_hex (temp )
375
- temp = (self ._io_mode_a & 0x0f ) << 4
376
- temp |= self ._io_mode_b & 0x0f
377
- result += self ._to_hex (temp )
378
- result += self ._to_hex (self ._output_a )
379
- result += self ._to_hex (self ._output_b )
380
- result += "000000"
381
- result += "-"
382
- result += address
383
- result += "\r "
240
+ result += START_BYTES
241
+ result += self ._to_hex (self ._output_1 ) # OUT1
242
+ result += self ._to_hex (self ._output_2 ) # OUT2
243
+ result += self ._to_hex (self ._output_3 ) # OUT3
244
+ result += self ._to_hex (0 ) # MLA
245
+ result += self ._to_hex (0 ) # MRA
246
+ result += self ._to_hex (0 ) # BUZZER
247
+ result += self ._to_hex (0 ) # FND
248
+ result += self ._to_hex (0 ) # Not Used
249
+ result += self ._to_hex (self ._make_checksum (result )) # Checksum
384
250
return result
385
251
386
252
def _decode_sensory_packet (self , packet ):
@@ -522,12 +388,12 @@ def _receive(self, connector):
522
388
if connector :
523
389
packet = connector .read ()
524
390
if packet :
525
- if self ._decode_sensory_packet (packet ):
391
+ # if self._decode_sensory_packet(packet): # Temporary blocking
526
392
if self ._ready == False :
527
393
self ._ready = True
528
394
Runner .register_checked ()
529
395
self ._notify_sensory_device_data_changed ()
530
- return True
396
+ return True
531
397
return False
532
398
533
399
def _send (self , connector ):
0 commit comments