Skip to content
Snippets Groups Projects
Commit 40416336 authored by Robert Cheetham's avatar Robert Cheetham
Browse files

Signed-off-by: Robert Cheetham <rec1g18@soton.ac.uk>

parent 75b31124
No related branches found
No related tags found
No related merge requests found
#ifndef STEPCOUNTER_HPP
#define STEPCOUNTER_HPP
#define ADXL375_DEBUG_PRINT /* If defined it will send ADXL375 x data trough UART */
//#define ADXL375_BOOL_OUTPUT /* Used if a boolean change is needed in stead of interger
#define _robADXL375_setup setup
#define _robADXL375_reading loop
#include <Wire.h> // Wire library - used for I2C communication
#include <Arduino.h>
#define ADXL375 0x53 // The ADXL375 sensor I2C address
void _robADXL375_step();
void _robADXL375_setup();
void _robADXL375_reading();
#endif
\ No newline at end of file
#include <Arduino.h> //
#include<Wire.h> // Base I2C code for interaction taken from
#include <math.h> // https://howtomechatronics.com/tutorials/arduino/how-to-track-orientation-with-arduino-and-adxl345-accelerometer/
//
// And then adapted to work with ADXL375
//
//
// Step counter sends an interger number of steps.
//
// This can be changed to a boolean by defining ADXL375_BOOL_OUTPUT in header. However not sure what is preferred.
//
// In order to full embed comment out #define _robADXL375_setup setup
// #define _robADXL375_reading loop Rob xx
//
#include "StepCounter.hpp"
const int MPU_addr=0x68; // I2C address of the MPU-6050 int16_t xRaw; // Outputs
int16_t xRawMin=100;
int16_t xRawMax=0;
int16_t AcX,AcY,AcZ; // These are the variables for reading the raw accelerometer value from the device. int steps=0; // Counts number of punches . Not needed in final.
double rRaw,previousR,previousPreviousR; // The resultant sqrt((x^2)+(y^2)+(z^2)) and the previous ones. #ifdef ADXL375_BOOL_OUTPUT
double total; // The total weighted as a low pass filter. Half current and quarter previous two. bool step =false;
#endif
double maxTotal,minTotal; // The max and minimun in the current session.
int steps = 0; // Number of steps. bool below=false; // Determines if a full pulse has occurred
bool below; // Boolean to determine if the value has gone 10000 less than the maximum in that period. bool above =false;
unsigned long lastTimeRead =0; // Used to determine how long until next reading
void _robADXL375_step(){ // I imagine this part will be where the bluetooth implemention is?
void step(){ // I imagine this part will be where the bluetooth implemention is?
steps++; steps++;
} #ifdef ADXL375_BOOL_OUTPUT
step =true;
void setup(){ #endif
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);
Serial.begin(9600);
previousR=14088.24; // To make sure the starting values arnt offset.
previousPreviousR=14088.24; // These are the common values.
below=true;
maxTotal=2000; // Will obviously be overwritten in first instance.
minTotal=50000;
} }
void loop(){
unsigned long now = millis(); // Checks current time
if ((now-lastTimeRead)>5) // Checks to see if enough time has elapsed (will reduce with ne sensor) void _robADXL375_setup() {
{ Serial.begin(115200); // Initiate serial communication for printing the results on the Serial monitor
Wire.beginTransmission(MPU_addr); // Reads accelerometer Wire.begin(); // Initiate the Wire library
Wire.write(0x3B); // // Set ADXL375 in measuring mode
Wire.beginTransmission(ADXL375); // Start communicating with the device
Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
// Enable measurement
Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable
Wire.endTransmission();
delay(10);
}
void _robADXL375_reading() {
// === Read acceleromter data === //
Wire.beginTransmission(ADXL375);
Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H)
Wire.endTransmission(false); Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // Wire.requestFrom(ADXL375, 2, true); // Read 2 registers xRaw,
AcX=Wire.read()<<8|Wire.read(); // xRaw = ( Wire.read()| Wire.read() << 8); // X-axis value
AcY=Wire.read()<<8|Wire.read(); //
AcZ=Wire.read()<<8|Wire.read(); //
rRaw=sqrt(pow(AcX,2)+pow(AcY,2)+pow(AcZ,2)); // The resultant of each axis xRawMin=min(xRawMin,xRaw); // Overrides min/max
total= 0.5*rRaw+0.25*previousR+0.25*previousPreviousR; // The curved amount xRawMax=max(xRawMax,xRaw);
if ((below)&&(total-minTotal>=10000)&&(total>=30000)){ // Make sure above a certain value and has increased by a certain amount. if ((below)&&(xRaw>=70)&&(xRaw-xRawMin>=30)){ // Make sure above a certain value and has increased by a certain amount.
step(); // These will be altered. above=true;
below=false; below=false;
minTotal=total; // Resets minmum. xRawMin=xRaw;
} }
minTotal=min(minTotal,total); // Overrides min/max if (xRawMax-xRaw>=30&&above){ // Ensures value has gone back below limit
maxTotal=max(maxTotal,total); _robADXL375_step();
if (maxTotal-total>=10000){ // Checks has dropped by 10000
maxTotal=total;
below=true; below=true;
above=false;
xRawMax=xRaw;
} }
if (xRawMax-xRaw>=30){ // Ensures value is below a limit.
lastTimeRead=now; // Resets to current amounts below=true;
previousPreviousR=previousR; xRawMax=xRaw;
previousR=rRaw; #ifdef ADXL375_BOOL_OUTPUT
step =false;
Serial.print("\n"); // Just prints steps, obviously remove. #endif
Serial.print(steps);Serial.print(",");
} }
delay(5); #ifdef ADXL375_DEBUG_PRINT
Serial.print(xRaw);Serial.print(","); // Debug printing
Serial.println(steps);
#endif
delay(10);
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment