@@ -12,8 +12,10 @@ int safety_ignition_hook();
12
12
uint32_t get_ts_elapsed (uint32_t ts , uint32_t ts_last );
13
13
int to_signed (int d , int bits );
14
14
void update_sample (struct sample_t * sample , int sample_new );
15
- int rt_rate_limit_check (int val , int val_last , const int MAX_RT_DELTA );
16
15
int max_limit_check (int val , const int MAX );
16
+ int dist_to_meas_check (int val , int val_last , struct sample_t * val_meas ,
17
+ const int MAX_RATE_UP , const int MAX_RATE_DOWN , const int MAX_ERROR );
18
+ int rt_rate_limit_check (int val , int val_last , const int MAX_RT_DELTA );
17
19
18
20
typedef void (* safety_hook_init )(int16_t param );
19
21
typedef void (* rx_hook )(CAN_FIFOMailBox_TypeDef * to_push );
@@ -144,17 +146,33 @@ void update_sample(struct sample_t *sample, int sample_new) {
144
146
}
145
147
}
146
148
149
+ int max_limit_check (int val , const int MAX ) {
150
+ return (val > MAX ) | (val < - MAX );
151
+ }
152
+
153
+ // check that commanded value isn't too far from measured
154
+ int dist_to_meas_check (int val , int val_last , struct sample_t * val_meas ,
155
+ const int MAX_RATE_UP , const int MAX_RATE_DOWN , const int MAX_ERROR ) {
156
+
157
+ // *** val rate limit check ***
158
+ int16_t highest_allowed_val = max (val_last , 0 ) + MAX_RATE_UP ;
159
+ int16_t lowest_allowed_val = min (val_last , 0 ) - MAX_RATE_UP ;
160
+
161
+ // if we've exceeded the meas val, we must start moving toward 0
162
+ highest_allowed_val = min (highest_allowed_val , max (val_last - MAX_RATE_DOWN , max (val_meas -> max , 0 ) + MAX_ERROR ));
163
+ lowest_allowed_val = max (lowest_allowed_val , min (val_last + MAX_RATE_DOWN , min (val_meas -> min , 0 ) - MAX_ERROR ));
164
+
165
+ // check for violation
166
+ return (val < lowest_allowed_val ) || (val > highest_allowed_val );
167
+ }
168
+
147
169
// real time check, mainly used for steer torque rate limiter
148
170
int rt_rate_limit_check (int val , int val_last , const int MAX_RT_DELTA ) {
149
171
150
172
// *** torque real time rate limit check ***
151
173
int16_t highest_val = max (val_last , 0 ) + MAX_RT_DELTA ;
152
174
int16_t lowest_val = min (val_last , 0 ) - MAX_RT_DELTA ;
153
175
154
- // return 1 if violation
176
+ // check for violation
155
177
return (val < lowest_val ) || (val > highest_val );
156
178
}
157
-
158
- int max_limit_check (int val , const int MAX ) {
159
- return (val > MAX ) | (val < - MAX );
160
- }
0 commit comments