Skip to content

Commit e95a33e

Browse files
authored
Merge pull request #23 from Rocketct/master
Modified file system for science kit rev 2
2 parents eb73bcb + 6d277b3 commit e95a33e

File tree

19 files changed

+746
-5
lines changed

19 files changed

+746
-5
lines changed

.github/workflows/compile-examples.yml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,45 @@ jobs:
4343
- name: Arduino_LSM9DS1
4444
- name: ArduinoBLE
4545
- name: Arduino_CMSIS-DSP
46-
sketch-paths: examples/Nano33BLESenseFirmware
46+
sketch-paths: examples/ScienceKit/Nano33BLESenseFirmware
4747
- fqbn: arduino:mbed_nano:nanorp2040connect
4848
platforms: |
4949
- name: arduino:mbed_nano
5050
libraries: |
5151
- name: Arduino_LSM6DSOX
5252
- name: ArduinoBLE
53-
sketch-paths: examples/RP2040ConnectFirmware
53+
sketch-paths: examples/ScienceKit/RP2040ConnectFirmware
5454
- fqbn: arduino:samd:mkrwifi1010
5555
platforms: |
5656
- name: arduino:samd
5757
libraries: |
5858
- name: ArduinoBLE
5959
- name: Adafruit LSM9DS1 Library
60-
sketch-paths: examples/PhysicsLabFirmware
60+
sketch-paths: examples/ScienceKit/PhysicsLabFirmware
61+
- fqbn: arduino:mbed_nano:nano33ble
62+
platforms: |
63+
- name: arduino:mbed_nano
64+
libraries: |
65+
- name: Arduino_APDS9960
66+
- name: Arduino_HTS221
67+
- name: Arduino_LPS22HB
68+
- name: Arduino_LSM6DSOX
69+
- name: Arduino_BMI270_BMM150
70+
- name: INA2xx
71+
- name: SerialFlash
72+
- name: ArduinoBLE
73+
- name: Arduino_CMSIS-DSP
74+
sketch-paths: examples/ScienceKitR2/Nano33BLESenseFirmware
75+
- fqbn: arduino:samd:mkrwifi1010
76+
platforms: |
77+
- name: arduino:samd
78+
libraries: |
79+
- name: ArduinoBLE
80+
- name: Arduino_LSM6DSOX
81+
- name: Arduino_BMI270_BMM150
82+
- name: INA2xx
83+
- name: SerialFlash
84+
sketch-paths: examples/ScienceKitR2/PhysicsLabFirmware
6185

