Skip to content

Commit 27f39d7

Browse files
committed
First commit of API for get_value()
1 parent 6101f63 commit 27f39d7

File tree

4 files changed

+65
-156
lines changed

4 files changed

+65
-156
lines changed

neobot/neosoco.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class Neosoco(Robot):
2828
OUTPUT_1 = 0x00400000
2929
OUTPUT_2 = 0x00400001
3030
OUTPUT_3 = 0x00400002
31+
INPUT_1 = 0x00400003
32+
INPUT_2 = 0x00400004
33+
INPUT_3 = 0x00400005
34+
REMOCTL = 0x00400006
35+
BATTERY = 0x00400007
3136

3237
LED_OFF = 0
3338
LED_BLUE = 1
@@ -328,6 +333,40 @@ def _notify_sensory_device_data_changed(self):
328333
def _notify_motoring_device_data_changed(self):
329334
self._roboid._notify_motoring_device_data_changed()
330335

336+
def led_on(self, port: str, brightness: str):
337+
if isinstance(port, str) and isinstance(brightness, int):
338+
if port.lower() =='out1':
339+
self.write(Neosoco.OUTPUT_1, Util.round(brightness))
340+
elif port.lower() =='out2':
341+
self.write(Neosoco.OUTPUT_2, Util.round(brightness))
342+
elif port.lower() =='out3':
343+
self.write(Neosoco.OUTPUT_3, Util.round(brightness))
344+
elif port.lower() =='all':
345+
self.write(Neosoco.OUTPUT_1, Util.round(brightness))
346+
self.write(Neosoco.OUTPUT_2, Util.round(brightness))
347+
self.write(Neosoco.OUTPUT_3, Util.round(brightness))
348+
else:
349+
raise ValueError
350+
else:
351+
raise TypeError
352+
353+
def get_value(self, port: str):
354+
if isinstance(port, str):
355+
if port.lower() =='in1':
356+
return self.read(Neosoco.INPUT_1)
357+
elif port.lower() =='in2':
358+
return self.read(Neosoco.INPUT_2)
359+
elif port.lower() =='in3':
360+
return self.read(Neosoco.INPUT_3)
361+
elif port.lower() =='remo':
362+
return self.read(Neosoco.REMOCTL)
363+
elif port.lower() =='bat':
364+
return self.read(Neosoco.BATTERY)
365+
else:
366+
raise ValueError
367+
else:
368+
raise TypeError
369+
331370
def wheels(self, left_velocity, right_velocity=None):
332371
self.write(Neosoco.LINE_TRACER_MODE, Neosoco.LINE_TRACER_MODE_OFF)
333372
if isinstance(left_velocity, (int, float)):
@@ -580,21 +619,6 @@ def leds(self, left_color, right_color=None):
580619
if isinstance(left_color, (int, float)):
581620
self.write(Neosoco.RIGHT_LED, int(left_color))
582621

583-
def led_on(self, port: str, brightness: str):
584-
if isinstance(port, str) and isinstance(brightness, int):
585-
if port.lower() =='out1':
586-
self.write(Neosoco.OUTPUT_1, Util.round(brightness))
587-
elif port.lower() =='out2':
588-
self.write(Neosoco.OUTPUT_2, Util.round(brightness))
589-
elif port.lower() =='out3':
590-
self.write(Neosoco.OUTPUT_3, Util.round(brightness))
591-
elif port.lower() =='all':
592-
self.write(Neosoco.OUTPUT_1, Util.round(brightness))
593-
self.write(Neosoco.OUTPUT_2, Util.round(brightness))
594-
self.write(Neosoco.OUTPUT_3, Util.round(brightness))
595-
else:
596-
raise TypeError
597-
598622
def left_led(self, color):
599623
if isinstance(color, (int, float)):
600624
self.write(Neosoco.LEFT_LED, int(color))

neobot/neosoco_neobot.py

Lines changed: 12 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ def _create_model(self):
120120
dict[Neosoco.OUTPUT_1] = self._output_1_device = self._add_device(Neosoco.OUTPUT_1, "Output1", DeviceType.EFFECTOR, DataType.INTEGER, 1, 0, 255, 0)
121121
dict[Neosoco.OUTPUT_2] = self._output_2_device = self._add_device(Neosoco.OUTPUT_2, "Output2", DeviceType.EFFECTOR, DataType.INTEGER, 1, 0, 255, 0)
122122
dict[Neosoco.OUTPUT_3] = self._output_3_device = self._add_device(Neosoco.OUTPUT_3, "Output3", DeviceType.EFFECTOR, DataType.INTEGER, 1, 0, 255, 0)
123+
dict[Neosoco.INPUT_1] = self._input_1_device = self._add_device(Neosoco.INPUT_1, "Input1", DeviceType.SENSOR, DataType.INTEGER, 1, 0, 255, 0)
124+
dict[Neosoco.INPUT_2] = self._input_2_device = self._add_device(Neosoco.INPUT_2, "Input2", DeviceType.SENSOR, DataType.INTEGER, 1, 0, 255, 0)
125+
dict[Neosoco.INPUT_3] = self._input_3_device = self._add_device(Neosoco.INPUT_3, "Input3", DeviceType.SENSOR, DataType.INTEGER, 1, 0, 255, 0)
126+
dict[Neosoco.REMOCTL] = self._remoctl_device = self._add_device(Neosoco.REMOCTL, "RemoteCtl", DeviceType.SENSOR, DataType.INTEGER, 1, 0, 255, 0)
127+
dict[Neosoco.BATTERY] = self._battery_device = self._add_device(Neosoco.BATTERY, "Battery", DeviceType.SENSOR, DataType.INTEGER, 1, 0, 255, 0)
123128

