+ All Categories
Home > Documents > Final_Report (1)

Final_Report (1)

Date post: 19-Jan-2017
Category:
Upload: nouran-nouh
View: 24 times
Download: 0 times
Share this document with a friend
45
Carleton University Department of Electronics ELEC 3907 - Team Mike A Smart Fitness Trainer Authors: Uzair Ahmad Swaiba Khan Sandy Elamin Nouran Nouh 100956310 100950570 100921260 100950771 Thursday 21 st April, 2016
Transcript
Page 1: Final_Report (1)

Carleton University

Department of Electronics

ELEC 3907 - Team Mike

A Smart Fitness Trainer

Authors:

Uzair Ahmad Swaiba Khan Sandy Elamin Nouran Nouh100956310 100950570 100921260 100950771

Thursday 21st April, 2016

Page 2: Final_Report (1)

Contents

1 Introduction 3

2 Measuring Force using an Accelerometer 4

3 The Balancing Mat 93.1 The Design Process . . . . . . . . . . . . . . . . . . . . . . . . 103.2 The Testing Process . . . . . . . . . . . . . . . . . . . . . . . 14

4 Liquid Crystal Display 194.1 LCD in the project . . . . . . . . . . . . . . . . . . . . . . . . 194.2 LCD and JeeNode . . . . . . . . . . . . . . . . . . . . . . . . 194.3 Testing of the LCD . . . . . . . . . . . . . . . . . . . . . . . . 21

5 Software Components 255.1 Technical Analysis . . . . . . . . . . . . . . . . . . . . . . . . 26

5.1.1 Wireless Communication . . . . . . . . . . . . . . . . . 265.1.2 Sensor Calibration . . . . . . . . . . . . . . . . . . . . 275.1.3 Measuring Force . . . . . . . . . . . . . . . . . . . . . 275.1.4 Measuring Speed . . . . . . . . . . . . . . . . . . . . . 27

5.2 Design and Implementation: Arduino Uno . . . . . . . . . . . 285.2.1 Data Serialization . . . . . . . . . . . . . . . . . . . . . 285.2.2 Sensor Calibration . . . . . . . . . . . . . . . . . . . . 305.2.3 Punch Rating . . . . . . . . . . . . . . . . . . . . . . . 305.2.4 Punching Speed . . . . . . . . . . . . . . . . . . . . . . 305.2.5 Integration . . . . . . . . . . . . . . . . . . . . . . . . 30

5.3 Design and Implementation: Android App . . . . . . . . . . . 315.3.1 App Design . . . . . . . . . . . . . . . . . . . . . . . . 315.3.2 App Functionality . . . . . . . . . . . . . . . . . . . . 32

5.4 Testing and Evaluation . . . . . . . . . . . . . . . . . . . . . . 32

6 Final Specifications 336.1 The Punching Bag . . . . . . . . . . . . . . . . . . . . . . . . 336.2 The Android Application . . . . . . . . . . . . . . . . . . . . . 34

7 Conclusion and Future Work 35

8 References 36

1

Page 3: Final_Report (1)

Executive SummarySandy Elamin

Studies have shown that monitoring and tracking ones fitness and progresswill have a marked improvement on the individuals fitness. Therefore, forour project we have come up with a smart boxing trainer that will providefeedback after an individuals workout session and keep a record of theirprogress. The smart boxing trainer consists of a punching bag, a balancemat and an Android app which interacts with the individual. Each of thesecomponents were equipped with the appropriate sensors in order to obtain therequired data. The components were then constructed and tested separatelybefore being combined. After building the final product, the trainer wastested and was observed to give the required data.

2

Page 4: Final_Report (1)

1 IntroductionNouran Nouh

The purpose of the smart punching bag trainer is to eliminate the needof actual gym trainer. The smart boxing project give the user feedback ontheir workout session which will help in increasing the progress of the trainer.This report will present the components such as accelerometer, gyroscope,Balancing mat, LCD, and wireless Bluetooth model that used to assemblethis project as well as the testing process for each component individuallyand the testing process for each component. However, two of the componentsthat was supposed to be used to build this project were not included when thefinal project built, the balancing mat which will keep truck of the accuratefooting of the user was not used because one of the group member left sowe had to concentrate on one aspect of the project, also The LCD was notincluded because it has many wires so it was hard to connect it and place onthe punching bag and also as the punching bag was moving a lot when a forceapplied to it which will make it hard to see what the LCD display insteadthe app used to display the force, speed, number of punches and finally thesession timing.

3

Page 5: Final_Report (1)

2 Measuring Force using an AccelerometerSandy Elamin

One of the most important devices being used to build the Boxing traineris the accelerometer. An accelerometer is an electromechanical device thatsenses either static or dynamic forces of acceleration. The role of the ac-celerometer in the Boxing trainer is to measure the acceleration of the punch-ing bag from which the force being applied to the bag can be calculated. Thiscan be obtained by applying equation 1 below.

F = m× a (1)

where,F = force applied in Newtonm = mass of punching bag in kga = acceleration of punching bag in m/s2

By sensing the amount of dynamic acceleration, the accelerometer can findout how fast and in what direction the punching bag is moving and thereforethe force can be calculated.

Figure 1: 3-Axis Accelerometer Module

For this particular project, the 3-Axis Accelerometer shown in Figure 1 wasused.

4

Page 6: Final_Report (1)

From this module, the pins that are needed to obtain the required data arethe G pin, Vin pin, X0 , Y0, and Z0 pin. The G pin represent the ground pin,the VIN pin is the power supply input, X0 is the X direction output voltage,Y0 is the Y direction output voltage and Z0 is the Z direction output voltage.In order to measure the acceleration, the accelerometer was connected to theArduino Uno microcontroller shown in Figure 2 below. The pins of theaccelerometer mentioned earlier were connected to the proper Arduino pinsas follows:

• VIN connected to power supply (+)

• X0, Y0, and Z0 were connected to analog pins

• G pin was connected to the ground pin of the Arduino (GND).

The schematic in Figure 2 below shows the connection between the ac-celerometer and the Arduino.

Figure 2: Schematic of Accelerometer and Arduino connection

5

Page 7: Final_Report (1)

After each accelerometer pin was connected to the proper Arduino pin, theArduino was then connected to a computer in order to be programmed intoreading the analog pins and obtaining the X ,Y, and Z acceleration values.The code below will display the X, Y, and Z acceleration from the board inthe serial monitor.

int x ; // x a x i s v a r i a b l eint y ; // y a x i s v a r i a b l eint z ; // z a x i s v a r i a b l e

