-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththermal-camera-code-master.ino
180 lines (157 loc) · 4.72 KB
/
thermal-camera-code-master.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
/*
Right now it should log all data to the SD card, some things in config.h may need to be cahnged but it should at least work
TODOS:
Add time logging, properly adhere to TREB file format spec
*/
#include <Wire.h>
#include <SD.h>
#include "MLX90640_API.h"
#include "MLX90640_I2C_Driver.h"
#include "config.h"
String logFileName; // Active logging file
String logFileBuffer; // Buffer for logged data. Max is set in config
const byte MLX90640_address = 0x33; //Default 7-bit unshifted address of the MLX90640
#define TA_SHIFT 8 //Default shift for MLX90640 in open air
static float mlx90640To[768];
paramsMLX90640 mlx90640;
void setup()
{
initHardware();
Wire.begin();
Wire.setClock(400000); //Increase I2C clock speed to 400kHz
if (isConnected() == false)
{
LOG_PORT.println("MLX90640 not detected at default I2C address. Please check wiring. Freezing.");
while (1);
}
LOG_PORT.println("MLX90640 online!");
if ( initSD() )
{
sdCardPresent = true;
// Get the next, available log file name
logFileName = nextLogFile();
logFile = SD.open(logFileName, FILE_WRITE);
startFile(logFile);
logFile.close();
}
//Get device parameters - We only have to do this once
int status;
uint16_t eeMLX90640[832];
status = MLX90640_DumpEE(MLX90640_address, eeMLX90640);
if (status != 0)
LOG_PORT.println("Failed to load system parameters");
status = MLX90640_ExtractParameters(eeMLX90640, &mlx90640);
if (status != 0)
LOG_PORT.println("Parameter extraction failed");
//Once params are extracted, we can release eeMLX90640 array
}
void loop()
{
for (byte x = 0 ; x < 2 ; x++) //Read both subpages
{
uint16_t mlx90640Frame[834];
int status = MLX90640_GetFrameData(MLX90640_address, mlx90640Frame);
if (status < 0)
{
LOG_PORT.print("GetFrame Error: ");
LOG_PORT.println(status);
}
float vdd = MLX90640_GetVdd(mlx90640Frame, &mlx90640);
float Ta = MLX90640_GetTa(mlx90640Frame, &mlx90640);
float tr = Ta - TA_SHIFT; //Reflected temperature based on the sensor ambient temperature
float emissivity = 0.95;
byte *a = (byte *)&vdd;
byte *b = (byte *)&tr;
byte toStore[846] = {83,77,67,83,a[0],a[1],a[2],a[3],b[0],b[1],b[2],b[3]};
for(int i=0;i<834;i++){
byte *c = (byte *)&mlx90640Frame[i];
toStore[2*i+12] = c[0];
toStore[2*i+13] = c[1];
}
sdLogByteArray(toStore);
//MLX90640_CalculateTo(mlx90640Frame, &mlx90640, emissivity, tr, mlx90640To);
}
blinkLED();
delay(1000);
}
//Returns true if the MLX90640 is detected on the I2C bus
boolean isConnected()
{
Wire.beginTransmission((uint8_t)MLX90640_address);
if (Wire.endTransmission() != 0)
return (false); //Sensor did not ACK
return (true);
}
void initHardware(void)
{
// Set up LED pin (active-high, default to off)
pinMode(HW_LED_PIN, OUTPUT);
digitalWrite(HW_LED_PIN, LOW);
// Set up MPU-9250 interrupt input (active-low)
pinMode(MPU9250_INT_PIN, INPUT_PULLUP);
// Set up serial log port
LOG_PORT.begin(SERIAL_BAUD_RATE);
}
void blinkLED()
{
static bool ledState = false;
digitalWrite(HW_LED_PIN, ledState);
ledState = !ledState;
}
bool initSD(void)
{
// SD.begin should return true if a valid SD card is present
if ( !SD.begin(SD_CHIP_SELECT_PIN) )
{
return false;
}
return true;
}
bool startFile(File f){
byte header[] = {116,114,101,98};
file.write(header);
}
bool sdLogByteArray(byte[] toLog)
{
// Open the current file name:
File logFile = SD.open(logFileName, FILE_WRITE);
// If the file will get too big with these new bytes, create
// a new one, and open it.
if (logFile.size() > (SD_MAX_FILE_SIZE - toLog.length))
{
logFileName = nextLogFile();
logFile = SD.open(logFileName, FILE_WRITE);
startFile(logFile);
}
// If the log file opened properly, add the byte array to it.
if (logFile)
{
logFile.write(toLog, toLog.length);
logFile.close();
return true; // Return success
}
return false; // Return fail
}
// Find the next available log file. Or return a null string
// if we've reached the maximum file limit.
String nextLogFile(void)
{
String filename;
int logIndex = 0;
for (int i = 0; i < LOG_FILE_INDEX_MAX; i++)
{
// Construct a file with PREFIX[Index].SUFFIX
filename = String(LOG_FILE_PREFIX);
filename += String(logIndex);
filename += ".";
filename += String(LOG_FILE_SUFFIX);
// If the file name doesn't exist, return it
if (!SD.exists(filename))
{
return filename;
}
// Otherwise increment the index, and try again
logIndex++;
}
return "";
}