add get/set firmware version and get/set ultrasonic threshold

This commit is contained in:
Silver Kuusik 2019-05-30 00:52:56 +02:00
parent 28a7b0a626
commit 1607d08f1c
3 changed files with 42 additions and 18 deletions

View File

@ -2,11 +2,11 @@
"status_led_pin": 5, "status_led_pin": 5,
"battery_coeff": 2.25, "battery_coeff": 2.25,
"sumo_id": "xxxxxxxx", "sumo_id": "xxxxxxxx",
"firmware_timestamp": "2019.05.26 14:17:00", "firmware_timestamp": "2019.05.30 00:42:00",
"firmware_version": "0.5.1", "firmware_version": "0.6.0",
"left_servo_tuning": 33, "left_servo_tuning": 33,
"right_servo_tuning": 33, "right_servo_tuning": 33,
"ultrasonic_distance": 40, "ultrasonic_threshold": 40,
"boot_code": "code.py", "boot_code": "code.py",
"left_line_value": 1000, "left_line_value": 1000,
"right_line_value": 1000, "right_line_value": 1000,

39
hal.py
View File

@ -136,7 +136,7 @@ class Sumorobot(object):
# Get the opponent distance # Get the opponent distance
self.opponent_distance = self.get_opponent_distance() self.opponent_distance = self.get_opponent_distance()
# When the opponent is close and the ping actually returned # When the opponent is close and the ping actually returned
if self.opponent_distance < self.config["ultrasonic_distance"] and self.opponent_distance > 0: if self.opponent_distance < self.config["ultrasonic_threshold"] and self.opponent_distance > 0:
# When not maximum score # When not maximum score
if self.opponent_score < 5: if self.opponent_score < 5:
# Increase the opponent score # Increase the opponent score
@ -156,25 +156,35 @@ class Sumorobot(object):
return opponent return opponent
# Function to update the config file
def update_config_file(self):
# Update the config file
with open("config.part", "w") as config_file:
config_file.write(ujson.dumps(self.config))
os.rename("config.part", "config.json")
# Function to update line calibration and write it to the config file # Function to update line calibration and write it to the config file
def calibrate_line_value(self): def calibrate_line_value(self):
# Read the line sensor values # Read the line sensor values
self.config["left_line_value"] = self.adc_line_left.read() self.config["left_line_value"] = self.adc_line_left.read()
self.config["right_line_value"] = self.adc_line_right.read() self.config["right_line_value"] = self.adc_line_right.read()
# Update the config file # Update the config file
with open("config.part", "w") as config_file: self.update_config_file()
config_file.write(ujson.dumps(self.config))
os.rename("config.part", "config.json")
# Function to update line threshold calibration and write it to the config file # Function to update line threshold and write it to the config file
def set_line_threshold(self, value): def set_line_threshold(self, value):
# Read the line sensor values # Read the line sensor values
self.config["left_line_threshold"] = value self.config["left_line_threshold"] = value
self.config["right_line_threshold"] = value self.config["right_line_threshold"] = value
# Update the config file # Update the config file
with open("config.part", "w") as config_file: self.update_config_file()
config_file.write(ujson.dumps(self.config))
os.rename("config.part", "config.json") # Function to update ultrasonic sensor threshold and write it to the config file
def set_ultrasonic_threshold(self, value):
# Read the line sensor values
self.config["ultrasonic_threshold"] = value
# Update the config file
self.update_config_file()
# Function to get light inensity from the phototransistors # Function to get light inensity from the phototransistors
def get_line(self, dir): def get_line(self, dir):
@ -299,18 +309,25 @@ class Sumorobot(object):
val = self.blockly_code val = self.blockly_code
) )
def get_firmware_version(self):
return dict(
type = "firmware_version",
val = self.config["firmware_version"]
)
def get_sensor_scope(self): def get_sensor_scope(self):
temp = self.sensor_scope temp = self.sensor_scope
temp['type'] = "sensor_scope" temp['type'] = "sensor_scope"
return temp return temp
def get_line_scope(self): def get_threshold_scope(self):
return dict( return dict(
type = "line_scope", type = "threshold_scope",
left_line_value = self.config["left_line_value"], left_line_value = self.config["left_line_value"],
right_line_value = self.config["right_line_value"], right_line_value = self.config["right_line_value"],
left_line_threshold = self.config["left_line_threshold"], left_line_threshold = self.config["left_line_threshold"],
right_line_threshold = self.config["right_line_threshold"] right_line_threshold = self.config["right_line_threshold"],
ultrasonic_threshold = self.config["ultrasonic_threshold"]
) )
def sleep(self, delay): def sleep(self, delay):

15
main.py
View File

@ -82,8 +82,8 @@ def ws_handler():
sumorobot.move(STOP) sumorobot.move(STOP)
# for terminating delays in code # for terminating delays in code
sumorobot.terminate = True sumorobot.terminate = True
elif b'get_line_scope' in data: elif b'get_threshold_scope' in data:
conn.send(ujson.dumps(sumorobot.get_line_scope())) conn.send(ujson.dumps(sumorobot.get_threshold_scope()))
elif b'get_sensor_scope' in data: elif b'get_sensor_scope' in data:
conn.send(ujson.dumps(sumorobot.get_sensor_scope())) conn.send(ujson.dumps(sumorobot.get_sensor_scope()))
elif b'get_python_code' in data: elif b'get_python_code' in data:
@ -92,6 +92,9 @@ def ws_handler():
elif b'get_blockly_code' in data: elif b'get_blockly_code' in data:
#print("main.py sending blockly code=", sumorobot.get_blockly_code()) #print("main.py sending blockly code=", sumorobot.get_blockly_code())
conn.send(ujson.dumps(sumorobot.get_blockly_code())) conn.send(ujson.dumps(sumorobot.get_blockly_code()))
elif b'get_firmware_version' in data:
#print("main.py get_firmware_version")
conn.send(ujson.dumps(sumorobot.get_firmware_version()))
elif b'toggle_sensor_feedback' in data: elif b'toggle_sensor_feedback' in data:
data = ujson.loads(data) data = ujson.loads(data)
sumorobot.sensor_feedback = not sumorobot.sensor_feedback sumorobot.sensor_feedback = not sumorobot.sensor_feedback
@ -107,11 +110,15 @@ def ws_handler():
sumorobot.compiled_python_code = compile(data['val'], "snippet", "exec") sumorobot.compiled_python_code = compile(data['val'], "snippet", "exec")
elif b'calibrate_line_value' in data: elif b'calibrate_line_value' in data:
sumorobot.calibrate_line_value() sumorobot.calibrate_line_value()
#print('main.py: calibrate_line_value') #print("main.py: calibrate_line_value")
elif b'set_line_threshold' in data: elif b'set_line_threshold' in data:
data = ujson.loads(data) data = ujson.loads(data)
sumorobot.set_line_threshold(int(data['val'])) sumorobot.set_line_threshold(int(data['val']))
#print('main.py: set_line_threshold') #print("main.py: set_line_threshold")
elif b'set_ultrasonic_threshold' in data:
data = ujson.loads(data)
sumorobot.set_ultrasonic_threshold(int(data['val']))
#print("main.py: set_ultrasonic_threshold")
elif b'Gone' in data: elif b'Gone' in data:
print("main.py: server said 410 Gone, attempting to reconnect...") print("main.py: server said 410 Gone, attempting to reconnect...")
#conn = uwebsockets.connect(url) #conn = uwebsockets.connect(url)