void setup ( )// opens s e r i a l port , s e t s data ra t e to 9600 bpsS e r i a l . begin ( 9 6 0 0 ) ;void loop ( )

x = analogRead (A2 ) ; // read A2 input piny = analogRead (A1 ) ; // read A1 input pinz = analogRead (A0 ) ; // read A0 input pin

S e r i a l . p r i n t ( ”X = ” ) ; // p r i n t X valueS e r i a l . p r i n t l n ( x ) ;

S e r i a l . p r i n t ( ”Y = ” ) ; // p r i n t Y valueS e r i a l . p r i n t l n ( y ) ;

S e r i a l . p r i n t ( ”Z = ” ) ; // p r i n t Z valueS e r i a l . p r i n t l n ( z ) ;

de lay ( 1 0 0 ) ;

6

Page 8: Final_Report (1)

Running the code above and placing the accelerometer parallel to the ground,the X,Y and Z values were observed. The results obtained are shown in Figure3.

Figure 3: Initial X,Y and Z values

However, when moving the board in the opposite X, Y and Z-axis, the signof the values did not change. As shown in figure 5, moving the accelerometernorth should give a positive Y value while moving it south should give anegative Y value. In the same manner, moving the accelerometer west shouldgive a negative X value while moving it east should give a positive X value.Lastly moving the accelerometer upwards should give a positive Z value whilemoving it downwards should give a negative Z value.

Figure 4: Direction of Movement in X,Y and Z-axis

7

Page 9: Final_Report (1)

In order fix this issue the values were centered around a zero point. To doso, each of the initial X,Y and Z values obtained were subtracted from theircorresponding variable in the code. The following changes were made to theprevious code as shown below:

S e r i a l . p r i n t ( ”X = ” ) ; // p r i n t X valueS e r i a l . p r i n t l n (x−487);

S e r i a l . p r i n t ( ”Y = ” ) ; // p r i n t Y valueS e r i a l . p r i n t l n (y−540);

S e r i a l . p r i n t ( ”Z = ” ) ; // p r i n t Z valueS e r i a l . p r i n t l n ( z−686);

This way when the board is held parallel to the ground, the X, Y and Zacceleration read 0. When the board is moved to the positive X-axis itdisplays a positive number, and when it is moved to to negative X-axis itdisplays a negative number as expected. The same was observed for Y andZ values. The results obtained for the X-axis acceleration values are shownin the Figure 5 below:

Figure 5: Results obtained for X -axis

Finally, once the accelerometer has been properly connected to the Arduinoand the Arduino has been coded accordingly to obtain the correct data, theaccelerometer was then placed into the punching bag in order to obtain theforce.

8

Page 10: Final_Report (1)

3 The Balancing MatSwaiba Khan

The balancing mat as mentioned before keeps track of the accurate footing ofthe user depending on the various stunts they are practicing. The user selectsthe type of boxing technique they are going to practice on the Android Appand the systems of the mat responds to the footing of the user accordingly.This is achieved by connecting few sub- systems together which forms intoa complete system of the mat. The overview of the complete system of themat is that there are arrays of pressure sensors installed into the mat whichcollects data and transmits it to an I2C controller. The I2C controller allowsmany sensors data to be transmitted using just two ports. The I2C con-troller and the JeeNode 2 communicate with each other and then JeeNode 2transmits the data using RF signal to JeeNode 1. JeeNode 1(in the punchingbag) is the main processor which analyses the data, communicates with theAndroid App and displays result on the LCD.

Figure 6: System level diagram of the balancing mat

Each section of the mats system has different circuit and hardware associ-ated with it which makes it function according to certain specification whichwould be explained in further details below.

9

Page 11: Final_Report (1)

3.1 The Design Process

The sensor used in the mat is called Velostat or Linqstat which is a conductivesheet that is sensitive to force per unit area. Its resistance decreases whenthe pressure applied on it increases [1]. It is flexible and can be cut intoany shape. Its flexibility, pressure-sensitivity and low cost made it an idealsensor for the mat. The conductive sheet was cut into rectangular shape withsize of 1.3cm x 5cm. The surface resistivity of the sensor as per label andaccording to the lab test is 31kΩ/sq.cm. Thus, the resistance of each 6.5cmpiece without any force applied to it is about 200kΩ. The rest of the circuitcan be designed by treating the sensor as a 200kΩ resistor. The following arethe technical details of the material,

Figure 7: Pressure Sensors

Resistance = Resistance per area× Area

= 31Ω

cm2× 1.3cm× 5cm

= 31Ω

cm2× 6.5cm2

= 201.5kΩ

10

Page 12: Final_Report (1)

The sensors are very sensitive and the reading from them fluctuate contin-uously with slightest change in environment. Thats why the sensors wereconnected with op-amps in a current stabilizing circuit in order to get steadyreading. Figure 8 below shows how the stabilizing circuit is connected.

Figure 8: Current Stabilizing circuit

The Velostat piece will be connected with conductive thread on the bothsides. One side of the thread is connected to 5V power supply and the otherside will be connected to pin#1 and pin#2 of an op-amp (LM324). Thisresults in a voltage reading from 0-3.7V at the output depending on thepressure applied. If a rail-to-rail op-amp is used, a voltage reading of 0-5Vmay be received at the output. The 20kΩ allows the adjustment of the DCoffset and sensibility[2].

The output from the stabilizing circuit is then connected to a voltagedivider circuit. In a voltage divider circuit, the output of the stabilizingcircuit is connected in series with another resistor. One end of the resistoris connected to the stabilizing circuit and another end to the ground. Thenode of the resistor and the stabilizing circuit is then connected to the I2Ccontroller and JeeNode for processing. The other resistor value of the voltagedivider circuit is chosen according to maximum power transfer theorem. Thismakes the system more energy efficient and less power is lost as heat. Thefigures below show the proof of maximum power transfer theorem.

11

Page 13: Final_Report (1)

Figure 9: Current Stabilizing circuit

According to maximum power transfer theorem, the values of two resistorsmust be equal in order to have most efficient power dissipation[3]. Thus,the thevenin equivalent resistance of the sensors must be equal to the otherresistor. As the resistance of the sensor without any load is 31kΩ, the value ofthe other resistor chosen is around 31kΩ. The I2C controller is then connectedto the analogue pin of JeeNode2. The JeeNode2 then transmits the data toJeeNode1 using RF signals. Following are the codes used to transmit thedata to JeeNode1 (receiver) and function of each line of the code. Jeenode 2acts as a transmitter as shown on the next page.

12

Page 14: Final_Report (1)

