diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index da5a641..c0a7b56 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -43,21 +43,45 @@ jobs: - name: Arduino_LSM9DS1 - name: ArduinoBLE - name: Arduino_CMSIS-DSP - sketch-paths: examples/Nano33BLESenseFirmware + sketch-paths: examples/ScienceKit/Nano33BLESenseFirmware - fqbn: arduino:mbed_nano:nanorp2040connect platforms: | - name: arduino:mbed_nano libraries: | - name: Arduino_LSM6DSOX - name: ArduinoBLE - sketch-paths: examples/RP2040ConnectFirmware + sketch-paths: examples/ScienceKit/RP2040ConnectFirmware - fqbn: arduino:samd:mkrwifi1010 platforms: | - name: arduino:samd libraries: | - name: ArduinoBLE - name: Adafruit LSM9DS1 Library - sketch-paths: examples/PhysicsLabFirmware + sketch-paths: examples/ScienceKit/PhysicsLabFirmware + - fqbn: arduino:mbed_nano:nano33ble + platforms: | + - name: arduino:mbed_nano + libraries: | + - name: Arduino_APDS9960 + - name: Arduino_HTS221 + - name: Arduino_LPS22HB + - name: Arduino_LSM6DSOX + - name: Arduino_BMI270_BMM150 + - name: INA2xx + - name: SerialFlash + - name: ArduinoBLE + - name: Arduino_CMSIS-DSP + sketch-paths: examples/ScienceKitR2/Nano33BLESenseFirmware + - fqbn: arduino:samd:mkrwifi1010 + platforms: | + - name: arduino:samd + libraries: | + - name: ArduinoBLE + - name: Arduino_LSM6DSOX + - name: Arduino_BMI270_BMM150 + - name: INA2xx + - name: SerialFlash + sketch-paths: examples/ScienceKitR2/PhysicsLabFirmware steps: - name: Checkout repository diff --git a/examples/Nano33BLESenseFirmware/LowPower.cpp b/examples/ScienceKit/Nano33BLESenseFirmware/LowPower.cpp similarity index 100% rename from examples/Nano33BLESenseFirmware/LowPower.cpp rename to examples/ScienceKit/Nano33BLESenseFirmware/LowPower.cpp diff --git a/examples/Nano33BLESenseFirmware/LowPower.h b/examples/ScienceKit/Nano33BLESenseFirmware/LowPower.h similarity index 100% rename from examples/Nano33BLESenseFirmware/LowPower.h rename to examples/ScienceKit/Nano33BLESenseFirmware/LowPower.h diff --git a/examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino b/examples/ScienceKit/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino similarity index 100% rename from examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino rename to examples/ScienceKit/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino diff --git a/examples/PhysicsLabFirmware/.DS_Store b/examples/ScienceKit/PhysicsLabFirmware/.DS_Store similarity index 100% rename from examples/PhysicsLabFirmware/.DS_Store rename to examples/ScienceKit/PhysicsLabFirmware/.DS_Store diff --git a/src/INA226.cpp b/examples/ScienceKit/PhysicsLabFirmware/INA226.cpp similarity index 100% rename from src/INA226.cpp rename to examples/ScienceKit/PhysicsLabFirmware/INA226.cpp diff --git a/src/INA226.h b/examples/ScienceKit/PhysicsLabFirmware/INA226.h similarity index 100% rename from src/INA226.h rename to examples/ScienceKit/PhysicsLabFirmware/INA226.h diff --git a/examples/PhysicsLabFirmware/PhysicsLabFirmware.ino b/examples/ScienceKit/PhysicsLabFirmware/PhysicsLabFirmware.ino similarity index 100% rename from examples/PhysicsLabFirmware/PhysicsLabFirmware.ino rename to examples/ScienceKit/PhysicsLabFirmware/PhysicsLabFirmware.ino diff --git a/examples/RP2040ConnectFirmware/RP2040ConnectFirmware.ino b/examples/ScienceKit/RP2040ConnectFirmware/RP2040ConnectFirmware.ino similarity index 100% rename from examples/RP2040ConnectFirmware/RP2040ConnectFirmware.ino rename to examples/ScienceKit/RP2040ConnectFirmware/RP2040ConnectFirmware.ino diff --git a/examples/ScienceKitR2/Nano33BLESenseFirmware/LowPower.cpp b/examples/ScienceKitR2/Nano33BLESenseFirmware/LowPower.cpp new file mode 100644 index 0000000..a7638f1 --- /dev/null +++ b/examples/ScienceKitR2/Nano33BLESenseFirmware/LowPower.cpp @@ -0,0 +1,36 @@ +#include "Arduino.h" +#include "mbed.h" +#include "ArduinoBLE.h" +#include "LowPower.h" + +#include "nrf_power.h" +#include "nrf_uarte.h" +#include "nrf_uart.h" + +void lowPower() +{ + // Disable UARTE0 which is initially enabled by the bootloader + nrf_uarte_task_trigger(NRF_UARTE0, NRF_UARTE_TASK_STOPRX); + while (!nrf_uarte_event_check(NRF_UARTE0, NRF_UARTE_EVENT_RXTO)) ; + NRF_UARTE0->ENABLE = 0; + NRF_UART0->ENABLE = 0; + + // Enable DCDC + nrf_power_dcdcen_set(true); + + // Turn off LED_BUILTIN + digitalWrite(LED_BUILTIN, LOW); +} + +void lowPowerWait(unsigned long time) +{ + rtos::ThisThread::sleep_for(time); +} + +void lowPowerBleWait(unsigned long time) +{ + unsigned long timeRef = millis(); + while (millis() - timeRef < time) { + BLE.poll(time - (millis() - timeRef)); + } +} diff --git a/examples/ScienceKitR2/Nano33BLESenseFirmware/LowPower.h b/examples/ScienceKitR2/Nano33BLESenseFirmware/LowPower.h new file mode 100644 index 0000000..1f77f5c --- /dev/null +++ b/examples/ScienceKitR2/Nano33BLESenseFirmware/LowPower.h @@ -0,0 +1,8 @@ +#ifndef _LOWPOWER_H_ +#define _LOWPOWER_H_ + +void lowPower(); +void lowPowerWait(unsigned long time); +void lowPowerBleWait(unsigned long time); + +#endif //_LOWPOWER_H_ \ No newline at end of file diff --git a/examples/ScienceKitR2/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino b/examples/ScienceKitR2/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino new file mode 100644 index 0000000..a9d96a0 --- /dev/null +++ b/examples/ScienceKitR2/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino @@ -0,0 +1,201 @@ +#include "LowPower.h" +#include + +#include "config.h" +#include + +#include + +const int VERSION = 0x00000001; + +#define SCIENCE_KIT_UUID(val) ("555a0002-" val "-467a-9538-01f0652c74e8") + +//#define DEBUG 0 + +BLEService service (SCIENCE_KIT_UUID("0000")); +BLEUnsignedIntCharacteristic versionCharacteristic (SCIENCE_KIT_UUID("0001"), BLERead); +BLECharacteristic accelerationCharacteristic (SCIENCE_KIT_UUID("0011"), BLENotify, 3 * sizeof(float)); +BLECharacteristic gyroscopeCharacteristic (SCIENCE_KIT_UUID("0012"), BLENotify, 3 * sizeof(float)); +BLECharacteristic magneticFieldCharacteristic(SCIENCE_KIT_UUID("0013"), BLENotify, 3 * sizeof(float)); +BLEFloatCharacteristic temperatureCharacteristic (SCIENCE_KIT_UUID("0014"), BLENotify); +BLEFloatCharacteristic pressureCharacteristic (SCIENCE_KIT_UUID("0015"), BLENotify); +BLEFloatCharacteristic humidityCharacteristic (SCIENCE_KIT_UUID("0016"), BLENotify); +BLEUnsignedIntCharacteristic proximityCharacteristic (SCIENCE_KIT_UUID("0017"), BLENotify); +BLECharacteristic colorCharacteristic (SCIENCE_KIT_UUID("0018"), BLENotify, 4 * sizeof(int)); +BLEUnsignedShortCharacteristic soundPressureCharacteristic(SCIENCE_KIT_UUID("0019"), BLENotify); +BLEFloatCharacteristic resistanceCharacteristic (SCIENCE_KIT_UUID("0020"), BLENotify); + +byte voltageBufferIndex = 0; +bool voltageBufferFilled = false; +short soundSampleBuffer[256]; + +void updateSubscribedCharacteristics(); + +void onPDMdata() { + // query the number of bytes available + int bytesAvailable = PDM.available(); + + // read into the sample buffer + PDM.read(soundSampleBuffer, bytesAvailable); +} + +uint16_t getSoundAverage() { + uint32_t avg = 0; + for (int i = 0; i < sizeof(soundSampleBuffer)/sizeof(soundSampleBuffer[0]); i++) { + avg += soundSampleBuffer[i]*soundSampleBuffer[i]; + } + return sqrt(avg); +} + +// String to calculate the local and device name +String name; +unsigned long lastNotify = 0; + +void printSerialMsg(const char * msg) { + #ifdef DEBUG + if (Serial) { + Serial.println(msg); + } + #endif +} + +void setup() { + #ifdef DEBUG + Serial.begin(9600); + while (!Serial); + Serial.println("Started"); + #endif + + delay(2000); + sensorsInit(); + + PDM.onReceive(onPDMdata); + if (!PDM.begin(1, 16000)) { + printSerialMsg("Failed to start PDM!"); + blinkLoop(); + } + + if (!BLE.begin()) { + printSerialMsg("Failed to initialized BLE!"); + blinkLoop(); + } + + String address = BLE.address(); + #ifdef DEBUG + if (Serial) { + Serial.print("address = "); + Serial.println(address); + } + #endif + address.toUpperCase(); + + name = "BLE Sense - "; + name += address[address.length() - 5]; + name += address[address.length() - 4]; + name += address[address.length() - 2]; + name += address[address.length() - 1]; + + #ifdef DEBUG + if (Serial) { + Serial.print("name = "); + Serial.println(name); + } + #endif + + BLE.setLocalName(name.c_str()); + BLE.setDeviceName(name.c_str()); + BLE.setAdvertisedService(service); + + service.addCharacteristic(versionCharacteristic); + service.addCharacteristic(accelerationCharacteristic); + service.addCharacteristic(gyroscopeCharacteristic); + service.addCharacteristic(magneticFieldCharacteristic); + service.addCharacteristic(temperatureCharacteristic); + service.addCharacteristic(pressureCharacteristic); + service.addCharacteristic(humidityCharacteristic); + service.addCharacteristic(proximityCharacteristic); + service.addCharacteristic(colorCharacteristic); + service.addCharacteristic(soundPressureCharacteristic); + service.addCharacteristic(resistanceCharacteristic); + + versionCharacteristic.setValue(VERSION); + + BLE.addService(service); + BLE.advertise(); + + lowPower(); +} + +void loop() { + BLE.poll(1000); + while (BLE.connected()) { + lowPowerBleWait(100); + updateSubscribedCharacteristics(); + } +} + +void updateSubscribedCharacteristics() { + if (accelerationCharacteristic.subscribed()) { + if (IMU_SK.accelerationAvailable()) { + float x, y, z; + IMU_SK.readAcceleration(x, y, z); + float acceleration[3]; + + acceleration[0] = x; + acceleration[1] = y; + acceleration[2] = z; + accelerationCharacteristic.writeValue((byte*)acceleration, sizeof(acceleration)); + } + } + if (gyroscopeCharacteristic.subscribed()) { + if (IMU_SK.gyroscopeAvailable()) { + float x, y, z; + IMU_SK.readGyroscope(x, y, z); + float gyroscope[3]; + + gyroscope[0] = x; + gyroscope[1] =y; + gyroscope[2] = z; + gyroscopeCharacteristic.writeValue((byte*)gyroscope, sizeof(gyroscope)); + } + } + + if (magneticFieldCharacteristic.subscribed()) { + float x, y, z; + if (BME.magneticFieldAvailable()) { + BME.readMagneticField(x, y, z); + float magneticField[3]; + magneticField[0] = x; + magneticField[1] = y; + magneticField[2] = z; + magneticFieldCharacteristic.writeValue((byte*)magneticField, sizeof(magneticField)); + } + } + + if (temperatureCharacteristic.subscribed()) { + float temperature = TempSens.getTemperature(); + temperatureCharacteristic.writeValue(temperature); + } + + + if(resistanceCharacteristic.subscribed()){ + float resistanceAvg = INFINITY; + resistanceAvg = ResSens.getResistorValue(); + resistanceCharacteristic.writeValue(resistanceAvg); + } + + if (proximityCharacteristic.subscribed() && APDS.proximityAvailable()) { + uint32_t proximity = APDS.readProximity(); + proximityCharacteristic.writeValue(proximity); + } + if (colorCharacteristic.subscribed() && APDS.colorAvailable()) { + int color[4]; + APDS.readColor(color[0], color[1], color[2], color[3]); + colorCharacteristic.writeValue((byte*)color, sizeof(color)); + } + + if (pressureCharacteristic.subscribed()) { + float pressure = BARO.readPressure(); + pressureCharacteristic.writeValue(pressure); + } +} diff --git a/examples/ScienceKitR2/Nano33BLESenseFirmware/config.h b/examples/ScienceKitR2/Nano33BLESenseFirmware/config.h new file mode 100644 index 0000000..9784db1 --- /dev/null +++ b/examples/ScienceKitR2/Nano33BLESenseFirmware/config.h @@ -0,0 +1,89 @@ +#include "ArduinoScienceJournal.h" + +// Flash memory +#include "SerialFlash.h" +#include + +const int FlashChipSelect = 2; + +#include +#include +#include + +// IMU +#include +// 3 axis magnetometer +#include +// INA +#include +#include + + +LSM6DSOXClass IMU_SK = LSM6DSOXClass(Wire,0x6A); +BoschSensorClass BME = BoschSensorClass(Wire); + +const uint32_t SHUNT_MICRO_OHM{100000}; ///< Shunt resistance in Micro-Ohm, e.g. 100000 is 0.1 Ohm +const uint16_t MAXIMUM_AMPS{1}; ///< Max expected amps, clamped from 1A to a max of 1022A + +INA_Class INA; + +SerialFlashFile file; + +void blinkLoop() { + while (1) { + digitalWrite(LED_BUILTIN, HIGH); + delay(500); + digitalWrite(LED_BUILTIN, LOW); + delay(500); + } +} + +void sensorsInit() { + // INA Init + if (!INA.begin(MAXIMUM_AMPS, SHUNT_MICRO_OHM)) { + Serial.println(F("No INA device found, retrying in 10 seconds...")); + while(1); + } // while no devices detected + Serial.println("INA init success!"); + INA.setBusConversion(8500); // Maximum conversion time 8.244ms + INA.setShuntConversion(8500); // Maximum conversion time 8.244ms + INA.setAveraging(128); // Average each reading n-times + INA.setMode(INA_MODE_CONTINUOUS_BOTH); // Bus/shunt measured continuously + INA.alertOnBusOverVoltage(true, 5000); // Trigger alert if over 5V on bus + + // bmm150 + if (!BME.begin()) { + Serial.println("Failed to initialize IMU!"); + while (1); + } + + // LSM6DSOX init + if (!IMU_SK.begin()) { + Serial.println("Failed to initialize IMU!"); + while (1); + } + Serial.println("IMU Initialized"); + + // Flash Init + if (!SerialFlash.begin(FlashChipSelect)) { + Serial.println(F("Failed to initialize Flash!")); + while (1); + } + + Serial.println("Flash Initialized"); + + if (!APDS.begin()) { + Serial.println("Failed to initialized APDS!"); + blinkLoop(); + } + + if (!HTS.begin()) { + Serial.println("Failed to initialized HTS!"); + blinkLoop(); + } + + if (!BARO.begin()) { + Serial.println("Failed to initialized BARO!"); + blinkLoop(); + } +} diff --git a/examples/ScienceKitR2/PhysicsLabFirmware/.DS_Store b/examples/ScienceKitR2/PhysicsLabFirmware/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/examples/ScienceKitR2/PhysicsLabFirmware/.DS_Store differ diff --git a/examples/ScienceKitR2/PhysicsLabFirmware/PhysicsLabFirmware.ino b/examples/ScienceKitR2/PhysicsLabFirmware/PhysicsLabFirmware.ino new file mode 100644 index 0000000..7806af4 --- /dev/null +++ b/examples/ScienceKitR2/PhysicsLabFirmware/PhysicsLabFirmware.ino @@ -0,0 +1,248 @@ +/* + This file is part of the PhysicsLabFirmware library. + Copyright (c) 2019 Arduino SA. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include // click here to install the library: http://librarymanager#ArduinoBLE +#include "config.h" + + +#define SCIENCE_KIT_UUID(val) ("555a0001-" val "-467a-9538-01f0652c74e8") + +BLEService service (SCIENCE_KIT_UUID("0000")); +BLEUnsignedIntCharacteristic versionCharacteristic (SCIENCE_KIT_UUID("0001"), BLERead); +BLEByteCharacteristic ledCharacteristic (SCIENCE_KIT_UUID("1001"), BLERead | BLEWrite); +BLEUnsignedShortCharacteristic input1Characteristic (SCIENCE_KIT_UUID("2001"), BLENotify); +BLEUnsignedShortCharacteristic input2Characteristic (SCIENCE_KIT_UUID("2002"), BLENotify); +BLEUnsignedShortCharacteristic input3Characteristic (SCIENCE_KIT_UUID("2003"), BLENotify); +BLEByteCharacteristic ouput1Characteristic (SCIENCE_KIT_UUID("3001"), BLERead | BLEWrite); +BLEByteCharacteristic ouput2Characteristic (SCIENCE_KIT_UUID("3002"), BLERead | BLEWrite); +BLEFloatCharacteristic voltageCharacteristic (SCIENCE_KIT_UUID("4001"), BLENotify); +BLEFloatCharacteristic currentCharacteristic (SCIENCE_KIT_UUID("4002"), BLENotify); +BLEFloatCharacteristic resistanceCharacteristic (SCIENCE_KIT_UUID("4003"), BLENotify); +BLECharacteristic accelerationCharacteristic (SCIENCE_KIT_UUID("5001"), BLENotify, 3 * sizeof(float)); +BLECharacteristic gyroscopeCharacteristic (SCIENCE_KIT_UUID("5002"), BLENotify, 3 * sizeof(float)); +BLECharacteristic magneticFieldCharacteristic(SCIENCE_KIT_UUID("5003"), BLENotify, 3 * sizeof(float)); +BLECharacteristic lightCharacteristic (SCIENCE_KIT_UUID("6001"), BLENotify, 3 * sizeof(float)); +BLEFloatCharacteristic TemperatureCharacteristic (SCIENCE_KIT_UUID("6002"), BLENotify); + +// check +const int LED_PIN = 0; // check +const int INPUT1_PIN = A3; +const int INPUT2_PIN = A1; +const int INPUT3_PIN = A0; +const int OUTPUT1_PIN = 5; +const int OUTPUT2_PIN = 1; +const int RESISTANCE_PIN = A2; + +String name; +unsigned long lastNotify = 0; + +unsigned long imuTime; + +#define RESISTOR_AUX_LOW 47000.0 +#define RESISTOR_AUX_HIGH 979.16 // 47k in parallel with 1k = 979.16 Ohm + +#define IMU_UPDATE_TIME 50 + +//#define DEBUG //uncomment to debug the code :) + +void setup() { + Serial.begin(9600); +#ifdef DEBUG + while (!Serial); + Serial.println("Started"); +#endif + + pinMode(LED_PIN, OUTPUT); + pinMode(INPUT1_PIN, INPUT); + pinMode(INPUT2_PIN, INPUT); + pinMode(INPUT3_PIN, INPUT); + pinMode(OUTPUT1_PIN, OUTPUT); + pinMode(OUTPUT2_PIN, OUTPUT); + + sensorsInit(); + + if (!BLE.begin()) { + Serial.println("Failed to initialized BLE!"); + + while (1); + } + + String address = BLE.address(); + + address.toUpperCase(); + + name = "MKRSci"; + name += address[address.length() - 5]; + name += address[address.length() - 4]; + name += address[address.length() - 2]; + name += address[address.length() - 1]; + + BLE.setLocalName(name.c_str()); + BLE.setDeviceName(name.c_str()); + BLE.setAdvertisedService(service); + + service.addCharacteristic(versionCharacteristic); + service.addCharacteristic(ledCharacteristic); + service.addCharacteristic(input1Characteristic); + service.addCharacteristic(input2Characteristic); + service.addCharacteristic(input3Characteristic); + service.addCharacteristic(ouput1Characteristic); + service.addCharacteristic(ouput2Characteristic); + service.addCharacteristic(voltageCharacteristic); + service.addCharacteristic(currentCharacteristic); + service.addCharacteristic(resistanceCharacteristic); + service.addCharacteristic(accelerationCharacteristic); + service.addCharacteristic(gyroscopeCharacteristic); + service.addCharacteristic(magneticFieldCharacteristic); + service.addCharacteristic(lightCharacteristic); + service.addCharacteristic(TemperatureCharacteristic); + BLE.addService(service); + + BLE.advertise(); + imuTime = millis(); +} + +void loop() { + lastNotify = 0; + + while (BLE.connected()) { + if (ledCharacteristic.written()) { // si usa? + analogWrite(LED_PIN, ledCharacteristic.value()); + } + + if (ouput1Characteristic.written()) { + analogWrite(OUTPUT1_PIN, ouput1Characteristic.value()); + } + + if (ouput2Characteristic.written()) { + analogWrite(OUTPUT2_PIN, ouput2Characteristic.value()); + } + + unsigned long now = millis(); + + if (abs((long)now - (long)lastNotify) >= 100) { + lastNotify = now; + + // every 100ms update subscribed characteristics + updateSubscribedCharacteristics(); + } + + updateSubscribedIMUCharacteristics(); + } +} + +void updateSubscribedCharacteristics() { + if (input1Characteristic.subscribed()) { + input1Characteristic.writeValue(analogReadAverage(INPUT1_PIN, 30)); + } + + if (input2Characteristic.subscribed()) { + input2Characteristic.writeValue(analogReadAverage(INPUT2_PIN, 30)); + } + + if (input3Characteristic.subscribed()) { + input3Characteristic.writeValue(analogReadAverage(INPUT3_PIN, 30)); + } + + if (voltageCharacteristic.subscribed()) { + //dtostrf(, 7, 4, busChar); + float voltage = INA.getBusMilliVolts(0) / 1000.0; +#ifdef DEBUG + Serial.print("Voltage: "); + Serial.print(voltage); + Serial.println("V"); +#endif + voltageCharacteristic.writeValue(voltage); + } + + if (currentCharacteristic.subscribed()) { + float current = (INA.getBusMicroAmps(0))/1000.0F; +#ifdef DEBUG + Serial.print("Current: "); + Serial.print(current); + Serial.println("mA"); +#endif + currentCharacteristic.writeValue(current/1000.0F); + } + + if (resistanceCharacteristic.subscribed()) { + float resistanceAvg = INFINITY; //open circuit as default + resistanceAvg = ResSens.getResistorValue(); +#ifdef DEBUG + Serial.print("Resistance: "); + Serial.print(resistanceAvg); + Serial.println(" Ohm"); +#endif + resistanceCharacteristic.writeValue(resistanceAvg); + } +} + + +int analogReadAverage(int pin, int numberOfSamples) { + int averageValue = 0; + for (int i = 0; i < numberOfSamples; i++) { + averageValue += analogRead(pin); + } + + return (averageValue / numberOfSamples); +} + +void updateSubscribedIMUCharacteristics() { + if (millis() - imuTime > IMU_UPDATE_TIME) { + imuTime = millis(); + if (accelerationCharacteristic.subscribed()) { + if (IMU_SK.accelerationAvailable()) { + float x, y, z; + IMU_SK.readAcceleration(x, y, z); + float acceleration[3]; + + acceleration[0] = x; + acceleration[1] = y; + acceleration[2] = z; + accelerationCharacteristic.writeValue((byte*)acceleration, sizeof(acceleration)); + } + } + + if (gyroscopeCharacteristic.subscribed()) { + if (IMU_SK.gyroscopeAvailable()) { + float x, y, z; + IMU_SK.readGyroscope(x, y, z); + float gyroscope[3]; + + gyroscope[0] = x; + gyroscope[1] =y; + gyroscope[2] = z; + gyroscopeCharacteristic.writeValue((byte*)gyroscope, sizeof(gyroscope)); + } + } + + + if (magneticFieldCharacteristic.subscribed()) { + float x, y, z; + if (BME.magneticFieldAvailable()) { + BME.readMagneticField(x, y, z); + float magneticField[3]; + magneticField[0] = x; + magneticField[1] = y; + magneticField[2] = z; + magneticFieldCharacteristic.writeValue((byte*)magneticField, sizeof(magneticField)); + } + } + } +} \ No newline at end of file diff --git a/examples/ScienceKitR2/PhysicsLabFirmware/config.h b/examples/ScienceKitR2/PhysicsLabFirmware/config.h new file mode 100644 index 0000000..774cf21 --- /dev/null +++ b/examples/ScienceKitR2/PhysicsLabFirmware/config.h @@ -0,0 +1,66 @@ +#include "ArduinoScienceJournal.h" + +#ifndef ARDUINO_ARCH_SAMD +#include +#endif + +// Flash memory +#include "SerialFlash.h" +#include +#include +// IMU +#include + +// BMM150 +#include + +// INA +#include +#include + +LSM6DSOXClass IMU_SK = LSM6DSOXClass(Wire,0x6A); +BoschSensorClass BME = BoschSensorClass(Wire); +const int FlashChipSelect = 2; + +const uint32_t SHUNT_MICRO_OHM{100000}; ///< Shunt resistance in Micro-Ohm, e.g. 100000 is 0.1 Ohm +const uint16_t MAXIMUM_AMPS{1}; ///< Max expected amps, clamped from 1A to a max of 1022A + +INA_Class INA; + +SerialFlashFile file; + +//ResistanceSensorClass ResSens; + +void sensorsInit() { + // INA Init + if (!INA.begin(MAXIMUM_AMPS, SHUNT_MICRO_OHM)) { + Serial.println(F("No INA device found, retrying in 10 seconds...")); + while(1); + } // while no devices detected + Serial.println("INA init success!"); + INA.setBusConversion(8500); // Maximum conversion time 8.244ms + INA.setShuntConversion(8500); // Maximum conversion time 8.244ms + INA.setAveraging(128); // Average each reading n-times + INA.setMode(INA_MODE_CONTINUOUS_BOTH); // Bus/shunt measured continuously + INA.alertOnBusOverVoltage(true, 5000); // Trigger alert if over 5V on bus + + // bmm150 + if (!BME.begin()) { + Serial.println("Failed to initialize IMU!"); + while (1); + } + + // LSM6DSOX init + if (!IMU_SK.begin()) { + Serial.println("Failed to initialize IMU!"); + while (1); + } + Serial.println("IMU Initialized"); + + // Flash Init + if (!SerialFlash.begin(FlashChipSelect)) { + Serial.println(F("Failed to initialize Flash!")); + while (1); + } + Serial.println("Flash Initialized"); +} \ No newline at end of file diff --git a/library.properties b/library.properties index 564cad5..10ee616 100644 --- a/library.properties +++ b/library.properties @@ -8,4 +8,4 @@ category=Communication url=https://github.com/arduino-libraries/Arduino_ScienceJournal architectures=samd,nrf52,mbed,mbed_nano includes=ArduinoScienceJournal.h -depends=Adafruit LSM9DS0 Library,Adafruit Zero PDM Library,Arduino_APDS9960,Arduino_HTS221,Arduino_LPS22HB,Arduino_LSM9DS1,ArduinoBLE,Arduino_CMSIS-DSP,Arduino_LSM6DSOX +depends=Adafruit LSM9DS0 Library,Adafruit Zero PDM Library,Arduino_APDS9960,Arduino_HTS221,Arduino_LPS22HB,Arduino_LSM9DS1,ArduinoBLE,Arduino_CMSIS-DSP,Arduino_LSM6DSOX,SerialFlash,WiFiNINA,INA2xx,Arduino_BMI270_BMM150 diff --git a/src/ArduinoScienceJournal.h b/src/ArduinoScienceJournal.h index de24ed0..7f455db 100644 --- a/src/ArduinoScienceJournal.h +++ b/src/ArduinoScienceJournal.h @@ -20,6 +20,69 @@ #ifndef _ARDUINO_SCIENCE_JOURNAL_H_ #define _ARDUINO_SCIENCE_JOURNAL_H_ -// Nothing here. Everything is in the example sketches! +#include "Arduino.h" + +#ifdef ARDUINO_ARCH_MBED + #include "mbed.h" +#endif + + +#ifdef ARDUINO_ARCH_SAMD + #define IMU_ADDRESS 0x00; + #define VOLTAGE_REF_LEVEL 3300 + #define LIGHT_SENSOR_PIN A5 + #define TEMP_SENSOR_PIN A4 +#else + #define IMU_ADDRESS 0x00; + #define VOLTAGE_REF_LEVEL 1100 + #define LIGHT_SENSOR_PIN A7 + #define TEMP_SENSOR_PIN 6 +#endif + +class ResistorSensorClass { +public: + int getResistorValue() { + int raw_voltage = analogRead(A2); + int voltage_in = (raw_voltage * VOLTAGE_REF_LEVEL / 1024); + int res = ((3300 * 1000) / voltage_in) - 1000; + return res; + } + + int getResistor1KValue() { + digitalWrite(6, HIGH); + int raw_voltage = analogRead(A2); + int voltage_in = (raw_voltage * VOLTAGE_REF_LEVEL / 1024); + int res = ((3300 * (47000*1000/48000)) / voltage_in) - (47000*1000/48000); + digitalWrite(6, LOW); + return res; + } +}; + +extern ResistorSensorClass ResSens; + +class TemperatureSensorClass { +public: + int getTemperature(){ + int raw_voltage = analogRead(TEMP_SENSOR_PIN); + int voltage_in = (raw_voltage * VOLTAGE_REF_LEVEL / 1024); + float temperature = ((voltage_in) - 500.0F) / 10.0F; + return temperature; + } +}; + +extern TemperatureSensorClass TempSens; + + +class LightSensorClass { +public: + int getLux(){ + int raw_voltage = analogRead( LIGHT_SENSOR_PIN); + int voltage_in = (raw_voltage * VOLTAGE_REF_LEVEL / 1024); + float lux = (voltage_in * 0.5F) + 0.5; + return lux; + } +}; + +extern LightSensorClass LightSens; #endif diff --git a/src/Object.cpp b/src/Object.cpp new file mode 100644 index 0000000..e388ca7 --- /dev/null +++ b/src/Object.cpp @@ -0,0 +1,6 @@ +#include "ArduinoScienceJournal.h" + + +ResistorSensorClass ResSens; +TemperatureSensorClass TempSens; +LightSensorClass LightSens;