Skip to content
This repository was archived by the owner on Oct 4, 2021. It is now read-only.
Merged
Changes from 3 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
43 changes: 29 additions & 14 deletions examples/PhysicsLabFirmware/PhysicsLabFirmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <ArduinoBLE.h>
#include <MKRIMU.h>
#include <ArduinoBLE.h> // click here to install the library: http://librarymanager#ArduinoBLE
#include <Adafruit_LSM9DS1.h> // click here to install the library: http://librarymanager#Adafruit&LSM9DS1
#include <Adafruit_Sensor.h> // click here to install the library: http://librarymanager#Adafruit&unified&Sensor&abstraction

#include "INA226.h"

Expand Down Expand Up @@ -46,7 +47,7 @@ const int INPUT3_PIN = A0;
const int OUTPUT1_PIN = 5;
const int OUTPUT2_PIN = 1;
const int RESISTANCE_PIN = A2;
const int RESISTANCE_AUX_PIN = 13;
const int RESISTANCE_AUX_PIN = 8;

String name;
unsigned long lastNotify = 0;
Expand All @@ -56,6 +57,8 @@ unsigned long lastNotify = 0;

//#define DEBUG //uncomment to debug the code :)

Adafruit_LSM9DS1 imu = Adafruit_LSM9DS1();

void setup() {
Serial.begin(9600);
#ifdef DEBUG
Expand All @@ -79,12 +82,16 @@ void setup() {
while (1);
}

if (!IMU.begin()) {
if (!imu.begin()) {
Serial.println("Failled to initialized IMU!");

while (1);
}

imu.setupAccel(imu.LSM9DS1_ACCELRANGE_2G);
imu.setupMag(imu.LSM9DS1_MAGGAIN_4GAUSS);
imu.setupGyro(imu.LSM9DS1_GYROSCALE_245DPS);

if (!BLE.begin()) {
Serial.println("Failled to initialized BLE!");

Expand Down Expand Up @@ -213,7 +220,7 @@ void updateSubscribedCharacteristics() {
} else if ((resistanceAuxHigh == INFINITY) && (resistanceAuxLow != INFINITY)) {
resistanceAvg = resistanceAuxLow;
}

#ifdef DEBUG
Serial.print("Resistance (AVG): ");
Serial.print(resistanceAvg);
Expand Down Expand Up @@ -248,27 +255,35 @@ int analogReadAverage(int pin, int numberOfSamples) {
}

void updateSubscribedIMUCharacteristics() {

imu.read();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@facchinm is this throttled to wait for a new sample?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, should we throttle it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!

The MKRIMU lib call's to available() return the data at 10 Hz (from what I remember). This has two purposes, limited BLE bandwidth and reduces the load on the mobile phone app.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense; @agdl can you take a look at this maybe?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at last commit and let me know if this is ok!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can do better and look at the datasheet of the part ... and see if the Adafruit lib is using it properly.

sensors_event_t a, m, g, temp;
imu.getEvent(&a, &m, &g, &temp);

if (accelerationCharacteristic.subscribed()) {
float acceleration[3];

if (IMU.accelerationAvailable() && IMU.readAcceleration(acceleration[0], acceleration[1], acceleration[2])) {
accelerationCharacteristic.writeValue((byte*)acceleration, sizeof(acceleration));
}
acceleration[0] = a.acceleration.x;
acceleration[1] = a.acceleration.y;
acceleration[2] = a.acceleration.z;
accelerationCharacteristic.writeValue((byte*)acceleration, sizeof(acceleration));
}

if (gyroscopeCharacteristic.subscribed()) {
float gyroscope[3];

if (IMU.gyroscopeAvailable() && IMU.readGyroscope(gyroscope[0], gyroscope[1], gyroscope[2])) {
gyroscopeCharacteristic.writeValue((byte*)gyroscope, sizeof(gyroscope));
}
gyroscope[0] = g.gyro.x;
gyroscope[1] = g.gyro.y;
gyroscope[2] = g.gyro.z;
gyroscopeCharacteristic.writeValue((byte*)gyroscope, sizeof(gyroscope));
}

if (magneticFieldCharacteristic.subscribed()) {
float magneticField[3];

if (IMU.magneticFieldAvailable() && IMU.readMagneticField(magneticField[0], magneticField[1], magneticField[2])) {
magneticFieldCharacteristic.writeValue((byte*)magneticField, sizeof(magneticField));
}
magneticField[0] = m.magnetic.x;
magneticField[1] = m.magnetic.y;
magneticField[2] = m.magnetic.z;
magneticFieldCharacteristic.writeValue((byte*)magneticField, sizeof(magneticField));
}
}