forked from omron-devhub/d6t-2jcieev01-arduino
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathd6t-1a.ino
More file actions
354 lines (330 loc) · 11.2 KB
/
d6t-1a.ino
File metadata and controls
354 lines (330 loc) · 11.2 KB
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
/*
* MIT License
* Copyright (c) 2019, 2018 - present OMRON Corporation
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/* includes */
#include <Wire.h>
/* defines */
#define D6T_ADDR 0x0A // for I2C 7bit address
#define D6T_CMD 0x4C // for D6T-44L-06/06H, D6T-8L-09/09H, for D6T-1A-01/02
#define N_ROW 1
#define N_PIXEL 1
#define N_READ ((N_PIXEL + 1) * 2 + 1)
#define SAMPLE_TIME_0009MS 9
#define SAMPLE_TIME_0010MS 10
#define SAMPLE_TIME_0012MS 12
#define SAMPLE_TIME_0015MS 15
#define SAMPLE_TIME_0020MS 20
#define SAMPLE_TIME_0040MS 40
#define SAMPLE_TIME_0060MS 60
#define SAMPLE_TIME_0100MS 100
#define SAMPLE_TIME_0200MS 200
#define SAMPLE_TIME_0400MS 400
#define SAMPLE_TIME_0800MS 800
#define SAMPLE_TIME_1600MS 1600
#define SAMPLE_TIME_3200MS 3200
#define PARA_0009MS_1 ((uint8_t)0x90)
#define PARA_0009MS_2 ((uint8_t)0xD3)
#define PARA_0009MS_3 ((uint8_t)0x29)
#define PARA_0010MS_1 ((uint8_t)0x90)
#define PARA_0010MS_2 ((uint8_t)0xD4)
#define PARA_0010MS_3 ((uint8_t)0x3C)
#define PARA_0012MS_1 ((uint8_t)0x90)
#define PARA_0012MS_2 ((uint8_t)0xD5)
#define PARA_0012MS_3 ((uint8_t)0x3B)
#define PARA_0015MS_1 ((uint8_t)0x90)
#define PARA_0015MS_2 ((uint8_t)0xD6)
#define PARA_0015MS_3 ((uint8_t)0x32)
#define PARA_0020MS_1 ((uint8_t)0x90)
#define PARA_0020MS_2 ((uint8_t)0xD7)
#define PARA_0020MS_3 ((uint8_t)0x35)
#define PARA_0040MS_1 ((uint8_t)0x90)
#define PARA_0040MS_2 ((uint8_t)0xD8)
#define PARA_0040MS_3 ((uint8_t)0x18)
#define PARA_0060MS_1 ((uint8_t)0x90)
#define PARA_0060MS_2 ((uint8_t)0xD9)
#define PARA_0060MS_3 ((uint8_t)0x1F)
#define PARA_0100MS_1 ((uint8_t)0x90)
#define PARA_0100MS_2 ((uint8_t)0xDA)
#define PARA_0100MS_3 ((uint8_t)0x16)
#define PARA_0200MS_1 ((uint8_t)0x90)
#define PARA_0200MS_2 ((uint8_t)0xDB)
#define PARA_0200MS_3 ((uint8_t)0x11)
#define PARA_0400MS_1 ((uint8_t)0x90)
#define PARA_0400MS_2 ((uint8_t)0xDC)
#define PARA_0400MS_3 ((uint8_t)0x04)
#define PARA_0800MS_1 ((uint8_t)0x90)
#define PARA_0800MS_2 ((uint8_t)0xDD)
#define PARA_0800MS_3 ((uint8_t)0x03)
#define PARA_1600MS_1 ((uint8_t)0x90)
#define PARA_1600MS_2 ((uint8_t)0xDE)
#define PARA_1600MS_3 ((uint8_t)0x0A)
#define PARA_3200MS_1 ((uint8_t)0x90)
#define PARA_3200MS_2 ((uint8_t)0xDF)
#define PARA_3200MS_3 ((uint8_t)0x0D)
/***** Setting Parameter 1 *****/
#define comparingNumInc 16 // x samplingTime ms (range: 1 to 39) (example) 16 x 100 ms -> 1.6 sec
#define comparingNumDec 16 // x samplingTime ms (range: 1 to 39) (example) 16 x 100 ms -> 1.6 sec
#define threshHoldInc 10 // /10 degC (example) 10 -> 1.0 degC (temperature change > 1.0 degC -> Enable)
#define threshHoldDec 10 // /10 degC (example) 10 -> 1.0 degC (temperature change > 1.0 degC -> Disable)
//bool enablePix[8] = {true, true, true, true, true, true, true, true};
/****************************/
/***** Setting Parameter 2 *****/
#define samplingTime SAMPLE_TIME_0100MS //ms (Can select only, 9ms, 10ms, 12ms, 15ms, 20ms, 40ms, 60ms, 100ms, 200ms, 400ms, 800ms, 1600ms, 3200ms)
/****************************/
uint8_t rbuf[N_READ];
int16_t pix_data = 0;
int16_t seqData[40] = {0};
bool occuPix = 0;
bool occuPixFlag = false;
uint8_t resultOccupancy = 0;
uint16_t totalCount = 0;
/** JUDGE_occupancy: judge occupancy*/
bool judge_seatOccupancy(void) {
int j = 0;
for (j = 0; j < 39; j++){
seqData[39 - j] = seqData[38 - j];
}
seqData[0] = pix_data;
if (totalCount <= comparingNumInc){
totalCount++;
}
if (totalCount > comparingNumInc){
if (occuPix == false){
if ((int16_t)(seqData[0] - seqData[comparingNumInc]) >= (int16_t)threshHoldInc){
occuPix = true;
}
}
else{ //resultOccupancy == true
if ((int16_t)(seqData[comparingNumDec] - seqData[0]) >= (int16_t)threshHoldDec){
occuPix = false;
}
}
if (resultOccupancy == 0) {
if(occuPix == true){
resultOccupancy = 1;
}
}
else{
occuPixFlag = false;
if (occuPix == true){
occuPixFlag = true;
}
if (occuPixFlag == false){
resultOccupancy = 0;
}
}
}
return true;
}
uint8_t calc_crc(uint8_t data) {
int index;
uint8_t temp;
for (index = 0; index < 8; index++) {
temp = data;
data <<= 1;
if (temp & 0x80) {data ^= 0x07;}
}
return data;
}
/** <!-- D6T_checkPEC {{{ 1--> D6T PEC(Packet Error Check) calculation.
* calculate the data sequence,
* from an I2C Read client address (8bit) to thermal data end.
*/
bool D6T_checkPEC(uint8_t buf[], int n) {
int i;
uint8_t crc = calc_crc((D6T_ADDR << 1) | 1); // I2C Read address (8bit)
for (i = 0; i < n; i++) {
crc = calc_crc(buf[i] ^ crc);
}
bool ret = crc != buf[n];
if (ret) {
Serial.print("PEC check failed:");
Serial.print(crc, HEX);
Serial.print("(cal) vs ");
Serial.print(buf[n], HEX);
Serial.println("(get)");
}
return ret;
}
/** <!-- conv8us_s16_le {{{1 --> convert a 16bit data from the byte stream.
*/
int16_t conv8us_s16_le(uint8_t* buf, int n) {
int ret;
ret = buf[n];
ret += buf[n + 1] << 8;
return (int16_t)ret; // and convert negative.
}
/** <!-- setup {{{1 -->
* 1. initialize a Serial port for output.
* 2. initialize an I2C peripheral.
*/
void setup() {
uint8_t para[3] = {0};
switch(samplingTime){
case SAMPLE_TIME_0009MS:
para[0] = PARA_0009MS_1;
para[1] = PARA_0009MS_2;
para[2] = PARA_0009MS_3;
break;
case SAMPLE_TIME_0010MS:
para[0] = PARA_0010MS_1;
para[1] = PARA_0010MS_2;
para[2] = PARA_0010MS_3;
break;
case SAMPLE_TIME_0012MS:
para[0] = PARA_0012MS_1;
para[1] = PARA_0012MS_2;
para[2] = PARA_0012MS_3;
break;
case SAMPLE_TIME_0015MS:
para[0] = PARA_0015MS_1;
para[1] = PARA_0015MS_2;
para[2] = PARA_0015MS_3;
break;
case SAMPLE_TIME_0020MS:
para[0] = PARA_0020MS_1;
para[1] = PARA_0020MS_2;
para[2] = PARA_0020MS_3;
break;
case SAMPLE_TIME_0040MS:
para[0] = PARA_0040MS_1;
para[1] = PARA_0040MS_2;
para[2] = PARA_0040MS_3;
break;
case SAMPLE_TIME_0060MS:
para[0] = PARA_0060MS_1;
para[1] = PARA_0060MS_2;
para[2] = PARA_0060MS_3;
break;
case SAMPLE_TIME_0100MS:
para[0] = PARA_0100MS_1;
para[1] = PARA_0100MS_2;
para[2] = PARA_0100MS_3;
break;
case SAMPLE_TIME_0200MS:
para[0] = PARA_0200MS_1;
para[1] = PARA_0200MS_2;
para[2] = PARA_0200MS_3;
break;
case SAMPLE_TIME_0400MS:
para[0] = PARA_0400MS_1;
para[1] = PARA_0400MS_2;
para[2] = PARA_0400MS_3;
break;
case SAMPLE_TIME_0800MS:
para[0] = PARA_0800MS_1;
para[1] = PARA_0800MS_2;
para[2] = PARA_0800MS_3;
break;
case SAMPLE_TIME_1600MS:
para[0] = PARA_1600MS_1;
para[1] = PARA_1600MS_2;
para[2] = PARA_1600MS_3;
break;
case SAMPLE_TIME_3200MS:
para[0] = PARA_3200MS_1;
para[1] = PARA_3200MS_2;
para[2] = PARA_3200MS_3;
break;
default:
para[0] = PARA_0100MS_1;
para[1] = PARA_0100MS_2;
para[2] = PARA_0100MS_3;
break;
}
Serial.begin(115200); // Serial baudrate = 115200bps
Wire.begin(); // i2c master
Wire.beginTransmission(D6T_ADDR); // I2C client address
Wire.write(0x02); // D6T register
Wire.write(0x00); // D6T register
Wire.write(0x01); // D6T register
Wire.write(0xEE); // D6T register
Wire.endTransmission(); // I2C repeated start for read
Wire.beginTransmission(D6T_ADDR); // I2C client address
Wire.write(0x05); // D6T register
Wire.write(para[0]); // D6T register
Wire.write(para[1]); // D6T register
Wire.write(para[2]); // D6T register
Wire.endTransmission(); // I2C repeated start for read
Wire.beginTransmission(D6T_ADDR); // I2C client address
Wire.write(0x03); // D6T register
Wire.write(0x00); // D6T register
Wire.write(0x03); // D6T register
Wire.write(0x8B); // D6T register
Wire.endTransmission(); // I2C repeated start for read
Wire.beginTransmission(D6T_ADDR); // I2C client address
Wire.write(0x03); // D6T register
Wire.write(0x00); // D6T register
Wire.write(0x07); // D6T register
Wire.write(0x97); // D6T register
Wire.endTransmission(); // I2C repeated start for read
Wire.beginTransmission(D6T_ADDR); // I2C client address
Wire.write(0x02); // D6T register
Wire.write(0x00); // D6T register
Wire.write(0x00); // D6T register
Wire.write(0xE9); // D6T register
Wire.endTransmission(); // I2C repeated start for read
}
/** <!-- loop - Thermal sensor {{{1 -->
* 1. read sensor.
* 2. output results, format is: [degC]
*/
void loop() {
int i, j;
memset(rbuf, 0, N_READ);
// Wire buffers are enough to read D6T-16L data (33bytes) with
// MKR-WiFi1010 and Feather ESP32,
// these have 256 and 128 buffers in their libraries.
Wire.beginTransmission(D6T_ADDR); // I2C client address
Wire.write(D6T_CMD); // D6T register
Wire.endTransmission(); // I2C repeated start for read
Wire.requestFrom(D6T_ADDR, N_READ);
i = 0;
while (Wire.available()) {
rbuf[i++] = Wire.read();
}
if (D6T_checkPEC(rbuf, N_READ - 1)) {
return;
}
// 1st data is PTAT measurement (: Proportional To Absolute Temperature)
int16_t itemp = conv8us_s16_le(rbuf, 0);
Serial.print("PTAT:");
Serial.print(itemp / 10.0, 1);
Serial.print(", Temperature:");
// loop temperature pixels of each thrmopiles measurements
for (i = 0, j = 2; i < N_PIXEL; i++, j += 2) {
itemp = conv8us_s16_le(rbuf, j);
pix_data = itemp;
Serial.print(itemp / 10.0, 1); // print PTAT & Temperature
if ((i % N_ROW) == N_ROW - 1) {
Serial.print(" [degC]"); // wrap text at ROW end.
} else {
Serial.print(", "); // print delimiter
}
}
judge_seatOccupancy(); //add
Serial.print(", Occupancy:");
Serial.println(resultOccupancy, 1);
delay(samplingTime);
}
// vi: ft=arduino:fdm=marker:et:sw=4:tw=80