-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_machine.c
More file actions
273 lines (247 loc) · 7.4 KB
/
state_machine.c
File metadata and controls
273 lines (247 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* state_machine.c
*
* Created: 22/04/2018 16:00:41
* Author : Tanguy Simon for DNV GL Fuel fighter
* Corresponding Hardware : not hardware specific
*/
#include <stdlib.h>
#include <avr/io.h>
#include "state_machine.h"
#include "controller.h"
#include "speed.h"
#define MAX_VOLT 55.0
#define MIN_VOLT 15.0
#define MAX_AMP 25.0
#define MAX_TEMP 100
static uint8_t b_major_fault = 0;
static uint8_t fault_count = 0;
static uint16_t fault_timeout = 0;
static uint8_t fault_clear_count = 0;
static uint8_t starting_engage = 0;
//static uint8_t engage_count = 0;
uint16_t actuator_target_position = 0;
void state_handler(volatile ModuleValues_t * vals)
{
/*
uint8_t b_board_powered = (vals->f32_batt_volt >= MIN_VOLT && vals->f32_batt_volt < 100.0);
if (b_board_powered && (vals->f32_motor_current >= MAX_AMP|| vals->f32_motor_current <= -MAX_AMP || vals->f32_batt_volt > MAX_VOLT))
{
fault_count ++ ;
if (fault_count == 3) // a fault is cleared after some time and a maximum of three times. If the fault occurs more than three times,
//the board needs to be reset. there is a real problem to be investigated.
{
b_major_fault = 1;
fault_timeout = 600 ;
fault_clear_count ++;
}
}
if (fault_timeout > 0)
{
fault_timeout -- ;
}else if(b_major_fault && fault_clear_count < 3){
b_major_fault = 0;
} */
//uart is not spamming the UM so must reload watchdog timer here
if(vals->message_mode == UART){
vals->u16_watchdog_can = WATCHDOG_CAN_RELOAD_VALUE;
vals->u16_watchdog_throttle = WATCHDOG_THROTTLE_RELOAD_VALUE;
}
//ATTENTION: "TAKEN OUT": (vals->f32_batt_volt >= MIN_VOLT && vals->f32_batt_volt < 100.0);
uint8_t b_board_powered = 1;
//ATTENTION: "TAKEN OUT": (vals->f32_motor_current >= MAX_AMP|| vals->f32_motor_current <= -MAX_AMP || && (vals->f32_batt_volt > MAX_VOLT)
if (!b_board_powered)
{
fault_count ++ ;
if (fault_count == 3) // a fault is cleared after some time and a maximum of three times. If the fault occurs more than three times,
//the board needs to be reset. there is a real problem to be investigated.
{
b_major_fault = 1;
fault_timeout = 600 ;
fault_clear_count ++;
}
}
if (fault_timeout > 0)
{
fault_timeout -- ;
}else if(b_major_fault && fault_clear_count < 3){
b_major_fault = 0;
}
switch(vals->motor_status)
{
case OFF:
//transition 1 (see documentation for the state machine description)
if (vals->u16_watchdog_can > 0 && b_board_powered)
{
vals->motor_status = IDLE;
}
//During
drivers(0);//drivers shutdown
vals->b_driver_status = 0;
reset_I(); //reset integrator
vals->u8_brake_cmd = 0;
vals->u8_accel_cmd = 0;
vals->u8_duty_cycle = 50;
vals->gear_required = NEUTRAL ;
break;
case IDLE:
if (vals->pwtrain_type == BELT)
{
//controller(vals);
drivers(0); //disable
reset_I();
vals->u8_duty_cycle = 50 ;
//transition 7
if (vals->u8_brake_cmd > 0)
{
vals->u8_duty_cycle = compute_synch_duty(vals->u16_car_speed, GEAR2, vals->f32_batt_volt) ; //Setting duty
set_I(vals->u8_duty_cycle) ; //set integrator
vals->motor_status = BRAKE;
}
//transition 5
if (vals->u8_accel_cmd > 0)
{
vals->u8_duty_cycle = compute_synch_duty(vals->u16_car_speed, GEAR2, vals->f32_batt_volt) ; //Setting duty
set_I(vals->u8_duty_cycle) ; //set integrator
vals->motor_status = ACCEL;
}
}
if (vals->pwtrain_type == GEAR)
{
//transition 5
if ((vals->u8_accel_cmd > 0 || vals->u8_brake_cmd > 0) && vals->gear_status == NEUTRAL)
{
vals->motor_status = ENGAGE;
starting_engage = 1;
}
drivers(0); //disable
vals->gear_required = NEUTRAL ;
reset_I();
vals->u8_duty_cycle = 50 ;
}
break;
case ENGAGE: // /!\ TODO : with the two gears, all turning motion has to be inverted for the inner gear.
vals->gear_required = GEAR1;
if (starting_engage)
{
vals->u8_duty_cycle = compute_synch_duty(vals->u16_car_speed, vals->gear_required, vals->f32_batt_volt) ; //Setting duty
set_I(vals->u8_duty_cycle) ; //set integrator
starting_engage = 0;
}
//save_ctrl_type = vals->ctrl_type ; // PWM type ctrl is needed only for the engagement process. The mode will be reverted to previous in ACCEL and BRAKE modes
vals->ctrl_type = PWM ;
controller(vals) ; //speed up motor to synch speed
drivers(1);
//*****************--------------------------------TESTING START--------------------------*****************
/*
switch(vals->gear_required){
case NEUTRAL:
actuator_target_position = vals->position_neutral;
break;
case GEAR1:
actuator_target_position = vals->position_gear_1;
break;
case GEAR2:
actuator_target_position = vals->position_gear_2;
break;
}
while (vals->f32_actuator_feedback < (float)0.75*actuator_target_position) {
//do absolutely nothing
}
//cut power to motor
vals->u8_duty_cycle = 50;
controller(vals);
//create delay
while (engage_count != 400) {
engage_count ++;
}
engage_count = 0;
*/
//*****************--------------------------------TESTING END--------------------------*****************
//transition 9, GEAR
if (vals->u8_brake_cmd > 0 && vals->gear_status == GEAR1)
{
vals->motor_status = BRAKE;
}
//transition 10, GEAR
if (vals->u8_accel_cmd > 0 && vals->gear_status == GEAR1)
{
vals->motor_status = ACCEL;
}
//transition 11, GEAR
if (vals->u8_accel_cmd == 0 && vals->u8_brake_cmd == 0 && vals->u16_watchdog_throttle == 0)
{
vals->motor_status = IDLE;
}
break;
case ACCEL:
vals->ctrl_type = CURRENT;
controller(vals);
drivers(1);
//transition 6
if (vals->u8_accel_cmd == 0 && vals->u16_watchdog_throttle == 0)
{
vals->motor_status = IDLE;
}
//transition 12, GEAR
if (vals->pwtrain_type == GEAR && vals->gear_status == NEUTRAL)
{
vals->motor_status = ENGAGE;
starting_engage = 1;
}
//transition 14
if (vals->u8_brake_cmd > 0 && vals->u8_accel_cmd == 0)
{
vals->motor_status = BRAKE;
}
break;
case BRAKE:
vals->ctrl_type = CURRENT ;
controller(vals); //negative throttle cmd
drivers(1);
//transition 8
if (vals->u8_brake_cmd == 0 && vals->u16_watchdog_throttle == 0)
{
vals->motor_status = IDLE;
}
//transition 13, GEAR
if (vals->pwtrain_type == GEAR && vals->gear_status == NEUTRAL)
{
vals->motor_status = ENGAGE;
starting_engage = 1;
}
//transition 15
if (vals->u8_brake_cmd == 0 && vals->u8_accel_cmd > 0)
{
vals->motor_status = ACCEL;
}
break;
case ERR:
//transition 4
//ATTENTION: "TAKEN OUT": && vals->u8_motor_temp < MAX_TEMP
//TODO: Implement what the actuator should do when in error mode
if (!b_major_fault)
{
vals->motor_status = IDLE;
}
drivers(0);//drivers shutdown
vals->b_driver_status = 0;
vals->gear_required = NEUTRAL;
reset_I(); //reset integrator
vals->u8_brake_cmd = 0;
vals->u8_accel_cmd = 0;
vals->u8_duty_cycle = 50;
break;
}
if ((vals->motor_status == IDLE || vals->motor_status == ACCEL || vals->motor_status == BRAKE || vals->motor_status == ENGAGE) && (vals->u16_watchdog_can == 0 || !b_board_powered))
{
// transition 2
vals->motor_status = OFF;
}
/*ATTENTION: "TAKEN OUT": COMMENTED OUT
if (b_major_fault || vals->u8_motor_temp >= MAX_TEMP) //over current, over voltage, over temp
{
//transition 3
vals->motor_status = ERR;
}*/
}