Skip to content

Commit

Permalink
Merge pull request #167 from Luos-io/rc_2.2.6
Browse files Browse the repository at this point in the history
Pyluos version 2.2.6
  • Loading branch information
JeromeGalan authored Oct 21, 2022
2 parents ec4fffb + 2fdc9d2 commit 667140d
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 85 deletions.
10 changes: 5 additions & 5 deletions pyluos/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,22 +251,22 @@ def _poll_and_up(self):

# Update our model with the new state.
def _update(self, new_state):
if 'dead_service' in new_state :
if 'dead_service' in new_state.keys() :
#we have lost a service put a flag on this service
alias = new_state['dead_service']
if hasattr(self, alias):
getattr(self, alias)._kill()
if 'assert' in new_state :
if 'assert' in new_state.keys() :
# A node assert, print assert informations
if (('node_id' in new_state['assert']) and ('file' in new_state['assert']) and ('line' in new_state['assert'])):
if (('node_id' in new_state.keys()['assert']) and ('file' in new_state.keys()['assert']) and ('line' in new_state.keys()['assert'])):
s = "************************* ASSERT *************************\n"
s += "* Node " + str(new_state['assert']['node_id']) + " assert in file " + new_state['assert']['file'] + " line " + str(new_state['assert']['line'])
s += "\n**********************************************************"
print (s)
if 'services' not in new_state:
if 'services' not in new_state.keys():
return

for alias, mod in new_state['services'].items():
for alias, mod in new_state.keys()['services'].items():
if hasattr(self, alias):
getattr(self, alias)._update(mod)

Expand Down
26 changes: 14 additions & 12 deletions pyluos/io/serial_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

class Serial(IOHandler):
poll_frequency = 200
period = 1 / poll_frequency
message_rate = 12000 # Max messages per seconds

@classmethod
def available_hosts(cls):
Expand All @@ -44,7 +46,7 @@ def __init__(self, host, baudrate=None):
self._serial = _serial.Serial(host, baudrate)
self._serial.flush()

self._msg = queue.Queue(500)
self._msg = queue.Queue(int((self.message_rate / self.period) / 1000))
self._running = True

self._poll_loop = Thread(target=self._poll)
Expand Down Expand Up @@ -112,22 +114,22 @@ def extract_line(s):
return extract_line(s[H+1:])
else:
# Footer is ok
data = s[data_start:data_end]
if data == b'{}\n':
# Datas are void
return b'', s[data_end + 1:]
else:
# Datas are not void
return data, s[data_end + 1:]

period = 1 / self.poll_frequency
for _, search_void in enumerate(s[data_start:6]):
if search_void == b'{}\n':
# Drop void datas
for index, search_header in enumerate(data):
if search_header == b'\x7E':
return extract_line(s[(data_start + index + 1):])
# Return the data
return s[data_start:data_end], s[data_end + 1:]

buff = b''

while self._running:
to_read = self._serial.in_waiting

if to_read == 0:
time.sleep(period)
if (to_read == 0) and (len(buff) == 0):
time.sleep(self.period)
continue

s = self._serial.read(to_read)
Expand Down
2 changes: 1 addition & 1 deletion pyluos/services/angle.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def rot_position(self, new_val):

def _update(self, new_state):
Service._update(self, new_state)
if 'rot_position' in new_state:
if 'rot_position' in new_state.keys():
new_val = new_state['rot_position']
if new_val != self._value:
self._pub_event('changed', self._value, new_val)
Expand Down
2 changes: 1 addition & 1 deletion pyluos/services/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def distance(self):

def _update(self, new_state):
Service._update(self, new_state)
if 'trans_position' in new_state:
if 'trans_position' in new_state.keys():
new_dist = new_state['trans_position']
if new_dist != self._value:
self._pub_event('changed', self._value, new_dist)
Expand Down
22 changes: 11 additions & 11 deletions pyluos/services/imu.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,27 +184,27 @@ def heading(self, enable):

def _update(self, new_state):
Service._update(self, new_state)
if 'quaternion' in new_state:
if 'quaternion' in new_state.keys():
self._quaternion = new_state['quaternion']
if 'accel' in new_state:
if 'accel' in new_state.keys():
self._acceleration = new_state['accel']
if 'gyro' in new_state:
if 'gyro' in new_state.keys():
self._gyro = new_state['gyro']
if 'compass' in new_state:
if 'compass' in new_state.keys():
self._compass = new_state['compass']
if 'euler' in new_state:
if 'euler' in new_state.keys():
self._euler = new_state['euler']
if 'rotational_matrix' in new_state:
if 'rotational_matrix' in new_state.keys():
self._rotational_matrix = new_state['rotational_matrix']
if 'pedometer' in new_state:
if 'pedometer' in new_state.keys():
self._pedometer = new_state['pedometer']
if 'walk_time' in new_state:
if 'walk_time' in new_state.keys():
self._walk_time = new_state['walk_time']
if 'linear_accel' in new_state:
if 'linear_accel' in new_state.keys():
self._linear_acceleration = new_state['linear_accel']
if 'gravity_vector' in new_state:
if 'gravity_vector' in new_state.keys():
self._gravity_vector = new_state['gravity_vector']
if 'heading' in new_state:
if 'heading' in new_state.keys():
self._heading = new_state['heading']