124129
def find_device_by_id(self, device_id):
125130
return self._device_dict.get(device_id)
@@ -250,150 +255,23 @@ def _encode_motoring_packet(self, address):
250255
return result
251256

252257
def _decode_sensory_packet(self, packet):
253-
packet = str(packet)
254-
if self._model_code == 0x0E:
255-
value = int(packet[0:1], 16)
256-
if value != 1: return False
257-
value = int(packet[6:8], 16)
258-
self._left_proximity_device._put(value)
259-
value = int(packet[8:10], 16)
260-
self._right_proximity_device._put(value)
261-
value2 = int(packet[38:40], 16)
262-
if (value2 & 0x01) == 0:
263-
self._light = int(packet[10:14], 16)
264-
else:
265-
value = int(packet[10:12], 16)
266-
if value > 0x7f: value -= 0x100
267-
self._temperature = Util.round(value / 2.0 + 23)
268-
self._light_device._put(self._light)
269-
self._temperature_device._put(self._temperature)
270-
value = int(packet[14:16], 16)
271-
self._left_floor_device._put(value)
272-
value = int(packet[16:18], 16)
273-
self._right_floor_device._put(value)
274-
acc_x = int(packet[18:22], 16)
275-
if acc_x > 0x7fff: acc_x -= 0x10000
276-
self._acceleration_device._put_at(0, acc_x)
277-
acc_y = int(packet[22:26], 16)
278-
if acc_y > 0x7fff: acc_y -= 0x10000
279-
self._acceleration_device._put_at(1, acc_y)
280-
acc_z = int(packet[26:30], 16)
281-
if acc_z > 0x7fff: acc_z -= 0x10000
282-
self._acceleration_device._put_at(2, acc_z)
283-
if acc_z < 8192 and acc_x > 8192 and acc_y > -4096 and acc_y < 4096: value = 1
284-
elif acc_z < 8192 and acc_x < -8192 and acc_y > -4096 and acc_y < 4096: value = -1
285-
elif acc_z < 8192 and acc_y > 8192 and acc_x > -4096 and acc_x < 4096: value = 2
286-
elif acc_z < 8192 and acc_y < -8192 and acc_x > -4096 and acc_x < 4096: value = -2
287-
elif acc_z > 12288 and acc_x > -8192 and acc_x < 8192 and acc_y > -8192 and acc_y < 8192: value = 3
288-
elif acc_z < -12288 and acc_x > -4096 and acc_x < 4096 and acc_y > -4096 and acc_y < 4096: value = -3
289-
else: value = 0
290-
if value != self._event_tilt:
291-
self._tilt_device._put(value, self._event_tilt != -4)
292-
self._event_tilt = value
293-
value = int(packet[30:32], 16)
294-
self._input_a_device._put(value)
295-
value = int(packet[32:34], 16)
296-
self._input_b_device._put(value)
297-
value = int(packet[36:38], 16)
298-
value -= 0x100
299-
self._signal_strength_device._put(value)
300-
value = (value2 >> 6) & 0x03
301-
if (value & 0x02) != 0:
302-
if self._line_tracer_event == 1:
303-
if value == 0x02:
304-
self._line_tracer_count += 1
305-
if self._line_tracer_count > 5: self._line_tracer_event = 2
306-
else:
307-
self._line_tracer_event = 2
308-
if self._line_tracer_event == 2:
309-
if value != self._line_tracer_state or self._line_tracer_count > 5:
310-
self._line_tracer_state = value
311-
self._line_tracer_state_device._put(value << 5)
312-
if value == 0x02:
313-
self._line_tracer_event = 0
314-
self._line_tracer_count = 0
315-
value = (value2 >> 1) & 0x03
316-
if value == 0: value = 2
317-
elif value >= 2: value = 0
318-
if value != self._event_battery_state:
319-
self._battery_state_device._put(value, self._event_battery_state != -1)
320-
self._event_battery_state = value
321-
else:
322-
value = int(packet[4:5], 16)
323-
if value != 1: return False
324-
value = int(packet[6:8], 16)
325-
value -= 0x100
326-
self._signal_strength_device._put(value)
327-
value = int(packet[8:10], 16)
328-
self._left_proximity_device._put(value)
329-
value = int(packet[10:12], 16)
330-
self._right_proximity_device._put(value)
331-
value = int(packet[12:14], 16)
332-
self._left_floor_device._put(value)
333-
value = int(packet[14:16], 16)
334-
self._right_floor_device._put(value)
335-
acc_x = int(packet[16:20], 16)
336-
if acc_x > 0x7fff: acc_x -= 0x10000
337-
self._acceleration_device._put_at(0, acc_x)
338-
acc_y = int(packet[20:24], 16)
339-
if acc_y > 0x7fff: acc_y -= 0x10000
340-
self._acceleration_device._put_at(1, acc_y)
341-
acc_z = int(packet[24:28], 16)
342-
if acc_z > 0x7fff: acc_z -= 0x10000
343-
self._acceleration_device._put_at(2, acc_z)
344-
if acc_z < 8192 and acc_x > 8192 and acc_y > -4096 and acc_y < 4096: value = 1
345-
elif acc_z < 8192 and acc_x < -8192 and acc_y > -4096 and acc_y < 4096: value = -1
346-
elif acc_z < 8192 and acc_y > 8192 and acc_x > -4096 and acc_x < 4096: value = 2
347-
elif acc_z < 8192 and acc_y < -8192 and acc_x > -4096 and acc_x < 4096: value = -2
348-
elif acc_z > 12288 and acc_x > -8192 and acc_x < 8192 and acc_y > -8192 and acc_y < 8192: value = 3
349-
elif acc_z < -12288 and acc_x > -4096 and acc_x < 4096 and acc_y > -4096 and acc_y < 4096: value = -3
350-
else: value = 0
351-
if value != self._event_tilt:
352-
self._tilt_device._put(value, self._event_tilt != -4)
353-
self._event_tilt = value
354-
value = int(packet[28:30], 16)
355-
if value == 0:
356-
self._light = int(packet[30:34], 16)
357-
else:
358-
value = int(packet[30:32], 16)
359-
if value > 0x7f: value -= 0x100
360-
self._temperature = Util.round(value / 2.0 + 24)
361-
value = (int(packet[32:34], 16) + 200) / 100.0
362-
if value < 3.6: value = 0
363-
elif value <= 3.7: value = 1
364-
else: value = 2
365-
if value != self._event_battery_state:
366-
self._battery_state_device._put(value, self._event_battery_state != -1)
367-
self._event_battery_state = value
368-
self._light_device._put(self._light)
369-
self._temperature_device._put(self._temperature)
370-
value = int(packet[34:36], 16)
371-
self._input_a_device._put(value)
372-
value = int(packet[36:38], 16)
373-
self._input_b_device._put(value)
374-
value = int(packet[38:40], 16)
375-
if (value & 0x40) != 0:
376-
if self._line_tracer_event == 1:
377-
if value != 0x40:
378-
self._line_tracer_event = 2
379-
if self._line_tracer_event == 2:
380-
if value != self._line_tracer_state:
381-
self._line_tracer_state = value
382-
self._line_tracer_state_device._put(value)
383-
if value == 0x40:
384-
self._line_tracer_event = 0
258+
self._input_1_device._put(packet[2])
259+
self._input_2_device._put(packet[3])
260+
self._input_3_device._put(packet[4])
261+
self._remoctl_device._put(packet[5])
262+
self._battery_device._put(packet[6])
385263
return True
386264

