Skip to content

Commit 7c982a5

Browse files
committed
send extra body data, same as v1 body
1 parent fd728fc commit 7c982a5

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

board/body/bldc/bldc.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ static P rtP_Right; /* Parameters */
3535
static int16_t curL_phaA = 0, curL_phaC = 0, curL_DC = 0;
3636
static int16_t curR_phaA = 0, curR_phaC = 0, curR_DC = 0;
3737

38+
volatile uint16_t batt_voltage_raw = 0;
39+
volatile uint16_t batt_percentage = 0;
40+
3841
volatile int rpml = 0;
3942
volatile int rpmr = 0;
4043

@@ -59,7 +62,7 @@ const adc_signal_t adc_curL_DC = ADC_CHANNEL_BLDC(ADC2, 18);
5962
const adc_signal_t adc_curR_phaA = ADC_CHANNEL_BLDC(ADC1, 7);
6063
const adc_signal_t adc_curR_phaC = ADC_CHANNEL_BLDC(ADC1, 15);
6164
const adc_signal_t adc_curR_DC = ADC_CHANNEL_BLDC(ADC1, 5);
62-
//const adc_signal_t adc_batVoltage = ADC_CHANNEL_BLDC(ADC1, 4);
65+
const adc_signal_t adc_batVoltage = ADC_CHANNEL_BLDC(ADC1, 4);
6366