Following are the codes used to program the JeeNode 1(receiver) in the test-ing stage using LEDs.

13

Page 15: Final_Report (1)

3.2 The Testing Process

Testing is one of the most significant stage of a project as it helps in fixing theproduct as desired, ensuring that it meets all the required specification andeven helps in improving it. This section consist of the detailed explanationof the testing method opted for the balancing mat. It is not a good ideato connect all the components at once, test it and expect the system towork. In case it fails to work, it becomes very difficult to figure out the exactproblem of the system. If you cannot locate the problem in the system, howare you supposed to solve it? You might have to start from the scratch bydissembling the system and then reassembling it again. This wastes a lot oftime and in certain cases valuable resources. This is why we decided to testeach separate part of the system individually before putting it altogether inone system. For example, our smart fitness system consists of three majorparts, namely the punching bag, balancing mat and the smart phone for theapp. Each of the parts would be tested separately before it is connected toeach other forming one complete system which would be tested as a wholesystem again.

Stage 1: Initial Research and Calculation

It is very important to plan out the exact experiment before carrying it out.Appropriate research and calculation should be carried out in order to havethe expected values to compare the tested result with. The pressure sensorused in the balancing mat is a pressure sensitive resistor whose resistancechanges as pressureInitial is applied to it. We had to research on if theresistance increases or decreases with the pressure and what is the resistanceof the sensor without any pressure being applied to it. Some circuit analysiscalculation was carried out (as shown previously) to figure out what valueresistor would be most compatible for the voltage divider circuit. Withoutthe initial research and, we would have had no values to compare our testedresult with.

Stage 2: Experimental Tool Testing

All the equipment and software being used to test the system should beindividually tested in order to ensure that the result we are obtaining from theexperiment is accurate. The software could be tested with test codes whichare easily found online. The hardware equipment are tested by measuring a

14

Page 16: Final_Report (1)

known variable and observing if the new measured value is same as the knowvalue. The multi-meter was used to measure resistors of different values andthe result obtained was compared to the label of the resistors. The multi-meter showed similar resistance values (within the error margin) and waslater used to measure the resistance of the sensors. Test codes from onlinewas used to test the Arduino software, i.e. if the software compiles and runsthe codes without error and that it is able to upload the codes to JeeNodes.This also ensured that the JeeNode is functioning properly.

Stage 3: Testing the Individual Components

Next the actual system components were tested and the result obtained wascompared with the calculated or researched value. For example, the resis-tance of the sensor without any applied pressure was measured using themulti-meter (200KΩ) and the result was compared to the researched value.At this stage, different magnitudes of pressure was applied to the sensors toobserve any change in the resistance value reading on the multi-meter. Thisgave us a general idea about the sensitivity of the sensors. In addition tothat the op-amps used for the current stabilizing circuit was tested as well.A simple feedback circuit was built and the signal at the input and outputwas compared to ensure that the op-amps were functioning properly.

Figure 10: Feedback Circuit

15

Page 17: Final_Report (1)

Rf = Rin = 20kΩ Vin = 5V

According to Kirchoffs Current Law: all the currents at a node sum up tozero. Using Kirchoffs Current Law and Ohms Law:

Vin − 0

Rin

=0− Vout

Rf

Vout = − Rf

Rin

× Vin

Rf = Rin

Vout = −Vin

= 5V

When the input and output voltages were measured using a multi-meter,the Vout was equal to Vin. This proves that the om-amps were functioningproperly and not burned out.

Stage 4: Connecting different components

Once we were sure that the individual components worked properly, they wereconnected to each other, one at a time. Each component was installed one ata time because it made debugging codes and circuit analysis much easier andefficient. The sensor was first connected in a voltage divider circuit with aresistor (calculated in stage 1), a LED and the JeeNode. The basic idea wasthat the LED would light up if certain magnitude of pressure is applied on thesensors. If the experiment failed, each section of the code could be debuggedeasily and the hardware components could be tested with other examples.However, the experiment was successful in first trial which ensured that allthe hardware and software components are compatible to each other.

Stage 5: Increase efficiency

Our next objective was to make the system as efficient as possible. This wasdone by trying different features of the components and experimenting usingtrial-error method. For instance, we tested the RF feature of the JeeNodenext using the same circuit. We observed that the two Jeenodes are able to

16

Page 18: Final_Report (1)

communicate with each other using RF signals and that the LED lights upwhen certain magnitude of pressure is applied on the sensor, even though thetwo JeeNodes are not physically connected by wires. Next, we tried alteringthe resistor values of the voltage divider circuit. We tested if increasing ordecreasing the resistor value has any effect on the sensitivity of the sensors.We noticed that using a resistance value lower than the original sensor re-sistance, makes the system most efficient. This is because, when pressure isapplied on the sensors, the resistance of the sensor decreases which causesvoltage across the sensor to decrease which is transmitted for analysis. Keep-ing the resistor value slightly larger than the new reduced resistance value ofthe sensor, ensures sufficient voltage is applied to both the components andthey function properly.

Stage 6: Final Compilation

The main functionality of the balancing mat is that it has sensors to collectmeasurable data which is transferred to the JeeNode 1 (transmitter) andJeeNode1 transmits the data to the JeeNode2 (receiver). The JeeNode 2then receives the data, processes it and outputs it to an output device. Tillnow, our output device was a LED which was then replaced by a LCD. Afterprograming JeeNode 2 with few lines of code, the LCD would be able todisplay more valuable information on the screen which would be useful to theuser. The experiment was successful up to a certain limit. The LCD displayedthe required information on the screen (the force of the foot on the mat andits position on the mat) but along with that there were addition numbersand letters displayed which did not mean anything. We encountered our firstmajor error. As we followed a certain procedure in testing the system, wecan eliminate certain things which could be the reason for the error. Forexample, the error was not due to malfunction of the different hardware ornot because the devices are not compatible to each other. Tests were carriedout to ensure that earlier. The LCD displays the right information thusdetermining that it functions properly. The only other possible error wasdue to the new codes regarding the LCD uploaded on the JeeNodes. Thattook us to the next stage of testing- debugging.

17

Page 19: Final_Report (1)

Stage 7: Debugging

