Skip to content

Commit cc5ed7a

Browse files
committed
added Support for ScienceKit rev 2
added Support for rScienceKit rev 2 and modified .github actions
1 parent c7a1b55 commit cc5ed7a

File tree

11 files changed

+758
-2
lines changed

11 files changed

+758
-2
lines changed

.github/workflows/compile-examples.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,30 @@ jobs:
5858
- name: ArduinoBLE
5959
- name: Adafruit LSM9DS1 Library
6060
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: DFRobot_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: DFRobot_BMM150
82+
- name: INA2xx
83+
- name: SerialFlash
84+
sketch-paths: examples/ScienceKitR2/PhysicsLabFirmware
6185

6286
steps:
6387
- name: Checkout repository
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: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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.accelerationAvailable()) {
140+
float x, y, z;
141+
IMU.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.gyroscopeAvailable()) {
152+
float x, y, z;
153+
IMU.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()) {// CHECK
164+
sBmm150MagData_t m = bmm150.getGeomagneticData();
165+
float magneticField[3];
166+
magneticField[0] = m.x;
167+
magneticField[1] = m.y;
168+
magneticField[2] = m.z;
169+
magneticFieldCharacteristic.writeValue((byte*)magneticField, sizeof(magneticField));
170+
}
171+
172+
if (temperatureCharacteristic.subscribed()) {
173+
float temperature = TempSens.getTemperature();
174+
temperatureCharacteristic.writeValue(temperature);
175+
}
176+
177+
178+
if(resistanceCharacteristic.subscribed()){
179+
float resistanceAvg = INFINITY;
180+
resistanceAvg = ResSens.getResistorValue();
181+
resistanceCharacteristic.writeValue(resistanceAvg);
182+
}
183+
184+
if (proximityCharacteristic.subscribed() && APDS.proximityAvailable()) {
185+
uint32_t proximity = APDS.readProximity();
186+
proximityCharacteristic.writeValue(proximity);
187+
}
188+
if (colorCharacteristic.subscribed() && APDS.colorAvailable()) {
189+
int color[4];
190+
APDS.readColor(color[0], color[1], color[2], color[3]);
191+
colorCharacteristic.writeValue((byte*)color, sizeof(color));
192+
}
193+
194+
if (pressureCharacteristic.subscribed()) {
195+
float pressure = BARO.readPressure();
196+
pressureCharacteristic.writeValue(pressure);
197+
}
198+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 "DFRobot_BMM150.h"
17+
// INA
18+
#include <INA.h>
19+
#include <avr/dtostrf.h>
20+
21+
22+
DFRobot_BMM150_I2C bmm150(&Wire, I2C_ADDRESS_1);
23+
24+
const uint32_t SHUNT_MICRO_OHM{100000}; ///< Shunt resistance in Micro-Ohm, e.g. 100000 is 0.1 Ohm
25+
const uint16_t MAXIMUM_AMPS{1}; ///< Max expected amps, clamped from 1A to a max of 1022A
26+
27+
INA_Class INA;
28+
29+
SerialFlashFile file;
30+
31+
void blinkLoop() {
32+
while (1) {
33+
digitalWrite(LED_BUILTIN, HIGH);
34+
delay(500);
35+
digitalWrite(LED_BUILTIN, LOW);
36+
delay(500);
37+
}
38+
}
39+
40+
void sensorsInit() {
41+
// INA Init
42+
if (!INA.begin(MAXIMUM_AMPS, SHUNT_MICRO_OHM)) {
43+
Serial.println(F("No INA device found, retrying in 10 seconds..."));
44+
while(1);
45+
} // while no devices detected
46+
Serial.println("INA init success!");
47+
INA.setBusConversion(8500); // Maximum conversion time 8.244ms
48+
INA.setShuntConversion(8500); // Maximum conversion time 8.244ms
49+
INA.setAveraging(128); // Average each reading n-times
50+
INA.setMode(INA_MODE_CONTINUOUS_BOTH); // Bus/shunt measured continuously
51+
INA.alertOnBusOverVoltage(true, 5000); // Trigger alert if over 5V on bus
52+
53+
// bmm150
54+
while(bmm150.begin()){
55+
Serial.println("bmm150 init failed, Please try again!");
56+
delay(1000);
57+
} Serial.println("bmm150 init success!");
58+
Serial.println("bmm150 init success!");
59+
60+
// BMM150 init
61+
// Set sensor operation mode
62+
bmm150.setOperationMode(BMM150_POWERMODE_NORMAL);
63+
//Set preset mode
64+
bmm150.setPresetMode(BMM150_PRESETMODE_HIGHACCURACY);
65+
// Set the rate of obtaining geomagnetic data, the higher, the faster (without delay function)
66+
bmm150.setRate(BMM150_DATA_RATE_10HZ);
67+
// Enable the measurement at x-axis, y-axis and z-axis,
68+
bmm150.setMeasurementXYZ();
69+
70+
// LSM6DSOX init
71+
if (!IMU.begin()) {
72+
Serial.println("Failed to initialize IMU!");
73+
while (1);
74+
}
75+
Serial.println("IMU Initialized");
76+
77+
// Flash Init
78+
if (!SerialFlash.begin(FlashChipSelect)) {
79+
Serial.println(F("Failed to initialize Flash!"));
80+
while (1);
81+
}
82+
83+
Serial.println("Flash Initialized");
84+
85+
if (!APDS.begin()) {
86+
Serial.println("Failed to initialized APDS!");
87+
blinkLoop();
88+
}
89+
90+
if (!HTS.begin()) {
91+
Serial.println("Failed to initialized HTS!");
92+
blinkLoop();
93+
}
94+
95+
if (!BARO.begin()) {
96+
Serial.println("Failed to initialized BARO!");
97+
blinkLoop();
98+
}
99+
}
Binary file not shown.

0 commit comments

Comments
 (0)