Skip to content

Commit

Permalink
pandad: reset safety mode on exit (#32103)
Browse files Browse the repository at this point in the history
* boardd: reset safety mode on exit

old-commit-hash: dd18ccb

* comment

old-commit-hash: 01b598e

* log it

old-commit-hash: 181c4d4

* logmessaged might not be alive

old-commit-hash: 7483ba0

* reproduction, manager gets SIGTERM from python_process

old-commit-hash: b90402b

* even smaller repro

old-commit-hash: 03dd430

* should work

old-commit-hash: 388c427

* let's not change that rn

old-commit-hash: d057299

* something like this

old-commit-hash: 123d6ed

* pandad.cc should receive same SIGTERM and exit

old-commit-hash: afc5ef6

* stash

old-commit-hash: e02e0dc

* remove debugging

old-commit-hash: ac170d0

* remove debugging

old-commit-hash: 5094960

* match behavior

old-commit-hash: 5f24167

* convention

old-commit-hash: 1664113

* systemd option

old-commit-hash: 95183ff

* manager option

old-commit-hash: 2071893

* just curious if this works, change to ELM327 on exit

old-commit-hash: 9674ed5

* Revert "just curious if this works, change to ELM327 on exit"

This reverts commit d4ae294d419dc3d787d11dee4474799f3fb2acef.

old-commit-hash: 6d24edd

* check onroad

same update

* useless

* comment

* fix

* debug

* Revert "debug"

This reverts commit 2bb1386.

* Update common/util.h

* double wait does not work, blocking in signal handler not good, exit on do_exit, change to SIGINT, use existing stop to support ctrl+c

nope

* organize?

* no sys

* None

---------

Co-authored-by: Shane Smiskol <[email protected]>
  • Loading branch information
adeebshihadeh and sshane authored Jan 22, 2025
1 parent 475c9ba commit bb09fd0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
22 changes: 16 additions & 6 deletions selfdrive/pandad/pandad.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,7 @@ void send_peripheral_state(Panda *panda, PubMaster *pm) {
pm->send("peripheralState", msg);
}

void process_panda_state(std::vector<Panda *> &pandas, PubMaster *pm, bool spoofing_started) {
static SubMaster sm({"selfdriveState"});

void process_panda_state(std::vector<Panda *> &pandas, PubMaster *pm, bool engaged, bool spoofing_started) {
std::vector<std::string> connected_serials;
for (Panda *p : pandas) {
connected_serials.push_back(p->hw_serial());
Expand Down Expand Up @@ -361,8 +359,6 @@ void process_panda_state(std::vector<Panda *> &pandas, PubMaster *pm, bool spoof
}
}

sm.update(0);
const bool engaged = sm.allAliveAndValid({"selfdriveState"}) && sm["selfdriveState"].getSelfdriveState().getEnabled();
for (const auto &panda : pandas) {
panda->send_heartbeat(engaged);
}
Expand Down Expand Up @@ -428,9 +424,11 @@ void pandad_run(std::vector<Panda *> &pandas) {
std::thread send_thread(can_send_thread, pandas, fake_send);

RateKeeper rk("pandad", 100);
SubMaster sm({"selfdriveState"});
PubMaster pm({"can", "pandaStates", "peripheralState"});
PandaSafety panda_safety(pandas);
Panda *peripheral_panda = pandas[0];
bool engaged = false;

// Main loop: receive CAN data and process states
while (!do_exit && check_all_connected(pandas)) {
Expand All @@ -443,7 +441,9 @@ void pandad_run(std::vector<Panda *> &pandas) {

// Process panda state at 10 Hz
if (rk.frame() % 10 == 0) {
process_panda_state(pandas, &pm, spoofing_started);
sm.update(0);
engaged = sm.allAliveAndValid({"selfdriveState"}) && sm["selfdriveState"].getSelfdriveState().getEnabled();
process_panda_state(pandas, &pm, engaged, spoofing_started);
panda_safety.configureSafetyMode();
}

Expand All @@ -455,6 +455,16 @@ void pandad_run(std::vector<Panda *> &pandas) {
rk.keepTime();
}

// Close relay on exit to prevent a fault
const bool is_onroad = Params().getBool("IsOnroad");
if (is_onroad && !engaged) {
for (auto &p : pandas) {
if (p->connected()) {
p->set_safety_model(cereal::CarParams::SafetyModel::NO_OUTPUT);
}
}
}

send_thread.join();
}

Expand Down
23 changes: 18 additions & 5 deletions selfdrive/pandad/pandad.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import os
import usb1
import time
import signal
import subprocess
from typing import NoReturn

from panda import Panda, PandaDFU, PandaProtocolMismatch, FW_PATH
from openpilot.common.basedir import BASEDIR
Expand Down Expand Up @@ -61,13 +61,25 @@ def flash_panda(panda_serial: str) -> Panda:
return panda


def main() -> NoReturn:
def main() -> None:
# signal pandad to close the relay and exit
def signal_handler(signum, frame):
cloudlog.info(f"Caught signal {signum}, exiting")
nonlocal do_exit
do_exit = True
if process is not None:
process.send_signal(signal.SIGINT)

process = None
do_exit = False
signal.signal(signal.SIGINT, signal_handler)

count = 0
first_run = True
params = Params()
no_internal_panda_count = 0

while True:
while not do_exit:
try:
count += 1
cloudlog.event("pandad.flash_and_connect", count=count)
Expand Down Expand Up @@ -159,8 +171,9 @@ def main() -> NoReturn:

# run pandad with all connected serials as arguments
os.environ['MANAGER_DAEMON'] = 'pandad'
os.chdir(os.path.join(BASEDIR, "selfdrive/pandad"))
subprocess.run(["./pandad", *panda_serials], check=True)
process = subprocess.Popen(["./pandad", *panda_serials], cwd=os.path.join(BASEDIR, "selfdrive/pandad"))
process.wait()


if __name__ == "__main__":
main()

0 comments on commit bb09fd0

Please sign in to comment.