The debugging stage may appear after any other previous stages of the test-ing. Basically whenever an error is encountered. It does not only refer tosoftware errors but may be errors encountered in hardware too. For exam-ple, finding misconnections in a circuit with a lot of devices connected withwires. For this case, our error was in the codes we used to program theJeeNodes. However, we did not know if it was in the transmitter or receiverJeeNode. So we connected the two JeeNodes with two different laptops andrun the respective codes. The Arduino has output monitor feature whichallows us to observe the data being transmitted and that is being received.The transmitter monitor showed the exact data being transmitted (withoutthe random numbers and letters) thus the error was not in the transmitterJeeNodes code. However, the receiver monitor displayed the same randomletters and numbers which appeared on the LCD along with the useful infor-mation. Thus, telling us the error was in the receiver end. After debuggingthe codes with the help of the LCD library and Arduino library, we were ableto edit the code and fix the problem.

Stage 8: Final Test

After the system worked without any error, it was tested with different vari-ables and in different conditions. This stage ensures that the product worksin different conditions and does not fail in extreme cases. Additionally, ithelps in making the instruction guide for the user. The balancing mat wastested with different magnitudes of weights and the results were recorded.The maximum and minimum weight it can withstand was recorded. Themaximum weight it can measure is 70Kg and the minimum weight it is sensi-tive to is 20g. Once all the sub-circuits are connected together into a systemand the Jeenodes are programmed with the appropriate codes, the balancingmat would function according to its specification.

18

Page 20: Final_Report (1)

4 Liquid Crystal DisplayNouran Nouh

LCDs consist of two sheets of polarizing material with a liquid crystal so-lution between them. An electric current passed through the liquid causesthe crystals to align so that light cannot pass through them. Each crystal,therefore, is like a shutter, either allowing light to pass through or blockingthe light[4]. An LCD is available as anactive-matrix ,dual-scan, orpassive-matrix[4]. LCDs are used in various technology such as, calculators, laptopslike dell, TVs, DVD player, watches and Clocks. LCD can display graph,words, digits and image.

4.1 LCD in the project

LCD was programmed to display a feedback for the trainer while the workoutsession for instance, it will display the force applied to the punching bag likea bar graph to show the trainer weather the force applied is less or morethan the average force which is 325N, and it displays punch more if the forceapplied by trainer is less than the 325N and no bar will be shown if the forceapplied is very low. The LCD also displays the number of punches, the timethe trainer spend training after each session and the position of the traineron the balancing mat weather the trainer position is right or wrong.

4.2 LCD and JeeNode

There are different types and sizes of the LCD, in this project the size of theLCD will be 16x2 and the type of the LCD is TG12864H3-05A. Figure 11shows the type of the LCD that will be used.

Figure 11: TG12864H3-05A LCD [5]

19

Page 21: Final_Report (1)

The LCD consist of pins, pin 1 is chip select input pin (CS), pin 2 is theRest (RST), pin 3 is the register select input pin (A0). When A0 is high, A0display data, when A0 is low it controls data. Pin 4 is serial clock (SLCK),pin 5 serial input Data (SID). Pin 6 is the power (VDD) which supply 3.3V,the pin 7 is the ground (GND). The other pins are R (LED-) which is thecathode of LED backlight, A (LED+) is the Anode of LED backlight, G(LED-) is the cathode of green LED backlight, and B (LED-) is the cathodeof LED backlight. The pins are shown is Figure 12.

Figure 12: The pins of the LCD

The pins of the LCD will be connected to the Jeenode pins as follow, SID isconnected to port 1 digital 4 in the Jeenode. In port 2 SCLK is connectedto digital 5 in the Jeenode, CS is connected to analoge1/digital 15, A+ isconnected to +3V in the Jeenode. A resistor soldered with the wire of A+ toprevent LCD from burning because it will decrease the current that flow inthe wire and they both connected to +3V in the Jeenode. In port 3 groundof LCD is connected to ground of the Jeenode, RST is connected to digital6, and VDD is connected to +3V. In port 4 A0 is connected to digital 7.

20

Page 22: Final_Report (1)

4.3 Testing of the LCD

In this project the LCD gets the information of the trainer like force, numberof punches and the time from the android app and display them to the user.At the beginning of the semester LCD was programmed to display hello worldto check whether it is working or not, after that the LCD tested with thebalancing the mat, balancing mat was connected to jeenode 1 while the LCDwas connected to jeenode 2, the LCD programed to display the informationthat Jeenode 2 get from the sensors that is connected to Jeenode 1. TheCode are shown below in figure 3. The LCD worked probably with the LCDand displayed the information that received from sensors.

#include ”ST7565 . h”#include <JeeLib . h>

ST7565 g lcd ( 4 , 5 , 7 , 6 , 1 5 ) ;

#define LCD HEIGHT 16#define LCD WIDTH 16

void setup ( )S e r i a l . begin (115200 ) ;

g l cd . begin (0 x18 ) ;

void loop ( )g lcd . c l e a r ( ) ;

S e r i a l . p r i n t l n ( ”%f ” , f o r c e ) ;

After that the LCD tested to display the force like a bar graph so it comparesthe received force by 325N and if it is less than 325N it will not display a barhowever it will display punch more, if the force is less than 325N and if theforce is more or equal to the average force it will display a bar for exampleif the force is 329N it will display a bar like the one shown in figure 5, thegreater the force of the trainer, the larger the bar of that force. The codethat is will be used are shown in figure 4.

21

Page 23: Final_Report (1)

void loop ( )g lcd . c l e a r ( ) ;

g l cd . drawstr ing (0 , 0 , ” Force : ” ) ;f o r c e = 329 ;bars = f i n d b a r s ( f o r c e , avgf , 8 8 ) ;S e r i a l . p r i n t l n ( bars ) ;for ( int i =0; i<bars ; i ++)

g lcd . drawchar (35+ i , 0 , 128 ) ;g lcd . d i sp l ay ( ) ;

// func t i on to d i sp l ay the barsdouble f i n d b a r s (double f o r c e , double avgf , double t t l )

double maxf = avgf + ( avgf / 2 . 0 ) ;double minf = avgf / 2 . 0 ;double bars ;

i f ( f o r c e >= minf )bars = ( fo r ce−minf )∗ ( t t l / avgf ) ;

else bars = 0 ;// d i sp l ay message i f f o r c e i s l e s s than averageg l cd . p r i n t ( ”punch more” )

Figure 13: Bar graph of the force

22

Page 24: Final_Report (1)