def control(self):
Expand Down
2 changes: 1 addition & 1 deletion pyluos/services/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def lux(self):

def _update(self, new_state):
Service._update(self, new_state)
if 'lux' in new_state:
if 'lux' in new_state.keys():
new_light = new_state['lux']
if new_light != self._value:
self._pub_event('changed', self._value, new_light)
Expand Down
2 changes: 1 addition & 1 deletion pyluos/services/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def scale(self, value):

def _update(self, new_state):
Service._update(self, new_state)
if 'force' in new_state:
if 'force' in new_state.keys():
new_force = new_state['force']
if new_force != self._value:
self._pub_event('changed', self._value, new_force)
Expand Down
2 changes: 1 addition & 1 deletion pyluos/services/pressure.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def pressure(self, new_val):

def _update(self, new_state):
Service._update(self, new_state)
if 'pressure' in new_state:
if 'pressure' in new_state.keys():
new_val = new_state['pressure']
if new_val != self._value:
self._pub_event('changed', self._value, new_val)
Expand Down
19 changes: 3 additions & 16 deletions pyluos/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def __init__(self,
self._firmware_revision = "Unknown"
self._luos_revision = "Unknown"
self._robus_revision = "Unknown"
self._uuid = [0, 0, 0]
self._killed = False
self._last_update = time.time()
self._luos_statistics = {}
Expand All @@ -55,13 +54,11 @@ def _update(self, new_state):
if ((time.time() - self._last_update) != 0):
self.refresh_freq = ((200.0 * self.refresh_freq) + (1.0 / (time.time() - self._last_update))) / 201.0
self._last_update = time.time()
if 'revision' in new_state:
if 'revision' in new_state.keys():
self._firmware_revision = new_state['revision']
if 'luos_revision' in new_state:
if 'luos_revision' in new_state.keys():
self._luos_revision = new_state['luos_revision']
if 'uuid' in new_state:
self._uuid = new_state['uuid']
if 'luos_statistics' in new_state:
if 'luos_statistics' in new_state.keys():
self._luos_statistics = new_state['luos_statistics']
self._luos_statistics['alias'] = self.alias

Expand Down Expand Up @@ -107,16 +104,6 @@ def luos_revision(self):
return self._luos_revision

@property
def uuid(self):
self._uuid = None
self._push_value('uuid', "")

tick_start = time.time()
while time.time() - tick_start < READ_TIMEOUT and self._uuid is None:
time.sleep(0.01)

return self._uuid

@property
def luos_statistics(self):
"""Get service statistics with a timeout of 1 second."""
Expand Down
12 changes: 6 additions & 6 deletions pyluos/services/servoMotor.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,17 +499,17 @@ def dxl_register(self, register, val):

def _update(self, new_state):
Service._update(self, new_state)
if 'rot_position' in new_state:
if 'rot_position' in new_state.keys():
self._rot_position = new_state['rot_position']
if 'rot_speed' in new_state:
if 'rot_speed' in new_state.keys():
self._rot_speed = new_state['rot_speed']
if 'trans_position' in new_state:
if 'trans_position' in new_state.keys():
self._trans_position = new_state['trans_position']
if 'trans_speed' in new_state:
if 'trans_speed' in new_state.keys():
self._trans_speed = new_state['trans_speed']
if 'current' in new_state:
if 'current' in new_state.keys():
self._current = new_state['current']
if 'temperature' in new_state:
if 'temperature' in new_state.keys():
self._temperature = new_state['temperature']

def control(self):
Expand Down
2 changes: 1 addition & 1 deletion pyluos/services/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def state(self, new_val):

def _update(self, new_state):
Service._update(self, new_state)
if 'io_state' in new_state:
if 'io_state' in new_state.keys():
new_state = new_state['io_state']
if new_state != self._value:
self._pub_event('changed', self._value, new_state)
Expand Down
22 changes: 11 additions & 11 deletions pyluos/services/unknown.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, id, alias, device):

