mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-09-10 02:03:57 +08:00
ee5c1c4507
* allow re-regage
* bump opendbc
* bump panda
* apply pause/resume fix for hyundai (should do this in a separate PR)
* bump opendbc
* fix
* rename
* Fix?
* make sure to disengage for allow always cars
* fix
* combine
* more fix
* not needed
* check if engagement is from openpilot's state machine
* Rename
* fix panda safety
* fix
* no fake lfa button for @devtekve ;)
* fix non drive gear re-engage
* fix settings
* combine
* add replace method
* use replace
* remoev already checks if it exists
* fix
* group
* add todo
* reserve events
* cleaner
* hyundai: only allow for cars with lfa button
* sunnyParams
* make sure it's car only
* Move car-specific changes to opendbc
* no need
* bump opendbc
* more fixes
* no more available
* more!
* final?
* always emit user disable
* no longer needed
* move unit test
* add sunnypilot to unit tests
* bump opendbc
* use new cereal
* bump opendbc
* static analysis
* no unittest
* no need available
* UI border update
* show MADS updates
* Add TODO
* no longer needed
* fix changed events
* fix cluster enabled
* don't add pre enable if not long
* should use enabled
* enabled <-> active
* better format
* bump opendbc
* static analysis
* static analysis
* Rename test as collector was dying
* Show our overriding
* Revert "show MADS updates"
This reverts commit daf0ad62
Revert "fix changed events"
This reverts commit 31d8c97f
* ignoring reserved events
* adjusting creation delays
* back to stock
removing allow_cancel
* should be enabled
* revert
* silent lkas disable
* no need
* user disable tests
* just warning
* MUST REMOVE test process replay
* fix no entry
* fixme
* bump opendbc
* need this check
* cleanup
* allow entering paused state if no entry from disabled
* brake hold should apply to all
* in lists
* update unit test
* simpler
* unused
* same thing
* fix
* only mads in enabled state and long in disabled state
* unify silent enable
* do this for dlob
* bump submodules
* fix
* bump submodules
* bump opendbc
* less frequent
* more events
* fix
* allow no entry to paused for non-drive gears
* fix
* use cereal
* Revert "allow no entry to paused for non-drive gears"
This reverts commit 6d64a4dd9c6de5ad14ef1e88dbc3b6ad174232d8.
* allow in all
* Revert "allow in all"
This reverts commit 6375f1489176dc6c69cb7fa388676e482713f80a.
* should not be all!
* rename for clarity
* silent park brake
* flipped
* bump submodules
* Bump to latest mads-new panda
* bump panda
* more nissan
* bump panda
* bump msgq
* bump panda
* bump submodules
* bump opendbc
* bump opendbc
* improving the state
* Revert "PlayStation® model (#34133)"
This reverts commit 5160bee543.
* should be none
* bump panda
* bump opendbc
* Apply suggestions from code review
* bump panda
* bump ref panda
* add todo-sp
* bump panda ref
* bump more panda
* changing refs
* nuke nuke nuke
* use sunny's newer states
* bump with new panda
* bump panda
* Parse more flags from alt exp, more tests, hyundai main cruise allowed
* Parse more flags from alt exp, more tests, hyundai main cruise allowed
* missed
* mutation for controls allowed rising edge
* ford mutation
* license
* remove
* unused
* bump submodules
* use always allowed mads button alt exp
* fix
* whitelist jason's lastname to codespell
* test_processes: update ref logs to 82c0278
* bump submodules
* bump submodules
* bump submodules
* bump panda
* add controls mismatch lateral event
* Simplify lateral disengagement logic for MADS configuration
Reversed the conditional to align the logic with the `disengage_lateral_on_brake` parameter. This ensures that lateral disengagement behavior is more intuitive and matches the expected configuration. Improves code readability and reduces potential misconfigurations.
* remove unified engagement mode in panda
* controls allow should be allowed at all times
* squash! treat MADS button as user entry
* heartbeat for mads
* heartbeat mismatch exit control
* remove always allow mads button from alt
* move to safety_mads
* remove main cruise allowed from alt
* bump panda
* heartbeat engaged mads mismatch mutation test
* bump panda
* use mads the third panda
* ignore pre enable with mads
* only force exit if actually actuating
* use brake signal instead of pedal events when dlob is active
* fix tests
* fix panda tests
* bump panda
* new events to retain long blocks
* format
* uem: do not engage mads if long is engaged
* bump submodules
* fix not allowed engaged bug
* block uem from engaging
* flipped
* use different heartbeat check if dlob
* hard code to skip heartbeat check
* remove toyota lta status for lkas, causes weird behaviors
* block tesla
* bump panda
* bump to merged panda
* bump opendbc
* bump opendbc
* bump opendbc
* bump opendbc
* Apply suggestions from code review
* code ignore spells
* needs to be in carstate
* Bump opendbc
* Update MADS toggle descriptions for clarity.
Added notes to clarify behavior of the "MadsMainCruiseAllowed" setting, particularly its impact on vehicles without LFA/LKAS buttons. This ensures users are informed about potential implications when disabling this feature.
* Updating translations + Adding spanish
* Disengage Lateral on Brake -> Pause Lateral on Brake
* test_processes: update ref logs to dd41005
* Apply suggestions from code review
* fix mads button not allowed
* bump submodules
* bump submodule
* test_processes: update ref logs to 0a0b998
* has multiple lists
* Revert "has multiple lists"
This reverts commit a37c1d26feac02f63bfb86a08f9b6fb06b095cf1.
* base
* Reapply "has multiple lists"
This reverts commit d1cd8dcc815721a4113b26ab98ee2571c071e225.
* migrate mads toggles to sp panel
* this is why it keeps crashing
* house keeping
* more housekeeping
* more housekeeping
* don't show description by default (yet)
* reset to main panel when clicked away
* more
* some more with interactions
* don't stretch cause it looks weird with descriptions
* simpler to handle offroad transition
* some are toggleable while onroad
* remove unused event
* slight cleanup
* default to true for HKG main cruise toggle
* append to list after
* add Customize MADS to UI preview
* simpler
* move to sp list
* how tf was this removed
* update mads settings button on show event
* test_processes: update ref logs to efa9c32
---------
Co-authored-by: Jason Wen <haibin.wen3@gmail.com>
294 lines
8.2 KiB
C++
294 lines
8.2 KiB
C++
#include "selfdrive/pandad/panda.h"
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <cassert>
|
|
#include <stdexcept>
|
|
#include <vector>
|
|
|
|
#include "cereal/messaging/messaging.h"
|
|
#include "common/swaglog.h"
|
|
#include "common/util.h"
|
|
|
|
const bool PANDAD_MAXOUT = getenv("PANDAD_MAXOUT") != nullptr;
|
|
|
|
Panda::Panda(std::string serial, uint32_t bus_offset) : bus_offset(bus_offset) {
|
|
// try USB first, then SPI
|
|
try {
|
|
handle = std::make_unique<PandaUsbHandle>(serial);
|
|
LOGW("connected to %s over USB", serial.c_str());
|
|
} catch (std::exception &e) {
|
|
#ifndef __APPLE__
|
|
handle = std::make_unique<PandaSpiHandle>(serial);
|
|
LOGW("connected to %s over SPI", serial.c_str());
|
|
#else
|
|
throw e;
|
|
#endif
|
|
}
|
|
|
|
hw_type = get_hw_type();
|
|
can_reset_communications();
|
|
}
|
|
|
|
bool Panda::connected() {
|
|
return handle->connected;
|
|
}
|
|
|
|
bool Panda::comms_healthy() {
|
|
return handle->comms_healthy;
|
|
}
|
|
|
|
std::string Panda::hw_serial() {
|
|
return handle->hw_serial;
|
|
}
|
|
|
|
std::vector<std::string> Panda::list(bool usb_only) {
|
|
std::vector<std::string> serials = PandaUsbHandle::list();
|
|
|
|
#ifndef __APPLE__
|
|
if (!usb_only) {
|
|
for (const auto &s : PandaSpiHandle::list()) {
|
|
if (std::find(serials.begin(), serials.end(), s) == serials.end()) {
|
|
serials.push_back(s);
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
return serials;
|
|
}
|
|
|
|
void Panda::set_safety_model(cereal::CarParams::SafetyModel safety_model, uint16_t safety_param) {
|
|
handle->control_write(0xdc, (uint16_t)safety_model, safety_param);
|
|
}
|
|
|
|
void Panda::set_alternative_experience(uint16_t alternative_experience) {
|
|
handle->control_write(0xdf, alternative_experience, 0);
|
|
}
|
|
|
|
cereal::PandaState::PandaType Panda::get_hw_type() {
|
|
unsigned char hw_query[1] = {0};
|
|
|
|
handle->control_read(0xc1, 0, 0, hw_query, 1);
|
|
return (cereal::PandaState::PandaType)(hw_query[0]);
|
|
}
|
|
|
|
void Panda::set_fan_speed(uint16_t fan_speed) {
|
|
handle->control_write(0xb1, fan_speed, 0);
|
|
}
|
|
|
|
uint16_t Panda::get_fan_speed() {
|
|
uint16_t fan_speed_rpm = 0;
|
|
handle->control_read(0xb2, 0, 0, (unsigned char*)&fan_speed_rpm, sizeof(fan_speed_rpm));
|
|
return fan_speed_rpm;
|
|
}
|
|
|
|
void Panda::set_ir_pwr(uint16_t ir_pwr) {
|
|
handle->control_write(0xb0, ir_pwr, 0);
|
|
}
|
|
|
|
std::optional<health_t> Panda::get_state() {
|
|
health_t health {0};
|
|
int err = handle->control_read(0xd2, 0, 0, (unsigned char*)&health, sizeof(health));
|
|
return err >= 0 ? std::make_optional(health) : std::nullopt;
|
|
}
|
|
|
|
std::optional<can_health_t> Panda::get_can_state(uint16_t can_number) {
|
|
can_health_t can_health {0};
|
|
int err = handle->control_read(0xc2, can_number, 0, (unsigned char*)&can_health, sizeof(can_health));
|
|
return err >= 0 ? std::make_optional(can_health) : std::nullopt;
|
|
}
|
|
|
|
void Panda::set_loopback(bool loopback) {
|
|
handle->control_write(0xe5, loopback, 0);
|
|
}
|
|
|
|
std::optional<std::vector<uint8_t>> Panda::get_firmware_version() {
|
|
std::vector<uint8_t> fw_sig_buf(128);
|
|
int read_1 = handle->control_read(0xd3, 0, 0, &fw_sig_buf[0], 64);
|
|
int read_2 = handle->control_read(0xd4, 0, 0, &fw_sig_buf[64], 64);
|
|
return ((read_1 == 64) && (read_2 == 64)) ? std::make_optional(fw_sig_buf) : std::nullopt;
|
|
}
|
|
|
|
std::optional<std::string> Panda::get_serial() {
|
|
char serial_buf[17] = {'\0'};
|
|
int err = handle->control_read(0xd0, 0, 0, (uint8_t*)serial_buf, 16);
|
|
return err >= 0 ? std::make_optional(serial_buf) : std::nullopt;
|
|
}
|
|
|
|
bool Panda::up_to_date() {
|
|
if (auto fw_sig = get_firmware_version()) {
|
|
for (auto fn : { "panda.bin.signed", "panda_h7.bin.signed" }) {
|
|
auto content = util::read_file(std::string("../../panda/board/obj/") + fn);
|
|
if (content.size() >= fw_sig->size() &&
|
|
memcmp(content.data() + content.size() - fw_sig->size(), fw_sig->data(), fw_sig->size()) == 0) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void Panda::set_power_saving(bool power_saving) {
|
|
handle->control_write(0xe7, power_saving, 0);
|
|
}
|
|
|
|
void Panda::enable_deepsleep() {
|
|
handle->control_write(0xfb, 0, 0);
|
|
}
|
|
|
|
void Panda::send_heartbeat(bool engaged, bool engaged_mads) {
|
|
handle->control_write(0xf3, engaged, engaged_mads);
|
|
}
|
|
|
|
void Panda::set_can_speed_kbps(uint16_t bus, uint16_t speed) {
|
|
handle->control_write(0xde, bus, (speed * 10));
|
|
}
|
|
|
|
void Panda::set_can_fd_auto(uint16_t bus, bool enabled) {
|
|
handle->control_write(0xe8, bus, enabled);
|
|
}
|
|
|
|
void Panda::set_data_speed_kbps(uint16_t bus, uint16_t speed) {
|
|
handle->control_write(0xf9, bus, (speed * 10));
|
|
}
|
|
|
|
void Panda::set_canfd_non_iso(uint16_t bus, bool non_iso) {
|
|
handle->control_write(0xfc, bus, non_iso);
|
|
}
|
|
|
|
static uint8_t len_to_dlc(uint8_t len) {
|
|
if (len <= 8) {
|
|
return len;
|
|
}
|
|
if (len <= 24) {
|
|
return 8 + ((len - 8) / 4) + ((len % 4) ? 1 : 0);
|
|
} else {
|
|
return 11 + (len / 16) + ((len % 16) ? 1 : 0);
|
|
}
|
|
}
|
|
|
|
void Panda::pack_can_buffer(const capnp::List<cereal::CanData>::Reader &can_data_list,
|
|
std::function<void(uint8_t *, size_t)> write_func) {
|
|
int32_t pos = 0;
|
|
uint8_t send_buf[2 * USB_TX_SOFT_LIMIT];
|
|
|
|
for (const auto &cmsg : can_data_list) {
|
|
// check if the message is intended for this panda
|
|
uint8_t bus = cmsg.getSrc();
|
|
if (bus < bus_offset || bus >= (bus_offset + PANDA_BUS_OFFSET)) {
|
|
continue;
|
|
}
|
|
auto can_data = cmsg.getDat();
|
|
uint8_t data_len_code = len_to_dlc(can_data.size());
|
|
assert(can_data.size() <= 64);
|
|
assert(can_data.size() == dlc_to_len[data_len_code]);
|
|
|
|
can_header header = {};
|
|
header.addr = cmsg.getAddress();
|
|
header.extended = (cmsg.getAddress() >= 0x800) ? 1 : 0;
|
|
header.data_len_code = data_len_code;
|
|
header.bus = bus - bus_offset;
|
|
header.checksum = 0;
|
|
|
|
memcpy(&send_buf[pos], (uint8_t *)&header, sizeof(can_header));
|
|
memcpy(&send_buf[pos + sizeof(can_header)], (uint8_t *)can_data.begin(), can_data.size());
|
|
uint32_t msg_size = sizeof(can_header) + can_data.size();
|
|
|
|
// set checksum
|
|
((can_header *) &send_buf[pos])->checksum = calculate_checksum(&send_buf[pos], msg_size);
|
|
|
|
pos += msg_size;
|
|
|
|
if (pos >= USB_TX_SOFT_LIMIT) {
|
|
write_func(send_buf, pos);
|
|
pos = 0;
|
|
}
|
|
}
|
|
|
|
// send remaining packets
|
|
if (pos > 0) write_func(send_buf, pos);
|
|
}
|
|
|
|
void Panda::can_send(const capnp::List<cereal::CanData>::Reader &can_data_list) {
|
|
pack_can_buffer(can_data_list, [=](uint8_t* data, size_t size) {
|
|
handle->bulk_write(3, data, size, 5);
|
|
});
|
|
}
|
|
|
|
bool Panda::can_receive(std::vector<can_frame>& out_vec) {
|
|
// Check if enough space left in buffer to store RECV_SIZE data
|
|
assert(receive_buffer_size + RECV_SIZE <= sizeof(receive_buffer));
|
|
|
|
int recv = handle->bulk_read(0x81, &receive_buffer[receive_buffer_size], RECV_SIZE);
|
|
if (!comms_healthy()) {
|
|
return false;
|
|
}
|
|
|
|
if (PANDAD_MAXOUT) {
|
|
static uint8_t junk[RECV_SIZE];
|
|
handle->bulk_read(0xab, junk, RECV_SIZE - recv);
|
|
}
|
|
|
|
bool ret = true;
|
|
if (recv > 0) {
|
|
receive_buffer_size += recv;
|
|
ret = unpack_can_buffer(receive_buffer, receive_buffer_size, out_vec);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
void Panda::can_reset_communications() {
|
|
handle->control_write(0xc0, 0, 0);
|
|
}
|
|
|
|
bool Panda::unpack_can_buffer(uint8_t *data, uint32_t &size, std::vector<can_frame> &out_vec) {
|
|
int pos = 0;
|
|
|
|
while (pos <= size - sizeof(can_header)) {
|
|
can_header header;
|
|
memcpy(&header, &data[pos], sizeof(can_header));
|
|
|
|
const uint8_t data_len = dlc_to_len[header.data_len_code];
|
|
if (pos + sizeof(can_header) + data_len > size) {
|
|
// we don't have all the data for this message yet
|
|
break;
|
|
}
|
|
|
|
if (calculate_checksum(&data[pos], sizeof(can_header) + data_len) != 0) {
|
|
LOGE("Panda CAN checksum failed");
|
|
size = 0;
|
|
can_reset_communications();
|
|
return false;
|
|
}
|
|
|
|
can_frame &canData = out_vec.emplace_back();
|
|
canData.address = header.addr;
|
|
canData.src = header.bus + bus_offset;
|
|
if (header.rejected) {
|
|
canData.src += CAN_REJECTED_BUS_OFFSET;
|
|
}
|
|
if (header.returned) {
|
|
canData.src += CAN_RETURNED_BUS_OFFSET;
|
|
}
|
|
|
|
canData.dat.assign((char *)&data[pos + sizeof(can_header)], data_len);
|
|
|
|
pos += sizeof(can_header) + data_len;
|
|
}
|
|
|
|
// move the overflowing data to the beginning of the buffer for the next round
|
|
memmove(data, &data[pos], size - pos);
|
|
size -= pos;
|
|
|
|
return true;
|
|
}
|
|
|
|
uint8_t Panda::calculate_checksum(uint8_t *data, uint32_t len) {
|
|
uint8_t checksum = 0U;
|
|
for (uint32_t i = 0U; i < len; i++) {
|
|
checksum ^= data[i];
|
|
}
|
|
return checksum;
|
|
}
|