Hello All,
My query is a bit straight forward.
I have multiple sensors (MPU, GPS, Pressure, Heart rate sensor) and I am accessing them from different fucntions.
Now, I want all the sensors to combine in a data structure and read the data from different sensors all at once.
Below is the code where I have written seperate sensor function in loop and reading them one at a time.
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <SimpleTimer.h>
#include "MAX30100_PulseOximeter.h"
#define REPORTING_PERIOD_MS 500
PulseOximeter pox;
const int MPU_addr = 0x68; // I2C address of the MPU-6050
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
SimpleTimer timer;
static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial GPS(RXPin, TXPin);
int fsrPin = A0; // the FSR and 10K pulldown are connected to a0
int fsrReading; // the analog reading from the FSR resistor divider
const int numReadings=10;
float filterweight=0.5;
uint32_t tsLastReport = 0;
uint32_t last_beat=0;
int readIndex=0;
int average_beat=0;
int average_SpO2=0;
bool calculation_complete=false;
bool calculating=false;
bool initialized=false;
byte beat=0;
void setup() {
Serial.begin(9600);
GPS.begin(GPSBaud);
pox.begin();
pox.setOnBeatDetectedCallback(onBeatDetected);
initial_display();
}
void loop() {
// put your main code here, to run repeatedly:
GPSfunc();
Pressurefunc();
MPUfunc();
Heartratefunc();
}
void GPSfunc(){
while (GPS.available() > 0){
gps.encode(GPS.read());
if (gps.location.isUpdated()){
// Latitude in degrees (double)
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
// Longitude in degrees (double)
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
// Year (2000+) (u16)
Serial.print("Year = ");
Serial.println(gps.date.year());
// Month (1-12) (u8)
Serial.print("Month = ");
Serial.println(gps.date.month());
// Day (1-31) (u8)
Serial.print("Day = ");
Serial.println(gps.date.day());
// Raw time in HHMMSSCC format (u32)
Serial.print("Raw time in HHMMSSCC = ");
Serial.println(gps.time.value());
}
}
}
void Pressurefunc(){
fsrReading = analogRead(fsrPin);
Serial.print("Analog reading = ");
Serial.print(fsrReading); // the raw analog reading
}
void GetValues()
{
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr, 14, true); // request a total of 14 registers
AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
}
void MPUfunc(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
timer.setInterval(200L, GetValues);
timer.run(); //to loop the reading
}
void onBeatDetected()
{
// show_beat();
last_beat=millis();
}
void display_calculating(int j)
{
if (not calculating) {
calculating=true;
initialized=false;
}
Serial.print(". ");
}
void display_values()
{
Serial.println("");
Serial.print(average_beat);
Serial.println(" Bpm");
Serial.print("SpO2 ");
Serial.print(average_SpO2);
Serial.println("%");
}
void initial_display()
{
if (not initialized)
{
Serial.print("Place finger on the sensor");
initialized=true;
}
}
void calculate_average(int beat, int SpO2)
{
if (readIndex==numReadings) {
calculation_complete=true;
calculating=false;
initialized=false;
readIndex=0;
display_values();
}
if (not calculation_complete and beat>30 and beat<220 and SpO2>50) {
average_beat = filterweight * (beat) + (1 - filterweight ) * average_beat;
average_SpO2 = filterweight * (SpO2) + (1 - filterweight ) * average_SpO2;
readIndex++;
display_calculating(readIndex);
}
}
void Heartratefunc(){
pox.update();
if ((millis() - tsLastReport > REPORTING_PERIOD_MS) and (not calculation_complete)) {
calculate_average(pox.getHeartRate(),pox.getSpO2());
tsLastReport = millis();
}
if ((millis()-last_beat>10000)) {
calculation_complete=false;
average_beat=0;
average_SpO2=0;
initial_display();
}
}
]
Now, I want to read all the sensor data al at once and compute for next level of processing (kalman computation)
Here is the structure I have declared.
#define SENSORMAX 100
typedef short int16;
typedef struct sensorData {
int16 accelX;
int16 accelY;
int16 accelZ;
int16 gyrpX;
int16 gyroY;
int16 gyroZ;
float temperature;
float lat;
float long;
float heartrate;
float pressure;
} SENSORDATA;
SENSORDATA sensorFusion[SENSORMAX];
int currIndex = 0; //Current index into sensorFusion
int dataCount = 0; //Number of valid sensor data in sensorFusion
So, how can I pass the serial data to the struct type variable so that I can read the data elements all at once?
Please Help!