-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECU_code1.ino
221 lines (196 loc) · 6.69 KB
/
ECU_code1.ino
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
// SCS Implementation Left - BPS, Buzzer
// Flags
int rtdmode = 0, brakefault = 0, appsfault = 0, apps_scs = 0;
// Pins utilized in DAQ - 2, 12, 13, 14, 15, 16, 17, 32(can change), 33(can change), 34
// Pins avaliable - 4, 5, 18, 19, 22, 23, 25, 26, 27, 35(input only), 36(input only), 39(input only)
// Pin Definitions
#define buzzer 4
#define RTD_LED 5
#define pwm 23
#define testled 40 // ?
#define apps_led 25 //?
#define bspd_led 36
#define apps1_pin 26
#define apps2_pin 27
#define BPS_pin 39
#define RTDB_pin 35
#define bps_scs_pin 18
#define ECU_SCS_pin 19
#define Air_State_pin 22
// Thresholds 12-bit ADC
#define lt1 1450.0 //1.25V
#define lt2 750.0 //0.75V
#define ht1 3102.27 //2.5V
#define ht2 1861.36 //1.5V
#define bps_th 930.68 //0.75V
void setup() {
Serial.begin(115200);
pinMode(buzzer, OUTPUT);
pinMode(RTD_LED, OUTPUT);
pinMode(pwm, OUTPUT);
pinMode(testled, OUTPUT);
pinMode(apps_led, OUTPUT);
pinMode(bspd_led, OUTPUT);
pinMode(apps1_pin, INPUT);
pinMode(apps2_pin, INPUT);
analogReadResolution(12); // Set ADC resolution to 12-bit
PWM_init();
}
void loop() {
float a, b, apps1, apps2, apps1_analog, apps2_analog,bps;
int rtdb, bps_scs, ECU_SCS, Air_State;
rtdb = analogRead(RTDB_pin);
bps = analogRead(BPS_pin);
bps_scs = digitalRead(bps_scs_pin);
ECU_SCS = digitalRead(ECU_SCS_pin);
Air_State = digitalRead(Air_State_pin);
//Print the states of each pin
Serial.print("RTDB Pin State: ");
Serial.println(rtdb);
Serial.print("BPS Pin State: ");
Serial.println(bps);
Serial.print("ECU_SCS Pin State: ");
Serial.println(ECU_SCS);
Serial.print("Air_State Pin State: ");
Serial.println(Air_State);
Serial.print("Value of BPS=");
Serial.println(bps*3.3/4095);
Serial.println(bps_th*3.3/4095);
if (bps >= bps_th && rtdb > 3000 && ECU_SCS == 1 && Air_State == 1 && rtdmode == 0 && brakefault == 0 && appsfault == 0) { // pending - bps scs check
unsigned long startTime = millis();
while (millis() - startTime <= 2000) {
digitalWrite(buzzer, HIGH);
}
digitalWrite(buzzer, LOW);
Serial.println("Buzz... Buzz... Buzz...");
rtdmode = 1;
}
if (rtdmode) {
digitalWrite(RTD_LED, HIGH);
Serial.println("RTD Mode Entered");
bps = analogRead(BPS_pin);
apps1_analog = analogRead(apps1_pin);
apps2_analog = analogRead(apps2_pin);
Serial.print("Apps1: ");
apps1 = apps1_analog * 3.6 / 4095;
Serial.println(apps1);
apps2 = apps2_analog * 3.6 / 4095;
Serial.print("Apps2: ");
Serial.println(apps2);
a = ((float)(apps1_analog - lt1) / (float)(ht1 - lt1));
b = ((float)(apps2_analog - lt2) / (float)(ht2 - lt2));
Serial.print("a: ");
Serial.println(a);
Serial.print("b: ");
Serial.println(b);
analogWrite(pwm, map(apps1_analog, lt1, ht1, 0, 255));
while (ECU_SCS == 0) {
ECU_SCS = digitalRead(ECU_SCS_pin);
analogWrite(pwm, 0);
digitalWrite(RTD_LED, LOW);
rtdmode = 0;
Serial.println("ECU SCS Check Failed... Exiting RTD Mode");
}
while (Air_State == 0) {
Air_State = digitalRead(Air_State_pin);
analogWrite(pwm, 0);
digitalWrite(RTD_LED, LOW);
rtdmode = 0;
Serial.println("Air State Low... Exiting RTD Mode");
}
// Check
// while (bps_scs == 0) {
// bps_scs = digitalRead(bps_scs_pin);
// analogWrite(pwm, 0);
// digitalWrite(RTD_LED, LOW);
// rtdmode = 0;
// Serial.println("BPS SCS Check Failed... Exiting RTD Mode");
// }
while((apps1_analog > 3500 || apps2_analog > 3500) && rtdmode == 1 && apps_scs==0)
{
Serial.println("APPS SCS Check Fault...");
analogWrite(pwm, 0);
digitalWrite(RTD_LED, LOW);
Serial.println("Exiting RTD Mode");
}
if (analogRead(apps1_analog) < 3500 && analogRead(apps2_analog) < 3500)
{
digitalWrite(RTD_LED, HIGH);
Serial.println("RTD Mode Entered...");
apps_scs=0;
analogWrite(pwm, map(apps1_analog, lt1, ht1, 0, 255));
}
bps = analogRead(BPS_pin);
// Brake fault condition
if (a > 0.25 && bps >= bps_th && brakefault == 0 && rtdmode == 1) {
digitalWrite(bspd_led, HIGH);
unsigned long startTime = millis();
Serial.println("BPS fault... BSPD LED On");
brakefault = 1;
while (brakefault == 1) {
bps = analogRead(BPS_pin);
apps1_analog = analogRead(apps1_pin);
a = ((float)(apps1_analog - lt1) / (float)(ht1 - lt1));
b = ((float)(apps2_analog - lt2) / (float)(ht2 - lt2));
if (millis() - startTime < 2000 && ((a < 0.25) || bps < bps_th)) { // 500ms plausibility check
digitalWrite(RTD_LED, HIGH);
digitalWrite(bspd_led, LOW);
brakefault = 0;
Serial.println("RTD Mode Re-entered");
}
if (millis() - startTime > 2000) {
analogWrite(pwm, 0);
digitalWrite(RTD_LED, LOW);
Serial.println("Exiting RTD Mode");
brakefault = 1;
}
apps1_analog = analogRead(apps1_pin);
apps2_analog = analogRead(apps2_pin);
a = ((float)(apps1_analog - lt1) / (float)(ht1 - lt1));
b = ((float)(apps2_analog - lt2) / (float)(ht2 - lt2));
if (a > 0.05 || b > 0.05) {
analogWrite(pwm, 0);
}
else if (a < 0.05 || b < 0.05) {
analogWrite(pwm, map(apps1_analog, lt1, ht1, 0, 255));
digitalWrite(RTD_LED, HIGH);
digitalWrite(bspd_led, LOW);
brakefault = 0;
Serial.println("BSPD LED Off");
Serial.println("RTD Mode Re-entered");
}
}
}
// APPS Fault condition
if (abs(a - b) > 0.3 && rtdmode == 1) { // 10% implausibility
unsigned long startTime = millis();
appsfault = 1;
while (appsfault == 1) {
apps1_analog = analogRead(apps1_pin);
apps2_analog = analogRead(apps2_pin);
digitalWrite(apps_led, HIGH);
a = ((float)(apps1_analog - lt1) / (float)(ht1 - lt1));
b = ((float)(apps2_analog - lt2) / (float)(ht2 - lt2));
if (millis() - startTime <= 200 && (abs(a - b) < 0.3)) { // 100ms condition
digitalWrite(apps_led, LOW);
Serial.println("APPS LED Off");
appsfault = 0;
}
if (millis() - startTime > 2000) { // 500ms plausibility check
analogWrite(pwm, 0);
digitalWrite(RTD_LED, LOW);
digitalWrite(apps_led, HIGH);
appsfault = 1;
rtdmode = 0;
Serial.println("Apps fault occurred. RTD LED turned LOW. APPS LED turned HIGH");
delay(1000);
}
}
}
}
}
void PWM_init() {
// for motor controller
ledcSetup(0, 5000, 8); // Use channel 0, 5000 Hz PWM, 8-bit resolution
ledcAttachPin(pwm, 0);
}