387265
def _receive(self, connector):
388266
if connector:
389267
packet = connector.read()
390268
if packet:
391-
# if self._decode_sensory_packet(packet): # Temporary blocking
269+
if self._decode_sensory_packet(packet):
392270
if self._ready == False:
393271
self._ready = True
394272
Runner.register_checked()
395273
self._notify_sensory_device_data_changed()
396-
return True
274+
return True
397275
return False
398276

399277
def _send(self, connector):

neobot/serial_connector.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ def _open_port(self, port_name):
6666
parity = serial.PARITY_NONE,
6767
stopbits = serial.STOPBITS_ONE,
6868
bytesize = serial.EIGHTBITS,
69-
# Number of serial commands to accept before timing out
70-
timeout=0.1,
69+
timeout = 0.1, # Set a read timeout value in seconds
7170
)
7271
s.reset_input_buffer()
7372
s.reset_output_buffer()
@@ -121,12 +120,12 @@ def _read_line(self, serial, start_byte=None):
121120
bufferBytes = serial.inWaiting()
122121
if bufferBytes:
123122
line = c + serial.read(bufferBytes)
124-
print('Recv: ', line) # For debug, temporary
123+
print('Recv: ', line.hex()) # For debug, temporary
125124
if line[:2] == start_byte:
126-
return line
125+
return line[:VALID_PACKET_LENGTH]
127126
else:
128127
return ""
129-
except:
128+
except: # Port is closed
130129
return ""
131130

132131
def _read_packet(self, serial, start_byte=None):

test.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,13 @@
22

33
n = Neosoco()
44

5-
n.led_on("out1", 255)
6-
wait(1000)
5+
# case1) Turn on LED during 1s
6+
# n.led_on("out1", 255)
7+
# wait(1000)
8+
9+
# case2) Turn on LED when the distance is under 10cm
10+
while True:
11+
if n.get_value('in1') < 10:
12+
n.led_on("out1", 255)
13+
else:
14+
n.led_on("out1", 0)

0 commit comments

Comments
 (0)