-
Notifications
You must be signed in to change notification settings - Fork 2
/
Grate.pde
93 lines (87 loc) · 3.05 KB
/
Grate.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
85
86
87
88
89
90
91
92
93
void InitGrate() {
//setup grate slopes
LoadGrate();
CalculateGrate();
}
void CalculateGrate() {
m_grate_bad = (GRATE_SHAKE_INIT-GRATE_SHAKE_CROSS)/grate_min_interval;
m_grate_good = (GRATE_SHAKE_INIT-GRATE_SHAKE_CROSS)/grate_max_interval;
m_grate_on = GRATE_SHAKE_CROSS/grate_on_interval;
}
void DoGrate() { // call once per second
pRatioReactor = (float)Press[P_COMB]/(float)Press[P_REACTOR];
if (pRatioReactor > pRatioReactorLevelBoundary[PR_LOW][0] && pRatioReactor < pRatioReactorLevelBoundary[PR_LOW][1]) {
pRatioReactorLevel = PR_LOW;
}
if (pRatioReactor > pRatioReactorLevelBoundary[PR_CORRECT][0] && pRatioReactor < pRatioReactorLevelBoundary[PR_CORRECT][1]) {
pRatioReactorLevel = PR_CORRECT;
}
if (pRatioReactor > pRatioReactorLevelBoundary[PR_HIGH][0] && pRatioReactor < pRatioReactorLevelBoundary[PR_HIGH][1]) {
pRatioReactorLevel = PR_HIGH;
}
// if pressure ratio is bad for a long time, shake harder
if (pRatioReactorLevel == PR_LOW && Press[P_REACTOR] < -50 && Press[P_COMB] < -50) {
grate_pratio_accumulator++;
} else {
grate_pratio_accumulator -= 5;
}
grate_pratio_accumulator = max(0,grate_pratio_accumulator); // don't let it go below 0
// handle different shaking modes
switch (grateMode) {
case GRATE_SHAKE_ON:
digitalWrite(FET_GRATE,HIGH);
grate_motor_state = GRATE_MOTOR_LOW;
break;
case GRATE_SHAKE_OFF:
digitalWrite(FET_GRATE,LOW);
grate_motor_state = GRATE_MOTOR_OFF;
break;
case GRATE_SHAKE_PRATIO:
if (engine_state == ENGINE_ON || engine_state == ENGINE_STARTING || P_reactorLevel != OFF) { //shake only if reactor is on and/or engine is on
//condition above will leave grate_val in the last state until conditions are met (not continuing to cycle)
if (grate_val >= GRATE_SHAKE_CROSS) { // not time to shake
if (pRatioReactorLevel == PR_LOW) {
grate_val -= m_grate_bad;
} else {
grate_val -= m_grate_good;
}
digitalWrite(FET_GRATE,LOW);
grate_motor_state = GRATE_MOTOR_OFF;
}
}
if (grate_val >= 0 & grate_val <= GRATE_SHAKE_CROSS) { //time to shake or reset
grate_motor_state = GRATE_MOTOR_LOW;
digitalWrite(FET_GRATE,HIGH);
grate_val -= m_grate_on;
}
if (grate_val <= 0) {
grate_val = GRATE_SHAKE_INIT;
grate_motor_state = GRATE_MOTOR_OFF;
digitalWrite(FET_GRATE,LOW);
}
break;
}
}
void LoadGrate() {
byte check;
double ming,maxg,gon;
check = EEPROM.read(16);
ming = EEPROM.read(17)*3;
maxg = EEPROM.read(19)*3;
gon= EEPROM.read(21);
if (check == 128) { //check to see if grate has been set
Serial.println("#Loading grate from EEPROM");
grate_min_interval = ming;
grate_max_interval = maxg;
grate_on_interval = gon;
} else {
WriteGrate();
}
}
void WriteGrate() {
Serial.println("#Writing grate to EEPROM");
EEPROM.write(16,128);
EEPROM.write(17,constrain(grate_min_interval/3,0,255));
EEPROM.write(19,constrain(grate_max_interval/3,0,255));
EEPROM.write(21,constrain(grate_on_interval,0,255));
}