-
Notifications
You must be signed in to change notification settings - Fork 0
/
compact_temp_code.ino
420 lines (346 loc) · 12.2 KB
/
compact_temp_code.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Temp Outside 1 //
// #include <Arduino.h>
// #include <FlexCAN_T4.h>
// #include <iostream>
// #include <string>
// #include <bitset>
// #include <cmath>
// #include <SD.h>
// #include <SPI.h>
// // Define the chip select pin for the SD card slot
// const int chipSelect = BUILTIN_SDCARD;
// // CAN
// FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> can1;
// CAN_message_t msg;
// int ID = 0;
// // hemsA(1): 20a
// // hemsC(3): 20c
// // emsA(1): 222
// // emsC(3): 225
// // frontMotor(1): 20e
// // ambient pressure: 220
// const int hemsA_tempID[1] = { 0x20a };
// const int hemsC_tempID[1] = { 0x20c };
// const int emsA_tempID[1] = { 0x222 };
// const int emsC_tempID[1] = { 0x225 };
// const int frontMotor_tempID[1] = { 0x20e };
// const int ambpressureID[1] = { 0x220 };
// //PINS
// const int HEMS_A = 14;
// const int EMS_A = 15;
// const int EMS_C = 16;
// const int FRONT_MOTOR = 18;
// const int HEMS_C = 20;
// const int PressureAnalog = 41;
// float HEMS_A_reading = 0;
// float EMS_A_reading = 0;
// float EMS_C_reading = 0;
// float FRONT_MOTOR_reading = 0;
// float HEMS_C_reading = 0;
// float PressureAnalog_reading = 0;
// // Calculating the Temp constants
// const int Beta = 3950; // Renamed from B to Beta
// const float T0 = 298.15; // T0 in Kelvin (25 degrees Celsius)
// const int maxAnalogValue = 1023; // Maximum analog value for 10-bit ADC
// int TempAnalog[5];
// float Temperature_reading[5];
// float calculateTemperature(int analogValue) {
// float a = (float)analogValue;
// float x = log((maxAnalogValue * 3.9) / (10 * a) - 3.9 / 10);
// float T = 1 / (x / Beta + 1 / T0) - 273; // Calculate the temperature in Kelvin
// return T;
// }
// void CANtransmit(const int* tempIDs, float average_temp, int size) {
// for (int i = 0; i < size; ++i) {
// msg.id = tempIDs[i]; // Assign a unique ID for each temperature
// msg.len = 2;
// int tempInt = static_cast<int>(average_temp * 100); // Convert float to int (with scaling)
// msg.buf[1] = tempInt & 0xff00;
// msg.buf[0] = tempInt & 0xff;
// can1.write(msg);
// }
// }
// void setup() {
// Serial.begin(9600);
// pinMode(LED_BUILTIN, OUTPUT);
// digitalWrite(LED_BUILTIN, HIGH);
// pinMode(HEMS_A, INPUT);
// //pinMode(Analog2, INPUT);
// pinMode(HEMS_C, INPUT);
// //pinMode(Analog4, INPUT);
// pinMode(EMS_A, INPUT);
// //pinMode(Analog6, INPUT);
// pinMode(EMS_C, INPUT);
// // pinMode(Analog8, INPUT);
// pinMode(FRONT_MOTOR, INPUT);
// //pinMode(Analog10, INPUT);
// pinMode(PressureAnalog, INPUT);
// // Initialize CAN bus
// can1.begin();
// can1.setBaudRate(1000000); // Set CAN baud rate
// }
// void loop() {
// // Read the analog values
// HEMS_A_reading = calculateTemperature(analogRead(HEMS_A));
// EMS_A_reading = calculateTemperature(analogRead(EMS_A));
// EMS_C_reading = calculateTemperature(analogRead(EMS_C));
// FRONT_MOTOR_reading = calculateTemperature(analogRead(FRONT_MOTOR));
// HEMS_C_reading = calculateTemperature(analogRead(HEMS_C));
// PressureAnalog_reading = (((analogRead(PressureAnalog)*3.3/1023)-0.14)/0.0064)/101.5;
// // Calculate the temperature for each analog value
// //Print the temperatures
// Serial.print("T_HEMS A (C): ");
// Serial.print(HEMS_A_reading);
// Serial.print(" T_HEMS C (C): ");
// Serial.print(HEMS_C_reading);
// Serial.print(" T_EMS A (C): ");
// Serial.print(EMS_A_reading);
// Serial.print(" T_EMS C (C): ");
// Serial.print(HEMS_C_reading);
// Serial.print(" T_FRONT MOTOR (C): ");
// Serial.print(FRONT_MOTOR_reading);
// Serial.print(" AMBIENT PRESSURE: ");
// Serial.println(PressureAnalog_reading);
// // Transmit data
// CANtransmit(hemsA_tempID, HEMS_A_reading, 1);
// CANtransmit(hemsC_tempID, HEMS_C_reading, 1);
// CANtransmit(emsA_tempID, EMS_A_reading , 1);
// CANtransmit(emsC_tempID, EMS_C_reading , 1);
// CANtransmit(frontMotor_tempID, FRONT_MOTOR_reading,1);
// CANtransmit(ambpressureID, PressureAnalog_reading*100,1);
// delay(100); // Wait before the next loop
// }
/// Temp Outside 2 ///
#include <Arduino.h>
#include <FlexCAN_T4.h>
#include <iostream>
#include <string>
#include <bitset>
#include <cmath>
#include <SD.h>
#include <SPI.h>
// Define the chip select pin for the SD card slot
const int chipSelect = BUILTIN_SDCARD;
// CAN
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can2;
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> can1;
CAN_message_t msg;
int ID = 0;
// hemsB(2): 20b
// hemsD(4): 20d
// emsB(2): 223
// emsD(4): 226
// backMotor(2): 20f
// ambient_temp: 224
const int hemsB_tempID[1] = { 0x20b };
const int hemsD_tempID[1] = { 0x20d };
const int emsB_tempID[1] = { 0x223 };
const int emsD_tempID[1] = { 0x226 };
const int backMotor_tempID[1] = { 0x20f };
const int amb_tempID[1] = { 0x224 };
const int AMB_TEMP = 16;
const int EMS_D = 15;
const int HEMS_B = 17;
const int HEMS_D = 18;
const int EMS_B = 20;
const int BACK_MOTOR = 22;
float HEMS_B_reading = 0;
float EMS_B_reading = 0;
float EMS_D_reading = 0;
float BACK_MOTOR_reading = 0;
float HEMS_D_reading = 0;
float AMB_TEMP_reading = 0;
// Calculating the Temp constants
const int Beta = 3950; // Renamed from B to Beta
const float T0 = 298.15; // T0 in Kelvin (25 degrees Celsius)
const int maxAnalogValue = 1023; // Maximum analog value for 10-bit ADC
int TempAnalog[6];
float Temperature_reading[6];
float calculateTemperature(int analogValue) {
float a = (float)analogValue;
float x = log((maxAnalogValue * 3.9) / (10 * a) - 3.9 / 10);
float T = 1 / (x / Beta + 1 / T0) - 273; // Calculate the temperature in Kelvin
return T;
}
void CANtransmit(const int* tempIDs, float average_temp, int size) {
for (int i = 0; i < size; ++i) {
msg.id = tempIDs[i]; // Assign a unique ID for each temperature
msg.len = 2;
int tempInt = static_cast<int>(average_temp * 100); // Convert float to int (with scaling)
msg.buf[1] = tempInt & 0xff00;
msg.buf[0] = tempInt & 0xff;
can1.write(msg);
}
}
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
pinMode(AMB_TEMP, INPUT);
pinMode(EMS_D, INPUT);
pinMode(HEMS_B, INPUT);
//pinMode(Analog4, INPUT);
pinMode(HEMS_D, INPUT);
//pinMode(Analog6, INPUT);
pinMode(EMS_B, INPUT);
// pinMode(Analog8, INPUT);
pinMode(BACK_MOTOR, INPUT);
//pinMode(Analog10, INPUT);
// Initialize CAN bus
can1.begin();
can1.setBaudRate(1000000); // Set CAN baud rate
}
void loop() {
// Read the analog values
HEMS_B_reading = calculateTemperature(analogRead(HEMS_B));
EMS_B_reading = calculateTemperature(analogRead(EMS_B));
EMS_D_reading = calculateTemperature(analogRead(EMS_D));
BACK_MOTOR_reading = calculateTemperature(analogRead(BACK_MOTOR));
HEMS_D_reading = calculateTemperature(analogRead(HEMS_D));
AMB_TEMP_reading = calculateTemperature(analogRead(AMB_TEMP));
// Calculate the temperature for each analog value
//Print the temperatures
Serial.print("T_HEMS B (C): ");
Serial.print(HEMS_B_reading);
Serial.print(" T_HEMS D (C): ");
Serial.print(EMS_D_reading);
Serial.print(" T_EMS B (C): ");
Serial.print(EMS_B_reading);
Serial.print(" T_EMS D (C): ");
Serial.print(EMS_D_reading);
Serial.print(" T_BACK MOTOR (C): ");
Serial.print(BACK_MOTOR_reading);
Serial.print(" T_AMB (C): ");
Serial.println(AMB_TEMP_reading);
// Transmit data
CANtransmit(hemsB_tempID, HEMS_B_reading, 1);
CANtransmit(hemsD_tempID, HEMS_D_reading, 1);
CANtransmit(emsB_tempID, EMS_B_reading, 1);
CANtransmit(emsD_tempID, EMS_D_reading, 1);
CANtransmit(backMotor_tempID, BACK_MOTOR_reading,1);
CANtransmit(amb_tempID, AMB_TEMP_reading,1);
delay(100); // Wait before the next loop
}
/// Temp VB ///
/*#include <Arduino.h>
#include <FlexCAN_T4.h>
#include <iostream>
#include <string>
#include <bitset>
#include <cmath>
#include <SD.h>
#include <SPI.h>
// Define the chip select pin for the SD card slot
const int chipSelect = BUILTIN_SDCARD;
// CAN
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can2;
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> can1;
CAN_message_t msg;
int ID = 0;
// pressure vb: 210
// temp vb top: 211
// temp vb bot: 200
const int bot_tempID[1] = { 0x200 };
const int top_tempID[1] = { 0x211 };
const int pressID[1] = { 0x210 };
const int Analog1 = 23;
const int Analog2 = 22;
const int Analog3 = 21;
const int Analog5 = 19;
const int Analog7 = 17;
const int Analog9 = 15;
const int PressureAnalog = 41;
// Calculating the Temp constants
const int Beta = 3950; // Renamed from B to Beta
const float T0 = 298.15; // T0 in Kelvin (25 degrees Celsius)
const int maxAnalogValue = 1023; // Maximum analog value for 10-bit ADC
float av_temp = 0;
int TempAnalog_bot[3]; // Array to store the temperature values
int TempAnalog_top[3]; // Array to store the temperature values
float Temperature_reading_bot[3];
float Temperature_reading_top[3];
float calculateTemperature(int analogValue) {
float a = (float)analogValue;
float x = log((maxAnalogValue * 3.9) / (10 * a) - 3.9 / 10);
float T = 1 / (x / Beta + 1 / T0) - 273; // Calculate the temperature in Kelvin
return T;
}
void CANtransmit(const int* tempIDs, float average_temp, int size) {
for (int i = 0; i < size; ++i) {
msg.id = tempIDs[i]; // Assign a unique ID for each temperature
msg.len = 2;
int tempInt = static_cast<int>(average_temp * 100); // Convert float to int (with scaling)
msg.buf[1] = tempInt & 0xff00;
msg.buf[0] = tempInt & 0xff;
can1.write(msg);
}
}
float averagetemp(int TempAnalog[], int size) {
float sum = 0;
for (int i = 0; i < size; i++) {
sum += TempAnalog[i];
}
return sum / size;
}
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
pinMode(Analog1, INPUT);
pinMode(Analog2, INPUT);
pinMode(Analog3, INPUT);
//pinMode(Analog4, INPUT);
pinMode(Analog5, INPUT);
//pinMode(Analog6, INPUT);
pinMode(Analog7, INPUT);
// pinMode(Analog8, INPUT);
pinMode(Analog9, INPUT);
//pinMode(Analog10, INPUT);
pinMode(PressureAnalog, INPUT);
// Initialize CAN bus
can1.begin();
can1.setBaudRate(1000000); // Set CAN baud rate
}
void loop() {
av_temp = 0;
Serial.println(analogRead(PressureAnalog));
float pressure = (((analogRead(PressureAnalog)*3.3/1023)-0.14)/0.0064)/101.5;
Serial.println(pressure);
// Read the analog values
TempAnalog_bot[0] = analogRead(Analog1);
TempAnalog_bot[1] = analogRead(Analog2);
TempAnalog_bot[2] = analogRead(Analog3);
TempAnalog_top[0] = analogRead(Analog5);
TempAnalog_top[1] = analogRead(Analog7);
TempAnalog_top[2] = analogRead(Analog9);
// Calculate the temperature for each analog value
for (int i = 0; i < 3; ++i) {
Temperature_reading_bot[i] = calculateTemperature(TempAnalog_bot[i]);
}
// Calculate the temperature for each analog value
for (int i = 0; i < 3; ++i) {
Temperature_reading_top[i] = calculateTemperature(TempAnalog_top[i]);
}
//Print the temperatures
Serial.print("Tbot 1 (C): ");
Serial.print(Temperature_reading_bot[0]);
Serial.print(" Tbot 2 (C): ");
Serial.print(Temperature_reading_bot[1]);
Serial.print(" Tbot 3 (C): ");
Serial.print(Temperature_reading_bot[2]);
Serial.print(" Ttop 1 (C): ");
Serial.print(Temperature_reading_top[0]);
Serial.print(" Ttop 2 (C): ");
Serial.print(Temperature_reading_top[1]);
Serial.print(" Ttop 3 (C): ");
Serial.print(Temperature_reading_top[2]);
float average_temp_bot = averagetemp(TempAnalog_bot, 3);
float average_temp_top = averagetemp(TempAnalog_top, 3);
// Transmit the average temperatures
CANtransmit(bot_tempID, average_temp_bot, 1);
CANtransmit(top_tempID, average_temp_top, 1);
CANtransmit(pressID, pressure*100,1);
// Transmit the individual temperatures
// CANtransmitIndividual(tempIDs3, Temperature_reading, 10);
delay(100); // Wait before the next loop
}*/