-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEngine.pde
86 lines (82 loc) · 2.85 KB
/
Engine.pde
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
void DoEngine() {
switch (engine_state) {
case ENGINE_OFF:
if (control_state == CONTROL_START) {
TransitionEngine(ENGINE_STARTING);
}
SetThrottleAngle(0);
break;
case ENGINE_ON:
if (control_state == CONTROL_OFF & millis()-control_state_entered > 100) {
TransitionEngine(ENGINE_OFF);
}
if (control_state == CONTROL_START) {
TransitionEngine(ENGINE_STARTING);
}
if (P_reactorLevel == OFF & millis()-engine_state_entered > 10000) { //if reactor is at low vacuum after ten seconds, engine did not catch, so turn off
TransitionEngine(ENGINE_OFF);
}
break;
case ENGINE_STARTING:
if (control_state == CONTROL_OFF & millis()-control_state_entered > 100) {
TransitionEngine(ENGINE_OFF);
}
SetThrottleAngle(100); // % open
// #ifdef INT_HERTZ
// // Use RPM detection to stop cranking automatically
// if (CalculatePeriodHertz() > 40) { //if engine is caught, stop cranking
// TransitionEngine(ENGINE_ON);
// }
// if (engine_end_cranking < millis()) { //if engine still has not caught, stop cranking
// TransitionEngine(ENGINE_OFF);
// }
// #else
// Use starter button in the standard manual control configuration (push button to start, release to stop cranking)
if (control_state == CONTROL_ON) {
TransitionEngine(ENGINE_ON);
}
// #endif
break;
case ENGINE_GOV_TUNING:
if (control_state == CONTROL_OFF) {
TransitionEngine(ENGINE_OFF);
}
break;
}
}
void TransitionEngine(int new_state) {
//can look at engine_state for "old" state before transitioning at the end of this method
engine_state_entered = millis();
switch (new_state) {
case ENGINE_OFF:
//analogWrite(FET_IGNITION,0);
analogWrite(FET_STARTER,0);
Serial.println("# New Engine State: Off");
TransitionMessage("Engine: Off ");
break;
case ENGINE_ON:
//analogWrite(FET_IGNITION,255);
analogWrite(FET_STARTER,0);
Serial.println("# New Engine State: On");
TransitionMessage("Engine: Running ");
break;
case ENGINE_STARTING:
//analogWrite(FET_IGNITION,255);
analogWrite(FET_STARTER,255);
engine_end_cranking = millis() + engine_crank_period;
Serial.println("# New Engine State: Starting");
TransitionMessage("Engine: Starting ");
break;
case ENGINE_GOV_TUNING:
//analogWrite(FET_IGNITION,255);
analogWrite(FET_STARTER,0);
Serial.println("# New Engine State: Governor Tuning");
TransitionMessage("Engine: Gov Tuning ");
break;
}
engine_state=new_state;
}
void SetThrottleAngle(double percent) {
Servo_Throttle.write(throttle_valve_closed + percent*(throttle_valve_open-throttle_valve_closed));
//servo2_pos = percent;
}