Skip to content

Commit 5395908

Browse files
authored
boardd: add CAN health to pandaStates (#25800)
* init * try this * mistake * fix * bump cereal * make obvious * fixes * remove comment * one helath header * .. * preallocate vectors
1 parent 2cba29e commit 5395908

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

cereal

selfdrive/boardd/boardd.cc

+44-2
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,19 @@ void send_empty_panda_state(PubMaster *pm) {
295295

296296
std::optional<bool> send_panda_states(PubMaster *pm, const std::vector<Panda *> &pandas, bool spoofing_started) {
297297
bool ignition_local = false;
298+
const uint32_t pandas_cnt = pandas.size();
298299

299300
// build msg
300301
MessageBuilder msg;
301302
auto evt = msg.initEvent();
302-
auto pss = evt.initPandaStates(pandas.size());
303+
auto pss = evt.initPandaStates(pandas_cnt);
303304

304305
std::vector<health_t> pandaStates;
306+
pandaStates.reserve(pandas_cnt);
307+
308+
std::vector<std::array<can_health_t, PANDA_CAN_CNT>> pandaCanStates;
309+
pandaCanStates.reserve(pandas_cnt);
310+
305311
for (const auto& panda : pandas){
306312
auto health_opt = panda->get_state();
307313
if (!health_opt) {
@@ -310,6 +316,16 @@ std::optional<bool> send_panda_states(PubMaster *pm, const std::vector<Panda *>
310316

311317
health_t health = *health_opt;
312318

319+
std::array<can_health_t, PANDA_CAN_CNT> can_health{};
320+
for (uint32_t i = 0; i < PANDA_CAN_CNT; i++) {
321+
auto can_health_opt = panda->get_can_state(i);
322+
if (!can_health_opt) {
323+
return std::nullopt;
324+
}
325+
can_health[i] = *can_health_opt;
326+
}
327+
pandaCanStates.push_back(can_health);
328+
313329
if (spoofing_started) {
314330
health.ignition_line_pkt = 1;
315331
}
@@ -319,7 +335,7 @@ std::optional<bool> send_panda_states(PubMaster *pm, const std::vector<Panda *>
319335
pandaStates.push_back(health);
320336
}
321337

322-
for (uint32_t i = 0; i < pandas.size(); i++) {
338+
for (uint32_t i = 0; i < pandas_cnt; i++) {
323339
auto panda = pandas[i];
324340
const auto &health = pandaStates[i];
325341

@@ -366,6 +382,32 @@ std::optional<bool> send_panda_states(PubMaster *pm, const std::vector<Panda *>
366382
ps.setInterruptLoad(health.interrupt_load);
367383
ps.setFanPower(health.fan_power);
368384

385+
std::array<cereal::PandaState::PandaCanState::Builder, PANDA_CAN_CNT> cs = {ps.initCanState0(), ps.initCanState1(), ps.initCanState2()};
386+
387+
for (uint32_t j = 0; j < PANDA_CAN_CNT; j++) {
388+
const auto &can_health = pandaCanStates[i][j];
389+
cs[j].setBusOff((bool)can_health.bus_off);
390+
cs[j].setBusOffCnt(can_health.bus_off_cnt);
391+
cs[j].setErrorWarning((bool)can_health.error_warning);
392+
cs[j].setErrorPassive((bool)can_health.error_passive);
393+
cs[j].setLastError(cereal::PandaState::PandaCanState::LecErrorCode(can_health.last_error));
394+
cs[j].setLastStoredError(cereal::PandaState::PandaCanState::LecErrorCode(can_health.last_stored_error));
395+
cs[j].setLastDataError(cereal::PandaState::PandaCanState::LecErrorCode(can_health.last_data_error));
396+
cs[j].setLastDataStoredError(cereal::PandaState::PandaCanState::LecErrorCode(can_health.last_data_stored_error));
397+
cs[j].setReceiveErrorCnt(can_health.receive_error_cnt);
398+
cs[j].setTransmitErrorCnt(can_health.transmit_error_cnt);
399+
cs[j].setTotalErrorCnt(can_health.total_error_cnt);
400+
cs[j].setTotalTxLostCnt(can_health.total_tx_lost_cnt);
401+
cs[j].setTotalRxLostCnt(can_health.total_rx_lost_cnt);
402+
cs[j].setTotalTxCnt(can_health.total_tx_cnt);
403+
cs[j].setTotalRxCnt(can_health.total_rx_cnt);
404+
cs[j].setTotalFwdCnt(can_health.total_fwd_cnt);
405+
cs[j].setCanSpeed(can_health.can_speed);
406+
cs[j].setCanDataSpeed(can_health.can_data_speed);
407+
cs[j].setCanfdEnabled(can_health.canfd_enabled);
408+
cs[j].setBrsEnabled(can_health.canfd_enabled);
409+
}
410+
369411
// Convert faults bitset to capnp list
370412
std::bitset<sizeof(health.faults_pkt) * 8> fault_bits(health.faults_pkt);
371413
auto faults = ps.initFaults(fault_bits.count());

selfdrive/boardd/panda.cc

+6
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,12 @@ std::optional<health_t> Panda::get_state() {
317317
return err >= 0 ? std::make_optional(health) : std::nullopt;
318318
}
319319

320+
std::optional<can_health_t> Panda::get_can_state(uint16_t can_number) {
321+
can_health_t can_health {0};
322+
int err = usb_read(0xc2, can_number, 0, (unsigned char*)&can_health, sizeof(can_health));
323+
return err >= 0 ? std::make_optional(can_health) : std::nullopt;
324+
}
325+
320326
void Panda::set_loopback(bool loopback) {
321327
usb_write(0xe5, loopback, 0);
322328
}

selfdrive/boardd/panda.h

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "panda/board/health.h"
1717

1818
#define TIMEOUT 0
19+
#define PANDA_CAN_CNT 3
1920
#define PANDA_BUS_CNT 4
2021
#define RECV_SIZE (0x4000U)
2122
#define USB_TX_SOFT_LIMIT (0x100U)
@@ -81,6 +82,7 @@ class Panda {
8182
uint16_t get_fan_speed();
8283
void set_ir_pwr(uint16_t ir_pwr);
8384
std::optional<health_t> get_state();
85+
std::optional<can_health_t> get_can_state(uint16_t can_number);
8486
void set_loopback(bool loopback);
8587
std::optional<std::vector<uint8_t>> get_firmware_version();
8688
std::optional<std::string> get_serial();

0 commit comments

Comments
 (0)