A function defined to compare to the minimum force (minf) which assumedto be the average force divided by 2.0 and then it will calculate the numberof the bar that can be displayed in single line of the LCD and will returnvariable bar. The for loop used to draw the bar using drawchar functionwhich has the length of the character which is from 35+i to 128 per line.The bar graph of the force had modified as shown in figure 6, the codeshown below had modified to show the average force like a character in themiddle the code is the same as the one shown in figure 5 only in the Voidloop function, glcd.drawchar(35+42,0, 7) function used to draw the diamondcharacter, which contain the number of the character and the position of thecharacter which is shown in figure 6 and the for loop had been modified toshow the character and the bar for the force each time the trainer apply aforce to the punching bag. If the force is for example 390N the bar graphwill be displayed as the first picture in figure 6 which shows that 390N ismore than the average force. However, if the force is below the average forcewhich is 325N the bar graph will be like the second picture shown in figure6 and a massage will be displayed to ask the user to punch more. Moreover,if the force is very low for example is 100N no bar will be shown instead amassage will be shown to ask the trainer to apply more force.

void loop ( ) // d i sp l ay the f o r c e in barg l cd . drawstr ing (0 , 0 , ” Force : ” ) ;f o r c e =390 ;bars = f i n d b a r s ( f o r c e , avgf , MAX BARS) ;S e r i a l . p r i n t l n ( bars ) ;i f ( bars > 88) bars = 88 ;i f ( bars == 0)

g lcd . drawstr ing (0 , 0 , ”Punch harder ! ” ) ;else i f ( bars < 44)g lcd . drawchar (35+42 , 0 , 7 ) ;g l cd . drawstr ing (0 , 2 , ”Punch a l i t t l e more ” ) ;for ( int i =0; i<bars ; i ++)

i f ( i ==42)g lcd . drawchar (35+ i , 0 , 8 ) ;

23

Page 25: Final_Report (1)

i+= 4 ; //number o f p i x e l o f each cha rac t e r

Figure 14: Bar graph of the force

The LCD has not programmed to display the time and number of punchesbecause when the final project built and tested, it was hard to put the LCDbecause it has many wires and since the punching bag is moving a lot whenthe trainer is punching it will be hard to read what the LCD displaying. Sothe LCD as mentioned before had not used in the final project. Instead theapp used to show the force, speed, number of punches and the session timing.

24

Page 26: Final_Report (1)

5 Software ComponentsUzair Ahmad

This section of the report refers to the software side of the project, mainlyprogramming of the mircocontroller and the Android application to providethe necessary framework for seamless wireless data communication, and theprocessing of raw sensor data. During the software design of the project, theapproach was to tackle one aspect at a time and follow the system develop-ment life cycles shown below in Figure 15.

Figure 15: System development life cycle

A development life cycle like this is very useful for developing software forsuch projects, because it can work inside an incremental model where onefeature is developed at a time and then implemented with the rest of theproject. The technical analysis subsection will explain the major challengesidentified during the technical analysis of the project, the two design sectionsexplain the design and implementation process for the Android applicationand the microcontroller program, and the testing section explain the testingprocess along with the evaluation of the design.

25

Page 27: Final_Report (1)

5.1 Technical Analysis

There were numerous technical challenges throughout the development soft-ware for this project. This subsection will discuss some of the challengesfaced, and the thought process behind picking a particular solutions.

5.1.1 Wireless Communication

Throughout most of the time during this project, the plan was to have twoindividual microcontrollers that communicate between each other using thebuilt in RF module in the JeeNode, and to use a Bluetooth module for com-munication between the Android application and one of the microcontrollers.Figure 16 below shows the system level diagram of data communication be-tween the three major parts of the project.

Figure 16: Data communication system level diagram

This created the problem of handling different kind of data structures whilemanaging the data interchange between two different kinds of programmingenvironments, namely C++ and Java. Although during the later stage theproject was reduced into using a single Arduino Uno instead of two JeeNodes,this problem of proper data communication still existed, and thus was solvedby serializing data into a pre-determined format before being transmitted,and then converting the data back to an appropriate format on the other endof the communication line. Initially, it was decided to use Javascript ObjectNotation format (JSON) for data serialization for both devices. It was laterdiscovered that the Android application development platform used did notsupport the generation of JSON strings from data structures. So it was de-cided to use JSON for sending data from Uno to the app, but a differentcustom format for sending the data from the app to the Uno.

26

Page 28: Final_Report (1)

5.1.2 Sensor Calibration

During the testing of the accelerometer, it was discovered that the sensoroutput changes a lot depending on the rest position of the sensor. Thismeant that the sensor needs to be calibrated every time its positon is evenslight changed. The initial solution to this problem was to program themicrocontroller to automatically calibrate the sensor during the initial setuploop. However, it was soon realized after further testing that for the purposeof demonstration, it was not possible to properly attach the sensor to thepunching bag, and the movement of the bag could at times change the sensor’sposition giving false readings. Thus, it was decided to also include the optionof re-calibrating the sensor from inside the app.

5.1.3 Measuring Force

The initial design of the project was to install the sensors inside a punchingbag that is hung from a ceiling. This kind of a setup is very similar to apendulum, and is relatively easier to model mathematically which means theforce applied can be calculated from the instantaneous acceleration of thepunching bag. Due to not finding a proper place for hanging the punchingbag inside the lab, we were forced to use a different kind of punching bag thatis mounted on a spring. The inclusion of the spring meant that we could notlonger directly measure the applied force from the instantaneous accelerationaccurately. Due to our inexperience with complicated mechanics, we decidedto introduce a punch rating system instead of the punching force. The ratingsystem could not be designed to include the effect of the spring, as long asit was consistent.

5.1.4 Measuring Speed

Another feature of our project was the measure the punching speed of theuser, that is, the number of punches per minute. Although, the sensitivityof the accelerometer allowed detection of all the different kinds of punches,to actually be able to count it was necessary to define a lower bound to howstrong a punch really is. The solution was to send the average force calculatedinside the app to the microcontroller along other data communication thatwas normally scheduled. This allowed the microcontroller to have a referencepoint for registering punches.

27

Page 29: Final_Report (1)

5.2 Design and Implementation: Arduino Uno

After analyzing each problem and technical challenge, the design process forthe decided solution was started. This subsection discusses how the issuesidentified earlier were actually solved during development.

5.2.1 Data Serialization

The data encoded for sending data to the Android application was picked tobe JSON. This is what the structure of the required string looks like,

”Rating”:29,”Speed”:72

The following Arduino code snippet shows the function used to generate thestring and send it via Bluetooth.

// func t i on to encode and update data on appvoid encode update ( )

// JSON b u f f e r to s t o r e the ob j e c t t r e eSta t i cJ sonBuf f e r <200> j s on B u f f e r ;

// c r e a t e a new JSON ob j e c tJsonObject& root = j s o n Bu f f e r . c r ea teObjec t ( ) ;

