-
Notifications
You must be signed in to change notification settings - Fork 1
/
MutichannelGasSensor.cpp
699 lines (579 loc) · 19.1 KB
/
MutichannelGasSensor.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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
MutichannelGasSensor.cpp
2015 Copyright (c) Seeed Technology Inc. All right reserved.
Author: Jacky Zhang
2015-3-17
http://www.seeed.cc/
modi by Jack, 2015-8
The MIT License (MIT)
Copyright (c) 2015 Seeed Technology Inc.
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.
TaktischerSpeck:
Works on Seeeduino:
Serial replaced with SerialUSB
Added Delays
*/
#include <Arduino.h>
#include <math.h>
#include <Wire.h>
#include "MutichannelGasSensor.h"
MutichannelGasSensor::MutichannelGasSensor() : __version(2), i2cAddress(0x19)
{
}
/*********************************************************************************************************
** Function name: begin
** Descriptions: initialize I2C
*********************************************************************************************************/
void MutichannelGasSensor::begin(int address)
{
__version = 1; // version 1/2
r0_inited = false;
Wire.begin();
i2cAddress = address;
__version = getVersion();
}
unsigned char MutichannelGasSensor::getVersion()
{
if(get_addr_dta(CMD_READ_EEPROM, ADDR_IS_SET) == 1126) // get version
{
__version = 2;
SerialUSB.println("version = 2");
return 2;
}
__version = 1;
SerialUSB.println("version = 1");
return 1;
}
void MutichannelGasSensor::begin()
{
begin(DEFAULT_I2C_ADDR);
}
/*********************************************************************************************************
** Function name: sendI2C
** Descriptions: send one byte to I2C Wire
*********************************************************************************************************/
void MutichannelGasSensor::sendI2C(unsigned char dta)
{
Wire.beginTransmission(i2cAddress); // transmit to device #4
Wire.write(dta); // sends one byte
Wire.endTransmission(); // stop transmitting
}
unsigned int MutichannelGasSensor::get_addr_dta(unsigned char addr_reg)
{
START:
Wire.beginTransmission(i2cAddress);
Wire.write(addr_reg);
delay(2);
Wire.endTransmission(); // stop transmitting
delay(2);
Wire.requestFrom(i2cAddress, 2);
unsigned int dta = 0;
unsigned char raw[10];
int cnt = 0;
while(Wire.available())
{
raw[cnt++] = Wire.read();
}
if(cnt == 0)goto START;
dta = raw[0];
dta <<= 8;
dta += raw[1];
switch(addr_reg)
{
case CH_VALUE_NH3:
if(dta > 0)
{
adcValueR0_NH3_Buf = dta;
}
else
{
dta = adcValueR0_NH3_Buf;
}
break;
case CH_VALUE_CO:
if(dta > 0)
{
adcValueR0_CO_Buf = dta;
}
else
{
dta = adcValueR0_CO_Buf;
}
break;
case CH_VALUE_NO2:
if(dta > 0)
{
adcValueR0_NO2_Buf = dta;
}
else
{
dta = adcValueR0_NO2_Buf;
}
break;
default:;
}
return dta;
}
unsigned int MutichannelGasSensor::get_addr_dta(unsigned char addr_reg, unsigned char __dta)
{
START:
Wire.beginTransmission(i2cAddress);
Wire.write(addr_reg);
Wire.write(__dta);
delay(2);
Wire.endTransmission(); // stop transmitting
delay(2);
Wire.requestFrom(i2cAddress, 2);
unsigned int dta = 0;
unsigned char raw[10];
int cnt = 0;
while(Wire.available())
{
raw[cnt++] = Wire.read();
}
if(cnt == 0)goto START;
dta = raw[0];
dta <<= 8;
dta += raw[1];
return dta;
}
void MutichannelGasSensor::write_i2c(unsigned char addr, unsigned char *dta, unsigned char dta_len)
{
Wire.beginTransmission(addr);
for(int i=0; i<dta_len; i++)
{
Wire.write(dta[i]);
}
Wire.endTransmission();
}
/*********************************************************************************************************
** Function name: readData
** Descriptions: read 4 bytes from I2C slave
*********************************************************************************************************/
int16_t MutichannelGasSensor::readData(uint8_t cmd)
{
uint16_t timeout = 0;
uint8_t buffer[4];
uint8_t checksum = 0;
int16_t rtnData = 0;
//send command
sendI2C(cmd);
//wait for a while
delay(2);
//get response
Wire.requestFrom(i2cAddress, (uint8_t)4); // request 4 bytes from slave device
while(Wire.available() == 0)
{
if(timeout++ > 100)
return -2;//time out
delay(2);
}
if(Wire.available() != 4)
return -3;//rtnData length wrong
buffer[0] = Wire.read();
buffer[1] = Wire.read();
buffer[2] = Wire.read();
buffer[3] = Wire.read();
checksum = (uint8_t)(buffer[0] + buffer[1] + buffer[2]);
if(checksum != buffer[3])
return -4;//checksum wrong
rtnData = ((buffer[1] << 8) + buffer[2]);
return rtnData;//successful
}
/*********************************************************************************************************
** Function name: readR0
** Descriptions: read R0 stored in slave MCU
*********************************************************************************************************/
int16_t MutichannelGasSensor::readR0(void)
{
int16_t rtnData = 0;
rtnData = readData(0x11);
if(rtnData > 0)
res0[0] = rtnData;
else
return rtnData; //unsuccessful
rtnData = readData(0x12);
if(rtnData > 0)
res0[1] = rtnData;
else
return rtnData; //unsuccessful
rtnData = readData(0x13);
if(rtnData > 0)
res0[2] = rtnData;
else
return rtnData; //unsuccessful
return 1;//successful
}
/*********************************************************************************************************
** Function name: readR
** Descriptions: read resistance value of each channel from slave MCU
*********************************************************************************************************/
int16_t MutichannelGasSensor::readR(void)
{
int16_t rtnData = 0;
rtnData = readData(0x01);
if(rtnData >= 0)
res[0] = rtnData;
else
return rtnData;//unsuccessful
rtnData = readData(0x02);
if(rtnData >= 0)
res[1] = rtnData;
else
return rtnData;//unsuccessful
rtnData = readData(0x03);
if(rtnData >= 0)
res[2] = rtnData;
else
return rtnData;//unsuccessful
return 0;//successful
}
/*********************************************************************************************************
** Function name: readR
** Descriptions: calculate gas concentration of each channel from slave MCU
** Parameters:
gas - gas type
** Returns:
float value - concentration of the gas
*********************************************************************************************************/
float MutichannelGasSensor::calcGas(int gas)
{
float ratio0, ratio1, ratio2;
if(1 == __version)
{
if(!r0_inited)
{
if(readR0() >= 0) r0_inited = true;
else return -1.0f;
}
if(readR() < 0)
return -2.0f;
ratio0 = (float)res[0] / res0[0];
ratio1 = (float)res[1] / res0[1];
ratio2 = (float)res[2] / res0[2];
}
else if(2 == __version)
{
// how to calc ratio/123
ledOn();
int A0_0 = get_addr_dta(6, ADDR_USER_ADC_HN3);
int A0_1 = get_addr_dta(6, ADDR_USER_ADC_CO);
int A0_2 = get_addr_dta(6, ADDR_USER_ADC_NO2);
int An_0 = get_addr_dta(CH_VALUE_NH3);
int An_1 = get_addr_dta(CH_VALUE_CO);
int An_2 = get_addr_dta(CH_VALUE_NO2);
ratio0 = (float)An_0/(float)A0_0*(1023.0-A0_0)/(1023.0-An_0);
ratio1 = (float)An_1/(float)A0_1*(1023.0-A0_1)/(1023.0-An_1);
ratio2 = (float)An_2/(float)A0_2*(1023.0-A0_2)/(1023.0-An_2);
}
float c = 0;
switch(gas)
{
case CO:
{
c = pow(ratio1, -1.179)*4.385; //mod by jack
break;
}
case NO2:
{
c = pow(ratio2, 1.007)/6.855; //mod by jack
break;
}
case NH3:
{
c = pow(ratio0, -1.67)/1.47; //modi by jack
break;
}
case C3H8: //add by jack
{
c = pow(ratio0, -2.518)*570.164;
break;
}
case C4H10: //add by jack
{
c = pow(ratio0, -2.138)*398.107;
break;
}
case CH4: //add by jack
{
c = pow(ratio1, -4.363)*630.957;
break;
}
case H2: //add by jack
{
c = pow(ratio1, -1.8)*0.73;
break;
}
case C2H5OH: //add by jack
{
c = pow(ratio1, -1.552)*1.622;
break;
}
default:
break;
}
if(2==__version)ledOff();
return isnan(c)?-3:c;
}
/*********************************************************************************************************
** Function name: changeI2cAddr
** Descriptions: change I2C address of the slave MCU, and this address will be stored in EEPROM of slave MCU
*********************************************************************************************************/
void MutichannelGasSensor::changeI2cAddr(uint8_t newAddr)
{
Wire.beginTransmission(i2cAddress); // transmit to device
Wire.write(0x23); // sends one byte
Wire.write(newAddr); // sends one byte
Wire.endTransmission(); // stop transmitting
i2cAddress = newAddr;
}
/*********************************************************************************************************
** Function name: doCalibrate
** Descriptions: tell slave to do a calibration, it will take about 8s
after the calibration, must reread the R0 values
*********************************************************************************************************/
void MutichannelGasSensor::doCalibrate(void)
{
if(1 == __version)
{
START:
sendI2C(0x22);
if(readR0() > 0)
{
for(int i=0; i<3; i++)
{
SerialUSB.print(res0[i]);
SerialUSB.print('\t');
}
}
else
{
delay(5000);
SerialUSB.println("continue...");
for(int i=0; i<3; i++)
{
SerialUSB.print(res0[i]);
SerialUSB.print('\t');
}
SerialUSB.println();
goto START;
}
}
else if(2 == __version)
{
unsigned int i, a0, a1, a2;
while(1)
{
a0 = get_addr_dta(CH_VALUE_NH3);
a1 = get_addr_dta(CH_VALUE_CO);
a2 = get_addr_dta(CH_VALUE_NO2);
SerialUSB.print(a0);
SerialUSB.print('\t');
SerialUSB.print(a1);
SerialUSB.print('\t');
SerialUSB.print(a2);
SerialUSB.println('\t');
ledOn();
int cnt = 0;
for(i=0; i<20; i++)
{
if((a0 - get_addr_dta(CH_VALUE_NH3)) > 2 || (get_addr_dta(CH_VALUE_NH3) - a0) > 2)cnt++;
if((a1 - get_addr_dta(CH_VALUE_CO)) > 2 || (get_addr_dta(CH_VALUE_CO) - a1) > 2)cnt++;
if((a2 - get_addr_dta(CH_VALUE_NO2)) > 2 || (get_addr_dta(CH_VALUE_NO2) - a2) > 2)cnt++;
if(cnt>5)
{
break;
}
delay(1000);
}
ledOff();
if(cnt <= 5)break;
delay(200);
}
SerialUSB.print("write user adc value: ");
SerialUSB.print(a0);SerialUSB.print('\t');
SerialUSB.print(a1);SerialUSB.print('\t');
SerialUSB.print(a2);SerialUSB.println('\t');
unsigned char tmp[7];
tmp[0] = 7;
tmp[1] = a0>>8;
tmp[2] = a0&0xff;
tmp[3] = a1>>8;
tmp[4] = a1&0xff;
tmp[5] = a2>>8;
tmp[6] = a2&0xff;
write_i2c(i2cAddress, tmp, 7);
}
}
/*********************************************************************************************************
** Function name: powerOn
** Descriptions: power on sensor heater
*********************************************************************************************************/
void MutichannelGasSensor::powerOn(void)
{
if(__version == 1)
sendI2C(0x21);
else if(__version == 2)
{
dta_test[0] = 11;
dta_test[1] = 1;
write_i2c(i2cAddress, dta_test, 2);
}
}
/*********************************************************************************************************
** Function name: powerOff
** Descriptions: power off sensor heater
*********************************************************************************************************/
void MutichannelGasSensor::powerOff(void)
{
if(__version == 1)
sendI2C(0x20);
else if(__version == 2)
{
dta_test[0] = 11;
dta_test[1] = 0;
write_i2c(i2cAddress, dta_test, 2);
}
}
void MutichannelGasSensor::display_eeprom()
{
if(__version == 1)
{
SerialUSB.println("ERROR: display_eeprom() is NOT support by V1 firmware.");
return ;
}
SerialUSB.print("ADDR_IS_SET = "); SerialUSB.println(get_addr_dta(CMD_READ_EEPROM, ADDR_IS_SET));
SerialUSB.print("ADDR_FACTORY_ADC_NH3 = "); SerialUSB.println(get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_NH3));
SerialUSB.print("ADDR_FACTORY_ADC_CO = "); SerialUSB.println(get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_CO));
SerialUSB.print("ADDR_FACTORY_ADC_NO2 = "); SerialUSB.println(get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_NO2));
SerialUSB.print("ADDR_USER_ADC_HN3 = "); SerialUSB.println(get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_HN3));
SerialUSB.print("ADDR_USER_ADC_CO = "); SerialUSB.println(get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_CO));
SerialUSB.print("ADDR_USER_ADC_NO2 = "); SerialUSB.println(get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_NO2));
SerialUSB.print("ADDR_I2C_ADDRESS = "); SerialUSB.println(get_addr_dta(CMD_READ_EEPROM, ADDR_I2C_ADDRESS));
}
float MutichannelGasSensor::getR0(unsigned char ch) // 0:CH3, 1:CO, 2:NO2
{
if(__version == 1)
{
SerialUSB.println("ERROR: getR0() is NOT support by V1 firmware.");
return -1;
}
int a = 0;
switch(ch)
{
case 0: // CH3
a = get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_HN3);
SerialUSB.print("a_ch3 = ");
SerialUSB.println(a);
break;
case 1: // CO
a = get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_CO);
SerialUSB.print("a_co = ");
SerialUSB.println(a);
break;
case 2: // NO2
a = get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_NO2);
SerialUSB.print("a_no2 = ");
SerialUSB.println(a);
break;
default:;
}
float r = 56.0*(float)a/(1023.0-(float)a);
return r;
}
float MutichannelGasSensor::getRs(unsigned char ch) // 0:CH3, 1:CO, 2:NO2
{
if(__version == 1)
{
SerialUSB.println("ERROR: getRs() is NOT support by V1 firmware.");
return -1;
}
int a = 0;
switch(ch)
{
case 0: // NH3
a = get_addr_dta(1);
break;
case 1: // CO
a = get_addr_dta(2);
break;
case 2: // NO2
a = get_addr_dta(3);
break;
default:;
}
float r = 56.0*(float)a/(1023.0-(float)a);
return r;
}
// 1. change i2c address to 0x04
// 2. change adc value of R0 to default
void MutichannelGasSensor::factory_setting()
{
unsigned char tmp[7];
unsigned char error;
unsigned char address = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
// change i2c to 0x04
SerialUSB.print("I2C address is: 0x");
SerialUSB.println(address, HEX);
SerialUSB.println("Change I2C address to 0x04");
dta_test[0] = CMD_CHANGE_I2C;
dta_test[1] = 0x04;
write_i2c(address, dta_test, 2);
i2cAddress = 0x04;
delay(100);
getVersion();
break;
}
}
unsigned int a0 = get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_NH3);
unsigned int a1 = get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_CO);
unsigned int a2 = get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_NO2);
tmp[0] = 7;
tmp[1] = a0>>8;
tmp[2] = a0&0xff;
tmp[3] = a1>>8;
tmp[4] = a1&0xff;
tmp[5] = a2>>8;
tmp[6] = a2&0xff;
delay(100);
write_i2c(i2cAddress, tmp, 7);
delay(100);
}
void MutichannelGasSensor::change_i2c_address(unsigned char addr)
{
dta_test[0] = CMD_CHANGE_I2C;
dta_test[1] = addr;
write_i2c(i2cAddress, dta_test, 2);
SerialUSB.print("FUNCTION: CHANGE I2C ADDRESS: 0X");
SerialUSB.print(i2cAddress, HEX);
SerialUSB.print(" > 0x");
SerialUSB.println(addr, HEX);
i2cAddress = addr;
}
MutichannelGasSensor gas;
/*********************************************************************************************************
END FILE
*********************************************************************************************************/