diff options
author | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2015-12-09 16:13:32 +0100 |
---|---|---|
committer | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2015-12-09 16:13:32 +0100 |
commit | 655d894cce5b0f01567d0a3e94bbb9cad024ad3e (patch) | |
tree | e2e83c37bc30785ba816853cf665bf37200d5066 /robot | |
parent | fa58ab7b297d1df467ac3a4b77397b3ca05325e9 (diff) |
Improve robot remote-control script
Diffstat (limited to 'robot')
-rwxr-xr-x | robot/main.py | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/robot/main.py b/robot/main.py index 06f10a0..7035829 100755 --- a/robot/main.py +++ b/robot/main.py @@ -17,9 +17,10 @@ import eventlet import ev3dev ev3_names = { - 'lego-ev3-gyro': 'gyroscope', - 'lego-ev3-touch': 'touch', - 'lego-ev3-us': 'ultrasonic' + 'lego-ev3-color': 'Color', + 'lego-ev3-gyro': 'Gyroscope', + 'lego-ev3-touch': 'Touch', + 'lego-ev3-us': 'Ultrasonic' } ev3_motors = [ @@ -57,28 +58,32 @@ def stop(): motor.stop(stop_command='brake') def notifier(): - def read_sensor(path): + sensors_path = '/sys/class/lego-sensor' + sensors = {} + + for sensor in os.listdir(sensors_path): + path = os.path.join(sensors_path, sensor) with open(os.path.join(path, 'driver_name'), 'r') as f: name = f.read().strip() - with open(os.path.join(path, 'value0'), 'r') as f: - value = int(f.read().strip()) with open(os.path.join(path, 'decimals'), 'r') as f: decimals = int(f.read().strip()) - if decimals > 0: - value /= 10**decimals + multiplier = 10**(-decimals) with open(os.path.join(path, 'units'), 'r') as f: - units = f.read().strip() - if units: - value = '{} {}'.format(value, units) - return (ev3_names.get(name, name), value) + unit = f.read().strip() + friendly_name = ev3_names.get(name, name) + sensors[path] = (friendly_name, multiplier, unit) + + print(sorted(sensors.items())) - sensors_path = '/sys/class/lego-sensor' while True: eventlet.sleep(0.2) try: message = {'event': 'update', 'sensors': {}} - for sensor in os.listdir(sensors_path): - name, value = read_sensor(os.path.join(sensors_path, sensor)) + for path, (name, multiplier, unit) in sorted(sensors.items()): + with open(os.path.join(path, 'value0'), 'rb') as f: + value = round(int(f.read().strip()) * multiplier, 1) + if unit: + value = '{} {}'.format(value, unit) message['sensors'][name] = value text = json.dumps(message) |