Skip to content

Commit f047233

Browse files
Ford: Support for LCA vehicles (commaai#23331)
* Ford: add Focus Mk4 Also removes support for the Ford Fusion. * Ford: LKAS/LCA steering and UI CAN commands * Ford: implement CarController w/ steering and lanes ui * Ford: FPv2 firmware request * Ford: Add FW for 2018 Ford Focus * Ford: add Escape Mk4 * bump panda * cleanup * add that back Co-authored-by: Adeeb Shihadeh <[email protected]>
1 parent dcd8341 commit f047233

File tree

8 files changed

+518
-164
lines changed

8 files changed

+518
-164
lines changed

release/files_common

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,8 @@ opendbc/gm_global_a_powertrain_generated.dbc
560560
opendbc/gm_global_a_object.dbc
561561
opendbc/gm_global_a_chassis.dbc
562562

563-
opendbc/ford_fusion_2018_pt.dbc
564563
opendbc/ford_fusion_2018_adas.dbc
564+
opendbc/ford_lincoln_base_pt.dbc
565565

566566
opendbc/honda_accord_2018_can_generated.dbc
567567
opendbc/acura_ilx_2016_can_generated.dbc

selfdrive/car/ford/carcontroller.py

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,91 @@
1-
import math
21
from cereal import car
3-
from selfdrive.car import make_can_msg
4-
from selfdrive.car.ford.fordcan import create_steer_command, create_lkas_ui, spam_cancel_button
2+
from common.numpy_fast import clip, interp
3+
from selfdrive.car.ford import fordcan
4+
from selfdrive.car.ford.values import CarControllerParams
55
from opendbc.can.packer import CANPacker
66

77
VisualAlert = car.CarControl.HUDControl.VisualAlert
88

9-
MAX_STEER_DELTA = 1
10-
TOGGLE_DEBUG = False
9+
10+
def apply_ford_steer_angle_limits(apply_steer, apply_steer_last, vEgo):
11+
# rate limit
12+
steer_up = apply_steer * apply_steer_last > 0. and abs(apply_steer) > abs(apply_steer_last)
13+
rate_limit = CarControllerParams.STEER_RATE_LIMIT_UP if steer_up else CarControllerParams.STEER_RATE_LIMIT_DOWN
14+
max_angle_diff = interp(vEgo, rate_limit.speed_points, rate_limit.max_angle_diff_points)
15+
apply_steer = clip(apply_steer, (apply_steer_last - max_angle_diff), (apply_steer_last + max_angle_diff))
16+
17+
return apply_steer
18+
1119

1220
class CarController():
1321
def __init__(self, dbc_name, CP, VM):
22+
self.CP = CP
23+
self.VM = VM
1424
self.packer = CANPacker(dbc_name)
15-
self.enabled_last = False
25+
26+
self.apply_steer_last = 0
27+
self.steer_rate_limited = False
1628
self.main_on_last = False
17-
self.vehicle_model = VM
18-
self.generic_toggle_last = 0
29+
self.lkas_enabled_last = False
1930
self.steer_alert_last = False
20-
self.lkas_action = 0
21-
22-
def update(self, enabled, CS, frame, actuators, visual_alert, pcm_cancel):
2331

32+
def update(self, CC, CS, frame):
2433
can_sends = []
25-
steer_alert = visual_alert in (VisualAlert.steerRequired, VisualAlert.ldw)
26-
27-
apply_steer = actuators.steer
2834

29-
if pcm_cancel:
30-
#print "CANCELING!!!!"
31-
can_sends.append(spam_cancel_button(self.packer))
35+
actuators = CC.actuators
36+
hud_control = CC.hudControl
3237

33-
if (frame % 3) == 0:
38+
main_on = CS.out.cruiseState.available
39+
steer_alert = hud_control.visualAlert in (VisualAlert.steerRequired, VisualAlert.ldw)
3440

35-
curvature = self.vehicle_model.calc_curvature(math.radians(actuators.steeringAngleDeg), CS.out.vEgo, 0.0)
41+
if CC.cruiseControl.cancel:
42+
# cancel stock ACC
43+
can_sends.append(fordcan.spam_cancel_button(self.packer))
3644

37-
# The use of the toggle below is handy for trying out the various LKAS modes
38-
if TOGGLE_DEBUG:
39-
self.lkas_action += int(CS.out.genericToggle and not self.generic_toggle_last)
40-
self.lkas_action &= 0xf
41-
else:
42-
self.lkas_action = 5 # 4 and 5 seem the best. 8 and 9 seem to aggressive and laggy
45+
# apply rate limits
46+
new_steer = actuators.steeringAngleDeg
47+
apply_steer = apply_ford_steer_angle_limits(new_steer, self.apply_steer_last, CS.out.vEgo)
48+
self.steer_rate_limited = new_steer != apply_steer
4349

44-
can_sends.append(create_steer_command(self.packer, apply_steer, enabled,
45-
CS.lkas_state, CS.out.steeringAngleDeg, curvature, self.lkas_action))
46-
self.generic_toggle_last = CS.out.genericToggle
50+
# send steering commands at 20Hz
51+
if (frame % CarControllerParams.LKAS_STEER_STEP) == 0:
52+
lca_rq = 1 if CC.active else 0
4753

48-
if (frame % 100) == 0:
54+
# use LatCtlPath_An_Actl to actuate steering for now until curvature control is implemented
55+
path_angle = apply_steer
4956

50-
can_sends.append(make_can_msg(973, b'\x00\x00\x00\x00\x00\x00\x00\x00', 0))
51-
#can_sends.append(make_can_msg(984, b'\x00\x00\x00\x00\x80\x45\x60\x30', 0))
57+
# convert steer angle to curvature
58+
curvature = self.VM.calc_curvature(apply_steer, CS.out.vEgo, 0.0)
5259

53-
if (frame % 100) == 0 or (self.enabled_last != enabled) or (self.main_on_last != CS.out.cruiseState.available) or \
54-
(self.steer_alert_last != steer_alert):
55-
can_sends.append(create_lkas_ui(self.packer, CS.out.cruiseState.available, enabled, steer_alert))
60+
# TODO: get other actuators
61+
curvature_rate = 0
62+
path_offset = 0
5663

57-
if (frame % 200) == 0:
58-
can_sends.append(make_can_msg(1875, b'\x80\xb0\x55\x55\x78\x90\x00\x00', 1))
64+
ramp_type = 3 # 0=Slow, 1=Medium, 2=Fast, 3=Immediately
65+
precision = 0 # 0=Comfortable, 1=Precise
5966

60-
if (frame % 10) == 0:
67+
self.apply_steer_last = apply_steer
68+
can_sends.append(fordcan.create_lkas_command(self.packer, apply_steer, curvature))
69+
can_sends.append(fordcan.create_tja_command(self.packer, lca_rq, ramp_type, precision,
70+
path_offset, path_angle, curvature_rate, curvature))
6171

62-
can_sends.append(make_can_msg(1648, b'\x00\x00\x00\x40\x00\x00\x50\x00', 1))
63-
can_sends.append(make_can_msg(1649, b'\x10\x10\xf1\x70\x04\x00\x00\x00', 1))
6472

65-
can_sends.append(make_can_msg(1664, b'\x00\x00\x03\xe8\x00\x01\xa9\xb2', 1))
66-
can_sends.append(make_can_msg(1674, b'\x08\x00\x00\xff\x0c\xfb\x6a\x08', 1))
67-
can_sends.append(make_can_msg(1675, b'\x00\x00\x3b\x60\x37\x00\x00\x00', 1))
68-
can_sends.append(make_can_msg(1690, b'\x70\x00\x00\x55\x86\x1c\xe0\x00', 1))
73+
### ui ###
74+
send_ui = (self.main_on_last != main_on) or (self.lkas_enabled_last != CC.active) or (self.steer_alert_last != steer_alert)
6975

70-
can_sends.append(make_can_msg(1910, b'\x06\x4b\x06\x4b\x42\xd3\x11\x30', 1))
71-
can_sends.append(make_can_msg(1911, b'\x48\x53\x37\x54\x48\x53\x37\x54', 1))
72-
can_sends.append(make_can_msg(1912, b'\x31\x34\x47\x30\x38\x31\x43\x42', 1))
73-
can_sends.append(make_can_msg(1913, b'\x31\x34\x47\x30\x38\x32\x43\x42', 1))
74-
can_sends.append(make_can_msg(1969, b'\xf4\x40\x00\x00\x00\x00\x00\x00', 1))
75-
can_sends.append(make_can_msg(1971, b'\x0b\xc0\x00\x00\x00\x00\x00\x00', 1))
76+
# send lkas ui command at 1Hz or if ui state changes
77+
if (frame % CarControllerParams.LKAS_UI_STEP) == 0 or send_ui:
78+
can_sends.append(fordcan.create_lkas_ui_command(self.packer, main_on, CC.active, steer_alert, CS.lkas_status_stock_values))
7679

77-
static_msgs = range(1653, 1658)
78-
for addr in static_msgs:
79-
cnt = (frame % 10) + 1
80-
can_sends.append(make_can_msg(addr, (cnt << 4).to_bytes(1, 'little') + b'\x00\x00\x00\x00\x00\x00\x00', 1))
80+
# send acc ui command at 20Hz or if ui state changes
81+
if (frame % CarControllerParams.ACC_UI_STEP) == 0 or send_ui:
82+
can_sends.append(fordcan.create_acc_ui_command(self.packer, main_on, CC.active, CS.acc_tja_status_stock_values))
8183

82-
self.enabled_last = enabled
83-
self.main_on_last = CS.out.cruiseState.available
84+
self.main_on_last = main_on
85+
self.lkas_enabled_last = CC.active
8486
self.steer_alert_last = steer_alert
8587

86-
return actuators, can_sends
88+
new_actuators = actuators.copy()
89+
new_actuators.steeringAngleDeg = apply_steer
90+
91+
return new_actuators, can_sends

0 commit comments

Comments
 (0)