forked from melexis/mlx90640-library
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacquisition.cpp
123 lines (107 loc) · 3.82 KB
/
acquisition.cpp
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
#include <stdint.h>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <fstream>
#include <chrono>
#include <thread>
#include "headers/MLX90640_API.h"
#include "headers/MLX90640_RPI_I2C_Driver.h"
#define MLX_I2C_ADDR 0x33
#define REFRESH_RATE MLX90640_CR1_REFRESH_16_HZ // Subpages rate is half
// WAIT_TIME_MILLISECONDS = 2*0.68*1000/fps
// 2: there are 2 subframes
// 0.68: wait 68% (a bit less then 80% from the datasheet)
// 1000: s -> ms
#define WAIT_TIME_MILLISECONDS 85
int main(int argc, char *argv[]){
static uint16_t eeMLX90640[832];
float emissivity = 1;
uint16_t frame[834];
static float image[768];
float eTa;
static uint16_t data[768*sizeof(float)];
int error;
int64_t max_elapsed=0;
int64_t max_calcs=0;
int16_t CtrlReg1 = 0;
static int64_t t_start=0,\
t_end=0,\
elapsed = 0;
static float mlx90640To[768];
std::ofstream f;
std::ofstream fTimings;
std::ofstream fReadErrors;
std::string base_path="./";
std::string thermalVals = "";
printf("Starting I2C...\n");
error = MLX90640_I2CInit();
printf("Error: %d\n", error);
printf("Setting I2C freq...\n");
MLX90640_I2CFreqSet(1000000); // Hz
// Configure Control Register 1
CtrlReg1 = MLX90640_CR1_SUBPAGE_MODE_ON &\
MLX90640_CR1_TRANSFER_TO_RAM_ON &\
MLX90640_CR1_SUBPAGE_REPEAT_OFF &\
REFRESH_RATE &\
MLX90640_CR1_ADC_RESOLUTION_16BIT &\
MLX90640_CR1_CHESS_MODE;
printf("Setting Control Register 1...\n");
error = MLX90640_ConfReg1(MLX_I2C_ADDR, CtrlReg1);
printf("Error: %d\n", error);
paramsMLX90640 mlx90640;
printf("Damping EE...\n");
error = MLX90640_DumpEE(MLX_I2C_ADDR, eeMLX90640);
printf("Error: %d\n", error);
printf("Extracting parameters...\n");
error = MLX90640_ExtractParameters(eeMLX90640, &mlx90640);
printf("Error: %d\n", error);
if (argc>1)
{
base_path += argv[1];
system(("mkdir " + base_path).c_str());
}
else
std::cout << "First argument must be the acquisition name! Nothing done." << std::endl;
base_path += "/";
std::cout << "Output dir: " << base_path << std::endl;
printf("Starting Loop...\r\n");
while(1){
do{
t_start = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
error = MLX90640_GetFullFrameData(MLX_I2C_ADDR, frame);
if(error) std::this_thread::sleep_for(std::chrono::milliseconds(5));
if (error != 0 && error != -100){
fReadErrors.open(base_path + "acqLog.txt", std::ios_base::app);
fReadErrors << "[" << t_start <<"]: " << error << "\n";
fReadErrors.close();
}
}while(error);
MLX90640_InterpolateOutliers(frame, eeMLX90640);
frame[833] = 0;
eTa = MLX90640_GetTa(frame, &mlx90640);
MLX90640_CalculateTo(frame, &mlx90640, emissivity, eTa, mlx90640To);
frame[833] = 1;
eTa = MLX90640_GetTa(frame, &mlx90640);
MLX90640_CalculateTo(frame, &mlx90640, emissivity, eTa, mlx90640To);
f.open(base_path + std::to_string(t_start));
f << std::setprecision(1) << std::fixed;
for(int y = 23; y > -1; y--){
for(int x = 31; x > -1; x--){
float val = mlx90640To[32 * (23-y) + x];
f << val << " ";
mlx90640To[32 * (23-y) + x] = 0;
}
f << std::endl;
}
f.close();
t_end = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
elapsed = t_end - t_start;
if( elapsed < WAIT_TIME_MILLISECONDS){
std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_MILLISECONDS-elapsed));
};
};
MLX90640_I2CEnd();
return 0;
}