Skip to content

Commit 16711b3

Browse files
authored
Merge pull request commaai#118 from arne182/stocklka
Stocklka feature
2 parents 4c35691 + 6e9dcba commit 16711b3

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

panda/board/safety/safety_toyota.h

+26-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ int toyota_giraffe_switch_1 = 0; // is giraffe switch 1 high?
22
int toyota_camera_forwarded = 0; // should we forward the camera bus?
33

44
// global torque limit
5-
const int TOYOTA_MAX_TORQUE = 1500; // max torque cmd allowed ever
5+
const int TOYOTA_MAX_TORQUE = 1800; // max torque cmd allowed ever
66

77
// rate based torque limit + stay within actually applied
88
// packet is sent at 100hz, so this limit is 1000/sec
@@ -16,8 +16,8 @@ const int TOYOTA_MAX_RT_DELTA = 375; // max delta torque allowed for real t
1616
const int TOYOTA_RT_INTERVAL = 250000; // 250ms between real time checks
1717

1818
// longitudinal limits
19-
const int TOYOTA_MAX_ACCEL = 1500; // 1.5 m/s2
20-
const int TOYOTA_MIN_ACCEL = -3000; // 3.0 m/s2
19+
const int TOYOTA_MAX_ACCEL = 1800; // 1.8 m/s2
20+
const int TOYOTA_MIN_ACCEL = -3600; // 3.6 m/s2
2121

2222
// global actuation limit state
2323
int toyota_actuation_limits = 1; // by default steer limits are imposed
@@ -28,10 +28,17 @@ int toyota_desired_torque_last = 0; // last desired steer torque
2828
int toyota_rt_torque_last = 0; // last desired torque for real time check
2929
uint32_t toyota_ts_last = 0;
3030
int toyota_cruise_engaged_last = 0; // cruise state
31+
int ego_speed_toyota = 0; // speed
3132
struct sample_t toyota_torque_meas; // last 3 motor torques produced by the eps
3233

3334

3435
static void toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
36+
// sample speed
37+
if ((to_push->RIR>>21) == 0xb4) {
38+
// Middle bytes needed
39+
ego_speed_toyota = (to_push->RDHR >> 8) & 0xFFFF; //Speed is 100x
40+
}// Special thanks to Willem Melching for the code
41+
3542
// get eps motor torque (0.66 factor in dbc)
3643
if ((to_push->RIR>>21) == 0x260) {
3744
int torque_meas_new = (((to_push->RDHR) & 0xFF00) | ((to_push->RDHR >> 16) & 0xFF));
@@ -100,11 +107,20 @@ static int toyota_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
100107
uint32_t ts = TIM2->CNT;
101108

102109
// only check if controls are allowed and actuation_limits are imposed
103-
if (controls_allowed && toyota_actuation_limits) {
110+
if (toyota_actuation_limits) {
104111

105112
// *** global torque limit check ***
106-
violation |= max_limit_check(desired_torque, TOYOTA_MAX_TORQUE, -TOYOTA_MAX_TORQUE);
107-
113+
114+
if (!toyota_cruise_engaged_last){
115+
if (ego_speed_toyota > 4500){
116+
violation |= max_limit_check(desired_torque, 805, -805);
117+
} else {
118+
violation = 1;
119+
}
120+
121+
} else {
122+
violation |= max_limit_check(desired_torque, TOYOTA_MAX_TORQUE, -TOYOTA_MAX_TORQUE);
123+
}
108124
// *** torque rate limit check ***
109125
violation |= dist_to_meas_check(desired_torque, toyota_desired_torque_last,
110126
&toyota_torque_meas, TOYOTA_MAX_RATE_UP, TOYOTA_MAX_RATE_DOWN, TOYOTA_MAX_TORQUE_ERROR);
@@ -124,12 +140,12 @@ static int toyota_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
124140
}
125141

126142
// no torque if controls is not allowed
127-
if (!controls_allowed && (desired_torque != 0)) {
128-
violation = 1;
129-
}
143+
//if (!controls_allowed && (desired_torque != 0)) {
144+
// violation = 1;
145+
//}
130146

131147
// reset to 0 if either controls is not allowed or there's a violation
132-
if (violation || !controls_allowed) {
148+
if (violation) {
133149
toyota_desired_torque_last = 0;
134150
toyota_rt_torque_last = 0;
135151
toyota_ts_last = ts;

selfdrive/car/toyota/carcontroller.py

+14
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,19 @@ def update(self, sendcan, enabled, CS, frame, actuators,
198198
apply_steer_req = 0
199199
else:
200200
apply_steer_req = 1
201+
if not enabled and rightLane_Depart and CS.v_ego > 12.5 and not CS.right_blinker_on:
202+
apply_steer = self.last_steer + 3
203+
apply_steer = min(apply_steer , 800)
204+
#print "right"
205+
#print apply_steer
206+
apply_steer_req = 1
207+
208+
if not enabled and leftLane_Depart and CS.v_ego > 12.5 and not CS.left_blinker_on:
209+
apply_steer = self.last_steer - 3
210+
apply_steer = max(apply_steer , -800)
211+
#print "left"
212+
#print apply_steer
213+
apply_steer_req = 1
201214

202215
self.steer_angle_enabled, self.ipas_reset_counter = \
203216
ipas_state_transition(self.steer_angle_enabled, enabled, CS.ipas_active, self.ipas_reset_counter)
@@ -288,6 +301,7 @@ def update(self, sendcan, enabled, CS, frame, actuators,
288301
else:
289302
if CS.lane_departure_toggle_on:
290303
can_sends.append(create_steer_command(self.packer, apply_steer, apply_steer_req, frame))
304+
#print "here"
291305
else:
292306
can_sends.append(create_steer_command(self.packer, 0., 0, frame))
293307
# rav4h with dsu disconnected

selfdrive/car/toyota/carstate.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ def update(self, cp, cp_cam):
378378
if self.read_distance_lines == 3:
379379
self.UE.custom_alert_message(2,"Following distance set to 2.7s",200,3)
380380
self.read_distance_lines_prev = self.read_distance_lines
381-
381+
if cp.vl["EPS_STATUS"]['LKA_STATE'] == 17:
382+
self.cstm_btns.set_button_status("lka", 0)
382383
if bool(cp.vl["JOEL_ID"]['ACC_SLOW']) <> self.acc_slow_on_prev:
383384
self.acc_slow_on = bool(cp.vl["JOEL_ID"]['ACC_SLOW'])
384385
if self.acc_slow_on:

selfdrive/controls/lib/planner.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,8 @@ def update(self, CS, CP, VM, PP, live20, live100, md, live_map_data):
542542
plan_send.plan.aTarget = self.a_acc
543543
plan_send.plan.vTargetFuture = self.v_acc_future
544544
plan_send.plan.hasLead = self.mpc1.prev_lead_status
545-
plan_send.plan.hasrightLaneDepart = bool(PP.r_poly[3] > -1.15 and not CS.carState.rightBlinker)
546-
plan_send.plan.hasleftLaneDepart = bool(PP.l_poly[3] < 1.15 and not CS.carState.leftBlinker)
545+
plan_send.plan.hasrightLaneDepart = bool(PP.r_poly[3] > -1.1 and not CS.carState.rightBlinker)
546+
plan_send.plan.hasleftLaneDepart = bool(PP.l_poly[3] < 1.05 and not CS.carState.leftBlinker)
547547
plan_send.plan.longitudinalPlanSource = self.longitudinalPlanSource
548548

549549
plan_send.plan.vCurvature = self.v_curvature

0 commit comments

Comments
 (0)