Skip to content

Commit 79ab5af

Browse files
author
Commaremote
committed
Toyota: moved common functions into safety header file
1 parent 40c8dda commit 79ab5af

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

board/safety.h

+23
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
// sample struct that keeps 3 samples in memory
2+
struct sample_t {
3+
int values[3];
4+
int min;
5+
int max;
6+
} sample_t_default = {{0, 0, 0}, 0, 0};
7+
18
void safety_rx_hook(CAN_FIFOMailBox_TypeDef *to_push);
29
int safety_tx_hook(CAN_FIFOMailBox_TypeDef *to_send);
310
int safety_tx_lin_hook(int lin_num, uint8_t *data, int len);
411
int safety_ignition_hook();
512
uint32_t get_ts_elapsed(uint32_t ts, uint32_t ts_last);
613
int to_signed(int d, int bits);
14+
void update_sample(struct sample_t *sample, int sample_new);
715

816
typedef void (*safety_hook_init)(int16_t param);
917
typedef void (*rx_hook)(CAN_FIFOMailBox_TypeDef *to_push);
@@ -118,3 +126,18 @@ int to_signed(int d, int bits) {
118126
}
119127
return d;
120128
}
129+
130+
// given a new sample, update the smaple_t struct
131+
void update_sample(struct sample_t *sample, int sample_new) {
132+
for (int i = sizeof(sample->values)/sizeof(sample->values[0]) - 1; i > 0; i--) {
133+
sample->values[i] = sample->values[i-1];
134+
}
135+
sample->values[0] = sample_new;
136+
137+
// get the minimum and maximum measured samples
138+
sample->min = sample->max = sample->values[0];
139+
for (int i = 1; i < sizeof(sample->values)/sizeof(sample->values[0]); i++) {
140+
if (sample->values[i] < sample->min) sample->min = sample->values[i];
141+
if (sample->values[i] > sample->max) sample->max = sample->values[i];
142+
}
143+
}

board/safety/safety_toyota.h

-19
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
// track the torque measured for limiting
2-
struct sample_t {
3-
int values[3];
4-
int min;
5-
int max;
6-
} sample_t_default = {{0, 0, 0}, 0, 0};
71
struct sample_t torque_meas; // last 3 motor torques produced by the eps
82

93
// global torque limit
@@ -34,19 +28,6 @@ int16_t rt_torque_last = 0; // last desired torque for real time chec
3428
uint32_t ts_last = 0;
3529
int cruise_engaged_last = 0; // cruise state
3630

37-
void update_sample(struct sample_t *sample, int sample_new) {
38-
for (int i = sizeof(sample->values)/sizeof(sample->values[0]) - 1; i > 0; i--) {
39-
sample->values[i] = sample->values[i-1];
40-
}
41-
sample->values[0] = sample_new;
42-
43-
// get the minimum and maximum measured torque over the last 3 frames
44-
sample->min = sample->max = sample->values[0];
45-
for (int i = 1; i < sizeof(sample->values)/sizeof(sample->values[0]); i++) {
46-
if (sample->values[i] < sample->min) sample->min = sample->values[i];
47-
if (sample->values[i] > sample->max) sample->max = sample->values[i];
48-
}
49-
}
5031

5132
static void toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
5233
// get eps motor torque (0.66 factor in dbc)

0 commit comments

Comments
 (0)