Skip to content

Commit 7c0c1d9

Browse files
jhuang2006stefpi
andauthored
Body v2: Hoverboard Motors (#2308)
* body * hoverboard motors * pass test * body ignition exception and remove used comms Co-authored-by: rexblade21 <zkhuang4968@gmail.com> * send extra empty message to distinguish fingerprint * can.h body v2 put safety hook = 1 * change v2 extra id to 0x396 * fix mirsa style error * move ignition exception to seperate bus 2 if statement * silence heartbeat siren on body * turn ignition off on switch repress * 0x250 interception in panda fw * send extra body data, same as v1 body * cleanup data passing for more predictable inputs and lower rpm deadband * remove extra downscaling * change v2 fingerprint from 0x002 to 0x222 * clean up, bus 0 ignition exception and trq mode * keep in SPD mode for now * remove comment * clean * apply suggestions --------- Co-authored-by: stefpi <19478336+stefpi@users.noreply.github.com>
1 parent 14b1906 commit 7c0c1d9

19 files changed

Lines changed: 5031 additions & 543 deletions

board/body/__init__.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,37 @@ class PandaBody(Panda):
88
MOTOR_LEFT: int = 1
99
MOTOR_RIGHT: int = 2
1010

11+
def __init__(self, *args, **kwargs):
12+
super().__init__(*args, **kwargs)
13+
self._rpm_left: int = 0
14+
self._rpm_right: int = 0
15+
16+
@property
17+
def rpm_left(self) -> int:
18+
return self._rpm_left
19+
20+
@rpm_left.setter
21+
def rpm_left(self, value: int) -> None:
22+
self._rpm_left = int(value)
23+
self._handle.controlWrite(Panda.REQUEST_OUT, 0xb3, self._rpm_left, self._rpm_right, b'')
24+
25+
@property
26+
def rpm_right(self) -> int:
27+
return self._rpm_right
28+
29+
@rpm_right.setter
30+
def rpm_right(self, value: int) -> None:
31+
self._rpm_right = int(value)
32+
self._handle.controlWrite(Panda.REQUEST_OUT, 0xb3, self._rpm_left, self._rpm_right, b'')
33+
1134
# ****************** Motor Control *****************
1235
@staticmethod
1336
def _ensure_valid_motor(motor: int) -> None:
1437
if motor not in (PandaBody.MOTOR_LEFT, PandaBody.MOTOR_RIGHT):
1538
raise ValueError("motor must be MOTOR_LEFT or MOTOR_RIGHT")
1639

17-
def motor_set_speed(self, motor: int, speed: int) -> None:
18-
self._ensure_valid_motor(motor)
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-
self._ensure_valid_motor(motor)
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-
self._ensure_valid_motor(motor)
32-
self._handle.controlWrite(Panda.REQUEST_OUT, 0xe1, motor, 0, b'')
33-
3440
def motor_get_encoder_state(self, motor: int) -> tuple[int, float]:
3541
self._ensure_valid_motor(motor)
3642
dat = self._handle.controlRead(Panda.REQUEST_IN, 0xe2, motor, 0, 8)
3743
position, rpm_milli = struct.unpack("<ii", dat)
3844
return position, rpm_milli / 1000.0
39-
40-
def motor_reset_encoder(self, motor: int) -> None:
41-
self._ensure_valid_motor(motor)
42-
self._handle.controlWrite(Panda.REQUEST_OUT, 0xe3, motor, 0, b'')

0 commit comments

Comments
 (0)