|
| 1 | +/* |
| 2 | + * This sketch shows how nicla can be used in standalone mode. |
| 3 | + * Without the need for an host, nicla can run sketches that |
| 4 | + * are able to configure the bhi sensors and are able to read all |
| 5 | + * the bhi sensors data. |
| 6 | +*/ |
| 7 | + |
| 8 | +#include "Arduino.h" |
| 9 | +#include "Arduino_BHY2.h" |
| 10 | + |
| 11 | +class IMUDataSyncer; |
| 12 | +class SensorMotion; |
| 13 | + |
| 14 | +#define DEBUG 0 |
| 15 | + |
| 16 | +#if DEBUG |
| 17 | +#include "MbedQDebug.h" |
| 18 | +#else |
| 19 | +void *nicla_dbg_info_ptr; |
| 20 | +#endif |
| 21 | + |
| 22 | +class IMUDataSyncer { |
| 23 | + public: |
| 24 | + IMUDataSyncer() { |
| 25 | + int i; |
| 26 | + for (i = 0; i < sizeof(_seq)/sizeof(_seq[0]); i++) { |
| 27 | + _seq[i] = -1; |
| 28 | + _data[i] = NULL; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + virtual bool begin(int accelSampleRate, int gyroSampleRate) { |
| 33 | + int sampleRate = accelSampleRate > gyroSampleRate ? accelSampleRate : gyroSampleRate; |
| 34 | + |
| 35 | + if ((accelSampleRate > 0) && (gyroSampleRate > 0)) { |
| 36 | + |
| 37 | + if (accelSampleRate != gyroSampleRate) { |
| 38 | + _logId = 'u'; //data cannot be synced |
| 39 | + return false; |
| 40 | + } else { |
| 41 | + _logId = 'i'; |
| 42 | + } |
| 43 | + } else if (accelSampleRate > 0) { |
| 44 | + _logId = 'a'; |
| 45 | + } else if (gyroSampleRate > 0){ |
| 46 | + _logId = 'g'; |
| 47 | + } else { |
| 48 | + _logId = 'n'; |
| 49 | + } |
| 50 | + |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + void onSensorDataUpdate(DataXYZ &data, int id) { |
| 55 | + _data[id] = &data; |
| 56 | + |
| 57 | + if ('i' == _logId) { |
| 58 | + //we assume id is always 0 (for accel) or 1 (for gyro) |
| 59 | + //when _seq[1 - id] is less than 0, it means the other sensor has not received any value yet |
| 60 | + if (_seq[1 - id] >= 0) { |
| 61 | + _seq[id] = (_seq[id]+1) % 10; |
| 62 | + } else { |
| 63 | + _seq[id] = 0; //wait for another sensor to generate the 1st sample |
| 64 | + } |
| 65 | + |
| 66 | + if (_seq[id] == _seq[1 - id]) { |
| 67 | + //data synced well and ready for sending out |
| 68 | + #if DEBUG |
| 69 | + mbq_dbg_1(1); |
| 70 | + #endif |
| 71 | + |
| 72 | + #if 0 |
| 73 | + //enable this if you want to send the sequence number in each line, |
| 74 | + //for highest ODR, we ignore this to save bandwidth |
| 75 | + Serial.print(_logId); |
| 76 | + Serial.print((char)(_seq[id]+'0')); Serial.print(','); |
| 77 | + #endif |
| 78 | + //accel data fields |
| 79 | + Serial.print(_data[0]->x); Serial.print(','); |
| 80 | + Serial.print(_data[0]->y); Serial.print(','); |
| 81 | + Serial.print(_data[0]->z); Serial.print(','); |
| 82 | + //gyro data fields |
| 83 | + Serial.print(_data[1]->x); Serial.print(','); |
| 84 | + Serial.print(_data[1]->y); Serial.print(','); |
| 85 | + Serial.print(_data[1]->z); Serial.println(); |
| 86 | + |
| 87 | + #if DEBUG |
| 88 | + mbq_dbg_1(0); |
| 89 | + #endif |
| 90 | + } |
| 91 | + } else { |
| 92 | + _seq[id] = (_seq[id]+1) % 10; |
| 93 | + Serial.print(_logId); |
| 94 | + Serial.print(_seq[id]); Serial.print(','); |
| 95 | + Serial.print(_data[id]->x); Serial.print(','); |
| 96 | + Serial.print(_data[id]->y); Serial.print(','); |
| 97 | + Serial.print(_data[id]->z); Serial.println(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + protected: |
| 102 | + char _logId; |
| 103 | + int8_t _seq[2]; |
| 104 | + DataXYZ *_data[2]; |
| 105 | +}; |
| 106 | + |
| 107 | +class SensorMotion : public SensorClass { |
| 108 | + public: |
| 109 | + SensorMotion(uint8_t id) : SensorClass(id) { |
| 110 | + _id = (id / 10); //accel: 0, gyro: 1, mag: 2 |
| 111 | + _dataSyncer = NULL; |
| 112 | + } |
| 113 | + |
| 114 | + void setDataSyncer(IMUDataSyncer &dataSyncer) { |
| 115 | + _dataSyncer = &dataSyncer; |
| 116 | + } |
| 117 | + |
| 118 | + void setData(SensorDataPacket &data) { |
| 119 | + DataParser::parse3DVector(data, _data); |
| 120 | + _dataSyncer->onSensorDataUpdate(_data, _id); |
| 121 | + } |
| 122 | + |
| 123 | + void setData(SensorLongDataPacket &data) {} |
| 124 | + |
| 125 | + String toString() { |
| 126 | + return _data.toString(); |
| 127 | + } |
| 128 | + |
| 129 | + protected: |
| 130 | + DataXYZ _data; |
| 131 | + uint8_t _id; |
| 132 | + IMUDataSyncer *_dataSyncer; |
| 133 | +}; |
| 134 | + |
| 135 | + |
| 136 | +SensorMotion accel(1); //sensor id 1: accel raw data passthrough |
| 137 | +SensorMotion gyro(10); //sensor id 10: gyro raw data passthrough |
| 138 | +IMUDataSyncer imuDataSyncer; |
| 139 | + |
| 140 | +#define IMU_DATA_RATE 1600 |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | +void testSerial() |
| 145 | +{ |
| 146 | + while (0) { |
| 147 | + Serial.print('*'); |
| 148 | + //Serial.println(); |
| 149 | + |
| 150 | + delay(1000); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +void setup() |
| 155 | +{ |
| 156 | + //Serial.begin(115200); |
| 157 | + Serial.begin(1000000); //max br: 1Mbps for nRF52 |
| 158 | + while(!Serial); |
| 159 | + |
| 160 | + BHY2.begin(NICLA_STANDALONE); |
| 161 | + |
| 162 | + testSerial(); |
| 163 | + |
| 164 | + SensorConfig cfg = accel.getConfiguration(); |
| 165 | + Serial.println(String("range of accel: +/-") + cfg.range + String("g")); |
| 166 | + accel.setRange(8); //this sets the range of accel to +/-8g, |
| 167 | + cfg = accel.getConfiguration(); |
| 168 | + Serial.println(String("range of accel: +/-") + cfg.range + String("g")); |
| 169 | + |
| 170 | + imuDataSyncer.begin(IMU_DATA_RATE, IMU_DATA_RATE); |
| 171 | + accel.setDataSyncer(imuDataSyncer); |
| 172 | + gyro.setDataSyncer(imuDataSyncer); |
| 173 | + accel.begin(IMU_DATA_RATE); |
| 174 | + gyro.begin(IMU_DATA_RATE); |
| 175 | +} |
| 176 | + |
| 177 | +void loop() |
| 178 | +{ |
| 179 | + #if DEBUG |
| 180 | + mbq_dbg_0(1); |
| 181 | + #endif |
| 182 | + |
| 183 | + BHY2.update(); |
| 184 | + |
| 185 | + #if DEBUG |
| 186 | + mbq_dbg_0(0); |
| 187 | + #endif |
| 188 | +} |
0 commit comments