Skip to content

Modified file system for science kit rev 2 #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions examples/ScienceKitR2/Nano33BLESenseFirmware/LowPower.cpp
Original file line number Diff line number Diff line change
@@ -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));
}
}
8 changes: 8 additions & 0 deletions examples/ScienceKitR2/Nano33BLESenseFirmware/LowPower.h
Original file line number Diff line number Diff line change
@@ -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_
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
#include "LowPower.h"
#include <arm_math.h>

#include "config.h"
#include <PDM.h>

#include <ArduinoBLE.h>

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);
}
}
89 changes: 89 additions & 0 deletions examples/ScienceKitR2/Nano33BLESenseFirmware/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "ArduinoScienceJournal.h"

// Flash memory
#include "SerialFlash.h"
#include <SPI.h>

const int FlashChipSelect = 2;

#include <Arduino_APDS9960.h>
#include <Arduino_HTS221.h>
#include <Arduino_LPS22HB.h>

// IMU
#include <Arduino_LSM6DSOX.h>
// 3 axis magnetometer
#include <BoschSensorClass.h>
// INA
#include <INA.h>
#include <avr/dtostrf.h>


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();
}
}
Binary file not shown.
Loading