6286
steps:
6387
- name: Checkout repository
File renamed without changes.
File renamed without changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "Arduino.h"
2+
#include "mbed.h"
3+
#include "ArduinoBLE.h"
4+
#include "LowPower.h"
5+
6+
#include "nrf_power.h"
7+
#include "nrf_uarte.h"
8+
#include "nrf_uart.h"
9+
10+
void lowPower()
11+
{
12+
// Disable UARTE0 which is initially enabled by the bootloader
13+
nrf_uarte_task_trigger(NRF_UARTE0, NRF_UARTE_TASK_STOPRX);
14+
while (!nrf_uarte_event_check(NRF_UARTE0, NRF_UARTE_EVENT_RXTO)) ;
15+
NRF_UARTE0->ENABLE = 0;
16+
NRF_UART0->ENABLE = 0;
17+
18+
// Enable DCDC
19+
nrf_power_dcdcen_set(true);
20+
21+
// Turn off LED_BUILTIN
22+
digitalWrite(LED_BUILTIN, LOW);
23+
}
24+
25+
void lowPowerWait(unsigned long time)
26+
{
27+
rtos::ThisThread::sleep_for(time);
28+
}
29+
30+
void lowPowerBleWait(unsigned long time)
31+
{
32+
unsigned long timeRef = millis();
33+
while (millis() - timeRef < time) {
34+
BLE.poll(time - (millis() - timeRef));
35+
}
36+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef _LOWPOWER_H_
2+
#define _LOWPOWER_H_
3+
4+
void lowPower();
5+
void lowPowerWait(unsigned long time);
6+
void lowPowerBleWait(unsigned long time);
7+
8+
#endif //_LOWPOWER_H_
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
#include "LowPower.h"
2+
#include <arm_math.h>
3+
4+
#include "config.h"
5+
#include <PDM.h>
6+
7+
#include <ArduinoBLE.h>
8+
9+
const int VERSION = 0x00000001;
10+
11+
#define SCIENCE_KIT_UUID(val) ("555a0002-" val "-467a-9538-01f0652c74e8")
12+
13+
//#define DEBUG 0
14+
15+
BLEService service (SCIENCE_KIT_UUID("0000"));
16+
BLEUnsignedIntCharacteristic versionCharacteristic (SCIENCE_KIT_UUID("0001"), BLERead);
17+
BLECharacteristic accelerationCharacteristic (SCIENCE_KIT_UUID("0011"), BLENotify, 3 * sizeof(float));
18+
BLECharacteristic gyroscopeCharacteristic (SCIENCE_KIT_UUID("0012"), BLENotify, 3 * sizeof(float));
19+
BLECharacteristic magneticFieldCharacteristic(SCIENCE_KIT_UUID("0013"), BLENotify, 3 * sizeof(float));
20+
BLEFloatCharacteristic temperatureCharacteristic (SCIENCE_KIT_UUID("0014"), BLENotify);
21+
BLEFloatCharacteristic pressureCharacteristic (SCIENCE_KIT_UUID("0015"), BLENotify);
22+
BLEFloatCharacteristic humidityCharacteristic (SCIENCE_KIT_UUID("0016"), BLENotify);
23+
BLEUnsignedIntCharacteristic proximityCharacteristic (SCIENCE_KIT_UUID("0017"), BLENotify);
24+
BLECharacteristic colorCharacteristic (SCIENCE_KIT_UUID("0018"), BLENotify, 4 * sizeof(int));
25+
BLEUnsignedShortCharacteristic soundPressureCharacteristic(SCIENCE_KIT_UUID("0019"), BLENotify);
26+
BLEFloatCharacteristic resistanceCharacteristic (SCIENCE_KIT_UUID("0020"), BLENotify);
27+
28+
byte voltageBufferIndex = 0;
29+
bool voltageBufferFilled = false;
30+
short soundSampleBuffer[256];
31+
32+
void updateSubscribedCharacteristics();
33+
34+
void onPDMdata() {
35+
// query the number of bytes available
36+
int bytesAvailable = PDM.available();
37+
38+
// read into the sample buffer
39+
PDM.read(soundSampleBuffer, bytesAvailable);
40+
}
41+
42+
uint16_t getSoundAverage() {
43+
uint32_t avg = 0;
44+
for (int i = 0; i < sizeof(soundSampleBuffer)/sizeof(soundSampleBuffer[0]); i++) {
45+
avg += soundSampleBuffer[i]*soundSampleBuffer[i];
46+
}
47+
return sqrt(avg);
48+
}
49+
50+
// String to calculate the local and device name
51+
String name;
52+
unsigned long lastNotify = 0;
53+
54+
void printSerialMsg(const char * msg) {
55+
#ifdef DEBUG
56+
if (Serial) {
57+
Serial.println(msg);
58+
}
59+
#endif
60+
}
61+
62+
void setup() {
63+
#ifdef DEBUG
64+
Serial.begin(9600);
65+
while (!Serial);
66+
Serial.println("Started");
67+
#endif
68+
69+
delay(2000);
70+
sensorsInit();
71+
72+
PDM.onReceive(onPDMdata);
73+
if (!PDM.begin(1, 16000)) {
74+
printSerialMsg("Failed to start PDM!");
75+
blinkLoop();
76+
}
77+
78+
if (!BLE.begin()) {
79+
printSerialMsg("Failed to initialized BLE!");
80+
blinkLoop();
81+
}
82+
83+
String address = BLE.address();
84+
#ifdef DEBUG
85+
if (Serial) {
86+
Serial.print("address = ");
87+
Serial.println(address);
88+
}
89+
#endif
90+
address.toUpperCase();
91+
92+
name = "BLE Sense - ";
93+
name += address[address.length() - 5];
94+
name += address[address.length() - 4];
95+
name += address[address.length() - 2];
96+
name += address[address.length() - 1];
97+
98+
#ifdef DEBUG
99+
if (Serial) {
100+
Serial.print("name = ");
101+
Serial.println(name);
102+
}
103+
#endif
104+
105+
BLE.setLocalName(name.c_str());
106+
BLE.setDeviceName(name.c_str());
107+
BLE.setAdvertisedService(service);
108+
109+
service.addCharacteristic(versionCharacteristic);
110+
service.addCharacteristic(accelerationCharacteristic);
111+
service.addCharacteristic(gyroscopeCharacteristic);
112+
service.addCharacteristic(magneticFieldCharacteristic);
113+
service.addCharacteristic(temperatureCharacteristic);
114+
service.addCharacteristic(pressureCharacteristic);
115+
service.addCharacteristic(humidityCharacteristic);
116+
service.addCharacteristic(proximityCharacteristic);
117+
service.addCharacteristic(colorCharacteristic);
118+
service.addCharacteristic(soundPressureCharacteristic);
119+
service.addCharacteristic(resistanceCharacteristic);
120+
121+
versionCharacteristic.setValue(VERSION);
122+
123+
BLE.addService(service);
124+
BLE.advertise();
125+
126+
lowPower();
127+
}
128+
129+
void loop() {
130+
BLE.poll(1000);
131+
while (BLE.connected()) {
132+
lowPowerBleWait(100);
133+
updateSubscribedCharacteristics();
134+
}
135+
}
136+
137+
void updateSubscribedCharacteristics() {
138+
if (accelerationCharacteristic.subscribed()) {
139+
if (IMU_SK.accelerationAvailable()) {
140+
float x, y, z;
141+
IMU_SK.readAcceleration(x, y, z);
142+
float acceleration[3];
143+
144+
acceleration[0] = x;
145+
acceleration[1] = y;
146+
acceleration[2] = z;
147+
accelerationCharacteristic.writeValue((byte*)acceleration, sizeof(acceleration));
148+
}
149+
}
150+
if (gyroscopeCharacteristic.subscribed()) {
151+
if (IMU_SK.gyroscopeAvailable()) {
152+
float x, y, z;
153+
IMU_SK.readGyroscope(x, y, z);
154+
float gyroscope[3];
155+
156+
gyroscope[0] = x;
157+
gyroscope[1] =y;
158+
gyroscope[2] = z;
159+
gyroscopeCharacteristic.writeValue((byte*)gyroscope, sizeof(gyroscope));
160+
}
161+
}
162+
163+
if (magneticFieldCharacteristic.subscribed()) {
164+
float x, y, z;
165+
if (BME.magneticFieldAvailable()) {
166+
BME.readMagneticField(x, y, z);
167+
float magneticField[3];
168+
magneticField[0] = x;
169+
magneticField[1] = y;
170+
magneticField[2] = z;
171+
magneticFieldCharacteristic.writeValue((byte*)magneticField, sizeof(magneticField));
172+
}
173+
}
174+
175+
if (temperatureCharacteristic.subscribed()) {
176+
float temperature = TempSens.getTemperature();
177+
temperatureCharacteristic.writeValue(temperature);
178+
}
179+
180+
181+
if(resistanceCharacteristic.subscribed()){
182+
float resistanceAvg = INFINITY;
183+
resistanceAvg = ResSens.getResistorValue();
184+
resistanceCharacteristic.writeValue(resistanceAvg);
185+
}
186+
187+
if (proximityCharacteristic.subscribed() && APDS.proximityAvailable()) {
188+
uint32_t proximity = APDS.readProximity();
189+
proximityCharacteristic.writeValue(proximity);
190+
}
191+
if (colorCharacteristic.subscribed() && APDS.colorAvailable()) {
192+
int color[4];
193+
APDS.readColor(color[0], color[1], color[2], color[3]);
194+
colorCharacteristic.writeValue((byte*)color, sizeof(color));
195+
}
196+
197+
if (pressureCharacteristic.subscribed()) {
198+
float pressure = BARO.readPressure();
199+
pressureCharacteristic.writeValue(pressure);
200+
}
201+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include "ArduinoScienceJournal.h"
2+
3+
// Flash memory
4+
#include "SerialFlash.h"
5+
#include <SPI.h>
6+
7+
const int FlashChipSelect = 2;
8+
9+
#include <Arduino_APDS9960.h>
10+
#include <Arduino_HTS221.h>
11+
#include <Arduino_LPS22HB.h>
12+
13+
// IMU
14+
#include <Arduino_LSM6DSOX.h>
15+
// 3 axis magnetometer
16+
#include <BoschSensorClass.h>
17+
// INA
18+
#include <INA.h>
19+
#include <avr/dtostrf.h>
20+
21+
22+
LSM6DSOXClass IMU_SK = LSM6DSOXClass(Wire,0x6A);
23+
BoschSensorClass BME = BoschSensorClass(Wire);
24+
25+
const uint32_t SHUNT_MICRO_OHM{100000}; ///< Shunt resistance in Micro-Ohm, e.g. 100000 is 0.1 Ohm
26+
const uint16_t MAXIMUM_AMPS{1}; ///< Max expected amps, clamped from 1A to a max of 1022A
27+
28+
INA_Class INA;
29+
30+
SerialFlashFile file;
31+
32+
void blinkLoop() {
33+
while (1) {
34+
digitalWrite(LED_BUILTIN, HIGH);
35+
delay(500);
36+
digitalWrite(LED_BUILTIN, LOW);
37+
delay(500);
38+
}
39+
}
40+
41+
void sensorsInit() {
42+
// INA Init
43+
if (!INA.begin(MAXIMUM_AMPS, SHUNT_MICRO_OHM)) {
44+
Serial.println(F("No INA device found, retrying in 10 seconds..."));
45+
while(1);
46+
} // while no devices detected
47+
Serial.println("INA init success!");
48+
INA.setBusConversion(8500); // Maximum conversion time 8.244ms
49+
INA.setShuntConversion(8500); // Maximum conversion time 8.244ms
50+
INA.setAveraging(128); // Average each reading n-times
51+
INA.setMode(INA_MODE_CONTINUOUS_BOTH); // Bus/shunt measured continuously
52+
INA.alertOnBusOverVoltage(true, 5000); // Trigger alert if over 5V on bus
53+
54+
// bmm150
55+
if (!BME.begin()) {
56+
Serial.println("Failed to initialize IMU!");
57+
while (1);
58+
}
59+
60+
// LSM6DSOX init
61+
if (!IMU_SK.begin()) {
62+
Serial.println("Failed to initialize IMU!");
63+
while (1);
64+
}
65+
Serial.println("IMU Initialized");
66+
67+
// Flash Init
68+
if (!SerialFlash.begin(FlashChipSelect)) {
69+
Serial.println(F("Failed to initialize Flash!"));
70+
while (1);
71+
}
72+
73+
Serial.println("Flash Initialized");
74+
75+
if (!APDS.begin()) {
76+
Serial.println("Failed to initialized APDS!");
77+
blinkLoop();
78+
}
79+
80+
if (!HTS.begin()) {
81+
Serial.println("Failed to initialized HTS!");
82+
blinkLoop();
83+
}
84+
85+
if (!BARO.begin()) {
86+
Serial.println("Failed to initialized BARO!");
87+
blinkLoop();
88+
}
89+
}
Binary file not shown.

0 commit comments

Comments
 (0)