Skip to content

Commit 69c6939

Browse files
committed
Added power switch can commands
1 parent 4a2f49b commit 69c6939

File tree

4 files changed

+136
-2
lines changed

4 files changed

+136
-2
lines changed

comm_can.c

+52-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ static can_status_msg_5 stat_msgs_5[CAN_STATUS_MSGS_TO_STORE];
7979
static io_board_adc_values io_board_adc_1_4[CAN_STATUS_MSGS_TO_STORE];
8080
static io_board_adc_values io_board_adc_5_8[CAN_STATUS_MSGS_TO_STORE];
8181
static io_board_digial_inputs io_board_digital_in[CAN_STATUS_MSGS_TO_STORE];
82+
static psw_status psw_stat[CAN_STATUS_MSGS_TO_STORE];
8283
static unsigned int detect_all_foc_res_index = 0;
8384
static int8_t detect_all_foc_res[50];
8485

@@ -120,6 +121,8 @@ void comm_can_init(void) {
120121
io_board_adc_1_4[i].id = -1;
121122
io_board_adc_5_8[i].id = -1;
122123
io_board_digital_in[i].id = -1;
124+
125+
psw_stat[i].id = -1;
123126
}
124127

125128
#if CAN_ENABLE
@@ -911,6 +914,35 @@ void comm_can_io_board_set_output_pwm(int id, int channel, float duty) {
911914
buffer, send_index, true);
912915
}
913916

917+
psw_status *comm_can_get_psw_status_index(int index) {
918+
if (index < CAN_STATUS_MSGS_TO_STORE) {
919+
return &psw_stat[index];
920+
} else {
921+
return 0;
922+
}
923+
}
924+
925+
psw_status *comm_can_get_psw_status_id(int id) {
926+
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
927+
if (psw_stat[i].id == id) {
928+
return &psw_stat[i];
929+
}
930+
}
931+
932+
return 0;
933+
}
934+
935+
void comm_can_psw_switch(int id, bool is_on, bool plot) {
936+
int32_t send_index = 0;
937+
uint8_t buffer[8];
938+
939+
buffer[send_index++] = is_on ? 1 : 0;
940+
buffer[send_index++] = plot ? 1 : 0;
941+
942+
comm_can_transmit_eid_replace(id | ((uint32_t)CAN_PACKET_PSW_SWITCH << 8),
943+
buffer, send_index, true);
944+
}
945+
914946
CANRxFrame *comm_can_get_rx_frame(void) {
915947
#if CAN_ENABLE
916948
chMtxLock(&can_rx_mtx);
@@ -1599,7 +1631,26 @@ static void decode_msg(uint32_t eid, uint8_t *data8, int len, bool is_replaced)
15991631
}
16001632
}
16011633
break;
1602-
break;
1634+
1635+
case CAN_PACKET_PSW_STAT: {
1636+
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
1637+
psw_status *msg = &psw_stat[i];
1638+
if (msg->id == id || msg->id == -1) {
1639+
ind = 0;
1640+
msg->id = id;
1641+
msg->rx_time = chVTGetSystemTime();
1642+
1643+
msg->v_in = buffer_get_float16(data8, 10.0, &ind);
1644+
msg->v_out = buffer_get_float16(data8, 10.0, &ind);
1645+
msg->temp = buffer_get_float16(data8, 10.0, &ind);
1646+
msg->is_out_on = (data8[ind] >> 0) & 1;
1647+
msg->is_pch_on = (data8[ind] >> 1) & 1;
1648+
msg->is_dsc_on = (data8[ind] >> 2) & 1;
1649+
ind++;
1650+
break;
1651+
}
1652+
}
1653+
} break;
16031654

16041655
default:
16051656
break;

comm_can.h

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ io_board_digial_inputs *comm_can_get_io_board_digital_in_id(int id);
7676
void comm_can_io_board_set_output_digital(int id, int channel, bool on);
7777
void comm_can_io_board_set_output_pwm(int id, int channel, float duty);
7878

79+
psw_status *comm_can_get_psw_status_index(int index);
80+
psw_status *comm_can_get_psw_status_id(int id);
81+
void comm_can_psw_switch(int id, bool is_on, bool plot);
82+
7983
CANRxFrame *comm_can_get_rx_frame(void);
8084

8185
#endif /* COMM_CAN_H_ */

commands.c

