-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
496 lines (409 loc) · 14.8 KB
/
main.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
/* mbed Microcontroller Library
* Copyright (c) 2019 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#include "Adafruit_SSD1306.h"
#include "Hx711.h"
#include "ADS1231.h"
#include "ADS1220.h"
#include "trace_helper.h"
#define TRACE_GROUP "main"
#include "lorawan_reporter.h"
/******************************************************************************
* Definitions
******************************************************************************/
#ifdef MBED_CONF_APP_ADC_SELECTED
#if MBED_CONF_APP_ADC_SELECTED == 0
#define __HX711__
#elif MBED_CONF_APP_ADC_SELECTED == 1
#define __ADS1232__
#elif MBED_CONF_APP_ADC_SELECTED == 2
#define __ADS1220__
#endif
#else
#define __HX711__
#endif
#ifdef MBED_CONF_APP_OLED_ENABLE
#if MBED_CONF_APP_OLED_ENABLE == 1
#define __OLED__
#endif
#else
#define __OLED__ // Comment me to blank the OLED
#endif
#define BLINKING_RATE_MS 1000
#define TEST_AMOUNT 20
#define LSB_SIZE(PGA, VREF) ((VREF/PGA) / (((long int)1<<23)))
/******************************************************************************
* LED
*
******************************************************************************/
DigitalOut led(LED3); // Initialise the digital pin LED1 as an output
volatile static uint16_t sample_count = 0;
/******************************************************************************
* OLED, SSD1306 adapted from Adafruit's library
*
* https://os.mbed.com/users/nkhorman/code/Adafruit_GFX/
******************************************************************************/
#ifdef __OLED__
class I2CPreInit : public I2C // an I2C sub-class that provides a constructed default
{
public:
I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) {
frequency(400000);
start();
};
};
//I2CPreInit
I2C gI2C(I2C_SDA, I2C_SCL);
Adafruit_SSD1306_I2c gOled2(gI2C, P_5, SSD_I2C_ADDRESS, 64, 128);
#endif
/******************************************************************************
* UART
*
* https://os.mbed.com/handbook/Serial
* https://os.mbed.com/docs/mbed-os/v5.15/apis/serial.html
******************************************************************************/
//Serial uart(UART_TX, UART_RX, NULL, 115200);
//#ifdef UART_INTR
//static void on_uart_receive(void)
//{
// char buf[10];
// while (uart.readable())
// {
// // __disable_irq();
// uart.read(buf, 10);
// // __enable_irq();
// }
// sample_count = 0;
//}
//#endif
/******************************************************************************
* HX711
*
* https://os.mbed.com/users/megrootens/code/HX711/
* https://os.mbed.com/users/jmiller322/code/SmartCrutches//file/d5e36ee82984/main.cpp/
* https://learn.sparkfun.com/tutorials/load-cell-amplifier-hx711-breakout-hookup-guide
* https://www.thaieasyelec.com/article-wiki/review-product-article/how-to-use-load-cell-and-hx711-amplifier-module.html
* https://www.thaieasyelec.com/media/wysiwyg/blog/30.png
******************************************************************************/
#ifdef __HX711__
#define HX711_CAL_RAW 346209 // of the weight 100g
#define HX711_CAL_OFFSET 11286 // raw, without weight
#define HX711_PGA 64
#define HX711_VREF 5.
#define HX711_CAL_WEIGHT 100. // 100g
#define HX711_CAL_SCALE (HX711_CAL_WEIGHT / (float)(HX711_CAL_RAW - HX711_CAL_OFFSET))
Hx711 loadcell_hx711(P_8, P_9, HX711_CAL_OFFSET, HX711_CAL_SCALE, HX711_PGA); // Hx711(PinName pin_sck, PinName pin_dt, int offset, float scale, uint8_t gain = 128)
// Hx711 loadcell_hx711(P_8, P_9, 25950, -0.0046522447, HX711_PGA); // Hx711(PinName pin_sck, PinName pin_dt, int offset, float scale, uint8_t gain = 128)
Ticker hx711_ticker;
struct
{
int32_t raw;
float volt;
float mass;
} hx711_sample;
void hx711_read(void)
{
hx711_sample.raw = loadcell_hx711.readRaw();
hx711_sample.volt = hx711_sample.raw * LSB_SIZE(HX711_PGA, HX711_VREF);
hx711_sample.mass = loadcell_hx711.read();
}
void hx711_init(void)
{
// loadcell_hx711.set_scale();
// loadcell_hx711.set_offset(124);
hx711_ticker.attach(&hx711_read, 1);
}
#endif
/******************************************************************************
* ADS1232
*
* https://os.mbed.com/users/mcm/code/ADS1231/
******************************************************************************/
#ifdef __ADS1232__
#define ADS1232_PGA 128
#define ADS1232_VREF 5.
#define ADS1232_CAL_MASS 0.100 // 100g
ADS1231 loadcell_ads1232(P_25, P_29); // ADS1231::ADS1231 ( PinName SCLK, PinName DOUT )
Ticker ads1232_ticker;
struct
{
ADS1231::ADS1231_status_t status;
ADS1231::Vector_count_t count;
uint8_t num_avg;
ADS1231::Vector_mass_t calculated_mass;
ADS1231::Vector_voltage_t calculated_volt;
} ads1232_sample;
void ads1232_read(void) {
ads1232_sample.status = loadcell_ads1232.ADS1231_ReadRawData(&ads1232_sample.count, ads1232_sample.num_avg);
ads1232_sample.calculated_volt = loadcell_ads1232.ADS1231_CalculateVoltage(&ads1232_sample.count, ADS1232_VREF);
ads1232_sample.calculated_mass = loadcell_ads1232.ADS1231_CalculateMass(&ads1232_sample.count, ADS1232_CAL_MASS, ADS1231::ADS1231_SCALE_g);
}
void ads1232_init(void)
{
#ifdef __OLED__
gOled2.clearDisplay();
gOled2.printf("Calibrating...\r\n");
gOled2.display();
#endif
ads1232_sample.num_avg = 1;
uint8_t num_avg_cal = 4;
ADS1231::ADS1231_status_t sts;
// Reset and wake the ADS1232 up
sts = loadcell_ads1232.ADS1231_PowerDown();
if (sts == ADS1231::ADS1231_status_t::ADS1231_FAILURE)
{
tr_debug("ADS1232 fail on power-down\r\n");
}
sts = loadcell_ads1232.ADS1231_Reset();
if (sts == ADS1231::ADS1231_status_t::ADS1231_FAILURE)
{
tr_debug("ADS1232 fail on reset\r\n");
}
ThisThread::sleep_for(1000);
/** CALIBRATION time start! **/
// 1. REMOVE THE MASS ON THE LOAD CELL ( ALL LEDs OFF ). Read data without any mass on the load cell
// aux = myWeightSensor.ADS1231_ReadData_WithoutMass ( &myData, 4 );
// 2. PUT A KNOWN MASS ON THE LOAD CELL ( JUST LED1 ON ). Read data with an user-specified calibration mass
// aux = myWeightSensor.ADS1231_ReadData_WithCalibratedMass ( &myData, 4 );
// Calculating the tare weight ( JUST LED3 ON )
// aux = myWeightSensor.ADS1231_SetAutoTare ( ADS1231::ADS1231_SCALE_kg, &myData, 5 );
int i;
tr_debug("ADS1232: please remove the calibrated mass before calibration ...");
#ifdef __OLED__
gOled2.printf("Remove mass...\r\n");
gOled2.display();
#endif
for (i = 5; i > 0; i--)
{
wait(1);
tr_debug("%d ", i);
}
wait(1);
tr_debug("WIP.\r\n");
loadcell_ads1232.ADS1231_ReadData_WithoutMass(&ads1232_sample.count, num_avg_cal);
tr_debug("ADS1232: .myRawValue_WithoutCalibratedMass > %f\r\n", ads1232_sample.count.myRawValue_WithoutCalibratedMass);
tr_debug("ADS1232: please put a calibrated mass %.1fg on the scale ...", ADS1232_CAL_MASS * 1000);
#ifdef __OLED__
gOled2.printf("Put mass...\r\n");
gOled2.display();
#endif
for (i = 10; i > 0; i--)
{
wait(1);
tr_debug("%d ", i);
}
wait(1);
tr_debug("WIP.\r\n");
loadcell_ads1232.ADS1231_ReadData_WithCalibratedMass(&ads1232_sample.count, num_avg_cal);
tr_debug("ADS1232: .myRawValue_WithCalibratedMass > %f\r\n", ads1232_sample.count.myRawValue_WithCalibratedMass);
tr_debug("ADS1232: please remove the calibrated mass before taring ...");
#ifdef __OLED__
gOled2.printf("Remove mass again...\r\n");
gOled2.display();
#endif
for (i = 5; i > 0; i--)
{
wait(1);
tr_debug("%d ", i);
}
wait(1);
tr_debug("WIP.\r\n");
loadcell_ads1232.ADS1231_SetAutoTare(ADS1232_CAL_MASS, ADS1231::ADS1231_SCALE_g, &ads1232_sample.count, num_avg_cal);
tr_debug("ADS1232: .myRawValue_TareWeight > %f\r\n", ads1232_sample.count.myRawValue_TareWeight);
// ads1232_sample.count.myRawValue_WithoutCalibratedMass = 8385827;
// ads1232_sample.count.myRawValue_WithCalibratedMass = 8590153; // @31g calibrated mass
// ads1232_sample.count.myRawValue_TareWeight = -0.025879;
ads1232_ticker.attach(&ads1232_read, 1); // the address of the function to be attached ( readDATA ) and the interval ( 0.5s ) ( JUST LED4 BLINKING )
}
#endif
/******************************************************************************
* ADS1220
*
* https://os.mbed.com/users/sandeepmalladi/code/ADS1220/
* https://os.mbed.com/users/sandeepmalladi/code/WeightScale//file/58df937cd05d/main.cpp/
******************************************************************************/
#ifdef __ADS1220__
#define ADS1220_CAL_RAW 342374 // of the weight 100g
#define ADS1220_CAL_OFFSET 14806 // raw, without weight
#define ADS1220_PGA 128
#define ADS1220_VREF 5.
#define ADS1220_CAL_WEIGHT 100. // 100g
#define ADS1220_CAL_SCALE (ADS1220_CAL_WEIGHT / (float)(ADS1220_CAL_RAW - ADS1220_CAL_OFFSET))
ADS1220 loadcell_ads1220(P_13, P_12, P_14, P_15); //(PinName mosi, PinName miso, PinName sclk, PinName cs)
InterruptIn pin_drdy(P_20);
Ticker ads1220_ticker;
struct
{
volatile bool available;
int32_t offset;
float scale;
int32_t raw;
float volt;
float mass;
} ads1220_sample = { false, ADS1220_CAL_OFFSET, ADS1220_CAL_SCALE, };
void ads1220_data_ready(void)
{
// ads1220_sample.raw = loadcell_ads1220.ReadData();
// ads1220_sample.volt = ads1220_sample.raw * LSB_SIZE(ADS1220_PGA, ADS1220_VREF);
ads1220_sample.available = true;
}
void ads1220_init(void)
{
// pin_drdy.rise(&ads1220_read);
pin_drdy.fall(&ads1220_data_ready); // Interrupt routine of End-of-conversion acknowledgement
loadcell_ads1220.SendResetCommand();
ThisThread::sleep_for(1);
loadcell_ads1220.Config();
ThisThread::sleep_for(1);
loadcell_ads1220.SendStartCommand();
}
void ads1220_read(void)
{
ads1220_sample.raw = loadcell_ads1220.ReadData();
ads1220_sample.volt = ads1220_sample.raw * LSB_SIZE(ADS1220_PGA, ADS1220_VREF);
ads1220_sample.mass = (ads1220_sample.raw - ads1220_sample.offset) * ads1220_sample.scale;
ads1220_sample.available = false;
}
#endif
/******************************************************************************
* AUX functions
******************************************************************************/
void print_memory_info() {
// allocate enough room for every thread's stack statistics
int cnt = osThreadGetCount();
mbed_stats_stack_t *stats = (mbed_stats_stack_t*) malloc(cnt * sizeof(mbed_stats_stack_t));
cnt = mbed_stats_stack_get_each(stats, cnt);
for (int i = 0; i < cnt; i++) {
tr_debug("Thread: 0x%lX, Stack size: %lu / %lu\r\n", stats[i].thread_id, stats[i].max_size, stats[i].reserved_size);
}
free(stats);
// Grab the heap statistics
mbed_stats_heap_t heap_stats;
mbed_stats_heap_get(&heap_stats);
tr_debug("Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);
}
/******************************************************************************
* Main
******************************************************************************/
int main()
{
// Setup tracing
setup_trace();
print_memory_info();
// Initialize lorawan & sending
lrw_init();
print_memory_info();
float raw = 0;
ThisThread::sleep_for(1000); // Delay for showing splash
#ifdef __OLED__
gOled2.clearDisplay();
gOled2.printf("%ux%u OLED Display\r\n", gOled2.width(), gOled2.height());
gOled2.display();
#endif
ThisThread::sleep_for(1000);
//#ifdef UART_INTR
//uart.attach(&on_uart_receive, Serial::RxIrq); // Bind with on-receiving callback function.
//#endif
#ifdef __HX711__
hx711_init();
#endif
#ifdef __ADS1232__
ads1232_init();
#endif
#ifdef __ADS1220__
ads1220_init();
#endif
tr_debug("----------------------------------------\r\n");
while (true) {
led = !led;
ThisThread::sleep_for(BLINKING_RATE_MS);
if (sample_count > TEST_AMOUNT+1)
{
/*
#if ! defined(UART_INTR) // Clear 'sample_count' without using rx interrupt
if (uart.readable())
{
while (uart.readable())
{
ThisThread::sleep_for(1);
uart.getc();
}
sample_count = 0;
raw = 0;
uart.printf("----------------------------------------\r\n");
}
#endif
*/
// To send LoRaWAN packet
//
sample_count = 0;
raw = 0;
tr_debug("----------------------------------------\r\n");
continue;
}
else
if (++sample_count > TEST_AMOUNT)
{
#ifdef __OLED__
gOled2.printf("\r\n raw:%.1f\r\n", raw/TEST_AMOUNT);
gOled2.printf(" -- Finish -- \r\n");
gOled2.display();
#endif
sample_count++;
continue;
}
#ifdef __OLED__
gOled2.clearDisplay();
gOled2.setTextCursor(0, 0);
#endif
#ifdef __HX711__
tr_debug("[%d] HX711: raw=%ld volt=%.3fmV mass=%.3fg\r\n", sample_count,
hx711_sample.raw,
hx711_sample.volt * 1000,
hx711_sample.mass
);
raw += hx711_sample.raw;
#ifdef __OLED__
gOled2.printf("%u:HX711 %.2fg\r\n", sample_count, hx711_sample.mass);
gOled2.display();
#endif
#endif
#ifdef __ADS1232__
if (ads1232_sample.status == ADS1231::ADS1231_status_t::ADS1231_FAILURE)
{
tr_debug("ADS1232 fail on readRaw()\r\n");
}
else
{
tr_debug("[%d] ADS1232: raw=%ld volt=%.3fmV mass=%.3fg\r\n", sample_count,
ads1232_sample.count.myRawValue,
ads1232_sample.calculated_volt.myVoltage * 1000,
ads1232_sample.calculated_mass.myMass // ADS1231::ADS1231_SCALE_g
);
raw += ads1232_sample.count.myRawValue;
#ifdef __OLED__
gOled2.printf("%u:1232 %.2fg\r\n", sample_count, ads1232_sample.calculated_mass.myMass);
gOled2.display();
#endif
}
#endif
#ifdef __ADS1220__
if (ads1220_sample.available)
{
ads1220_read();
tr_debug("[%d] ADS1220: raw=%ld volt=%.3fmV mass=%.3fg\r\n", sample_count,
ads1220_sample.raw,
ads1220_sample.volt * 1000,
ads1220_sample.mass
);
raw += ads1220_sample.raw;
#ifdef __OLED__
gOled2.printf("%u:1220 %.2fg\r\n", sample_count, ads1220_sample.mass);
gOled2.display();
#endif
}
#endif
}
}