-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvb_temp.ino
154 lines (132 loc) · 4.68 KB
/
vb_temp.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
#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[0] = (tempInt >> 8) & 0xff;
msg.buf[1] = tempInt & 0xff;
can1.write(msg);
}
}
void CANtransmitIndividual(const int* tempIDs, float* Temperature_reading, 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>(Temperature_reading[i] * 100); // Convert float to int (with scaling)
msg.buf[0] = (tempInt >> 8) & 0xff;
msg.buf[1] = 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
}