20
20
signal .signal (signal .SIGINT , signal .SIG_DFL ) # Enable Ctrl-C on plot windows
21
21
22
22
ACC_G = 9.81
23
+ FPS = 10
23
24
CONTROL_START_IDX = 100
25
+ COST_END_IDX = 550
24
26
CONTEXT_LENGTH = 20
25
27
VOCAB_SIZE = 1024
26
28
LATACCEL_RANGE = [- 5 , 5 ]
29
31
DEL_T = 0.1
30
32
LAT_ACCEL_COST_MULTIPLIER = 5.0
31
33
34
+ FUTURE_PLAN_STEPS = FPS * 5 # 5 secs
35
+
32
36
State = namedtuple ('State' , ['roll_lataccel' , 'v_ego' , 'a_ego' ])
33
37
34
38
@@ -95,10 +99,12 @@ def __init__(self, model: TinyPhysicsModel, data_path: str, controller: BaseCont
95
99
96
100
def reset (self ) -> None :
97
101
self .step_idx = CONTEXT_LENGTH
98
- self .state_history = [self .get_state_target (i )[0 ] for i in range (self .step_idx )]
102
+ state_targetfutures = [self .get_state_targetfuture (i ) for i in range (self .step_idx )]
103
+ self .state_history = [x ['state' ] for x in state_targetfutures ]
99
104
self .action_history = self .data ['steer_command' ].values [:self .step_idx ].tolist ()
100
- self .current_lataccel_history = [self .get_state_target (i )[1 ] for i in range (self .step_idx )]
101
- self .target_lataccel_history = [self .get_state_target (i )[1 ] for i in range (self .step_idx )]
105
+ self .current_lataccel_history = [x ['targetfuture' ][0 ] for x in state_targetfutures ]
106
+ self .target_lataccel_history = [x ['targetfuture' ][0 ] for x in state_targetfutures ]
107
+ self .target_future = None
102
108
self .current_lataccel = self .current_lataccel_history [- 1 ]
103
109
seed = int (md5 (self .data_path .encode ()).hexdigest (), 16 ) % 10 ** 4
104
110
np .random .seed (seed )
@@ -124,25 +130,29 @@ def sim_step(self, step_idx: int) -> None:
124
130
if step_idx >= CONTROL_START_IDX :
125
131
self .current_lataccel = pred
126
132
else :
127
- self .current_lataccel = self .get_state_target (step_idx )[1 ]
133
+ self .current_lataccel = self .get_state_targetfuture (step_idx )['targetfuture' ][ 0 ]
128
134
129
135
self .current_lataccel_history .append (self .current_lataccel )
130
136
131
137
def control_step (self , step_idx : int ) -> None :
132
- action = self .controller .update (self .target_lataccel_history [step_idx ], self .current_lataccel , self .state_history [step_idx ])
138
+ action = self .controller .update (self .target_lataccel_history [step_idx ], self .current_lataccel , self .state_history [step_idx ], target_future = self . target_future )
133
139
if step_idx < CONTROL_START_IDX :
134
140
action = self .data ['steer_command' ].values [step_idx ]
135
141
action = np .clip (action , STEER_RANGE [0 ], STEER_RANGE [1 ])
136
142
self .action_history .append (action )
137
143
138
- def get_state_target (self , step_idx : int ) -> Tuple [State , float ]:
144
+ def get_state_targetfuture (self , step_idx : int ) -> Tuple [State , float ]:
139
145
state = self .data .iloc [step_idx ]
140
- return State (roll_lataccel = state ['roll_lataccel' ], v_ego = state ['v_ego' ], a_ego = state ['a_ego' ]), state ['target_lataccel' ]
146
+ return {
147
+ 'state' : State (roll_lataccel = state ['roll_lataccel' ], v_ego = state ['v_ego' ], a_ego = state ['a_ego' ]),
148
+ 'targetfuture' : self .data ['target_lataccel' ].values [step_idx :step_idx + FUTURE_PLAN_STEPS ].tolist ()
149
+ }
141
150
142
151
def step (self ) -> None :
143
- state , target = self .get_state_target (self .step_idx )
144
- self .state_history .append (state )
145
- self .target_lataccel_history .append (target )
152
+ state_targetfuture = self .get_state_targetfuture (self .step_idx )
153
+ self .state_history .append (state_targetfuture ['state' ])
154
+ self .target_lataccel_history .append (state_targetfuture ['targetfuture' ][0 ])
155
+ self .target_future = state_targetfuture ['targetfuture' ][1 :]
146
156
self .control_step (self .step_idx )
147
157
self .sim_step (self .step_idx )
148
158
self .step_idx += 1
@@ -158,8 +168,8 @@ def plot_data(self, ax, lines, axis_labels, title) -> None:
158
168
ax .set_ylabel (axis_labels [1 ])
159
169
160
170
def compute_cost (self ) -> dict :
161
- target = np .array (self .target_lataccel_history )[CONTROL_START_IDX :]
162
- pred = np .array (self .current_lataccel_history )[CONTROL_START_IDX :]
171
+ target = np .array (self .target_lataccel_history )[CONTROL_START_IDX :COST_END_IDX ]
172
+ pred = np .array (self .current_lataccel_history )[CONTROL_START_IDX :COST_END_IDX ]
163
173
164
174
lat_accel_cost = np .mean ((target - pred )** 2 ) * 100
165
175
jerk_cost = np .mean ((np .diff (pred ) / DEL_T )** 2 ) * 100
0 commit comments