-
Notifications
You must be signed in to change notification settings - Fork 23
/
ArdusatSDK.cpp
1671 lines (1531 loc) · 52.2 KB
/
ArdusatSDK.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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file ArdusatSDK.cpp
* @author Ben Peters ([email protected])
* @author Sam Olds ([email protected])
* @date December 3, 2014
* @brief Implements ArdusatSDK generic sensor reading and configuration for Space Kit Sensors.
*
* Provides a unifying wrapper of sensor specific functionality to provide a
* consistent interface to interact with each type of sensor.
*/
#include <stdio.h>
#include <string.h>
#include "ArdusatSDK.h"
boolean MANUAL_CONFIG = false;
boolean ARDUSAT_SPACEBOARD = false;
int OUTPUT_BUF_SIZE = 256;
char * _output_buffer;
static int _output_buf_len = 0;
// BecauseLearning Logo
static uint8_t buffer[32 * 128 / 8] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0xF3, 0x53, 0x53, 0x53, 0xA3, 0x03, 0x03,
0x03, 0xF3, 0x53, 0x53, 0x53, 0x53, 0x03, 0x03, 0xE3, 0x33, 0x13, 0x13, 0x13, 0x23, 0x03, 0x03,
0x03, 0xC3, 0x63, 0x33, 0x63, 0xC3, 0x03, 0x03, 0x03, 0xF3, 0x03, 0x03, 0x03, 0xF3, 0x03, 0x03,
0x03, 0x63, 0x53, 0xD3, 0x93, 0xA3, 0x03, 0x03, 0xF3, 0x53, 0x53, 0x53, 0x53, 0x03, 0x03, 0x03,
0x03, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0xE3, 0xF3,
0xFB, 0xFB, 0xFB, 0xF3, 0xE3, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0xFF, 0xFF, 0xFF,
0xF0, 0xF0, 0xF0, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x3F, 0x3F, 0x3F, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x33, 0x32, 0x32, 0x32, 0x31, 0x30, 0x30,
0x30, 0x33, 0x32, 0x32, 0x32, 0x32, 0x30, 0x30, 0x31, 0x33, 0x32, 0x32, 0x32, 0x31, 0x30, 0x30,
0x33, 0x31, 0x31, 0x31, 0x31, 0x31, 0x33, 0x30, 0x30, 0x33, 0x32, 0x32, 0x32, 0x33, 0x30, 0x30,
0x30, 0x31, 0x32, 0x32, 0x32, 0x31, 0x30, 0x30, 0x33, 0x32, 0x32, 0x32, 0x32, 0x30, 0x30, 0x30,
0x30, 0x30, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x7F,
0xFF, 0xFF, 0xFF, 0x7F, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xFE, 0xFE, 0x66, 0x66, 0x66, 0x66, 0x06, 0x00, 0x00, 0x00, 0x00, 0x80, 0xE0,
0xFC, 0x8E, 0x8E, 0xFC, 0xE0, 0x80, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x66, 0xE6, 0xEE, 0x7C,
0x38, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x1E, 0x78, 0xE0, 0xC0, 0xFE, 0xFE, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x1E, 0x78, 0xF0, 0xC0, 0xFE, 0xFE,
0x00, 0x00, 0x00, 0x00, 0x60, 0xF8, 0xFC, 0x0E, 0x06, 0x66, 0x66, 0xE6, 0xE4, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x07, 0x1F, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC7, 0xC7, 0xC6, 0xC6, 0xC6, 0xC6,
0xC0, 0xC0, 0xC0, 0xC7, 0xC7, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC0, 0xC0, 0xC0, 0xC6, 0xC7, 0xC3,
0xC1, 0xC1, 0xC1, 0xC1, 0xC3, 0xC7, 0xC6, 0xC0, 0xC0, 0xC0, 0xC7, 0xC7, 0xC0, 0xC0, 0xC3, 0xC7,
0xC6, 0xC0, 0xC0, 0xC0, 0xC0, 0xC7, 0xC7, 0xC0, 0xC0, 0xC0, 0xC1, 0xC7, 0xC7, 0xC0, 0xC0, 0xC0,
0xC0, 0xC0, 0xC7, 0xC7, 0xC0, 0xC0, 0xC0, 0xC0, 0xC7, 0xC7, 0xC0, 0xC0, 0xC0, 0xC1, 0xC7, 0xC7,
0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC1, 0xC3, 0xC7, 0xC6, 0xC6, 0xC6, 0xC7, 0xC3, 0xC0, 0xC0, 0xC0,
0xC0, 0xC0, 0xFF, 0xFF, 0xFF, 0xFF, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xCE,
0xDF, 0xDF, 0xDF, 0xCE, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xFF, 0xFF, 0xFF
};
// TODO: Change these error messages to be JSON that can easily be caught by the Experiment Platform
const char concat_string[] PROGMEM = "%s%s";
const char begin_error_msg[] PROGMEM = "begin %s failed. Check wiring!";
const char unavailable_on_hardware_error_msg[] PROGMEM = "%s is not available with %s";
const char spacekit_hardware_name[] PROGMEM = "Space Kit";
const char spaceboard_hardware_name[] PROGMEM = "SpaceBoard";
const char acceleration_sensor_name[] PROGMEM = "Acceleration";
const char gyro_sensor_name[] PROGMEM = "Gyro";
const char luminosity_sensor_name[] PROGMEM = "Luminosity";
const char magnetic_sensor_name[] PROGMEM = "Magnetic";
const char orientation_sensor_name[] PROGMEM = "Orientation";
const char pressure_sensor_name[] PROGMEM = "BarometricPressure";
const char temperature_sensor_name[] PROGMEM = "Temperature";
const char irtemperature_sensor_name[] PROGMEM = "IRTemperature";
const char rgblight_sensor_name[] PROGMEM = "RGBLight";
const char uvlight_sensor_name[] PROGMEM = "UVLight";
const char CSV_TIMESTAMP[] PROGMEM = "timestamp(seconds)";
const char CSV_CHECKSUM[] PROGMEM = "checksum";
static char CSV_SEPARATOR = ',';
static char JSON_PREFIX = '~';
static char JSON_SUFFIX = '|';
const char json_format[] PROGMEM = "%c{\"sensorName\":\"%s\",\"unit\":\"%s\",\"value\":%s,\"cs\":%d}%c\n";
/*
* Gets the output buffer used for storing sensor data, or initializes
* it if it doesn't yet exist
*
* @return the current output buffer
*/
char * _getOutBuf() {
if (_output_buffer == NULL) {
_output_buffer = new char[OUTPUT_BUF_SIZE];
}
return _output_buffer;
}
/*
* Resets the output buffer to be blank
*/
void _resetOutBuf() {
memset(_getOutBuf(), 0, OUTPUT_BUF_SIZE);
_output_buf_len = 0;
}
/**
* Convert an enumerated unit code to a string representation.
*
* @param unit code (see units.h defines)
*
* @return string representation of unit
*/
const char * unit_to_str(unsigned char unit) {
switch (unit) {
case (DATA_UNIT_NONE):
return "";
case (DATA_UNIT_METER_PER_SECONDSQUARED):
return "m/s^2";
case (DATA_UNIT_RADIAN_PER_SECOND):
return "rad/s";
case (DATA_UNIT_MICROTESLA):
return "uT";
case (DATA_UNIT_DEGREES_CELSIUS):
return "C";
case (DATA_UNIT_METER_PER_SECOND):
return "m/s";
case (DATA_UNIT_LUX):
return "lux";
case (DATA_UNIT_MILLIWATT_PER_CMSQUARED):
return "mW/cm^2";
case (DATA_UNIT_DEGREES):
return "deg";
case (DATA_UNIT_HECTOPASCAL):
return "hPa";
default:
return "";
};
}
/*
* Prints an error message that has exactly two "%s" format specifiers in error_msg
* This relies on a 256 character output buffer. Make sure that sensorName isn't too long!
*
* @param error_msg the base error message
* @param sensorName name of sensor that failed.
* @param hardwareBuild empty string, space kit, or spaceboard
*/
void _writeErrorMessage(const char error_msg[] PROGMEM, const char sensorName[] PROGMEM, const char hardwareBuild[] PROGMEM) {
char err_msg[30];
char sensor[50];
char hardware[15];
strcpy_P(err_msg, error_msg);
strcpy_P(sensor, sensorName);
strcpy_P(hardware, hardwareBuild);
// Make SURE sensorName isn't too long for the output buffer!!!
_resetOutBuf();
sprintf(_getOutBuf(), err_msg, sensor, hardware);
Serial.println(_getOutBuf());
}
/*
* Prints an error message that has exactly one "%s" format specifiers in error_msg
* This relies on a 256 character output buffer. Make sure that sensorName isn't too long!
*
* @param error_msg the base error message
* @param sensorName name of sensor that failed.
*/
void _writeErrorMessage(const char error_msg[] PROGMEM, const char sensorName[] PROGMEM) {
char err_msg[30];
char sensor[50];
strcpy_P(err_msg, error_msg);
strcpy_P(sensor, sensorName);
// Make SURE sensorName isn't too long for the output buffer!!!
_resetOutBuf();
sprintf(_getOutBuf(), err_msg, sensor);
Serial.println(_getOutBuf());
}
/*
* Internal helper to do shared checksum logic
*/
int _calculateCheckSumValue(const char *sensor_name, int num_vals, va_list values) {
int cs = 0;
const char *c_ptr = sensor_name;
for (int i = 0; i < num_vals; ++i) {
cs += lround(va_arg(values, double));
}
while (*c_ptr != 0) {
cs += *c_ptr++;
}
return cs;
}
/*
* Calculates a checksum value for a given sensorName and value
*
* @param sensor_name name of sensor
* @param num_vals Number of values
* @param values variable number of floats to write
*
* @return checksum
*/
int calculateCheckSum(const char *sensor_name, int num_vals, ...) {
va_list values;
va_start(values, num_vals);
int cs = _calculateCheckSumValue(sensor_name, num_vals, values);
va_end(values);
return cs;
}
/**
* Create a CSV string with a generic array of float values and a sensor name. Optional timestamp
* argument allows passing in a timestamp; will use millis() otherwise.
*
* @param sensorName string sensor name
* @param timestamp optional timestamp. If 0, millis() will be called.
* @param numValues number of float values
* @param variable float values
*
* @return pointer to output buffer
*/
const char * valuesToCSV(const char *sensorName, unsigned long timestamp, int numValues, ...) {
int i, name_len;
va_list args;
if (timestamp == 0) {
timestamp = millis();
}
_resetOutBuf();
ultoa(timestamp, _getOutBuf(), 10);
_output_buf_len = strlen(_getOutBuf());
if (sensorName != NULL) {
if ((name_len = strlen(sensorName)) > 50) {
name_len = 50;
}
_getOutBuf()[_output_buf_len++] = CSV_SEPARATOR;
memcpy(&(_getOutBuf()[_output_buf_len]), sensorName, name_len);
_output_buf_len += name_len;
}
va_start(args, numValues);
for (i = 0; i < numValues; ++i) {
// We don't know *exactly* how long the floating point value is
// going to be, so just take a guess here...
if (_output_buf_len > OUTPUT_BUF_SIZE - 10) {
break;
}
_getOutBuf()[_output_buf_len++] = CSV_SEPARATOR;
dtostrf(va_arg(args, double), 2, 3, _getOutBuf() + _output_buf_len);
_output_buf_len = strlen(_getOutBuf());
}
va_end(args);
if (_output_buf_len < OUTPUT_BUF_SIZE - 10) {
va_start(args, numValues);
int cs = _calculateCheckSumValue(sensorName, numValues, args);
va_end(args);
_getOutBuf()[_output_buf_len++] = CSV_SEPARATOR;
itoa(cs, _getOutBuf() + _output_buf_len, 10);
_output_buf_len = strlen(_getOutBuf());
}
_getOutBuf()[_output_buf_len++] = '\n';
return _getOutBuf();
}
/**
* Create a CSV string with a generic float value and a sensor name. Optional timestamp
* argument allows passing in a timestamp; will use millis() otherwise.
*
* @param sensorName string sensor name
* @param value value to write
* @param timestamp optional timestamp. If 0, millis() will be called.
*
* @return pointer to output buffer
*/
const char * valueToCSV(const char *sensorName, float value, unsigned long timestamp) {
return valuesToCSV(sensorName, timestamp, 1, value);
}
/*
* Internal helper to build the JSON string at the proper point in the output buffer
* with the correct values and labels
*/
int _writeJSONValue(char *buf, const char *sensor_name, const char *unit, float value) {
char num [32];
char format_str[80];
// inexact estimate on the number of characters the value will take up...
if (strlen(sensor_name) + strlen(unit) + 10 + _output_buf_len > OUTPUT_BUF_SIZE) {
return -1;
}
dtostrf(value, 4, 2, num);
strcpy_P(format_str, json_format);
_output_buf_len += sprintf(buf, format_str,
JSON_PREFIX, sensor_name, unit, num,
calculateCheckSum(sensor_name, 1, value), JSON_SUFFIX);
return _output_buf_len;
}
/**
* Create a JSON string with a generic array of float values and a sensor name.
*
* @param sensorName string sensor name
* @param unit unit the sensor values are in
* @param numValues number of pairs of string labels and float values
* @param variable pairs of string labels and float values
*
* @return pointer to output buffer
*/
const char * valuesToJSON(const char *sensorName, unsigned char unit, int numValues, ...) {
int i = 0;
size_t nameLength = strlen(sensorName) + 1; // For null terminator
va_list args;
char concatBuf[5];
strcpy_P(concatBuf, concat_string);
_resetOutBuf();
va_start(args, numValues);
for (i = 0; i < numValues; ++i) {
char * label = va_arg(args, char *);
float value = va_arg(args, double);
char nameBuf[nameLength + strlen(label) + 1];
sprintf(nameBuf, concatBuf, sensorName, label); // concatBuf = "%s%s";
_writeJSONValue(&_getOutBuf()[_output_buf_len], nameBuf, unit_to_str(unit), value);
}
va_end(args);
return _getOutBuf();
}
/**
* Creates a JSON string with the appropriate values
*
* @param sensorName the label to be given to some data
* @param unit the unit of measurement used
* @param value the sensor value
*
* @return the output buffer
*/
const char * valueToJSON(const char *sensorName, unsigned char unit, float value) {
_resetOutBuf();
_writeJSONValue(_getOutBuf(), sensorName, unit_to_str(unit), value);
return _getOutBuf();
}
/**************************************************************************//**
* @brief Initializes the sensor with any set configurations
* @ingroup sensor
*
* @retval true Successfully initialized
* @retval false Failed to initialize
*****************************************************************************/
boolean Sensor::begin(void) {
catchSpaceboard();
this->initialized = this->initialize();
if (!this->initialized) {
_writeErrorMessage(begin_error_msg, this->name);
}
return this->initialized;
}
/**
* @brief Makes sure the sensor was initialized then calls the sensor specific read
* @ingroup sensor
* @retval true A sensor reading was attempted
* @retval false No sensor reading was attempted. Must `begin()` first
*/
boolean Sensor::read(void) {
if (this->initialized) {
this->header.timestamp = millis();
return this->readSensor();
}
return this->initialized;
}
/**
* @brief Takes a reading from the sensor and returns value in CSV format
* @ingroup sensor
* @param sensorName The text to display next to the value
* @return sensor readings in CSV format or empty string if uninitialized
*/
const char * Sensor::readToCSV(const char * sensorName) {
this->read();
return this->toCSV(sensorName);
}
/**
* @brief Takes a reading from the sensor and returns value in JSON format
* @ingroup sensor
* @param sensorName The text to display next to the value
* @return sensor readings in JSON format or empty string if uninitialized
*/
const char * Sensor::readToJSON(const char * sensorName) {
this->read();
return this->toJSON(sensorName);
}
/**
* @brief Initializes member variables for each sensor
* @ingroup sensor
* @param sensor_id The type of sensor
* @param unit The unit the sensor values use
* @param name The default name the sensor uses
*
* @note These values are not initialized as member variables in child class
* constructors because they are inherited from the base Sensor class
* and need a Sensor Constructor they can be passed to. However, this
* constructor causes more memory overhead than it was decided to be
* worth. Which is why they're explicitly set instead of initialized.
*/
void Sensor::initializeHeader(sensor_id_t sensor_id, data_unit_t unit, const char name[] PROGMEM) {
this->name = name;
this->header.sensor_id = sensor_id;
this->header.unit = unit;
this->header.timestamp = 0;
this->initialized = false;
}
/**************************************************************************//**
* @brief Constructs Acceleration sensor object
* @ingroup acceleration
*
* Example Usage:
* @code
* Acceleration accel; // Instantiate sensor object
* accel.begin(); // Initialize sensor
* Serial.println(accel.readToJSON("accel")); // Read and print values in JSON
* @endcode
*****************************************************************************/
Acceleration::Acceleration(void) :
gGain(LSM303_ACCEL_GAIN8G)
{
this->initializeHeader(SENSORID_ADAFRUIT9DOFIMU, DATA_UNIT_METER_PER_SECONDSQUARED, acceleration_sensor_name);
}
/**
* @brief Sets the gain configuration variable and constructs an object
* @ingroup acceleration
*
* @param gain Advanced configuration for accelerometer's gain
* - LSM303_ACCEL_GAIN2G
* - LSM303_ACCEL_GAIN4G
* - LSM303_ACCEL_GAIN6G
* - LSM303_ACCEL_GAIN8G (Default)
* - LSM303_ACCEL_GAIN16G
*
* Example Usage:
* @code
* Acceleration accel(LSM303_ACCEL_GAIN2G); // Instantiate sensor object
* accel.begin(); // Initialize sensor
* Serial.println(accel.readToJSON("accel")); // Read and print values in JSON
* @endcode
*/
Acceleration::Acceleration(lsm303_accel_gain_e gain) :
gGain(gain)
{
this->initializeHeader(SENSORID_ADAFRUIT9DOFIMU, DATA_UNIT_METER_PER_SECONDSQUARED, acceleration_sensor_name);
}
/**
* @brief Initializes the sensor with any set configurations or defaults
* @ingroup acceleration
*
* @retval true Successfully initialized
* @retval false Failed to initialize
*/
boolean Acceleration::initialize(void) {
return lsm303_accel_init(this->gGain);
}
/**
* @brief Takes a reading from the sensor
* @ingroup acceleration
*
* @retval true Successfully read
* @retval false Failed to read
*/
boolean Acceleration::readSensor(void) {
lsm303_getAccel(&(this->x), &(this->y), &(this->z));
return true;
}
/**
* @brief Returns last read value in CSV format
* @ingroup acceleration
* @param sensorName The text to display next to the value
* @return sensor readings in CSV format or empty string if uninitialized
*/
const char * Acceleration::toCSV(const char * sensorName) {
if (this->header.timestamp != 0) {
return valuesToCSV(sensorName, this->header.timestamp, 3,
this->x, this->y, this->z);
} else {
return "";
}
}
/**
* @brief Returns last read value in JSON format
* @ingroup acceleration
* @param sensorName The text to display next to the value
* @return sensor readings in JSON format or empty string if uninitialized
*/
const char * Acceleration::toJSON(const char * sensorName) {
if (this->header.timestamp != 0) {
return valuesToJSON(sensorName, this->header.unit, 3, "X", this->x,
"Y", this->y, "Z", this->z);
} else {
return "";
}
}
/**************************************************************************//**
* @brief Constructs Gyro sensor object
* @ingroup gyro
*
* Example Usage:
* @code
* Gyro gyro; // Instantiate sensor object
* gyro.begin(); // Initialize sensor
* Serial.println(gyro.readToJSON("gyro")); // Read and print values in JSON
* @endcode
*****************************************************************************/
Gyro::Gyro(void) :
range(0x20)
{
this->initializeHeader(SENSORID_ADAFRUIT9DOFIMU, DATA_UNIT_RADIAN_PER_SECOND, gyro_sensor_name);
}
/**
* @brief Sets the range configuration variable and constructs an object
* @ingroup gyro
*
* @param range Advanced configuration for gyro's range
* - 0x00 (SENSITIVITY_250DPS)
* - 0x10 (SENSITIVITY_500DPS)
* - 0x20 (SENSITIVITY_2000DPS) (Default)
*
* Example Usage:
* @code
* Gyro gyro(0x00); // Instantiate sensor object
* gyro.begin(); // Initialize sensor
* Serial.println(gyro.readToJSON("gyro")); // Read and print values in JSON
* @endcode
*/
Gyro::Gyro(uint8_t range) :
range(range)
{
this->initializeHeader(SENSORID_ADAFRUIT9DOFIMU, DATA_UNIT_RADIAN_PER_SECOND, gyro_sensor_name);
}
/**
* @brief Initializes the sensor with advanced configurations or defaults
* @ingroup gyro
*
* @retval true Successfully initialized
* @retval false Failed to initialize
*/
boolean Gyro::initialize(void) {
return l3gd20h_init(this->range);
}
/**
* @brief Takes a reading from the sensor
* @ingroup gyro
*
* @retval true Successfully read
* @retval false Failed to read
*/
boolean Gyro::readSensor(void) {
l3gd20h_getOrientation(&(this->x), &(this->y), &(this->z));
return true;
}
/**
* @brief Returns last read value in CSV format
* @ingroup gyro
* @param sensorName The text to display next to the value
* @return sensor readings in CSV format or empty string if uninitialized
*/
const char * Gyro::toCSV(const char * sensorName) {
if (this->header.timestamp != 0) {
return valuesToCSV(sensorName, this->header.timestamp, 3,
this->x, this->y, this->z);
} else {
return "";
}
}
/**
* @brief Returns last read value in JSON format
* @ingroup gyro
* @param sensorName The text to display next to the value
* @return sensor readings in JSON format or empty string if uninitialized
*/
const char * Gyro::toJSON(const char * sensorName) {
if (this->header.timestamp != 0) {
return valuesToJSON(sensorName, this->header.unit, 3, "X", this->x,
"Y", this->y, "Z", this->z);
} else {
return "";
}
}
/**************************************************************************//**
* @brief Constructs Luminosity sensor object
* @ingroup luminosity
*
* Example Usage:
* @code
* Luminosity lum; // Instantiate sensor object
* lum.begin(); // Initialize sensor
* Serial.println(lum.readToJSON("lum")); // Read and print values in JSON
* @endcode
*****************************************************************************/
Luminosity::Luminosity(void) :
gain(TCS34725_GAIN_16X),
intTime(TCS34725_INTEGRATIONTIME_24MS)
{
this->initializeHeader(SENSORID_TCS34725, DATA_UNIT_LUX, luminosity_sensor_name);
}
/**
* @brief Sets the integration time and gain configuration variables and constructs an object
* @ingroup luminosity
*
* @param intTime Advanced configuration for TCS34725 integration time
* - TCS34725_INTEGRATIONTIME_2_4MS
* - TCS34725_INTEGRATIONTIME_24MS (Default)
* - TCS34725_INTEGRATIONTIME_50MS
* - TCS34725_INTEGRATIONTIME_101MS
* - TCS34725_INTEGRATIONTIME_154MS
* - TCS34725_INTEGRATIONTIME_700MS
* @param gain Advanced configuration for TCS34725 gain
* - TCS34725_GAIN_1X
* - TCS34725_GAIN_4X
* - TCS34725_GAIN_16X (Default)
* - TCS34725_GAIN_60X
*
* Example Usage:
* @code
* Luminosity lum(TCS34725_INTEGRATIONTIME_101MS, TCS34725_GAIN_16X);
* lum.begin(); // Initialize sensor
* Serial.println(lum.readToJSON("lum")); // Read and print values in JSON
* @endcode
*/
Luminosity::Luminosity(tcs34725IntegrationTime_t intTime, tcs34725Gain_t gain) :
gain(gain),
intTime(intTime)
{
this->initializeHeader(SENSORID_TCS34725, DATA_UNIT_LUX, luminosity_sensor_name);
}
/**
* @brief Sets the gain and integration time configuration variables and constructs an object
* @ingroup luminosity
*
* @param gain Advanced configuration for TCS34725 gain
* - TCS34725_GAIN_1X
* - TCS34725_GAIN_4X
* - TCS34725_GAIN_16X (Default)
* - TCS34725_GAIN_60X
* @param intTime Advanced configuration for TCS34725 integration time
* - TCS34725_INTEGRATIONTIME_2_4MS
* - TCS34725_INTEGRATIONTIME_24MS (Default)
* - TCS34725_INTEGRATIONTIME_50MS
* - TCS34725_INTEGRATIONTIME_101MS
* - TCS34725_INTEGRATIONTIME_154MS
* - TCS34725_INTEGRATIONTIME_700MS
*
* Example Usage:
* @code
* Luminosity lum(TCS34725_GAIN_16X, TCS34725_INTEGRATIONTIME_101MS);
* lum.begin(); // Initialize sensor
* Serial.println(lum.readToJSON("lum")); // Read and print values in JSON
* @endcode
*/
Luminosity::Luminosity(tcs34725Gain_t gain, tcs34725IntegrationTime_t intTime) :
gain(gain),
intTime(intTime)
{
this->initializeHeader(SENSORID_TCS34725, DATA_UNIT_LUX, luminosity_sensor_name);
}
/**
* @brief Sets the integration time configuration variable and constructs an object
* @ingroup luminosity
*
* @param intTime Advanced configuration for TCS34725 integration time
* - TCS34725_INTEGRATIONTIME_2_4MS
* - TCS34725_INTEGRATIONTIME_24MS (Default)
* - TCS34725_INTEGRATIONTIME_50MS
* - TCS34725_INTEGRATIONTIME_101MS
* - TCS34725_INTEGRATIONTIME_154MS
* - TCS34725_INTEGRATIONTIME_700MS
*
* Example Usage:
* @code
* Luminosity lum(TCS34725_INTEGRATIONTIME_101MS); // Instantiate sensor object
* lum.begin(); // Initialize sensor
* Serial.println(lum.readToJSON("lum")); // Read and print values in JSON
* @endcode
*/
Luminosity::Luminosity(tcs34725IntegrationTime_t intTime) :
gain(TCS34725_GAIN_16X),
intTime(intTime)
{
this->initializeHeader(SENSORID_TCS34725, DATA_UNIT_LUX, luminosity_sensor_name);
}
/**
* @brief Sets the gain configuration variable and constructs an object
* @ingroup luminosity
*
* @param gain Advanced configuration for TCS34725 gain
* - TCS34725_GAIN_1X
* - TCS34725_GAIN_4X
* - TCS34725_GAIN_16X (Default)
* - TCS34725_GAIN_60X
*
* Example Usage:
* @code
* Luminosity lum(TCS34725_GAIN_16X); // Instantiate sensor object
* lum.begin(); // Initialize sensor
* Serial.println(lum.readToJSON("lum")); // Read and print values in JSON
* @endcode
*/
Luminosity::Luminosity(tcs34725Gain_t gain) :
gain(gain),
intTime(TCS34725_INTEGRATIONTIME_24MS)
{
this->initializeHeader(SENSORID_TCS34725, DATA_UNIT_LUX, luminosity_sensor_name);
}
/**
* @brief Initializes the sensor with any set configurations or defaults
* @ingroup luminosity
*
* @retval true Successfully initialized
* @retval false Failed to initialize
*/
boolean Luminosity::initialize(void) {
return tcs34725_init(this->intTime, this->gain);
}
/**
* @brief Takes a reading from the sensor
* @ingroup luminosity
*
* @retval true Successfully read
* @retval false Failed to read
*/
boolean Luminosity::readSensor(void) {
this->lux = tcs34725_getLux();
return true;
}
/**
* @brief Returns last read value in CSV format
* @ingroup luminosity
* @param sensorName The text to display next to the value
* @return sensor readings in CSV format or empty string if uninitialized
*/
const char * Luminosity::toCSV(const char * sensorName) {
if (this->header.timestamp != 0) {
return valueToCSV(sensorName, this->lux, this->header.timestamp);
} else {
return "";
}
}
/**
* @brief Returns last read value in JSON format
* @ingroup luminosity
* @param sensorName The text to display next to the value
* @return sensor readings in JSON format or empty string if uninitialized
*/
const char * Luminosity::toJSON(const char * sensorName) {
if (this->header.timestamp != 0) {
return valueToJSON(sensorName, this->header.unit, this->lux);
} else {
return "";
}
}
/**************************************************************************//**
* @brief Constructs Magnetic sensor object
* @ingroup magnetic
*
* Example Usage:
* @code
* Magnetic mag; // Instantiate sensor object
* mag.begin(); // Initialize sensor
* Serial.println(mag.readToJSON("mag")); // Read and print values in JSON
* @endcode
*****************************************************************************/
Magnetic::Magnetic(void) :
gaussScale(LSM303_MAG_SCALE4GAUSS)
{
this->initializeHeader(SENSORID_ADAFRUIT9DOFIMU, DATA_UNIT_MICROTESLA, magnetic_sensor_name);
}
/**
* @brief Sets the gauss scale configuration variable and constructs an object
* @ingroup magnetic
*
* @param gaussScale Advanced configuration for magnetometer's scale
* - LSM303_MAG_SCALE1_3GAUSS
* - LSM303_MAG_SCALE2GAUSS
* - LSM303_MAG_SCALE2_5GAUSS
* - LSM303_MAG_SCALE4GAUSS (Default)
* - LSM303_MAG_SCALE4_7GAUSS
* - LSM303_MAG_SCALE5_6GAUSS
* - LSM303_MAG_SCALE8GAUSS
* - LSM303_MAG_SCALE12GAUSS
*
* Example Usage:
* @code
* Magnetic mag(LSM303_MAG_SCALE8GAUSS); // Instantiate sensor object
* mag.begin(); // Initialize sensor
* Serial.println(mag.readToJSON("mag")); // Read and print values in JSON
* @endcode
*/
Magnetic::Magnetic(lsm303_mag_scale_e gaussScale) :
gaussScale(gaussScale)
{
this->initializeHeader(SENSORID_ADAFRUIT9DOFIMU, DATA_UNIT_MICROTESLA, magnetic_sensor_name);
}
/**
* @brief Initializes the sensor with any set configurations or defaults
* @ingroup magnetic
*
* @retval true Successfully initialized
* @retval false Failed to initialize
*/
boolean Magnetic::initialize(void) {
return lsm303_mag_init(this->gaussScale);
}
/**
* @brief Takes a reading from the sensor
* @ingroup magnetic
*
* @retval true Successfully read
* @retval false Failed to read
*/
boolean Magnetic::readSensor(void) {
lsm303_getMag(&(this->x), &(this->y), &(this->z));
return true;
}
/**
* @brief Returns last read value in CSV format
* @ingroup magnetic
* @param sensorName The text to display next to the value
* @return sensor readings in CSV format or empty string if uninitialized
*/
const char * Magnetic::toCSV(const char * sensorName) {
if (this->header.timestamp != 0) {
return valuesToCSV(sensorName, this->header.timestamp, 3,
this->x, this->y, this->z);
} else {
return "";
}
}
/**
* @brief Returns last read value in JSON format
* @ingroup magnetic
* @param sensorName The text to display next to the value
* @return sensor readings in JSON format or empty string if uninitialized
*/
const char * Magnetic::toJSON(const char * sensorName) {
if (this->header.timestamp != 0) {
return valuesToJSON(sensorName, this->header.unit, 3, "X", this->x,
"Y", this->y, "Z", this->z);
} else {
return "";
}
}
/**************************************************************************//**
* @brief Constructs Orientation calculation object using provided Acceleration and Magnetic objects
* @ingroup orientation
*
* Example Usage:
* @code
* Acceleration accel;
* Magnetic mag;
* Orientation orient(accel, mag); // Instantiate sensor object
* orient.begin(); // Initialize sensor
* Serial.println(orient.readToJSON("orientation")); // Read and print values in JSON
* @endcode
*****************************************************************************/
Orientation::Orientation(Acceleration & accel, Magnetic & mag) :
accel(&accel),
mag(&mag)
{
this->initializeHeader(SENSORID_ADAFRUIT9DOFIMU, DATA_UNIT_DEGREES, orientation_sensor_name);
}
/**
* @brief Initializes the sensor with any set configurations or defaults
* @ingroup orientation
*
* @retval true Successfully initialized
* @retval false Failed to initialize
*/
boolean Orientation::initialize(void) {
return accel->initialized && mag->initialized;
}
/**
* @brief Takes a reading from the sensor
* @ingroup orientation
*
* @retval true Successfully read
* @retval false Failed to read
*/
boolean Orientation::readSensor(void) {
this->accel->read();
this->mag->read();
float roll;
float pitch;
float heading;
const float PI_F = 3.141592653F;
// Roll is rotation around x-axis (-180 <= roll <= 180)
// Positive roll is clockwise rotation wrt positive x axis
roll = (float) atan2(this->accel->y, this->accel->z);
// Pitch is rotation around y-axis (-180 <= pitch <= 180)
// Positive pitch is clockwise rotation wrt positive y axis
if (this->accel->y * sin(roll) + this->accel->z * cos(roll) == 0) {
pitch = this->accel->x > 0 ? (PI_F / 2) : (-PI_F / 2);
} else {
pitch = (float)atan(-this->accel->x / (this->accel->y * sin(roll) + this->accel->z * cos(roll)));
}
// Heading is rotation around z-axis
// Positive heading is clockwise rotation wrt positive z axis
heading = (float)atan2(this->mag->z * sin(roll) - this->mag->y * cos(roll),
this->mag->x * cos(pitch) + this->mag->y * sin(pitch) * sin(roll) +
this->mag->z * sin(pitch) * cos(roll));
// Convert radians to degrees
this->roll = roll * 180 / PI_F;
this->pitch = pitch * 180 / PI_F;
this->heading = heading * 180 / PI_F;
this->header.timestamp = max(this->accel->header.timestamp, this->mag->header.timestamp);
return true;
}
/**
* @brief Returns last read value in CSV format
* @ingroup orientation
* @param sensorName The text to display next to the value
* @return sensor readings in CSV format or empty string if uninitialized
*/
const char * Orientation::toCSV(const char * sensorName) {
if (this->header.timestamp != 0) {
return valuesToCSV(sensorName, this->header.timestamp, 3,
this->roll, this->pitch, this->heading);
} else {
return "";
}
}
/**
* @brief Returns last read value in JSON format
* @ingroup orientation
* @param sensorName The text to display next to the value
* @return sensor readings in JSON format or empty string if uninitialized
*/
const char * Orientation::toJSON(const char * sensorName) {