|
1 |
| -import math |
2 | 1 | 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 |
5 | 5 | from opendbc.can.packer import CANPacker
|
6 | 6 |
|
7 | 7 | VisualAlert = car.CarControl.HUDControl.VisualAlert
|
8 | 8 |
|
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 | + |
11 | 19 |
|
12 | 20 | class CarController():
|
13 | 21 | def __init__(self, dbc_name, CP, VM):
|
| 22 | + self.CP = CP |
| 23 | + self.VM = VM |
14 | 24 | self.packer = CANPacker(dbc_name)
|
15 |
| - self.enabled_last = False |
| 25 | + |
| 26 | + self.apply_steer_last = 0 |
| 27 | + self.steer_rate_limited = False |
16 | 28 | self.main_on_last = False
|
17 |
| - self.vehicle_model = VM |
18 |
| - self.generic_toggle_last = 0 |
| 29 | + self.lkas_enabled_last = False |
19 | 30 | self.steer_alert_last = False
|
20 |
| - self.lkas_action = 0 |
21 |
| - |
22 |
| - def update(self, enabled, CS, frame, actuators, visual_alert, pcm_cancel): |
23 | 31 |
|
| 32 | + def update(self, CC, CS, frame): |
24 | 33 | can_sends = []
|
25 |
| - steer_alert = visual_alert in (VisualAlert.steerRequired, VisualAlert.ldw) |
26 |
| - |
27 |
| - apply_steer = actuators.steer |
28 | 34 |
|
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 |
32 | 37 |
|
33 |
| - if (frame % 3) == 0: |
| 38 | + main_on = CS.out.cruiseState.available |
| 39 | + steer_alert = hud_control.visualAlert in (VisualAlert.steerRequired, VisualAlert.ldw) |
34 | 40 |
|
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)) |
36 | 44 |
|
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 |
43 | 49 |
|
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 |
47 | 53 |
|
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 |
49 | 56 |
|
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) |
52 | 59 |
|
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 |
56 | 63 |
|
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 |
59 | 66 |
|
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)) |
61 | 71 |
|
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)) |
64 | 72 |
|
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) |
69 | 75 |
|
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)) |
76 | 79 |
|
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)) |
81 | 83 |
|
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 |
84 | 86 | self.steer_alert_last = steer_alert
|
85 | 87 |
|
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