def _update(self, new_state):
Service._update(self, new_state)
if 'io_state' in new_state:
if 'io_state' in new_state.keys():
val = new_state['io_state']
if val != self._state:
self._pub_event('changed', self._state, val)
Expand All @@ -41,25 +41,25 @@ def _update(self, new_state):
self._pub_event(evt, self._state, val)

self._state = val
if 'rot_position' in new_state:
if 'rot_position' in new_state.keys():
self._angular_position = new_state['rot_position']
if 'rot_speed' in new_state:
if 'rot_speed' in new_state.keys():
self._angular_speed = new_state['rot_speed']
if 'trans_position' in new_state:
if 'trans_position' in new_state.keys():
self._trans_position = new_state['trans_position']
if 'trans_speed' in new_state:
if 'trans_speed' in new_state.keys():
self._trans_speed = new_state['trans_speed']
if 'current' in new_state:
if 'current' in new_state.keys():
self._current = new_state['current']
if 'temperature' in new_state:
if 'temperature' in new_state.keys():
self._temperature = new_state['temperature']
if 'pressure' in new_state:
if 'pressure' in new_state.keys():
self._pressure = new_state['pressure']
if 'lux' in new_state:
if 'lux' in new_state.keys():
self._lux = new_state['lux']
if 'force' in new_state:
if 'force' in new_state.keys():
self._load = new_state['force']
if 'volt' in new_state:
if 'volt' in new_state.keys():
self._volt = new_state['volt']


Expand Down
2 changes: 1 addition & 1 deletion pyluos/services/voltage.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def volt(self, new_val):

def _update(self, new_state):
Service._update(self, new_state)
if 'volt' in new_state:
if 'volt' in new_state.keys():
new_val = new_state['volt']
if new_val != self._value:
self._pub_event('changed', self._value, new_val)
Expand Down
6 changes: 3 additions & 3 deletions pyluos/tools/bootloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def luos_flash(args):
try:
args.port= serial_discover(os.getenv('LUOS_BAUDRATE', args.baudrate))[0]
except:
sys.exit("Can't find any Gate interface")
sys.exit()
return

# state used to check each step
Expand Down Expand Up @@ -553,7 +553,7 @@ def luos_detect(args):
try:
args.port= serial_discover(os.getenv('LUOS_BAUDRATE', args.baudrate))[0]
except:
sys.exit("Can't find any Gate interface")
sys.exit()
return

# detect network
Expand All @@ -577,7 +577,7 @@ def luos_reset(args):
try:
args.port= serial_discover(os.getenv('LUOS_BAUDRATE', args.baudrate))[0]
except:
sys.exit("Can't find any Gate interface")
sys.exit()
return


Expand Down
44 changes: 31 additions & 13 deletions pyluos/tools/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,46 @@ def serial_discover(baudrate=1000000):
available_serial = []
print("Searching for a gate available")
for serial_host in serial_hosts:
print("Testing " + str(serial_host))
print("Search a Gate on port " + str(serial_host))
try:
port = serial.Serial(serial_host, baudrate, timeout=0.05)
port = serial.Serial(serial_host, baudrate, timeout=0.2)
time.sleep(0.1)
except:
continue

s = b'{}'
port.write(b'\x7E' + struct.pack('<H', len(s)) + s + b'\x81')
time.sleep(0.01)
s = b'{\"discover\": {}}'
port.write(b'\x7E' + struct.pack('<H', len(s)) + s + b'\x81')
port.flush()
for x in range(10):
if port is not None:
s = b'{}'
port.write(b'\x7E' + struct.pack('<H', len(s)) + s + b'\x81')
time.sleep(0.01)
port.readline()
port.flush()
time.sleep(0.01)
s = b'{\"discover\": {}}'
port.write(b'\x7E' + struct.pack('<H', len(s)) + s + b'\x81')
state = port.readline()
if ('gate'.encode() in state):
gateResponse = False
if 'gate'.encode() in state:
gateResponse = True
elif len(state):
# if many other messages are received, drop them and retry reception
for read_retry in range(1000):
state = port.readline()
if 'gate'.encode() in state:
gateResponse = True
break
if gateResponse:
available_serial.append(serial_host)
continue
port.reset_output_buffer()
port.close()

port.close()
return available_serial
if available_serial :
return available_serial
else:
print("... No gate detected")
return []

def main():
parser = argparse.ArgumentParser()
parser.add_argument("--baudrate", action="store",
help="Choose pyluos serial baudrate default value = 1000000",
default=1000000)
Expand Down
2 changes: 1 addition & 1 deletion pyluos/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = '2.2.5'
version = '2.2.6'

0 comments on commit 667140d

Please sign in to comment.