Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pandad: reset safety mode on exit #32103

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class ExitHandler {
ExitHandler() {
std::signal(SIGINT, (sighandler_t)set_do_exit);
std::signal(SIGTERM, (sighandler_t)set_do_exit);
std::signal(SIGHUP, (sighandler_t)set_do_exit);

#ifndef __APPLE__
std::signal(SIGPWR, (sighandler_t)set_do_exit);
Expand Down
9 changes: 8 additions & 1 deletion selfdrive/pandad/pandad.cc
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ void panda_state_thread(std::vector<Panda *> pandas, bool spoofing_started) {
PubMaster pm({"pandaStates", "peripheralState"});

Panda *peripheral_panda = pandas[0];
bool engaged = false;
bool is_onroad = false;
bool is_onroad_last = false;
std::future<bool> safety_future;
Expand Down Expand Up @@ -483,14 +484,20 @@ void panda_state_thread(std::vector<Panda *> pandas, bool spoofing_started) {
is_onroad_last = is_onroad;

sm.update(0);
const bool engaged = sm.allAliveAndValid({"controlsState"}) && sm["controlsState"].getControlsState().getEnabled();
engaged = sm.allAliveAndValid({"controlsState"}) && sm["controlsState"].getControlsState().getEnabled();

for (const auto &panda : pandas) {
panda->send_heartbeat(engaged);
}

rk.keepTime();
}

if (is_onroad && !engaged) {
for (auto &p : pandas) {
p->set_safety_model(cereal::CarParams::SafetyModel::NO_OUTPUT);
}
}
}


Expand Down
19 changes: 17 additions & 2 deletions selfdrive/pandad/pandad.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import os
import usb1
import time
import signal
import subprocess
import sys
from typing import NoReturn

from panda import Panda, PandaDFU, PandaProtocolMismatch, FW_PATH
Expand Down Expand Up @@ -62,6 +64,17 @@ def flash_panda(panda_serial: str) -> Panda:


def main() -> NoReturn:
# signal pandad to close the relay and exit
def signal_handler(signum, frame):
cloudlog.info(f"Caught signal {signum}, exiting")
if process is not None and process.poll() is None:
process.terminate()
process.wait()
sys.exit(1)

process = None
signal.signal(signal.SIGTERM, signal_handler)

count = 0
first_run = True
params = Params()
Expand Down Expand Up @@ -159,8 +172,10 @@ 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()

sys.exit(1)

if __name__ == "__main__":
main()
5 changes: 5 additions & 0 deletions system/manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import signal
import sys
import time
import traceback

from cereal import log
Expand Down Expand Up @@ -172,6 +173,10 @@ def manager_thread() -> None:
cloudlog.warning(f"Shutting down manager - {param} set")

if shutdown:
# Signal processes to shut down with a small grace period
for p in managed_processes.values():
p.signal(signal.SIGTERM)
time.sleep(1)
break


Expand Down
Loading