Skip to content

Commit

Permalink
Untested multi wrapper support
Browse files Browse the repository at this point in the history
Look, it's not really multi-wrapper when it's hardcoded paddles only
  • Loading branch information
WilliamsJack committed Jun 21, 2021
1 parent 28760d9 commit 50f2586
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
3 changes: 3 additions & 0 deletions configs/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"motors": {
"type": "virtual"
},
"paddles": {
"type": "virtual"
},
"interface": {
"notifications": {
"enabled": true,
Expand Down
2 changes: 2 additions & 0 deletions interface/js/sights.control.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ $(document).on("ready", function () {
createMovementKeyBind(['d', 'right'], "RIGHT");
createFunctionKeyBind(['+', '='], "SPEED_UP");
createFunctionKeyBind(['-', '_'], "SPEED_DOWN");
createFunctionKeyBind(['i', 'space'], "PADDLE_FORWARD");
createFunctionKeyBind(['k', 'shift'], "PADDLE_REVERSE");
// Disable keyboard controls when modal is open
$(".modal").on('shown.bs.modal', function () {
keyboardJS.setContext(this.id);
Expand Down
15 changes: 15 additions & 0 deletions src/control_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self, mpid, pipe, config_file):
self.motors = MotorHandler(self.config)
# When script exits or is interrupted stop all servos
atexit.register(self.motors.close)
atexit.register(self.motors.close_paddle())
# Default controller state object
self.state = {
"LEFT_STICK_X": 0.0,
Expand Down Expand Up @@ -86,6 +87,10 @@ def keyboard_handler(self, control, value):
self.motors.speed = max(127, speed - 128)
# Send a message to SensorStream to update the interface with the current speed
self.pipe.send(["SYNC_SPEED", self.motors.speed])
elif control == "PADDLE_FORWARD":
self.motors.move_paddle(speed, -speed)
elif control == "PADDLE_REVERSE":
self.motors.move_paddle(-speed, speed)

def message_handler(self, buf):
# Load object from JSON
Expand Down Expand Up @@ -114,6 +119,16 @@ def message_handler(self, buf):
if value == "DOWN":
self.motors.speed = max(127, self.motors.speed - 128)
self.pipe.send(["SYNC_SPEED", self.motors.speed])
elif control == "DPAD_UP":
if value == "DOWN":
self.keyboard_handler("PADDLE_FORWARD", self.motors.speed)
elif value == "UP":
self.motors.stop_paddle()
elif control == "DPAD_DOWN":
if value == "DOWN":
self.keyboard_handler("PADDLE_REVERSE", self.motors.speed)
elif value == "UP":
self.motors.stop_paddle()
elif typ == "AXIS":
# If axis, store as float
value = float(msg["value"])
Expand Down
32 changes: 30 additions & 2 deletions src/motor_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ def __init__(self, config):
self.pm = PluginManager(MotorWrapper, os.getcwd() + "/src/motors")
# Load values from configuration file
self.type = config['motors']['type'].lower()
self.paddle_type = config['paddles']['type'].lower()
# Log loaded type
self.logger.info(f"Opening motor connection of type '{self.type}'")
# Create motor connection (from a list loaded by the plugin manager) using class specified in the config
try:
# Create motor connection (from a list loaded by the plugin manager) using class specified in the config
self.connection = self.pm.wrappers[self.type](config['motors'])
except Exception as e:
if isinstance(e, KeyError):
Expand All @@ -29,9 +30,23 @@ def __init__(self, config):
else:
traceback.print_exc()
# Fall back to virtual connection common to all motor connection errors
self.logger.warning("Falling back to virtual connection")
self.logger.warning("Falling back to virtual connection for motors")
self.connection = VirtualConnection(config['motors'])
self.type = 'virtual'
# Create paddle connection
try:
self.paddle_connection = self.pm.wrappers[self.paddle_type](config['paddles'])
except Exception as e:
if isinstance(e, KeyError):
self.logger.error(f"Could not determine motor connection type '{self.paddle_type}'")
elif isinstance(e, serial.serialutil.SerialException):
self.logger.error(f"Could not open motor connection of type '{self.paddle_type}'")
else:
traceback.print_exc()
# Fall back to virtual connection common to all motor connection errors
self.logger.warning("Falling back to virtual connection for paddles")
self.paddle_connection = VirtualConnection(config['paddles'])
self.paddle_type = 'virtual'
# Load speed defaults
self.speed = config['control']['default_speed'] * 128 - 1
self.last_left = 0
Expand Down Expand Up @@ -70,3 +85,16 @@ def move(self, left, right, independent=False):
# Store this message for comparison next time
self.last_left = left
self.last_right = right

def move_paddle(self, probably_left, probably_right):
self.paddle_connection.move_raw(left=probably_left, right=probably_right)

def stop_paddle(self):
# Set all servos to 0
self.paddle_connection.stop()

def close_paddle(self):
self.logger.info("Closing Paddle connection")
# Set all servos to 0 and close connection
self.paddle_connection.stop()
self.paddle_connection.close()

0 comments on commit 50f2586

Please sign in to comment.