// encode the g l o b a l v a r i a b l e s Rating and Speedroot [ ” Rating ” ] = Rating ;root [ ”Speed” ] = Speed ;

// p r i n t the r e s u l t i n g ob j e c t t r e e to the BT s e r i a lroot . printTo (BT) ;

While this easily takes care of sending data from the Uno to the app, the appwas however unable to generate JSON strings due to the limited capability ofdevelopment platform (MIT App Inventor). This forced the use of a customdata format that could be easily generated using the app, and that was usinga comma-delimited format. The string below represents the typical kind ofstring sent from the app.

Uzair, 1, 0, 0, 25

28

Page 30: Final_Report (1)

As can be seen, the JSON format is much easier to understand. This formatrequires the Uno to be pre-programmed to understand what each value rep-resents. In this case, the values represent username, calib mode, force mode,speed mode respectively. The following function was used to decode thementioned data format sent from the app, and store the data inside globalvariables.

// func t i on to decode comma−de l im i t ed va luesvoid decode update ( )

// a b u f f e r to s t o r e decoded datachar∗ b u f f e r ;// a temporary v a r i a b l e to s t o r e the b luetooth datachar temp [ BTdata . l ength ( ) ] ;// convert s t r i n g data to char arrayBTdata . toCharArray ( temp , BTdata . l ength ( ) ) ;

/∗ the s t r t o k func t i on e x t r a c t s data u n t i l acha rac t e r i s found , a comma in t h i s case ∗/

b u f f e r = s t r t o k ( temp , ” , ” ) ;// s t o r e the data in a g l o b a l v a r i a b l eusername = b u f f e r ;

// decode the next value in to the b u f f e rb u f f e r = s t r t o k (NULL, ” , ” ) ;

/∗ s i n c e the data decoded needs to be s to r ed as an i n tva r i ab l e , the a t o i f unc t i on conver t s char to i n t ∗/

cal ib mode = a t o i ( b u f f e r ) ;

// repeat f o r other v a r i a b l e s as we l lb u f f e r = s t r t o k (NULL, ” , ” ) ;force mode = a t o i ( b u f f e r ) ;b u f f e r = s t r t o k (NULL, ” , ” ) ;speed mode = a t o i ( b u f f e r ) ;b u f f e r = s t r t o k (NULL, ” , ” ) ;avg ra t ing = a t o i ( b u f f e r ) ;

29

Page 31: Final_Report (1)

5.2.2 Sensor Calibration

The microcontroller is programmed to calibrate once at the start of power-ing up, and any calibration after that is done through the app. When themicrocontroller receive the signal to go into calibration mode, it takes 100readings and keeps track of the highest readings. At the end of 100 readings,the highest values are stored inside a global variable to be used as an offset tonormalize the rest acceleration to zero. It is very crucial that the punchingbag is not moving during the calibration process for proper calibration.

5.2.3 Punch Rating

The analog input of the Uno is capable of reading the voltage in only 1024discrete levels. The raw sensor data is first converted to the actual voltage,then the voltage is converted to acceleration by using the factors mentionedin the data sheet for the accelerometer. The resulting acceleration is in unitsof g, that is a multiple of 9.8 ms−1. This scaled acceleration is then multipliedby a factor of 10, and is used as a form of rating system, to give a punchrating close to 20 for light punches, and a punch rating close to 40 for strongpunches.

5.2.4 Punching Speed

The main issue with measuring the punching speed was the lack of a referencepoint to determine if a punch should actually be counted as a valid punchwhen counting the total number of punches per minute. The reference pointwas then designed to be half the average rating the user has achieved.

5.2.5 Integration

All the functions designed also need to be integrated properly now. Thecalibration function is initialized once the microcontroller first powers on,and once the calibration is complete, it enters an idle mode. During theidle mode, the microcontroller waits for a command from the app to runthe appropriate testing mode. Once a command is received, the appropriatefunction is initialized, and at the end of it, the readings are encoded andsent back to the app. The microcontroller then goes back to the idle modeuntil the next command. The code for the entire program is included in theAppendix.

30

Page 32: Final_Report (1)

5.3 Design and Implementation: Android App

Although the aim of the project was to develop the application in Androidstudio, it was not very feasible due to time limitations that were not seen fore-seen at the start of the project. Therefore, the app was developed in MIT’sApp Inventor, which has a relatively easier learning curve and consumes lesstime.

5.3.1 App Design

This platform is specifically made for such applications where the functional-ity of the applications is of higher importance than the appearance. However,a minimal and clean design was still emphasized. Figure 17 below shows thetwo parts of the app, the welcome screen allows adding new users, while themain menu contains all the statistics and controls. When one of the buttonsis pressed, the results of the action are displayed in a dialog box.

Figure 17: The welcome screen and the main menu

31

Page 33: Final_Report (1)

5.3.2 App Functionality

MIT’s App Inventor allows development of an application using code blocksinstead of creating java classes as in the case of Android Studio. Althoughthis makes the process considerably faster, the functionality is also limitedin some aspects, an example of this was the inability of the platform to allowJSON generation even though it supports JSON parsing. However, it stillallows most of the necessary tools required for a functioning app, some ofthe components used for this app were databases to store user data, notifiedblocks for creating dialog boxes, and clock blocks for keeping track of thesession time. Figure 18 below shows an example of such a block combinationto detect if Bluetooth is turned one when a use tries to press the continuebutton on the welcome screen.

Figure 18: A simple block combination for welcome screen

5.4 Testing and Evaluation

The stage was visited after every single feature was developed to make sureeverything was integrated properly before moving to the next step. Sincemost of the testing was done during the early stages, the punching bag wasnot yet ready to be integrated with the electronics. This meant that a lot ofassumptions were made during the design process for parts that involved theuse of the accelerometer. As a consequence there were several problems thathad required further analysis and a better design following the developmentlife cycle. The testing phase was also extended to allow other members tomake sure their part of project was properly integrated in the project. Thetesting phase was very necessary for proper development of the software side.The time allotted for testing was still not enough by the end of project, assome minor bugs were still detected later during the day of project demon-stration.

32

Page 34: Final_Report (1)

6 Final SpecificationsSwaiba Khan

This section summarizes the final specifications of the project as a whole,the performance of the punching bag and the final features of the Androidapplication.

6.1 The Punching Bag

