From bee14887a2b67244febd7a2c9c8981205b58a60c Mon Sep 17 00:00:00 2001 From: OrganomagnesiumHalide <99297401+OrganomagnesiumHalide@users.noreply.github.com> Date: Sat, 12 Nov 2022 19:12:25 -0800 Subject: [PATCH] Changed command to IntEnum. Fixes issue #675 --- .../sabertooth2x12/sabertooth2x12/board.py | 38 ++++++++++++++----- mil_common/perception/yolov7-ros | 2 +- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/mil_common/drivers/sabertooth2x12/sabertooth2x12/board.py b/mil_common/drivers/sabertooth2x12/sabertooth2x12/board.py index 1392058d4..7ea54ef44 100644 --- a/mil_common/drivers/sabertooth2x12/sabertooth2x12/board.py +++ b/mil_common/drivers/sabertooth2x12/sabertooth2x12/board.py @@ -1,11 +1,19 @@ #!/usr/bin/env python3 import struct +from enum import IntEnum import serial from .simulated import SimulatedSabertooth2x12 +class CommandEnum(IntEnum): + MOTOR1_FORWARD + MOTOR1_BACKWARD + MOTOR2_FORWARD + MOTOR2_BACKWARD + + class Sabertooth2x12: """ Helper class to interface with the Sabertooth 2x12 regenerative motor driver. @@ -38,7 +46,7 @@ def __init__( self.ser = SimulatedSabertooth2x12() @staticmethod - def make_packet(address: int, command: int, data: int) -> bytes: + def make_packet(address: int, command: CommandEnum, data: int) -> bytes: """ Constructs a packet given an address, command, and data. The checksum is added on to the end of the packet. All four integers are packed as unsigned @@ -46,21 +54,31 @@ def make_packet(address: int, command: int, data: int) -> bytes: Args: address (int): ??? - command (int): The command to send. + command (CommandEnum): The command to send. data (int): The data to put in the packet. Returns: bytes: The constructed packet. """ - checksum = (address + command + data) & 127 - return struct.pack("BBBB", address, command, data, checksum) + intCommand = 0 + if command == CommandEnum.MOTOR1_FORWARDS: + intCommand = 0 + elif command == CommandEnum.MOTOR2_FORWARDS: + intCommand = 4 + elif command == CommandEnum.MOTOR1_BACKWARDS: + intCommand = 1 + elif command == CommandEnum.MOTOR2_BACKWARDS: + intCommand = 5 + + checksum = (address + intCommand + data) & 127 + return struct.pack("BBBB", address, intCommand, data, checksum) - def send_packet(self, command: int, data: int) -> None: + def send_packet(self, command: CommandEnum, data: int) -> None: """ Sends a packet over the serial connection using the class' address. Args: - command (int): The command to send. + command (Command): The command to send. data (int): The data to put in the packet. """ packet = self.make_packet(self.address, command, data) @@ -75,9 +93,9 @@ def set_motor1(self, speed: float) -> None: speed (float): The speed to set the first motor to. """ if speed < 0: - command = 1 + command = CommandEnum.MOTOR1_BACKWARDS else: - command = 0 + command = CommandEnum.MOTOR1_FORWARDS data = int(min(1.0, abs(speed)) * 127) self.send_packet(command, data) @@ -90,8 +108,8 @@ def set_motor2(self, speed: float) -> None: speed (float): The speed to set the second motor to. """ if speed < 0: - command = 5 + command = CommandEnum.MOTOR2_BACKWARDS else: - command = 4 + command = CommandEnum.MOTOR2_FORWARDS data = int(min(1.0, abs(speed)) * 127) self.send_packet(command, data) diff --git a/mil_common/perception/yolov7-ros b/mil_common/perception/yolov7-ros index dba012183..beadcf868 160000 --- a/mil_common/perception/yolov7-ros +++ b/mil_common/perception/yolov7-ros @@ -1 +1 @@ -Subproject commit dba012183a5d2ab1035c232fa7174debdfdf37e9 +Subproject commit beadcf868bb4b7a9be532c449af258e85c52bfa0