Degree Computation

Dependencies:   aconno_SEGGER_RTT LSM9DS1 Si7006A20 adc52832_common aconnoMPL115A1 aconno_bsp

Files at this revision

API Documentation at this revision

Comitter:
jurica238814
Date:
Wed Dec 12 19:28:16 2018 +0100
Child:
1:082c2c339909
Commit message:
Init commit

Changed in this revision

.gitignore Show annotated file Show diff for this revision Revisions of this file
.hgignore Show annotated file Show diff for this revision Revisions of this file
LSM9DS1.lib Show annotated file Show diff for this revision Revisions of this file
README.md Show annotated file Show diff for this revision Revisions of this file
Si7006A20.lib Show annotated file Show diff for this revision Revisions of this file
acn_nrf52_saadc.lib Show annotated file Show diff for this revision Revisions of this file
aconnoHelpers/aconnoHelpers.cpp Show annotated file Show diff for this revision Revisions of this file
aconnoHelpers/aconnoHelpers.h Show annotated file Show diff for this revision Revisions of this file
aconnoMPL115A1.lib Show annotated file Show diff for this revision Revisions of this file
aconno_SEGGER_RTT.lib Show annotated file Show diff for this revision Revisions of this file
aconno_bsp.lib Show annotated file Show diff for this revision Revisions of this file
aconno_nrf52_uart.lib Show annotated file Show diff for this revision Revisions of this file
adc52832_common.lib Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_settings.py Show annotated file Show diff for this revision Revisions of this file
mbed_settings.pyc Show annotated file Show diff for this revision Revisions of this file
readme.md Show annotated file Show diff for this revision Revisions of this file
source/aconnoConfig.h Show annotated file Show diff for this revision Revisions of this file
source/main.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/tasks.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/tasks.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.gitignore	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,10 @@
+.hg/
+.hg*
+BUILD
+LSM9DS1
+acn_nrf52_saadc
+aconno_bsp
+aconno_nrf52_uart
+adc52832_common
+aconno_SEGGER_RTT
+aconnoMPL115A
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,7 @@
+syntax: glob
+mbed-os/
+.git
+.mbed
+BUILD/
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LSM9DS1.lib	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,1 @@
+http://os.mbed.com/users/jurica238814/code/LSM9DS1/#d6a0d907988f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,1 @@
+acnSENSA
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Si7006A20.lib	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/jurica238814/code/Si7006A20/#cc2c5d49fe63
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/acn_nrf52_saadc.lib	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,1 @@
+http://os.mbed.com/users/Dautor/code/acn_nrf52_saadc/#a11936e7e0af
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aconnoHelpers/aconnoHelpers.cpp	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,195 @@
+/**
+ * Made by Jurica @ aconno
+ * jurica@aconno.de
+ * More info @ aconno.de
+ */
+
+#include "mbed.h"
+#include "ble/BLE.h"
+#include "acd52832_bsp.h"
+#include "GapAdvertisingData.h"
+#include "Si7006A20.h"
+#include "LSM9DS1.h"
+#include "math.h"
+#include "adc52832_common/utilities.h"
+#include "MPL115A1.h"
+#include "acd_nrf52_saadc.h"
+#include "aconnoConfig.h"
+
+NRF52_SAADC analogIn;
+extern DigitalOut lightPower;
+extern DigitalOut temperaturePower;
+extern DigitalOut shdn;
+extern DigitalOut power;
+Si7006   *si;
+extern LSM9DS1  *mems;
+vector3_s memsAccelerometerInit;
+vector3_s memsGyroscopeInit;
+vector3_s memsMagnetometerInit;
+extern GapAdvertisingData adv_data;
+extern advertising_packet advertisementPacket;
+DigitalOut cs(p7);
+
+float getLight(){
+    return ((float)analogIn.getData()[1])/ADC_RESOLUTION * VALUE_TO_PERCENTAGE;
+}
+
+float voltage2temp(float vOut){
+    return ((float)vOut - (float)V0)/((float)TC);
+}
+
+float getTemperature(){
+    return voltage2temp(((float)analogIn.getData()[2])/ADC_RESOLUTION * (float)VCC);
+}
+
+uint8_t getBattery(){
+    uint16_t batteryVoltage = analogIn.getData()[0];
+    
+    const uint16_t zero_percent_limit = 739;
+    const uint16_t onehundred_percent_limit = 810;
+    const uint16_t percentage_increments = 5;
+    uint8_t percentage;
+    
+    if (batteryVoltage < zero_percent_limit)
+    {
+        percentage = 0;
+    }
+    else if(batteryVoltage > onehundred_percent_limit)
+    {
+        percentage = 100;
+    }
+    else
+    {
+        batteryVoltage -= zero_percent_limit;
+        percentage = (batteryVoltage*100)/(onehundred_percent_limit - zero_percent_limit);
+        percentage = percentage/percentage_increments*percentage_increments;
+    }
+    
+    return percentage;
+}
+
+float getHumidity(){
+    float result;
+    si->getHumidity(&result);
+    return result;
+}
+
+void readGyroscope(vector3_s *gyroscopeData){
+    mems->readGyroscope((int16_t *)gyroscopeData);
+    *gyroscopeData -= memsGyroscopeInit;
+}
+
+void readAccelerometer(vector3_s *accelerometerData){
+    mems->readAccelerometer((int16_t *)accelerometerData);
+    *accelerometerData -= memsAccelerometerInit;
+}
+
+void readMagnetometer(vector3_s *magnetometerData){
+    mems->readMagnetometer((int16_t *)magnetometerData);
+    *magnetometerData -= memsMagnetometerInit;
+}
+
+void calibrateAccelerometer(){
+    vector3_s accelerometerData;
+    for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){
+        readAccelerometer(&accelerometerData);
+        memsAccelerometerInit += accelerometerData;
+    }
+    memsAccelerometerInit /= CALIBRATION_STEPS;
+}
+
+void calibrateGyroscope(){
+    vector3_s gyroscopeData;
+    for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){
+        readGyroscope(&gyroscopeData);
+        memsGyroscopeInit += gyroscopeData;
+    }
+    memsGyroscopeInit /= CALIBRATION_STEPS;
+}
+
+void calibrateMag(){
+    vector3_s magnetometerData;
+    for(uint8_t counter = 0; counter < CALIBRATION_STEPS; ++counter){
+        readMagnetometer(&magnetometerData);
+        memsMagnetometerInit += magnetometerData;
+    }
+    memsMagnetometerInit /= CALIBRATION_STEPS;
+}
+
+void disableI2C()
+{
+    
+    NRF_TWI0->ENABLE = 0;
+    NRF_TWI0->PSELSCL = 0xFFFFFFFF;
+    NRF_TWI1->ENABLE = 0;
+    NRF_TWI1->PSELSCL = 0xFFFFFFFF;
+    NRF_TWIM0->ENABLE = 0;
+    NRF_TWIM0->PSEL.SCL = 0x80000000;
+    NRF_TWIM0->PSEL.SDA = 0x80000000;
+    NRF_TWIM1->ENABLE = 0;
+    NRF_TWIM0->PSEL.SCL = 0x80000000;
+    NRF_TWIM0->PSEL.SDA = 0x80000000;
+    
+    //foo = new DigitalOut(I2C_DATA);
+    //bar = new DigitalOut(I2C_CLK);
+    
+    //foo = 1;
+    //bar = 1;
+   
+}
+
+void updateData()
+{
+    static uint8_t advertisementType = 0;
+    int16_t temp_acc[3];
+    BLE &ble = BLE::Instance();
+
+    I2C i2c(I2C_DATA, I2C_CLK);   
+    si = new Si7006(&i2c);
+    SPI spi(SPI_MOSI, SPI_MISO, SPI_SCLK);
+    MPL115A1 mpl115a1(spi, cs);
+    
+    float result;
+    
+    NRF_SAADC->ENABLE = 1;
+    power = 1;
+    temperaturePower = 1;
+    lightPower = 1;
+    shdn = 1;
+    analogIn.addChannel(9); // Set VDD  as source to SAADC
+    analogIn.addChannel(6); // Light
+    analogIn.addChannel(7); // Temp
+    analogIn.calibrate();
+    wait_ms(WAKEUP_TIME_DELAY_MS);
+
+    analogIn.updateData();
+
+    advertisementPacket.type = 0x01;
+    advertisementPacket.temperature = getTemperature();
+    advertisementPacket.light       = getLight();
+    advertisementPacket.humidity    = getHumidity();
+    advertisementPacket.pressure    = mpl115a1.getPressure();
+
+    //disableI2C();
+    //I2C dummy(I2C_DATA, I2C_CLK);
+    
+    SPI dummySpi(SPI_MOSI, SPI_MISO, SPI_SCLK);
+    power = 0;
+    shdn = 0;
+    temperaturePower = 0;
+    lightPower = 0;
+    NRF_SAADC->ENABLE = 0;
+
+    
+    //DigitalOut foo(I2C_DATA);
+    //DigitalOut bar(I2C_CLK);
+    //foo = 1;
+    //bar = 1; 
+    
+    if(++advertisementType > 2) advertisementType = 0;    
+    adv_data = ble.gap().getAdvertisingPayload();
+    adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
+    ble.gap().setAdvertisingPayload(adv_data);
+
+    //printf("Data updated.\r\n");
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aconnoHelpers/aconnoHelpers.h	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,18 @@
+/**
+ * Made by Jurica @ aconno
+ * jurica@aconno.de
+ * More info @ aconno.de
+ */
+
+#ifndef __ACONNO_HELPERS_H__
+#define __ACONNO_HELPERS_H__
+
+/**
+ * Made by Jurica @ aconno
+ * jurica@aconno.de
+ * More info @ aconno.de
+ */
+
+void updateData();
+
+#endif  // __ACONNO_HELPERS_H__
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aconnoMPL115A1.lib	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/jurica238814/code/aconnoMPL115A1/#3b754d29069f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aconno_SEGGER_RTT.lib	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/jurica238814/code/aconno_SEGGER_RTT/#abdc4190029b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aconno_bsp.lib	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,1 @@
+https://mbed.org/users/jurica238814/code/aconno_bsp/#6d9c6c231034
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aconno_nrf52_uart.lib	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,1 @@
+https://mbed.org/users/Dautor/code/aconno_nrf52_uart/#0014952b2b75
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/adc52832_common.lib	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/aconno-dev-team/code/adc52832_common/#3e57d0e33abd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#2e5e23f7c1b3f4e7989897a49b059d72c8f3229a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_settings.py	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,45 @@
+"""
+mbed SDK
+Copyright (c) 2016 ARM Limited
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+"""
+
+from os.path import join, abspath, dirname
+
+#ROOT = abspath(join(dirname(__file__), "."))
+
+##############################################################################
+# Build System Settings
+##############################################################################
+#BUILD_DIR = abspath(join(ROOT, "build"))
+
+# ARM
+#ARM_PATH = "C:/Program Files/ARM"
+
+# GCC ARM
+#GCC_ARM_PATH = ""
+
+# GCC CodeRed
+#GCC_CR_PATH = "C:/code_red/RedSuite_4.2.0_349/redsuite/Tools/bin"
+
+# IAR
+#IAR_PATH = "C:/Program Files (x86)/IAR Systems/Embedded Workbench 7.0/arm"
+
+# Goanna static analyser. Please overload it in private_settings.py
+#GOANNA_PATH = "c:/Program Files (x86)/RedLizards/Goanna Central 3.2.3/bin"
+
+#BUILD_OPTIONS = []
+
+# mbed.org username
+#MBED_ORG_USER = ""
Binary file mbed_settings.pyc has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/readme.md	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,13 @@
+SETTING THE TIMINGS WITH THE FOLLOWING 2 DEFINES
+
+    WAKE_UP_TIME determines how often data is read from sensors
+    
+    ADV_INTERVAL determines how often data is advertised, don't go under 100 ms
+    
+    These are both defined in /main.cpp
+    
+ACCELERATION FACTOR
+
+    Acceleration value can be calculated from raw sensor value by the following
+    formula:
+        acceleration = raw_value * (correction_factor/2^16)
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/aconnoConfig.h	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,58 @@
+/**
+ * Made by Jurica @ aconno
+ * jurica@aconno.com
+ * More info @ aconno.de
+ */
+
+#ifndef __ACONNO_CONFIG_H__
+#define __ACONNO_CONFIG_H__
+
+#define V0                      (0.47)      // In volts 
+#define TC                      (0.01)      // In volts 
+#define VCC                     (3.6)
+#define VALUE_TO_PERCENTAGE     (100)
+#define APPLICATION_ID          (0xCF170059)
+
+#define ADC_REFERENCE           (3.6f)      // adc reference voltage 
+#define ADC_RESOLUTION          (1024)      // 10-bit adc            
+#define CALIBRATION_STEPS       (20)
+#define WAKEUP_TIME_DELAY_MS    (150)       // Time for sensors to wake up
+
+#define I2C_DATA                (p19)
+#define I2C_CLK                 (p20)
+#define SPI_MISO                (p5)
+#define SPI_MOSI                (p3)
+#define SPI_SCLK                (p4)
+
+#define SENSORS_REFRESH_RATE_MS (1000)      // NOT IN USE!!
+// The beacon wakes up every ADV_PERIOD_TIME_MS
+#define USER_ADV_PERIOD_TIME_MS (1000-125)
+#define ADV_PERIOD_TIME_MS      (USER_ADV_PERIOD_TIME_MS-WAKEUP_TIME_DELAY_MS)      
+// The beacon advertises for RADIO_ACTIVE_TIME_MS
+#define BLE_PACKETS_TO_SEND     (3)
+#define ADV_INTERVAL_MS         (100)        // Adv interval in ms (BLE level!)
+#define RADIO_ACTIVE_TIME_MS    (ADV_INTERVAL_MS*BLE_PACKETS_TO_SEND)
+#define TX_POWER_DB             (4)
+
+struct __attribute__((packed, aligned(1))) advertising_packet
+{
+    uint32_t header;
+    uint8_t  type;
+    union{
+        struct{
+            int16_t gyroscope[3];
+            int16_t accelerometer[3];
+            int16_t magnetometer[3];
+            uint16_t acc_lsb_value;
+        };
+        struct{
+            float temperature;
+            float humidity;
+            float pressure;
+            float light;
+            uint8_t battery;
+        };
+    };
+};
+
+#endif  // __ACONNO_CONFIG_H__
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/main.cpp	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,104 @@
+/* 
+ * aconno.de
+ * Made by Jurica Resetar
+ * Edited by Karlo Milicevic
+ * Edited by Dominik Bartolovic
+ * All right reserved 
+ *
+ */
+
+#include "mbed.h"
+#include "ble/BLE.h"
+#include "acd52832_bsp.h"
+#include "GapAdvertisingData.h"
+#include "aconnoConfig.h"
+#include "Si7006A20.h"
+#include "LSM9DS1.h"
+#include "adc52832_common/utilities.h"
+#include "acd_nrf52_saadc.h"
+#include "tasks.h"
+#include <events/mbed_events.h>
+#include "aconnoHelpers.h"
+
+GapAdvertisingData adv_data = GapAdvertisingData();
+advertising_packet advertisementPacket;
+LSM9DS1  *mems;
+DigitalOut lightPower(p28);
+DigitalOut temperaturePower(p31);
+DigitalOut shdn(p6);
+DigitalOut power(p2);
+
+Thread updateDataThread;
+Thread sendDataThread;
+
+EventQueue eventQueue;
+
+/**
+ * Callback triggered when the ble initialization process has finished
+ */
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
+{
+    BLE &ble = BLE::Instance();
+
+    advertisementPacket.header = APPLICATION_ID;
+    /* setup advertising */
+    ble.gap().accumulateAdvertisingPayload(
+        GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    ble.gap().accumulateAdvertisingPayload(
+        GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, 
+        (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
+    ble.gap().setAdvertisingType(
+        GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);    
+    ble.gap().setAdvertisingInterval(ADV_INTERVAL_MS);
+    ble.gap().setTxPower(TX_POWER_DB);        // Set TX power to TX_POWER 
+}
+
+void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context)
+{
+    BLE &ble = BLE::Instance();
+    eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
+}
+
+int main()
+{
+    //printf("Main started.\r\n");
+
+    BLE &ble = BLE::Instance();
+    ble.init(bleInitComplete);
+    ble.onEventsToProcess(scheduleBleEventsProcessing);
+    while (ble.hasInitialized()  == false) { /* spin loop */ }
+    
+    updateDataThread.start(updateDataTask);
+    sendDataThread.start(sendDataTask);
+    
+    
+    /*
+    //DigitalOut bar(I2C_DATA);
+    //DigitalOut foo(I2C_CLK);
+    I2C *i2c;
+    float result;
+    while(true)
+    {
+        power = 1;
+        ThisThread::sleep_for(500);
+        i2c = new I2C(I2C_DATA, I2C_CLK);
+        //I2C i2c(I2C_DATA, I2C_CLK);
+        Si7006 si(i2c);
+        si.getHumidity(&result);
+        printf("Humidity is: %f\r\n", result);
+        power = 0;
+        //wait_ms(1000);
+        
+        //foo = 1;
+        //bar = 1;     
+
+        //I2C fooI2C(I2C_DATA, I2C_CLK);
+        ThisThread::sleep_for(1000);
+    }
+    */
+
+    while(true)
+    {
+        Thread::wait(0xFFFFFFFF);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/tasks.cpp	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,48 @@
+/**
+ * Made by Jurica @ aconno
+ * jurica@aconno.de
+ * More info @ aconno.de
+ */
+
+#include "mbed.h"
+#include "aconnoHelpers.h"
+#include "ble/BLE.h"
+#include "aconnoConfig.h"
+
+#define UPDATE_SENSORS_SIGNAL   (0x10001000)
+#define SEND_DATA_SIGNAL        (0x10001001) 
+extern Thread updateDataThread;
+extern Thread sendDataThread;
+
+void updateDataTask()
+{
+    //printf("updateDataTask started.\r\n");
+    while(1)
+    {
+        
+        ThisThread::flags_wait_any(UPDATE_SENSORS_SIGNAL);
+        //printf("Ulazim\r\n");
+        updateData();
+        sendDataThread.flags_set(SEND_DATA_SIGNAL);
+        //printf("Vrtim se.\r\n");
+    }
+}
+
+void sendDataTask()
+{
+    //printf("sendDataTask started.\r\n");
+    BLE &ble = BLE::Instance();
+    while(1)
+    {
+        updateDataThread.flags_set(UPDATE_SENSORS_SIGNAL);
+        ThisThread::flags_wait_any(SEND_DATA_SIGNAL);
+        //printf("Turning BLE radion on.\r\n");
+        ble.gap().startAdvertising();
+        ThisThread::sleep_for(RADIO_ACTIVE_TIME_MS);
+        //printf("Turining BLE radio off.\r\n");
+        ble.gap().stopAdvertising();
+        MBED_ASSERT((ADV_PERIOD_TIME_MS-RADIO_ACTIVE_TIME_MS) > 0);
+        ThisThread::sleep_for(ADV_PERIOD_TIME_MS-RADIO_ACTIVE_TIME_MS);
+        
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/tasks.h	Wed Dec 12 19:28:16 2018 +0100
@@ -0,0 +1,13 @@
+/**
+ * Made by Jurica @ aconno
+ * jurica@aconno.de
+ * More info @ aconno.de
+ */
+
+#ifndef __TASKS_H__
+#define __TASKS_H__
+
+void updateDataTask();
+void sendDataTask();
+
+#endif  // __TASKS_H__
\ No newline at end of file