The punching bag responses to the users punches in 3 main ways. Each ofthese responses give certain useful information to the user on the varioustechniques of boxing. Firstly, it responses to the various force with whichthe user punches the bag. That is, if a user punches the bag hard with greatforce, the magnitude of force displayed on the Android app is high (40 -50)while if the user punches with lesser force then the value is lower (20-30).This helps the user in keeping track of their punching force and practice ontheir techniques accordingly. Secondly, the punching bags response dependson the positions of the punch on the bag too. The punching bag was dividedinto 3 sections, the upper, middle and lower section. It was observed thatwhen same force punches were applied in the three different sections, theresults were different. The following table shows the results obtained fromexperimenting with the punching bag.

Table 1: Punching bag specificationsArea Force Applied Result shown on the App.

Upper Section High 48Upper Section Low 30Middle Section High 45Middle Section Low 25Lower Section High 41Lower Section Low 23

After analysing the result shown on the table above, it was determined thatit requires more force to apply high magnitude punch on the lower section.This is due to the angle at which the punch is applied and other anatomyfactors which are beyond the scope of this project. However, an analogy canbe made that when a boxer is punching someone, he/she would land a more

33

Page 35: Final_Report (1)

effective punch on the upper level of the body compared to lower. Thirdly,the punching bag also responses to the number of punches applied to it. Thishelps in keeping track of the frequency of the punches and the user can workon his/her punching technique accordingly.

6.2 The Android Application

The Android application has the following functionality:

• Allows the user to measure punch rating

• Allows the user to measure punching speed

• Tracks top user statistics

• Calculates the average rating for users

• Keeps track of the session time

• Multiple user support

• Allows the user to re-calibrate the sensors

34

Page 36: Final_Report (1)

7 Conclusion and Future WorkUzair Ahmad

Despite of multiple setbacks, some of which were detrimental to the progressof the project, a basic framework for such a project was successfully achieved.It started with researching on new hardware which was much different com-pared to the exposure the authors had throughout their university career,then followed the stage of testing where individual parts were tested, and werethen slowly developed for particular applications. These separate psuedo-projects were then integrated with each other to form a working model ofa smart fitness trainer. This project provided the opportunity to actuallyexperience the design process behind engineering projects, ranging from ex-tensive testing to iterative design processes.Although idea of this project is much more bigger in scope than what wasactually demonstrated during this short project, just the concept of a smartfitness trainer plays very well into the future of a smart and connected world.With everything moving towards automation and the internet of things, it isonly a matter of time before the fitness market is also transformed. This hasalready started with the advent of smart fitness bands and smart watcheswhich are capable of measuring foots, heartbeat, blood pressure, and manyother kind of data. As part of a bigger project, perhaps a fourth year project,this idea of a smart fitness trainer can be expanded to include multiple sen-sors, synchronization with the mentioned smart bands and watches, have aproper app which is capable of analyzing all the data from the sensors, andperhaps a better application than inside a punching bag.

35

Page 37: Final_Report (1)

8 References

[1] Pressure-sensitive conductive sheet (velostat/linqstat). [Online]. Avail-able: https://www.adafruit.com/products/1361

[2] Velostat pressure sensor circuit. [Online]. Available:http://forum.arduino.cc/index.php?topic=58058.0

[3] Maximum power transfer theorem. [Online]. Avail-able: http://www.allaboutcircuits.com/textbook/direct-current/chpt-10/maximum-power-transfer-theorem/

[4] Lcd - liquid crystal display. [Online]. Available:http://www.webopedia.com/TERM/L/LCD.html

[5] St7565 lcd tutorial. [Online]. Available:http://www.ladyada.net/learn/lcd/st7565.html

36

Page 38: Final_Report (1)

Appendix

The complete code for the Arduino program:

// open source l i b r a r i e s#include ”ArduinoJson . h”#include <S o f t w a r e S e r i a l . h>#include ”ST7565 . h”

// d e f i n e so f tware s e r i a l p insS o f t w a r e S e r i a l BT(10 , 1 1 ) ; // RX, TX

// g l o b a l sS t r ing username ;S t r ing BTdata ;int avg ra t ing ;

// modesint ca l ib mode ;int force mode ;int speed mode ;

// c a l i b r a t i o n o f f s e tdouble o f f s e t [ 3 ] ;

// v a r i a b l e s f o r BTdouble Rating ;int Speed ;

void setup ( ) S e r i a l . begin ( 9 6 0 0 ) ; // i n i t i a l i z e S e r i a lBT. begin ( 9 6 0 0 ) ; // i n i t i a l i z e b luetooth S e r i a l

// i n i t i a l i z e v a r i a b l e savg ra t ing = 0 ;Rating = 0 ;Speed = 0 ;BTdata = ”User , 0 , 0 , 0 , 0” ;

37

Page 39: Final_Report (1)

// wait f o r app to connectS e r i a l . p r i n t l n ( ”Waiting f o r BT Connection ” ) ;while ( !BT. a v a i l a b l e ( ) )

delay ( 1 0 0 ) ;// get i n i t i a l data from appi f (BT. a v a i l a b l e ( ) )

BTdata = BT. readSt r ing ( ) ;decode update ( ) ;

// i n i t i a l i z e c a l i b r a t i o n mode to be oncal ib mode = 1 ;

void loop ( ) // mul t ip l e modes o f opera t i oni f ( ca l ib mode==1)

c a l i b r a t e ( ) ; else i f ( force mode == 1)

measure fo rce ( ) ; else i f ( speed mode == 1)

measure speed ( ) ; else

i d l e ( ) ;// end ca s e s

de lay ( 1 0 0 ) ;

// func t i on to encode and update data on appvoid encode update ( )

// JSON b u f f e r to s t o r e the ob j e c t t r e eSta t i cJ sonBuf f e r <200> j s on B u f f e r ;

// c r e a t e a new JSON ob j e c tJsonObject& root = j s o n Bu f f e r . c r ea teObjec t ( ) ;

38

Page 40: Final_Report (1)

// encode the g l o b a l v a r i a b l e s Rating and Speedroot [ ” Rating ” ] = Rating ;root [ ”Speed” ] = Speed ;

// p r i n t the r e s u l t i n g ob j e c t t r e e to the BT s e r i a lroot . printTo (BT) ;

// func t i on to decode comma−de l im i t ed va luesvoid decode update ( )

// a b u f f e r to s t o r e decoded datachar∗ b u f f e r ;// a temporary v a r i a b l e to s t o r e the b luetooth datachar temp [ BTdata . l ength ( ) ] ;// convert s t r i n g data to char arrayBTdata . toCharArray ( temp , BTdata . l ength ( ) ) ;

/∗ the s t r t o k func t i on e x t r a c t s data u n t i l acha rac t e r i s found , a comma in t h i s case ∗/

