Najib Kassabsuzan na
Published © GPL3+

Step-counting bracelet

Wearable step-counting using Hexabitz modules.

IntermediateFull instructions provided1 hour206
Step-counting bracelet

Things used in this project

Story

Read more

Code

the code file

C/C++
this is the IMU code file
/**
  ******************************************************************************
  * File Name          : main.c
  * Description        : Main program body
  ******************************************************************************
  *
  * COPYRIGHT(c) 2015 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
	
/*
		MODIFIED by Hexabitz for BitzOS (BOS) V0.1.6 - Copyright (C) 2017-2019 Hexabitz
    All rights reserved
*/

/* Includes ------------------------------------------------------------------*/
#include "BOS.h"


/* Private variables ---------------------------------------------------------*/


/* Private function prototypes -----------------------------------------------*/



/* Main functions ------------------------------------------------------------*/

int main(void)
{


  /* MCU Configuration----------------------------------------------------------*/

  /* Reset all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all user peripherals */

	/* Initialize BitzOS */
	BOS_Init();

  /* Call init function for freertos objects (in freertos.c) */
  MX_FREERTOS_Init();

  /* Start scheduler */
  osKernelStart();
  
  /* We should never get here as control is now taken by the scheduler */

  /* Infinite loop */
  while (1)
  {


  }


}

/*-----------------------------------------------------------*/

/* User Task */

void UserTask(void * argument)
{
	float threshold =	2;
	
	float xval;
	float yval;
	float zval;
	float xavg;
	float yavg;
	float zavg;
  int x=0;
	float vector;
	float vectorLastValue=0.0;
	float average;
	
	long steps=0;
  int oneTimeTaskFlag=0;
	BOS.trace=BOS_RESPONSE_NONE;
  /* Infinite loop */
  for(;;)
  {
		/*
			Calibration:
			By getting the average values for 100 sample of each accelerometer sensor axis.
			This step run just for one time when powering the module.
		*/
		if(oneTimeTaskFlag==0)
		{
			float sum=0;
			float sum1=0;
			float sum2=0;
			for (int i=0;i<100;i++)
			{
				SampleAccG(&xval, &yval, &zval);
				sum=xval+sum;
				sum1=  yval+sum1;
				sum2=zval+sum2;
			}
			xavg=sum/100.0;
			yavg=sum1/100.0;
			zavg=sum2/100.0;
			oneTimeTaskFlag = 1;
		}
		
		/*
			Getting x, y and z axis value and find how each axis is far from average value.
			By calculate the total acceleration vector with respect to average value
			vector=SQRT((x-xavg)^2+(y-yavg)^2+(z-zavg)^2)

		*/
		SampleAccG(&xval, &yval, &zval);
		vector = sqrt(((xval-xavg)* (xval-xavg))+ ((yval- yavg)*(yval-yavg)) + ((zval-zavg)*(zval-zavg)));
		Delay_ms(1);
		
		//Find the average value between current vector and last vector.
		average = (vector + vectorLastValue)/2;
		vectorLastValue = vector;
 
		if(average>threshold )
		{
        messageParams[0]=1;
        messageParams[1]=200;
        messageParams[2]=0;
        messageParams[3]=0;
        messageParams[4]=50;
			//Make indicator LED blink every step
			//Module 2
			if((steps>=0) && (steps<49))
			{
				SendMessageToModule(2, 103, 5);
				Delay_ms(50);
				SendMessageToModule(2, 101, 0);
			}
			
			//Module 3
			else if((steps>=51) && (steps<99))
			{
				SendMessageToModule(3, 103, 5);
				Delay_ms(50);
				SendMessageToModule(3, 101, 0);
			}
			
			//Module 4
			else if((steps>=101) && (steps<149))
			{
				SendMessageToModule(4, 103, 5);
				Delay_ms(50);
				SendMessageToModule(4, 101, 0);
			}
			
			//Module 5
      else if((steps>=151) && (steps<199))
			{
				SendMessageToModule(5, 103, 5);
				Delay_ms(50);
				SendMessageToModule(5, 101, 0);
			}
		

			steps=steps+1;
		}
			  Delay_ms(2);
        messageParams[0]=1;
        messageParams[1]=200;
        messageParams[2]=0;
        messageParams[3]=0;
        messageParams[4]=50;
		//Turn on module x every 100 steps
		switch (steps)
		{
			case 50:
				SendMessageToModule(2, 103, 5);
				break;
			case 100:
				SendMessageToModule(3, 103, 5);
				break;
			case 150:
				SendMessageToModule(4, 103, 5);
				break;
			case 200:
				SendMessageToModule(5, 103, 5);
				break;
		}
		if(steps>= 201)
		{	
      SendMessageToModule(BOS_BROADCAST, 101,0);
			steps=0;
      x=x+50;
      if(x>155)
      {
        x=0;
      }
		}	
	}
}

/*-----------------------------------------------------------*/

/************************ (C) COPYRIGHT HEXABITZ *****END OF FILE****/

RGB LED Module H01R0

RGB LED Module H01R0

Credits

Najib Kassab

Najib Kassab

5 projects • 3 followers
suzan na

suzan na

1 project • 0 followers

Comments