+56
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ void commands_process_packet(unsigned char *data, unsigned int len,
212212

213213
send_buffer[ind++] = 0; // No custom config
214214

215+
#ifdef HW_HAS_PHASE_FILTERS
216+
send_buffer[ind++] = 1;
217+
#else
218+
send_buffer[ind++] = 0;
219+
#endif
220+
215221
fw_version_sent_cnt++;
216222

217223
reply_func(send_buffer, ind);
@@ -1179,6 +1185,56 @@ void commands_process_packet(unsigned char *data, unsigned int len,
11791185
break;
11801186
}
11811187

1188+
// Power switch
1189+
case COMM_PSW_GET_STATUS: {
1190+
int32_t ind = 0;
1191+
bool by_id = data[ind++];
1192+
int id_ind = buffer_get_int16(data, &ind);
1193+
1194+
int psws_num = 0;
1195+
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
1196+
psw_status *stat = comm_can_get_psw_status_index(i);
1197+
if (stat->id >= 0) {
1198+
psws_num++;
1199+
} else {
1200+
break;
1201+
}
1202+
}
1203+
1204+
psw_status *stat = 0;
1205+
if (by_id) {
1206+
stat = comm_can_get_psw_status_id(id_ind);
1207+
} else if (id_ind < psws_num) {
1208+
stat = comm_can_get_psw_status_index(id_ind);
1209+
}
1210+
1211+
if (stat) {
1212+
ind = 0;
1213+
uint8_t send_buffer[70];
1214+
1215+
send_buffer[ind++] = packet_id;
1216+
buffer_append_int16(send_buffer, stat->id, &ind);
1217+
buffer_append_int16(send_buffer, psws_num, &ind);
1218+
buffer_append_float32_auto(send_buffer, UTILS_AGE_S(stat->rx_time), &ind);
1219+
buffer_append_float32_auto(send_buffer, stat->v_in, &ind);
1220+
buffer_append_float32_auto(send_buffer, stat->v_out, &ind);
1221+
buffer_append_float32_auto(send_buffer, stat->temp, &ind);
1222+
send_buffer[ind++] = stat->is_out_on;
1223+
send_buffer[ind++] = stat->is_pch_on;
1224+
send_buffer[ind++] = stat->is_dsc_on;
1225+
1226+
reply_func(send_buffer, ind);
1227+
}
1228+
} break;
1229+
1230+
case COMM_PSW_SWITCH: {
1231+
int32_t ind = 0;
1232+
int id = buffer_get_int16(data, &ind);
1233+
bool is_on = data[ind++];
1234+
bool plot = data[ind++];
1235+
comm_can_psw_switch(id, is_on, plot);
1236+
} break;
1237+
11821238
// Blocking commands. Only one of them runs at any given time, in their
11831239
// own thread. If other blocking commands come before the previous one has
11841240
// finished, they are discarded.

datatypes.h

+24-1
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,12 @@ typedef struct {
376376
uint16_t foc_hfi_start_samples;
377377
float foc_hfi_obs_ovr_sec;
378378
foc_hfi_samples foc_hfi_samples;
379+
bool foc_offsets_measured;
380+
float foc_offsets_current[3];
381+
float foc_offsets_voltage[3];
382+
float foc_offsets_voltage_undriven[3];
383+
bool foc_phase_filter_enable;
384+
float foc_phase_filter_max_erpm;
379385

380386
// GPDrive
381387
int gpd_buffer_notify_left;
@@ -924,6 +930,10 @@ typedef enum {
924930
COMM_ERASE_BOOTLOADER_ALL_CAN_HW,
925931

926932
COMM_SET_ODOMETER,
933+
934+
// Power switch commands
935+
COMM_PSW_GET_STATUS,
936+
COMM_PSW_SWITCH,
927937
} COMM_PACKET_ID;
928938

929939
// CAN commands
@@ -973,7 +983,9 @@ typedef enum {
973983
CAN_PACKET_BMS_BAL,
974984
CAN_PACKET_BMS_TEMPS,
975985
CAN_PACKET_BMS_HUM,
976-
CAN_PACKET_BMS_SOC_SOH_TEMP_STAT
986+
CAN_PACKET_BMS_SOC_SOH_TEMP_STAT,
987+
CAN_PACKET_PSW_STAT,
988+
CAN_PACKET_PSW_SWITCH
977989
} CAN_PACKET_ID;
978990

979991
// Logged fault data
@@ -1070,6 +1082,17 @@ typedef struct {
10701082
uint64_t inputs;
10711083
} io_board_digial_inputs;
10721084

1085+
typedef struct {
1086+
int id;
1087+
systime_t rx_time;
1088+
float v_in;
1089+
float v_out;
1090+
float temp;
1091+
bool is_out_on;
1092+
bool is_pch_on;
1093+
bool is_dsc_on;
1094+
} psw_status;
1095+
10731096
typedef struct {
10741097
uint8_t js_x;
10751098
uint8_t js_y;

0 commit comments

Comments
 (0)