b u f f e r = s t r t o k ( temp , ” , ” ) ;// s t o r e the data in a g l o b a l v a r i a b l eusername = b u f f e r ;

// decode the next value in to the b u f f e rb u f f e r = s t r t o k (NULL, ” , ” ) ;

/∗ s i n c e the data decoded needs to be s to r ed as an i n tva r i ab l e , the a t o i f unc t i on conver t s char to i n t ∗/

cal ib mode = a t o i ( b u f f e r ) ;

// repeat f o other v a r i a b l e s as we l lb u f f e r = s t r t o k (NULL, ” , ” ) ;force mode = a t o i ( b u f f e r ) ;

b u f f e r = s t r t o k (NULL, ” , ” ) ;speed mode = a t o i ( b u f f e r ) ;

39

Page 41: Final_Report (1)

b u f f e r = s t r t o k (NULL, ” , ” ) ;avg ra t ing = a t o i ( b u f f e r ) ;

// a func t i on to measure the f o r c e ( punch r a t i n g )void measure fo rce ( )

S e r i a l . p r i n t l n ( ” f o r c e mode s t a r t e d ” ) ;

double raw [ 3 ] ;double v o l t [ 3 ] ;double f o r c e =0;

for ( int p=0; p<200; p++)// read raw senso r dataraw [ 0 ] = analogRead (A0 ) ;raw [ 1 ] = analogRead (A1 ) ;raw [ 2 ] = analogRead (A2 ) ;

// convert to vo l tagefor ( int i = 0 ; i <3; i ++)

v o l t [ i ] = raw [ i ] ∗ 5 .0 / 1023 ;

double a c c e l [ 3 ] ;

// subt rac t o f f s e tfor ( int i =0; i <3; i ++)

v o l t [ i ]−= o f f s e t [ i ] ;

// convert to a c c e l e r a t i o n ( un i t i s g m/ s )for ( int i = 0 ; i <3; i ++)

a c c e l [ i ] = v o l t [ i ] / 0 . 8 ;;

40

Page 42: Final_Report (1)

//remove no i s e when i d l efor ( int i = 0 ; i <3; i ++)

i f ( f abs ( a c c e l [ i ] ) <= 0 .05 )a c c e l [ i ] = 0 ;

// c a l c u l a t e r e s u l t a n t a c c e l e r a t i o ndouble t t l = s q r t ( ( a c c e l [ 0 ] ∗ a c c e l [ 0 ] )+

( a c c e l [ 1 ] ∗ a c c e l [ 1 ] ) + (a c c e l [ 2 ] ∗ a c c e l [ 2 ] ) ) ;

i f ( t t l >f o r c e )f o r c e = t t l ;

delay ( 2 0 ) ;

//end f o r c e f i n d i n gRating = f o r c e ∗10 ;force mode = 0 ;encode update ( ) ;S e r i a l . p r i n t ( ” Rating = ” ) ;S e r i a l . p r i n t l n ( Rating ) ;

// func t i on to c a l c u l a t e net o f f s e t s f o r acce l e romete rvoid c a l i b r a t e ( )

S e r i a l . p r i n t l n ( ” S ta r t i ng Ca l i b ra t i on ” ) ;for ( int i =0; i <3; i++)o f f s e t [ i ] = 0 . 0 ;

for ( int z=0; z<100; z++)double raw [ 3 ] ;double v o l t [ 3 ] ;// read raw senso r dataraw [ 0 ] = analogRead (A0 ) ;raw [ 1 ] = analogRead (A1 ) ;raw [ 2 ] = analogRead (A2 ) ;

// convert to vo l tage

41

Page 43: Final_Report (1)

for ( int i = 0 ; i <3; i ++)v o l t [ i ] = raw [ i ] ∗ 5 .0 / 1023 ;

// get the r i g h t o f f s e tfor ( int n=0; n<3; n++)

i f ( f abs ( v o l t [ n ] ) >= o f f s e t [ n ] )o f f s e t [ n ] = fabs ( v o l t [ n ] ) ;

delay ( 2 0 ) ;//end f o r

S e r i a l . p r i n t l n ( ”Done Ca l i b ra t i ng ” ) ;ca l ib mode = 0 ;encode update ( ) ;S e r i a l . p r i n t l n ( ) ;

// func t i on to measure punching speedvoid measure speed ( )

S e r i a l . p r i n t l n ( ” S ta r t i ng Speed Mode” ) ;

double raw [ 3 ] ;double v o l t [ 3 ] ;double f o r c e =0;

int num=0;

for ( int p=0; p<200; p++)

// read raw senso r dataraw [ 0 ] = analogRead (A0 ) ;raw [ 1 ] = analogRead (A1 ) ;raw [ 2 ] = analogRead (A2 ) ;

// convert to vo l tagefor ( int i = 0 ; i <3; i ++)

42

Page 44: Final_Report (1)

v o l t [ i ] = raw [ i ] ∗ 5 .0 / 1023 ;

double a c c e l [ 3 ] ;

// subt rac t o f f s e tfor ( int i =0; i <3; i ++)

v o l t [ i ]−= o f f s e t [ i ] ;

// convert to a c c e l e r a t i o n ( un i t i s g m/ s )for ( int i = 0 ; i <3; i ++)

a c c e l [ i ] = v o l t [ i ] / 0 . 8 ;

//remove no i s e when i d l efor ( int i = 0 ; i <3; i ++)

i f ( f abs ( a c c e l [ i ] ) <= 0 .05 )a c c e l [ i ] = 0 ;

// c a l c u l a t e r e s u l t a n t a c c e l e r a t i o ndouble t t l = s q r t ( ( a c c e l [ 0 ] ∗ a c c e l [ 0 ] )+

( a c c e l [ 1 ] ∗ a c c e l [ 1 ] ) + (a c c e l [ 2 ] ∗ a c c e l [ 2 ] ) ) ;

// determine i f v a l i d punchi f ( ( t t l ∗10.0)>( avg ra t ing / 2 . 0 ) )

num+=1;delay ( 5 0 ) ;

//end f o r c e f i n d i n gSpeed = num∗6 ;speed mode = 0 ;encode update ( ) ;S e r i a l . p r i n t ( ”Speed = ” ) ;S e r i a l . p r i n t l n ( Speed ) ;

43

Page 45: Final_Report (1)

void i d l e ( )// wait f o r next command from app

while ( !BT. a v a i l a b l e ( ) )delay ( 1 0 0 ) ;

delay ( 1 0 0 ) ;i f (BT. a v a i l a b l e ( ) )

BTdata = BT. readSt r ing ( ) ;decode update ( ) ;

44


Recommended