|
| 1 | +# python helpers for the body panda |
| 2 | +import struct |
| 3 | + |
| 4 | +from panda import Panda |
| 5 | + |
| 6 | + |
| 7 | +class PandaBody(Panda): |
| 8 | + SUPPORTED_DEVICES = (Panda.HW_TYPE_BODY,) |
| 9 | + |
| 10 | + def __init__(self, *args, **kwargs): |
| 11 | + super().__init__(*args, **kwargs) |
| 12 | + if self.get_type() not in PandaBody.SUPPORTED_DEVICES: |
| 13 | + raise ValueError("connected device is not a body panda") |
| 14 | + |
| 15 | + # ****************** Motor Control ***************** |
| 16 | + |
| 17 | + def motor_set_speed(self, motor: int, speed: int) -> None: |
| 18 | + assert motor in (1, 2), "motor must be 1 or 2" |
| 19 | + assert -100 <= speed <= 100, "speed must be between -100 and 100" |
| 20 | + speed_param = speed if speed >= 0 else (256 + speed) |
| 21 | + self._handle.controlWrite(Panda.REQUEST_OUT, 0xe0, motor, speed_param, b'') |
| 22 | + |
| 23 | + def motor_set_target_rpm(self, motor: int, rpm: float) -> None: |
| 24 | + assert motor in (1, 2), "motor must be 1 or 2" |
| 25 | + target_deci_rpm = int(round(rpm * 10.0)) |
| 26 | + assert -32768 <= target_deci_rpm <= 32767, "target rpm out of range (-3276.8 to 3276.7)" |
| 27 | + target_param = target_deci_rpm if target_deci_rpm >= 0 else (target_deci_rpm + (1 << 16)) |
| 28 | + self._handle.controlWrite(Panda.REQUEST_OUT, 0xe4, motor, target_param, b'') |
| 29 | + |
| 30 | + def motor_stop(self, motor: int) -> None: |
| 31 | + assert motor in (1, 2), "motor must be 1 or 2" |
| 32 | + self._handle.controlWrite(Panda.REQUEST_OUT, 0xe1, motor, 0, b'') |
| 33 | + |
| 34 | + def motor_get_encoder_state(self, motor: int) -> tuple[int, float]: |
| 35 | + assert motor in (1, 2), "motor must be 1 or 2" |
| 36 | + dat = self._handle.controlRead(Panda.REQUEST_IN, 0xe2, motor, 0, 8) |
| 37 | + position, rpm_milli = struct.unpack("<ii", dat) |
| 38 | + return position, rpm_milli / 1000.0 |
| 39 | + |
| 40 | + def motor_reset_encoder(self, motor: int) -> None: |
| 41 | + assert motor in (1, 2), "motor must be 1 or 2" |
| 42 | + self._handle.controlWrite(Panda.REQUEST_OUT, 0xe3, motor, 0, b'') |
0 commit comments