Detecting Movement
The Android phones we are using can measure acceleration in three dimensions.
This experiment will show you the data that is output and you should construct some graphs to see what sort of data you get.
What we would like you to think about is how you could use this data to determine things like which direction a person is moving and how fast.
The program
Very similar to the GPS one except it reads the accelerometer.
import android, time, os
droid = android.Android()
dt = 100 #100ms between sensings \
endTime = 3000 #sample for 3000ms
f = open('/sdcard/sl4a/scripts/logfile.txt','w')
f.write('x,y,z\n')
f.close()
timeSensed=0
droid.startSensingTimed(2,dt)
while timeSensed <= endTime:
value = droid.sensorsReadAccelerometer().result
x = value[0]
y = value[1]
z = value[2]
f = open('/sdcard/sl4a/scripts/logfile.txt','a')
f.write(str(x) + ',' + str(y) + ',' + str(z) + '\n')
print(str(x) + ',' + str(y) + ',' + str(z) + '\n')
f.close()
droid.stopSensing()
1. Load the Code
Here is the script:
2. Run the Experiment
Try jumping up and down, the program will run until you stop it (or 3 minutes, whichever happens first).
Another experiment (afterwards) is try dropping the phone
ONTO A PILLOW.
3. Copy the Data from the Phone
1. You need to copy across the data that you have collected. Connect the phone to the PC using the USB cable, on the PC open the
terminal window and enter the following command to copy the data file across.
getlog
IF YOU HAVE A PROBLEM WITH THE ABOVE STEP, ASK SOMEONE, MAYBE THE COMPUTER NEEDS SOME SETTING UP!
2. Open the data file in
gedit by entering the following command:
gedit logfile.txt
3. Select and copy the contents of the data file into the clipboard.
4. Graphing the Data
Try graphing the different pieces of data. Use a spreadsheet as we did with the altitude.
5. How well does the accelerometer work?
See if you can answer the following questions. Discuss them with the mentors:
- Why is it never zero even when the phone isn't moving?
- Do you think the accelerometer is very accurate?
- How could you work out if you are going up or down?
- Can you think of any ways you could use this data for a program?