Skip to content

Commit

Permalink
Squashed 'panda/' changes from 4dd3f5a..293fa33
Browse files Browse the repository at this point in the history
293fa33 Honda: fixed message forwarding addresses from can2 to can0
799c338 Chrysler safety controls (#130)
c05b15b Toyota frc on (#141)
8291971 Toyota: using a more generalized CRUISE_ACTIVE bit that works on all cars
6f157aa Toyota frc on (#140)
8d2470c bumped panda version
915ee4f Honda: forwarding CAN0 to camera, so camera can stay on (#139)

git-subtree-dir: panda
git-subtree-split: 293fa33
  • Loading branch information
Vehicle Researcher committed Nov 17, 2018
1 parent a51a60b commit c10a755
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 23 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.1.6
v1.1.7
3 changes: 3 additions & 0 deletions board/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ int controls_allowed = 0;
#include "safety/safety_ford.h"
#include "safety/safety_cadillac.h"
#include "safety/safety_hyundai.h"
#include "safety/safety_chrysler.h"
#include "safety/safety_elm327.h"

const safety_hooks *current_hooks = &nooutput_hooks;
Expand Down Expand Up @@ -102,6 +103,7 @@ typedef struct {
#define SAFETY_CADILLAC 6
#define SAFETY_HYUNDAI 7
#define SAFETY_TESLA 8
#define SAFETY_CHRYSLER 9
#define SAFETY_TOYOTA_IPAS 0x1335
#define SAFETY_TOYOTA_NOLIMITS 0x1336
#define SAFETY_ALLOUTPUT 0x1337
Expand All @@ -116,6 +118,7 @@ const safety_hook_config safety_hook_registry[] = {
{SAFETY_FORD, &ford_hooks},
{SAFETY_CADILLAC, &cadillac_hooks},
{SAFETY_HYUNDAI, &hyundai_hooks},
{SAFETY_CHRYSLER, &chrysler_hooks},
{SAFETY_TOYOTA_NOLIMITS, &toyota_nolimits_hooks},
#ifdef PANDA
{SAFETY_TOYOTA_IPAS, &toyota_ipas_hooks},
Expand Down
136 changes: 136 additions & 0 deletions board/safety/safety_chrysler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// board enforces
// in-state
// ACC is active (green)
// out-state
// brake pressed
// stock LKAS ECU is online
// ACC is not active (white or disabled)

// chrysler_: namespacing
int chrysler_speed = 0;

// silence everything if stock ECUs are still online
int chrysler_lkas_detected = 0;
int chrysler_desired_torque_last = 0;

static void chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
int bus_number = (to_push->RDTR >> 4) & 0xFF;
uint32_t addr;
if (to_push->RIR & 4) {
// Extended
// Not looked at, but have to be separated
// to avoid address collision
addr = to_push->RIR >> 3;
} else {
// Normal
addr = to_push->RIR >> 21;
}

if (addr == 0x144 && bus_number == 0) {
chrysler_speed = ((to_push->RDLR & 0xFF000000) >> 16) | (to_push->RDHR & 0xFF);
}

// check if stock LKAS ECU is still online
if (addr == 0x292 && bus_number == 0) {
chrysler_lkas_detected = 1;
controls_allowed = 0;
}

// ["ACC_2"]['ACC_STATUS_2'] == 7 for active (green) Adaptive Cruise Control
if (addr == 0x1f4 && bus_number == 0) {
if (((to_push->RDLR & 0x380000) >> 19) == 7) {
controls_allowed = 1;
} else {
controls_allowed = 0;
}
}

// exit controls on brake press by human
if (addr == 0x140) {
if (to_push->RDLR & 0x4) {
controls_allowed = 0;
}
}
}

// if controls_allowed
// allow steering up to limit
// else
// block all commands that produce actuation
static int chrysler_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
// There can be only one! (LKAS)
if (chrysler_lkas_detected) {
return 0;
}

uint32_t addr;
if (to_send->RIR & 4) {
// Extended
addr = to_send->RIR >> 3;
} else {
// Normal
addr = to_send->RIR >> 21;
}

// LKA STEER: Too large of values cause the steering actuator ECU to silently
// fault and no longer actuate the wheel until the car is rebooted.
if (addr == 0x292) {
int rdlr = to_send->RDLR;
int straight = 1024;
int steer = ((rdlr & 0x7) << 8) + ((rdlr & 0xFF00) >> 8) - straight;
int max_steer = 230;
int max_rate = 50; // ECU is fine with 100, but 3 is typical.
if (steer > max_steer) {
return false;
}
if (steer < -max_steer) {
return false;
}
if (!controls_allowed && steer != 0) {
// If controls not allowed, only allow steering to move closer to 0.
if (chrysler_desired_torque_last == 0) {
return false;
}
if ((chrysler_desired_torque_last > 0) && (steer >= chrysler_desired_torque_last)) {
return false;
}
if ((chrysler_desired_torque_last < 0) && (steer <= chrysler_desired_torque_last)) {
return false;
}
}
if (steer < (chrysler_desired_torque_last - max_rate)) {
return false;
}
if (steer > (chrysler_desired_torque_last + max_rate)) {
return false;
}

chrysler_desired_torque_last = steer;
}

// 1 allows the message through
return true;
}

static int chrysler_tx_lin_hook(int lin_num, uint8_t *data, int len) {
// LIN is not used.
return false;
}

static void chrysler_init(int16_t param) {
controls_allowed = 0;
}

static int chrysler_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
return -1;
}

const safety_hooks chrysler_hooks = {
.init = chrysler_init,
.rx = chrysler_rx_hook,
.tx = chrysler_tx_hook,
.tx_lin = chrysler_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = chrysler_fwd_hook,
};

34 changes: 25 additions & 9 deletions board/safety/safety_honda.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,29 @@ static void honda_init(int16_t param) {
honda_alt_brake_msg = false;
}

const safety_hooks honda_hooks = {
.init = honda_init,
.rx = honda_rx_hook,
.tx = honda_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = nooutput_fwd_hook,
};

static void honda_bosch_init(int16_t param) {
controls_allowed = 0;
bosch_hardware = true;
// Checking for alternate brake override from safety parameter
honda_alt_brake_msg = param == 1 ? true : false;
}

static int honda_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
// fwd from car to camera. also fwd certain msgs from camera to car
// 0xE4 is steering on all cars except CRV and RDX, 0x194 for CRV and RDX,
// 0x1FA is brake control, 0x30C is acc hud, 0x33D is lkas hud,
// 0x39f is radar hud
int addr = to_fwd->RIR>>21;
if (bus_num == 0) {
return 2;
} else if (bus_num == 2 && addr != 0xE4 && addr != 0x194 && addr != 0x1FA &&
addr != 0x30C && addr != 0x33D && addr != 0x39F) {
return 0;
}

return -1;
}

static int honda_bosch_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
if (bus_num == 1 || bus_num == 2) {
int addr = to_fwd->RIR>>21;
Expand All @@ -162,6 +169,15 @@ static int honda_bosch_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
return -1;
}

const safety_hooks honda_hooks = {
.init = honda_init,
.rx = honda_rx_hook,
.tx = honda_tx_hook,
.tx_lin = nooutput_tx_lin_hook,
.ignition = default_ign_hook,
.fwd = honda_fwd_hook,
};

const safety_hooks honda_bosch_hooks = {
.init = honda_bosch_init,
.rx = honda_rx_hook,
Expand Down
20 changes: 11 additions & 9 deletions board/safety/safety_toyota.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
int toyota_no_dsu_car = 0; // ch-r and camry don't have the DSU
int toyota_giraffe_switch_1 = 0; // is giraffe switch 1 high?
int toyota_camera_forwarded = 0; // should we forward the camera bus?

// global torque limit
const int TOYOTA_MAX_TORQUE = 1500; // max torque cmd allowed ever
Expand Down Expand Up @@ -49,8 +49,8 @@ static void toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {

// enter controls on rising edge of ACC, exit controls on ACC off
if ((to_push->RIR>>21) == 0x1D2) {
// 4 bits: 55-52
int cruise_engaged = to_push->RDHR & 0xF00000;
// 5th bit is CRUISE_ACTIVE
int cruise_engaged = to_push->RDLR & 0x20;
if (cruise_engaged && !toyota_cruise_engaged_last) {
controls_allowed = 1;
} else if (!cruise_engaged) {
Expand All @@ -60,16 +60,15 @@ static void toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
}

int bus = (to_push->RDTR >> 4) & 0xF;
// 0x680 is a radar msg only found in dsu-less cars
if ((to_push->RIR>>21) == 0x680 && (bus == 1)) {
toyota_no_dsu_car = 1;
// msgs are only on bus 2 if panda is connected to frc
if (bus == 2) {
toyota_camera_forwarded = 1;
}

// 0x2E4 is lkas cmd. If it is on bus 0, then giraffe switch 1 is high
if ((to_push->RIR>>21) == 0x2E4 && (bus == 0)) {
toyota_giraffe_switch_1 = 1;
}

}

static int toyota_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
Expand Down Expand Up @@ -150,13 +149,15 @@ static void toyota_init(int16_t param) {
controls_allowed = 0;
toyota_actuation_limits = 1;
toyota_giraffe_switch_1 = 0;
toyota_camera_forwarded = 0;
toyota_dbc_eps_torque_factor = param;
}

static int toyota_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {

// forward cam to radar and viceversa if car is dsu-less, except lkas cmd and hud
if ((bus_num == 0 || bus_num == 2) && toyota_no_dsu_car && !toyota_giraffe_switch_1) {
// forward cam to radar and viceversa if car, except lkas cmd and hud
// don't forward when switch 1 is high
if ((bus_num == 0 || bus_num == 2) && toyota_camera_forwarded && !toyota_giraffe_switch_1) {
int addr = to_fwd->RIR>>21;
bool is_lkas_msg = (addr == 0x2E4 || addr == 0x412) && bus_num == 2;
return is_lkas_msg? -1 : (uint8_t)(~bus_num & 0x2);
Expand All @@ -177,6 +178,7 @@ static void toyota_nolimits_init(int16_t param) {
controls_allowed = 0;
toyota_actuation_limits = 0;
toyota_giraffe_switch_1 = 0;
toyota_camera_forwarded = 0;
toyota_dbc_eps_torque_factor = param;
}

Expand Down
7 changes: 7 additions & 0 deletions python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@ class Panda(object):
SAFETY_NOOUTPUT = 0
SAFETY_HONDA = 1
SAFETY_TOYOTA = 2
SAFETY_GM = 3
SAFETY_HONDA_BOSCH = 4
SAFETY_FORD = 5
SAFETY_CADILLAC = 6
SAFETY_HYUNDAI = 7
SAFETY_TESLA = 8
SAFETY_CHRYSLER = 9
SAFETY_TOYOTA_IPAS = 0x1335
SAFETY_TOYOTA_NOLIMITS = 0x1336
SAFETY_ALLOUTPUT = 0x1337
SAFETY_ELM327 = 0xE327
Expand Down
6 changes: 6 additions & 0 deletions tests/safety/libpandasafety_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
void toyota_ipas_rx_hook(CAN_FIFOMailBox_TypeDef *to_push);
int toyota_ipas_tx_hook(CAN_FIFOMailBox_TypeDef *to_send);
void init_tests_chrysler(void);
void chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push);
int chrysler_tx_hook(CAN_FIFOMailBox_TypeDef *to_send);
void chrysler_init(int16_t param);
void set_chrysler_desired_torque_last(int t);
""")

libpandasafety = ffi.dlopen(libpandasafety_fn)
8 changes: 8 additions & 0 deletions tests/safety/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ void set_hyundai_desired_torque_last(int t){
hyundai_desired_torque_last = t;
}

void set_chrysler_desired_torque_last(int t){
chrysler_desired_torque_last = t;
}

int get_ego_speed(void){
return ego_speed;
}
Expand Down Expand Up @@ -184,6 +188,10 @@ void init_tests_honda(void){
gas_prev = 0;
}

void init_tests_chrysler(void){
chrysler_desired_torque_last = 0;
set_timer(0);
}

void set_gmlan_digital_output(int to_set){
}
Expand Down
Loading

0 comments on commit c10a755

Please sign in to comment.