uint16_tAccX,AccY,AccZ;// returned by BSP as 10-bit numbers
// *********Task1_Init*********
// initializes accelerometer
// Task1 counts Steps
// Inputs: none
// Outputs: none
voidTask1_Init(void){
BSP_Accelerometer_Init();
// initialize the exponential weighted moving average filter
BSP_Accelerometer_Input(&AccX,&AccY,&AccZ);
Magnitude=sqrt32(AccX*AccX+AccY*AccY+AccZ*AccZ);
EWMA=Magnitude;// this is a guess; there are many options
Steps=0;
}
// *********Task1*********
// collects data from accelerometer
// sends data to Task2
// Inputs: none
// Outputs: none
voidTask1(void){uint32_tsquared;
TExaS_Task1();// records system time in array, toggles virtual logic analyzer
Profile_Toggle1();// viewed by a real logic analyzer to know Task1 started
BSP_Accelerometer_Input(&AccX,&AccY,&AccZ);
squared=AccX*AccX+AccY*AccY+AccZ*AccZ;
OS_MailBox_Send(squared);// makes Task2 run every 100ms
Time++;// in 100ms units
}
/* ****************************************** */
/* End of Task1 Section */
/* ****************************************** */
//---------------- Task2 calculates steps and plots data on LCD ----------------
// Main thread scheduled by OS round robin preemptive scheduler
// accepts data from accelerometer, calculates steps, plots on LCD, and output to LED
// If no data are lost, the main loop in Task2 runs exactly at 10 Hz, but not in real time
enumstate{// the step counting algorithm cycles through four states
LookingForMax,// looking for a local maximum in current magnitude
LookingForCross1,// looking for current magnitude to cross average magnitude, minus a constant
LookingForMin,// looking for a local minimum in current magnitude
LookingForCross2// looking for current magnitude to cross average magnitude, plus a constant
};
enumstateAlgorithmState=LookingForMax;
#define LOCALCOUNTTARGET 5 // The number of valid measured magnitudes needed to confirm a local min or local max. Increase this number for longer strides or more frequent measurements.
#define AVGOVERSHOOT 25 // The amount above or below average a measurement must be to count as "crossing" the average. Increase this number to reject increasingly hard shaking as steps.
#define ACCELERATION_MAX 1400
#define ACCELERATION_MIN 600
#define ALPHA 128 // The degree of weighting decrease, a constant smoothing factor between 0 and 1,023. A higher ALPHA discounts older observations faster.
// basic step counting algorithm is based on a forum post from