6467
void motor_set_enable(bool enable) {
6568
enable_motors = enable;
@@ -121,7 +124,7 @@ void bldc_init(void) {
121124
// Setup the model pointers for Right motor
122125
rtP_Right = rtP_Left; // copy parameters
123126
// Right motor measured current phases A & C (based on adc_curR_phaA/C definitions)
124-
rtP_Right.z_selPhaCurMeasABC = 2;
127+
rtP_Right.z_selPhaCurMeasABC = 2;
125128
rtM_Right->defaultParam = &rtP_Right;
126129
rtM_Right->inputs = &rtU_Right;
127130
rtM_Right->outputs = &rtY_Right;
@@ -252,6 +255,12 @@ void bldc_step(void) {
252255
RIGHT_TIM->BDTR |= TIM_BDTR_MOE;
253256
}
254257

258+
// read battery voltage
259+
batt_voltage_raw = adc_get_raw(&adc_batVoltage);
260+
261+
int16_t batVoltageCalib = batt_voltage_raw * BAT_CALIB_REAL_VOLTAGE / BAT_CALIB_ADC;
262+
batt_percentage = 100 - (((420 * BAT_CELLS) - batVoltageCalib) / BAT_CELLS / VOLTS_PER_PERCENT / 100);
263+
255264
// ========================= LEFT MOTOR ===========================
256265
rtU_Left.b_motEna = enableFin;
257266
rtU_Left.z_ctrlModReq = CTRL_MOD_REQ; // Speed Mode
@@ -283,7 +292,7 @@ void bldc_step(void) {
283292
deadband_rpmr = 0;
284293
}
285294
rtU_Right.r_inpTgt = (int16_t)(CLAMP(deadband_rpmr, -MAX_RPM, MAX_RPM) * RPM_TO_UNIT);
286-
295+
287296
rtU_Right.i_phaAB = curR_phaA;
288297
rtU_Right.i_phaBC = curR_phaC;
289298
rtU_Right.i_DCLink = curR_DC;

board/body/bldc/bldc_defs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
#define FIELD_WEAK_HI 1000 // Input target High threshold
4343
#define FIELD_WEAK_LO 750 // Input target Low threshold
4444

45+
// Battery configuration
46+
#define BAT_CELLS 3 // 3 sets in series
47+
#define BAT_CELL_FULL_MV 4200U // mV per cell at 100%
48+
#define BAT_CELL_EMPTY_MV 3386U // mV per cell at 0% (from V1: 4200 - 100 * 8.14)
49+
#define VOLTS_PER_PERCENT 0.00814 // Volts per percent, for conversion of volts to percentage
50+
#define BAT_CALIB_REAL_VOLTAGE 1260U // multimeter voltage
51+
#define BAT_CALIB_ADC 1275U // adc reading voltage
52+
4553
void bldc_init(void);
4654
void bldc_step(void);
4755

board/body/can.h

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
#include "board/body/bldc/bldc.h"
1212

1313
#define BODY_CAN_ADDR_MOTOR_SPEED 0x201U
14-
#define BODY_CAN_ADDR_BLDC_ID 0x396U
14+
#define BODY_CAN_ADDR_VAR_VALUES 0x202U
15+
#define BODY_CAN_ADDR_BODY_DATA 0x203U
16+
#define BODY_CAN_ADDR_V2_ID 0x002U
1517
#define BODY_CAN_MOTOR_SPEED_PERIOD_US 10000U
1618
#define BODY_CAN_CMD_TIMEOUT_US 100000U
1719
#define BODY_BUS_NUMBER 0U
@@ -42,6 +44,39 @@ void body_can_send_motor_speeds(uint8_t bus, float left_speed_rpm, float right_s
4244
counter++;
4345
}
4446

47+
void body_can_send_var_values(uint8_t bus, bool ignition, bool enable_motors, uint8_t fault, uint8_t left_z_errcode, uint8_t right_z_errcode) {
48+
CANPacket_t pkt;
49+
pkt.bus = bus;
50+
pkt.addr = BODY_CAN_ADDR_VAR_VALUES;
51+
pkt.returned = 0;
52+
pkt.rejected = 0;
53+
pkt.extended = 0;
54+
pkt.fd = 0;
55+
pkt.data_len_code = 3;
56+
pkt.data[0] = (ignition ? 1U : 0U) | ((enable_motors ? 1U : 0U) << 1U) | ((fault & 0x3FU) << 2U);
57+
pkt.data[1] = left_z_errcode;
58+
pkt.data[2] = right_z_errcode;
59+
can_set_checksum(&pkt);
60+
can_send(&pkt, bus, true);
61+
}
62+
63+
void body_can_send_body_data(uint8_t bus, uint8_t mcu_temp_raw, uint16_t batt_voltage_raw, uint8_t batt_percentage, bool charger_connected) {
64+
CANPacket_t pkt;
65+
pkt.bus = bus;
66+
pkt.addr = BODY_CAN_ADDR_BODY_DATA;
67+
pkt.returned = 0;
68+
pkt.rejected = 0;
69+
pkt.extended = 0;
70+
pkt.fd = 0;
71+
pkt.data_len_code = 4;
72+
pkt.data[0] = mcu_temp_raw;
73+
pkt.data[1] = (uint8_t)((batt_voltage_raw >> 8) & 0xFFU);
74+
pkt.data[2] = (uint8_t)(batt_voltage_raw & 0xFFU);
75+
pkt.data[3] = (charger_connected ? 1U : 0U) | ((batt_percentage & 0x7FU) << 1U);
76+
can_set_checksum(&pkt);
77+
can_send(&pkt, bus, true);
78+
}
79+
4580
void body_can_process_target(int16_t left_target_deci_rpm, int16_t right_target_deci_rpm) {
4681
rpml = (int)(((float)left_target_deci_rpm) * 0.1f);
4782
rpmr = (int)(((float)right_target_deci_rpm) * 0.1f);
@@ -65,7 +100,7 @@ void body_can_init(void) {
65100
can_init_all();
66101
}
67102

68-
void body_can_periodic(uint32_t now) {
103+
void body_can_periodic(uint32_t now, bool ignition, bool plug_charging) {
69104
if ((last_can_cmd_timestamp_us != 0U) &&
70105
((now - last_can_cmd_timestamp_us) >= BODY_CAN_CMD_TIMEOUT_US)) {
71106
rpml = 0;
@@ -78,11 +113,13 @@ void body_can_periodic(uint32_t now) {
78113
float left_speed_rpm = motor_encoder_get_speed_rpm(BODY_MOTOR_LEFT);
79114
float right_speed_rpm = motor_encoder_get_speed_rpm(BODY_MOTOR_RIGHT);
80115
body_can_send_motor_speeds(BODY_BUS_NUMBER, left_speed_rpm, right_speed_rpm);
116+
body_can_send_var_values(BODY_BUS_NUMBER, ignition, enable_motors, 0U, rtY_Left.z_errCode, rtY_Right.z_errCode);
117+
body_can_send_body_data(BODY_BUS_NUMBER, 0U, batt_voltage_raw, batt_percentage, plug_charging);
81118

82-
// Send message on 0x301 to identify as BLDC body (v2)
119+
// Send message on 0x002 to identify as body v2
83120
CANPacket_t id_pkt = {0};
84121
id_pkt.bus = BODY_BUS_NUMBER;
85-
id_pkt.addr = BODY_CAN_ADDR_BLDC_ID;
122+
id_pkt.addr = BODY_CAN_ADDR_V2_ID;
86123
id_pkt.data_len_code = 1;
87124
id_pkt.data[0] = 1U;
88125
can_set_checksum(&id_pkt);

board/body/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ int main(void) {
132132

133133
if (ignition) {
134134
motor_set_enable(true);
135-
body_can_periodic(now);
135+
body_can_periodic(now, ignition, plug_charging);
136136
} else {
137137
motor_set_enable(false);
138138
}

0 commit comments

Comments
 (0)