Skip to content

Commit 07b26ee

Browse files
committed
motors
1 parent 16f17ae commit 07b26ee

22 files changed

+1135
-3
lines changed

SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ if os.getenv("FINAL_PROVISIONING"):
161161
flags += ["-DFINAL_PROVISIONING"]
162162
build_project("panda_jungle_h7", base_project_h7, "./board/jungle/main.c", flags)
163163

164+
# body fw
165+
build_project("body_h7", base_project_h7, "./board/body/main.c", ["-DPANDA_BODY"])
166+
164167
# test files
165168
if GetOption('extras'):
166169
SConscript('tests/libpanda/SConscript')

__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88

99
# panda jungle
1010
from .board.jungle import PandaJungle, PandaJungleDFU # noqa: F401
11+
12+
# panda body
13+
from .board.body import PandaBody # noqa: F401

board/boards/board_declarations.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ struct board {
5252
#define HW_TYPE_RED_PANDA 7U
5353
#define HW_TYPE_TRES 9U
5454
#define HW_TYPE_CUATRO 10U
55+
#define HW_TYPE_BODY 0xB0U
5556

5657
// CAN modes
5758
#define CAN_MODE_NORMAL 0U
@@ -60,3 +61,4 @@ struct board {
6061
extern struct board board_tres;
6162
extern struct board board_cuatro;
6263
extern struct board board_red;
64+
extern struct board board_body;

board/boards/body.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
#include "board_declarations.h"
4+
#include "unused_funcs.h"
5+
6+
static void body_init(void) {
7+
}
8+
9+
static void body_enable_can_transceiver(uint8_t transceiver, bool enabled) {
10+
(void)transceiver;
11+
(void)enabled;
12+
}
13+
14+
static void body_set_can_mode(uint8_t mode) {
15+
(void)mode;
16+
}
17+
18+
static uint32_t body_read_voltage_mV(void) {
19+
return 0U;
20+
}
21+
22+
board board_body = {
23+
.harness_config = 0,
24+
.led_GPIO = {GPIOC, GPIOC, GPIOC},
25+
.led_pin = {7U, 7U, 7U},
26+
.led_pwm_channels = {0U, 0U, 0U},
27+
.has_spi = false,
28+
.has_fan = false,
29+
.avdd_mV = 0U,
30+
.fan_enable_cooldown_time = 0U,
31+
.init = body_init,
32+
.init_bootloader = unused_init_bootloader,
33+
.enable_can_transceiver = body_enable_can_transceiver,
34+
.set_can_mode = body_set_can_mode,
35+
.read_voltage_mV = body_read_voltage_mV,
36+
.read_current_mA = unused_read_current,
37+
.set_ir_power = unused_set_ir_power,
38+
.set_fan_enabled = unused_set_fan_enabled,
39+
.set_siren = unused_set_siren,
40+
.set_bootkick = unused_set_bootkick,
41+
.read_som_gpio = unused_read_som_gpio,
42+
.set_amp_enabled = unused_set_amp_enabled
43+
};

board/body/__init__.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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'')

board/body/boards/board_body.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
#include "board/body/boards/motor_control.h"
3+
4+
void board_body_init(void) {
5+
motor_init();
6+
motor_encoder_init();
7+
motor_speed_controller_init();
8+
motor_stop(1);
9+
motor_stop(2);
10+
motor_encoder_reset(1);
11+
motor_encoder_reset(2);
12+
13+
}
14+
15+
void board_body_init_bootloader(void) {
16+
}
17+
18+
void board_body_tick(void) {
19+
}
20+
21+
void board_body_enable_can_transceiver(uint8_t transceiver, bool enabled) {
22+
UNUSED(transceiver);
23+
set_gpio_output(GPIOD, 2U, enabled ? 1 : 0);
24+
}
25+
26+
void board_body_set_can_mode(uint8_t mode) {
27+
UNUSED(mode);
28+
}
29+
30+
uint32_t board_body_read_voltage_mV(void) {
31+
return 0U;
32+
}
33+
34+
uint32_t board_body_read_current_mA(void) {
35+
return 0U;
36+
}
37+
38+
board board_body = {
39+
.led_GPIO = {GPIOC, GPIOC, GPIOC},
40+
.led_pin = {7, 7, 7},
41+
.led_pwm_channels = {0, 0, 0},
42+
.avdd_mV = 3300U,
43+
.init = board_body_init,
44+
.init_bootloader = board_body_init_bootloader,
45+
.board_tick = board_body_tick,
46+
.enable_can_transceiver = board_body_enable_can_transceiver,
47+
.set_can_mode = board_body_set_can_mode,
48+
.read_voltage_mV = board_body_read_voltage_mV,
49+
.read_current_mA = board_body_read_current_mA,
50+
.has_spi = false,
51+
.has_fan = false,
52+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
#include <stdbool.h>
5+
6+
// ******************** Prototypes ********************
7+
typedef void (*board_init)(void);
8+
typedef void (*board_init_bootloader)(void);
9+
typedef void (*board_board_tick)(void);
10+
typedef void (*board_enable_can_transceiver)(uint8_t transceiver, bool enabled);
11+
typedef void (*board_set_can_mode)(uint8_t mode);
12+
typedef uint32_t (*board_read_voltage_mV)(void);
13+
typedef uint32_t (*board_read_current_mA)(void);
14+
15+
struct board {
16+
GPIO_TypeDef * const led_GPIO[3];
17+
const uint8_t led_pin[3];
18+
const uint8_t led_pwm_channels[3]; // leave at 0 to disable PWM
19+
const uint16_t avdd_mV;
20+
board_init init;
21+
board_init_bootloader init_bootloader;
22+
board_board_tick board_tick;
23+
board_enable_can_transceiver enable_can_transceiver;
24+
board_set_can_mode set_can_mode;
25+
board_read_voltage_mV read_voltage_mV;
26+
board_read_current_mA read_current_mA;
27+
28+
// Body-specific features can be added here
29+
bool has_spi;
30+
bool has_fan;
31+
};
32+
33+
// ******************* Definitions ********************
34+
#define HW_TYPE_UNKNOWN 0U
35+
#define HW_TYPE_BODY 0xB0U
36+
37+
// CAN modes
38+
#define CAN_MODE_NORMAL 0U
39+
40+
// ********************* Globals **********************
41+
uint8_t can_mode = CAN_MODE_NORMAL;
42+

0 commit comments

Comments
 (0)