Skip to content

Commit cf762fb

Browse files
committed
Make robot connections over 2 acceptable
1 parent 211ac94 commit cf762fb

File tree

6 files changed

+102
-12
lines changed

6 files changed

+102
-12
lines changed

multi-robots_test.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from neopia import *
2+
3+
socos = (Neosoco(0), Neosoco(1))
4+
5+
# wait until two robots are ready
6+
wait_until_ready()
7+
8+
def init():
9+
for soco in socos:
10+
soco.servo_reset_degree() # servo moto init
11+
12+
def ready_condition(soco):
13+
print(soco.get_value('in3'))
14+
return soco.get_value('in3') < 30
15+
16+
def act_condition(soco):
17+
print(soco.get_value('in3'))
18+
return soco.get_value('in3') > 70 # when recognize a clap
19+
20+
def led_blink(soco):
21+
soco.led_on('out3')
22+
wait(500)
23+
soco.led_off('out3')
24+
wait(500)
25+
26+
def wag_tail(soco):
27+
soco.servo_rotate_by_degree('out1', 'forward', '100', '60')
28+
soco.servo_rotate_by_degree('out1', 'backward', '100', '60')
29+
soco.servo_rotate_by_degree('out1', 'forward', '100', '0')
30+
31+
def move_forth_back(soco, reverse=False):
32+
if reverse:
33+
dir1 = 'backward'
34+
dir2 = 'forward'
35+
else:
36+
dir1 = 'forward'
37+
dir2 = 'backward'
38+
39+
soco.motor_move(dir1)
40+
wait(1000)
41+
soco.motor_move(dir2)
42+
wait(1000)
43+
soco.motor_stop('both')
44+
45+
def turn_right_left(soco, reverse=False):
46+
if reverse:
47+
wheel1 = 'right'
48+
wheel2 = 'left'
49+
else:
50+
wheel1 = 'left'
51+
wheel2 = 'right'
52+
53+
soco.motor_rotate(wheel1, 'forward', '30') # turn right
54+
soco.motor_rotate(wheel2, 'backward', '30')
55+
wait(2000)
56+
soco.motor_rotate(wheel2, 'forward', '30') # turn left
57+
soco.motor_rotate(wheel1, 'backward', '30')
58+
wait(2000)
59+
soco.motor_stop('both')
60+
61+
def dance(soco):
62+
move_forth_back(soco)
63+
move_forth_back(soco, True)
64+
turn_right_left(soco)
65+
turn_right_left(soco, True)
66+
67+
soco.motor_move('forward')
68+
wait(1000)
69+
soco.motor_stop('both')
70+
71+
init()
72+
for soco in socos:
73+
while_do(ready_condition, led_blink, soco)
74+
while_do(ready_condition, wag_tail, soco)
75+
when_do(act_condition, dance, soco)
76+
wait(-1) # wait forever

neopia/neosoco.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def _init(self, port_name):
333333
self._add_neobot(self._neobot)
334334
Runner.register_robot(self)
335335
Runner.start()
336-
self._neobot._init(port_name)
336+
self._neobot._init(port_name, Runner.get_robots())
337337

338338
def find_device_by_id(self, device_id):
339339
return self._neobot.find_device_by_id(device_id)
@@ -599,6 +599,7 @@ def motor_stop(self, which_motor: str):
599599
else:
600600
Runner.shutdown() # The motor will not stop unless the shutdown() function is used
601601
raise TypeError
602+
Runner.wait(100) # Since broadcast from controller is per 100ms
602603

603604
def _convert_sacle_within_100(self, value, cvt_max_val):
604605
# Map to 0~limited_val from 0~100(max), it's same as Entry

neopia/neosoco_neobot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def _run(self):
118118
except:
119119
pass
120120

121-
def _init(self, port_name=None):
121+
def _init(self, port_name=None, reg_neobots=None):
122122
Runner.register_required()
123123
self._running = True
124124
thread = threading.Thread(target=self._run)
@@ -128,7 +128,7 @@ def _init(self, port_name=None):
128128

129129
tag = "Neosoco[{}]".format(self._index)
130130
self._connector = SerialConnector(tag, NeosocoConnectionChecker(self))
131-
result = self._connector.open(port_name)
131+
result = self._connector.open(port_name, reg_neobots)
132132
if result == Result.FOUND:
133133
while self._ready == False and self._is_disposed() == False:
134134
time.sleep(0.01)

neopia/runner.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ def shutdown():
165165
def register_robot(robot):
166166
Runner._added.append(robot)
167167

168+
@staticmethod
169+
def get_robots():
170+
return Runner._robots
171+
168172
@staticmethod
169173
def unregister_robot(robot):
170174
Runner._removed.append(robot)

neopia/serial_connector.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
BAUD_RATE = 115200
2929
VALID_PACKET_LENGTH = 8
3030
RETRY = 10
31-
TIMEOUT = 0.4
31+
TIMEOUT = 0.5
3232
START_BYTES = bytearray([0xab, 0xcd])
3333

3434
class SerialConnector(object):
@@ -43,17 +43,26 @@ def __init__(self, tag, connection_checker, loader=None):
4343
self._timestamp = 0
4444
self._connected = False
4545

46-
def open(self, port_name=None):
46+
def open(self, port_name=None, reg_robots=None):
4747
if port_name:
4848
result = self._open_port(port_name)
4949
if result != Result.NOT_AVAILABLE:
5050
return result
5151
else:
52+
# Get all using ports
53+
if reg_robots:
54+
reg_ports = []
55+
[reg_ports.append(robot._neobot._connector._port_name) for robot in reg_robots]
56+
# Get all ports on the computer
5257
ports = serial.tools.list_ports.comports()
58+
5359
for port in ports:
54-
result = self._open_port(port[0])
55-
if result != Result.NOT_AVAILABLE:
56-
return result
60+
if reg_ports and (port[0] in reg_ports):
61+
continue
62+
else:
63+
result = self._open_port(port[0])
64+
if result != Result.NOT_AVAILABLE:
65+
return result
5766
self._print_error("No available USB to BLE bridge")
5867
return Result.NOT_AVAILABLE
5968

@@ -120,7 +129,7 @@ def _read_line(self, serial, start_byte=None):
120129
bufferBytes = serial.inWaiting()
121130
if bufferBytes:
122131
line = c + serial.read(bufferBytes)
123-
print('Recv: ', line.hex()) # For debug, temporary
132+
# print('Recv: ', line.hex()) # For debug, temporary
124133
if line[:2] == start_byte:
125134
return line[:VALID_PACKET_LENGTH]
126135
else:
@@ -143,7 +152,7 @@ def write(self, packet):
143152
if self._serial:
144153
try:
145154
self._serial.write(bytes.fromhex(packet))
146-
print('Sent: ', bytes.fromhex(packet)) # For debug, temporary
155+
# print('Sent: ', bytes.fromhex(packet)) # For debug, temporary
147156
except:
148157
pass
149158

setup.py

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

33
setup(
44
name="neopia",
5-
version="0.1.4",
5+
version="0.1.5",
66
author="RoboticsWare",
7-
author_email="neopia.uz@google.ocm",
7+
author_email="neopia.uz@google.com",
88
description="Python library for Neopia neobot",
99
url="https://github.com/roboticsware/pylib_neobot.git",
1010
download_url="https://github.com/roboticsware/pylib_neobot/archive/refs/heads/master.zip",

0 commit comments

Comments
 (0)