Degree Computation
Dependencies: aconno_SEGGER_RTT LSM9DS1 Si7006A20 adc52832_common aconnoMPL115A1 aconno_bsp
aconnoHelpers/aconnoHelpers.cpp@3:b62a73dc76b7, 2018-12-14 (annotated)
- Committer:
- jurica238814
- Date:
- Fri Dec 14 13:33:03 2018 +0100
- Revision:
- 3:b62a73dc76b7
- Parent:
- 0:442d98af8cc7
- Child:
- 4:cb3513aa9814
All sensors. Cleaner code
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jurica238814 | 0:442d98af8cc7 | 1 | /** |
| jurica238814 | 0:442d98af8cc7 | 2 | * Made by Jurica @ aconno |
| jurica238814 | 0:442d98af8cc7 | 3 | * jurica@aconno.de |
| jurica238814 | 0:442d98af8cc7 | 4 | * More info @ aconno.de |
| jurica238814 | 0:442d98af8cc7 | 5 | */ |
| jurica238814 | 0:442d98af8cc7 | 6 | |
| jurica238814 | 0:442d98af8cc7 | 7 | #include "mbed.h" |
| jurica238814 | 0:442d98af8cc7 | 8 | #include "ble/BLE.h" |
| jurica238814 | 0:442d98af8cc7 | 9 | #include "acd52832_bsp.h" |
| jurica238814 | 0:442d98af8cc7 | 10 | #include "GapAdvertisingData.h" |
| jurica238814 | 0:442d98af8cc7 | 11 | #include "Si7006A20.h" |
| jurica238814 | 0:442d98af8cc7 | 12 | #include "LSM9DS1.h" |
| jurica238814 | 0:442d98af8cc7 | 13 | #include "math.h" |
| jurica238814 | 0:442d98af8cc7 | 14 | #include "adc52832_common/utilities.h" |
| jurica238814 | 0:442d98af8cc7 | 15 | #include "MPL115A1.h" |
| jurica238814 | 0:442d98af8cc7 | 16 | #include "acd_nrf52_saadc.h" |
| jurica238814 | 0:442d98af8cc7 | 17 | #include "aconnoConfig.h" |
| jurica238814 | 0:442d98af8cc7 | 18 | |
| jurica238814 | 0:442d98af8cc7 | 19 | NRF52_SAADC analogIn; |
| jurica238814 | 0:442d98af8cc7 | 20 | extern DigitalOut lightPower; |
| jurica238814 | 0:442d98af8cc7 | 21 | extern DigitalOut temperaturePower; |
| jurica238814 | 0:442d98af8cc7 | 22 | extern DigitalOut shdn; |
| jurica238814 | 0:442d98af8cc7 | 23 | extern DigitalOut power; |
| jurica238814 | 0:442d98af8cc7 | 24 | vector3_s memsAccelerometerInit; |
| jurica238814 | 0:442d98af8cc7 | 25 | vector3_s memsGyroscopeInit; |
| jurica238814 | 0:442d98af8cc7 | 26 | vector3_s memsMagnetometerInit; |
| jurica238814 | 0:442d98af8cc7 | 27 | extern GapAdvertisingData adv_data; |
| jurica238814 | 0:442d98af8cc7 | 28 | extern advertising_packet advertisementPacket; |
| jurica238814 | 0:442d98af8cc7 | 29 | DigitalOut cs(p7); |
| jurica238814 | 0:442d98af8cc7 | 30 | |
| jurica238814 | 3:b62a73dc76b7 | 31 | I2C i2c(I2C_DATA, I2C_CLK); |
| jurica238814 | 3:b62a73dc76b7 | 32 | Si7006 si(&i2c); |
| jurica238814 | 3:b62a73dc76b7 | 33 | SPI spi(SPI_MOSI, SPI_MISO, SPI_SCLK); |
| jurica238814 | 3:b62a73dc76b7 | 34 | MPL115A1 mpl115a1(spi, cs); |
| jurica238814 | 3:b62a73dc76b7 | 35 | LSM9DS1 mems(&i2c); |
| jurica238814 | 3:b62a73dc76b7 | 36 | |
| jurica238814 | 0:442d98af8cc7 | 37 | float getLight(){ |
| jurica238814 | 0:442d98af8cc7 | 38 | return ((float)analogIn.getData()[1])/ADC_RESOLUTION * VALUE_TO_PERCENTAGE; |
| jurica238814 | 0:442d98af8cc7 | 39 | } |
| jurica238814 | 0:442d98af8cc7 | 40 | |
| jurica238814 | 0:442d98af8cc7 | 41 | float voltage2temp(float vOut){ |
| jurica238814 | 0:442d98af8cc7 | 42 | return ((float)vOut - (float)V0)/((float)TC); |
| jurica238814 | 0:442d98af8cc7 | 43 | } |
| jurica238814 | 0:442d98af8cc7 | 44 | |
| jurica238814 | 0:442d98af8cc7 | 45 | float getTemperature(){ |
| jurica238814 | 0:442d98af8cc7 | 46 | return voltage2temp(((float)analogIn.getData()[2])/ADC_RESOLUTION * (float)VCC); |
| jurica238814 | 0:442d98af8cc7 | 47 | } |
| jurica238814 | 0:442d98af8cc7 | 48 | |
| jurica238814 | 0:442d98af8cc7 | 49 | uint8_t getBattery(){ |
| jurica238814 | 0:442d98af8cc7 | 50 | uint16_t batteryVoltage = analogIn.getData()[0]; |
| jurica238814 | 0:442d98af8cc7 | 51 | |
| jurica238814 | 0:442d98af8cc7 | 52 | const uint16_t zero_percent_limit = 739; |
| jurica238814 | 0:442d98af8cc7 | 53 | const uint16_t onehundred_percent_limit = 810; |
| jurica238814 | 0:442d98af8cc7 | 54 | const uint16_t percentage_increments = 5; |
| jurica238814 | 0:442d98af8cc7 | 55 | uint8_t percentage; |
| jurica238814 | 0:442d98af8cc7 | 56 | |
| jurica238814 | 0:442d98af8cc7 | 57 | if (batteryVoltage < zero_percent_limit) |
| jurica238814 | 0:442d98af8cc7 | 58 | { |
| jurica238814 | 0:442d98af8cc7 | 59 | percentage = 0; |
| jurica238814 | 0:442d98af8cc7 | 60 | } |
| jurica238814 | 0:442d98af8cc7 | 61 | else if(batteryVoltage > onehundred_percent_limit) |
| jurica238814 | 0:442d98af8cc7 | 62 | { |
| jurica238814 | 0:442d98af8cc7 | 63 | percentage = 100; |
| jurica238814 | 0:442d98af8cc7 | 64 | } |
| jurica238814 | 0:442d98af8cc7 | 65 | else |
| jurica238814 | 0:442d98af8cc7 | 66 | { |
| jurica238814 | 0:442d98af8cc7 | 67 | batteryVoltage -= zero_percent_limit; |
| jurica238814 | 0:442d98af8cc7 | 68 | percentage = (batteryVoltage*100)/(onehundred_percent_limit - zero_percent_limit); |
| jurica238814 | 0:442d98af8cc7 | 69 | percentage = percentage/percentage_increments*percentage_increments; |
| jurica238814 | 0:442d98af8cc7 | 70 | } |
| jurica238814 | 0:442d98af8cc7 | 71 | |
| jurica238814 | 0:442d98af8cc7 | 72 | return percentage; |
| jurica238814 | 0:442d98af8cc7 | 73 | } |
| jurica238814 | 0:442d98af8cc7 | 74 | |
| jurica238814 | 0:442d98af8cc7 | 75 | float getHumidity(){ |
| jurica238814 | 0:442d98af8cc7 | 76 | float result; |
| jurica238814 | 3:b62a73dc76b7 | 77 | si.getHumidity(&result); |
| jurica238814 | 0:442d98af8cc7 | 78 | return result; |
| jurica238814 | 0:442d98af8cc7 | 79 | } |
| jurica238814 | 0:442d98af8cc7 | 80 | |
| jurica238814 | 0:442d98af8cc7 | 81 | void readGyroscope(vector3_s *gyroscopeData){ |
| jurica238814 | 3:b62a73dc76b7 | 82 | mems.readGyroscope((int16_t *)gyroscopeData); |
| jurica238814 | 0:442d98af8cc7 | 83 | *gyroscopeData -= memsGyroscopeInit; |
| jurica238814 | 0:442d98af8cc7 | 84 | } |
| jurica238814 | 0:442d98af8cc7 | 85 | |
| jurica238814 | 0:442d98af8cc7 | 86 | void readAccelerometer(vector3_s *accelerometerData){ |
| jurica238814 | 3:b62a73dc76b7 | 87 | mems.readAccelerometer((int16_t *)accelerometerData); |
| jurica238814 | 0:442d98af8cc7 | 88 | *accelerometerData -= memsAccelerometerInit; |
| jurica238814 | 0:442d98af8cc7 | 89 | } |
| jurica238814 | 0:442d98af8cc7 | 90 | |
| jurica238814 | 0:442d98af8cc7 | 91 | void readMagnetometer(vector3_s *magnetometerData){ |
| jurica238814 | 3:b62a73dc76b7 | 92 | mems.readMagnetometer((int16_t *)magnetometerData); |
| jurica238814 | 0:442d98af8cc7 | 93 | *magnetometerData -= memsMagnetometerInit; |
| jurica238814 | 0:442d98af8cc7 | 94 | } |
| jurica238814 | 0:442d98af8cc7 | 95 | |
| jurica238814 | 0:442d98af8cc7 | 96 | void calibrateAccelerometer(){ |
| jurica238814 | 0:442d98af8cc7 | 97 | vector3_s accelerometerData; |
| jurica238814 | 0:442d98af8cc7 | 98 | for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){ |
| jurica238814 | 0:442d98af8cc7 | 99 | readAccelerometer(&accelerometerData); |
| jurica238814 | 0:442d98af8cc7 | 100 | memsAccelerometerInit += accelerometerData; |
| jurica238814 | 0:442d98af8cc7 | 101 | } |
| jurica238814 | 0:442d98af8cc7 | 102 | memsAccelerometerInit /= CALIBRATION_STEPS; |
| jurica238814 | 0:442d98af8cc7 | 103 | } |
| jurica238814 | 0:442d98af8cc7 | 104 | |
| jurica238814 | 0:442d98af8cc7 | 105 | void calibrateGyroscope(){ |
| jurica238814 | 0:442d98af8cc7 | 106 | vector3_s gyroscopeData; |
| jurica238814 | 0:442d98af8cc7 | 107 | for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){ |
| jurica238814 | 0:442d98af8cc7 | 108 | readGyroscope(&gyroscopeData); |
| jurica238814 | 0:442d98af8cc7 | 109 | memsGyroscopeInit += gyroscopeData; |
| jurica238814 | 0:442d98af8cc7 | 110 | } |
| jurica238814 | 0:442d98af8cc7 | 111 | memsGyroscopeInit /= CALIBRATION_STEPS; |
| jurica238814 | 0:442d98af8cc7 | 112 | } |
| jurica238814 | 0:442d98af8cc7 | 113 | |
| jurica238814 | 0:442d98af8cc7 | 114 | void calibrateMag(){ |
| jurica238814 | 0:442d98af8cc7 | 115 | vector3_s magnetometerData; |
| jurica238814 | 0:442d98af8cc7 | 116 | for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){ |
| jurica238814 | 0:442d98af8cc7 | 117 | readMagnetometer(&magnetometerData); |
| jurica238814 | 0:442d98af8cc7 | 118 | memsMagnetometerInit += magnetometerData; |
| jurica238814 | 0:442d98af8cc7 | 119 | } |
| jurica238814 | 0:442d98af8cc7 | 120 | memsMagnetometerInit /= CALIBRATION_STEPS; |
| jurica238814 | 0:442d98af8cc7 | 121 | } |
| jurica238814 | 0:442d98af8cc7 | 122 | |
| jurica238814 | 0:442d98af8cc7 | 123 | void updateData() |
| jurica238814 | 0:442d98af8cc7 | 124 | { |
| jurica238814 | 0:442d98af8cc7 | 125 | static uint8_t advertisementType = 0; |
| jurica238814 | 0:442d98af8cc7 | 126 | int16_t temp_acc[3]; |
| jurica238814 | 0:442d98af8cc7 | 127 | BLE &ble = BLE::Instance(); |
| jurica238814 | 0:442d98af8cc7 | 128 | |
| jurica238814 | 0:442d98af8cc7 | 129 | float result; |
| jurica238814 | 0:442d98af8cc7 | 130 | |
| jurica238814 | 3:b62a73dc76b7 | 131 | if(advertisementType < 1) |
| jurica238814 | 3:b62a73dc76b7 | 132 | { |
| jurica238814 | 3:b62a73dc76b7 | 133 | power = 1; |
| jurica238814 | 3:b62a73dc76b7 | 134 | mems.startAccelerometer(); |
| jurica238814 | 3:b62a73dc76b7 | 135 | mems.startGyroscope(); |
| jurica238814 | 3:b62a73dc76b7 | 136 | mems.startMagnetometer(); |
| jurica238814 | 3:b62a73dc76b7 | 137 | wait_ms(WAKEUP_TIME_DELAY_MS); |
| jurica238814 | 3:b62a73dc76b7 | 138 | advertisementPacket.type = 0x00; |
| jurica238814 | 3:b62a73dc76b7 | 139 | readGyroscope((vector3_s *)advertisementPacket.gyroscope); |
| jurica238814 | 3:b62a73dc76b7 | 140 | readAccelerometer((vector3_s *)temp_acc); |
| jurica238814 | 3:b62a73dc76b7 | 141 | readMagnetometer((vector3_s *)advertisementPacket.magnetometer); |
| jurica238814 | 3:b62a73dc76b7 | 142 | advertisementPacket.acc_lsb_value = (0xF9E);//(0x3D80); |
| jurica238814 | 3:b62a73dc76b7 | 143 | advertisementPacket.accelerometer[0] = temp_acc[1]; |
| jurica238814 | 3:b62a73dc76b7 | 144 | advertisementPacket.accelerometer[1] = temp_acc[0]; |
| jurica238814 | 3:b62a73dc76b7 | 145 | advertisementPacket.accelerometer[2] = temp_acc[2]; |
| jurica238814 | 3:b62a73dc76b7 | 146 | // ^--- That's in ug cuz MSB is 1 |
| jurica238814 | 3:b62a73dc76b7 | 147 | } |
| jurica238814 | 3:b62a73dc76b7 | 148 | else |
| jurica238814 | 3:b62a73dc76b7 | 149 | { |
| jurica238814 | 3:b62a73dc76b7 | 150 | NRF_SAADC->ENABLE = 1; |
| jurica238814 | 3:b62a73dc76b7 | 151 | power = 1; |
| jurica238814 | 3:b62a73dc76b7 | 152 | temperaturePower = 1; |
| jurica238814 | 3:b62a73dc76b7 | 153 | lightPower = 1; |
| jurica238814 | 3:b62a73dc76b7 | 154 | shdn = 1; |
| jurica238814 | 3:b62a73dc76b7 | 155 | analogIn.addChannel(9); // Set VDD as source to SAADC |
| jurica238814 | 3:b62a73dc76b7 | 156 | analogIn.addChannel(6); // Light |
| jurica238814 | 3:b62a73dc76b7 | 157 | analogIn.addChannel(7); // Temp |
| jurica238814 | 3:b62a73dc76b7 | 158 | analogIn.calibrate(); |
| jurica238814 | 3:b62a73dc76b7 | 159 | wait_ms(WAKEUP_TIME_DELAY_MS); |
| jurica238814 | 3:b62a73dc76b7 | 160 | analogIn.updateData(); |
| jurica238814 | 3:b62a73dc76b7 | 161 | wait_ms(WAKEUP_TIME_DELAY_MS); |
| jurica238814 | 0:442d98af8cc7 | 162 | |
| jurica238814 | 3:b62a73dc76b7 | 163 | advertisementPacket.type = 0x01; |
| jurica238814 | 3:b62a73dc76b7 | 164 | advertisementPacket.temperature = getTemperature(); |
| jurica238814 | 3:b62a73dc76b7 | 165 | advertisementPacket.light = getLight(); |
| jurica238814 | 3:b62a73dc76b7 | 166 | advertisementPacket.humidity = getHumidity(); |
| jurica238814 | 3:b62a73dc76b7 | 167 | advertisementPacket.pressure = mpl115a1.getPressure(); |
| jurica238814 | 3:b62a73dc76b7 | 168 | advertisementPacket.battery = getBattery(); |
| jurica238814 | 3:b62a73dc76b7 | 169 | } |
| jurica238814 | 0:442d98af8cc7 | 170 | |
| jurica238814 | 0:442d98af8cc7 | 171 | SPI dummySpi(SPI_MOSI, SPI_MISO, SPI_SCLK); |
| jurica238814 | 0:442d98af8cc7 | 172 | power = 0; |
| jurica238814 | 0:442d98af8cc7 | 173 | shdn = 0; |
| jurica238814 | 0:442d98af8cc7 | 174 | temperaturePower = 0; |
| jurica238814 | 0:442d98af8cc7 | 175 | lightPower = 0; |
| jurica238814 | 0:442d98af8cc7 | 176 | NRF_SAADC->ENABLE = 0; |
| jurica238814 | 0:442d98af8cc7 | 177 | |
| jurica238814 | 0:442d98af8cc7 | 178 | if(++advertisementType > 2) advertisementType = 0; |
| jurica238814 | 0:442d98af8cc7 | 179 | adv_data = ble.gap().getAdvertisingPayload(); |
| jurica238814 | 3:b62a73dc76b7 | 180 | adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, |
| jurica238814 | 3:b62a73dc76b7 | 181 | (uint8_t *)&advertisementPacket, sizeof(advertisementPacket)); |
| jurica238814 | 0:442d98af8cc7 | 182 | ble.gap().setAdvertisingPayload(adv_data); |
| jurica238814 | 0:442d98af8cc7 | 183 | } |

