Guest User

Admar

a guest
Feb 3rd, 2010
1,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. #Get acceleration data from Chronos watch.
  2.  
  3. #Taken from info posted at: http://e2e.ti.com/support/microcontrollers/msp43016-bit_ultra-low_power_mcus/f/166/t/32714.aspx
  4.  
  5. #x, y, and z values may not be "in order". The first three bytes from the packet
  6.  
  7. #of data sent from the watch seemed to be different than what was listed
  8.  
  9. #on the page, though the datatype byte was in the correct place. So YMMV.
  10.  
  11. #
  12.  
  13. #Written by Sean Brewer (seabre)
  14.  
  15.  
  16. #
  17.  
  18.  
  19.  
  20.  
  21.  
  22. import serial
  23.  
  24. import array
  25.  
  26. import csv
  27.  
  28.  
  29.  
  30. #Open a CSV file for writing
  31.  
  32. accelcsvfile = csv.writer(open('acceldata.csv', 'w'))
  33.  
  34.  
  35.  
  36. def startAccessPoint():
  37.  
  38.     return array.array('B', [0xFF, 0x07, 0x03]).tostring()
  39.  
  40.  
  41.  
  42. def accDataRequest():
  43.  
  44.     return array.array('B', [0xFF, 0x08, 0x07, 0x00, 0x00, 0x00, 0x00]).tostring()
  45.  
  46.  
  47.  
  48. #Open COM port 6 (check your system info to see which port
  49.  
  50. #yours is actually on.)
  51.  
  52. #argments are 5 (COM6), 115200 (bit rate), and timeout is set so
  53.  
  54. #the serial read function won't loop forever.
  55.  
  56. # ser = serial.Serial(5,115200,timeout=1)
  57.  
  58. ser = serial.Serial("/dev/ttyACM0",115200,timeout=1)
  59.  
  60.  
  61.  
  62. #Start access point
  63.  
  64. ser.write(startAccessPoint())
  65.  
  66.  
  67.  
  68. #Write the top row so you know what everything is.
  69.  
  70. accelcsvfile.writerow(["x","y","z", "M1", "M2", "S1"])
  71.  
  72.  
  73.  
  74. while True:
  75.  
  76.     #Send request for acceleration data
  77.  
  78.     ser.write(accDataRequest())
  79.  
  80.     accel = ser.read(7)
  81.  
  82.     M1 = 0
  83.  
  84.     M2 = 0
  85.  
  86.     S1 = 0
  87.  
  88.     unknown = 1 # indicate that the received data is of unkown type
  89.  
  90.  
  91.  
  92.     if len(accel) != 7:
  93.  
  94.         continue
  95.  
  96.     if ord(accel[6]) == 1 and ord(accel[5]) == 7 and ord(accel[4]) == 6 and ord(accel[3]) == 255 and ord(accel[2]) == 0 and ord(accel[1]) == 0 and ord(accel[0]) == 0:
  97.  
  98.     # bogus data?
  99.  
  100.         unknown = 0
  101.  
  102.         continue
  103.  
  104.     if ord(accel[6]) == 17 and ord(accel[5]) == 7 and ord(accel[4]) == 6 and ord(accel[3]) == 255 and ord(accel[2]) == 0 and ord(accel[1]) == 0 and ord(accel[0]) == 0:
  105.  
  106.     print "M1 pressed"
  107.  
  108.     M1 = 1
  109.  
  110.         unknown = 0
  111.  
  112.     if ord(accel[6]) == 33 and ord(accel[5]) == 7 and ord(accel[4]) == 6 and ord(accel[3]) == 255 and ord(accel[2]) == 0 and ord(accel[1]) == 0 and ord(accel[0]) == 0:
  113.  
  114.     print "M2 pressed"
  115.  
  116.     M2 = 1
  117.  
  118.         unknown = 0
  119.  
  120.     if ord(accel[6]) == 49 and ord(accel[5]) == 7 and ord(accel[4]) == 6 and ord(accel[3]) == 255 and ord(accel[2]) == 0 and ord(accel[1]) == 0 and ord(accel[0]) == 0:
  121.  
  122.     print "S1 pressed"
  123.  
  124.     S1 = 1
  125.  
  126.         unknown = 0
  127.  
  128.     if ord(accel[6]) == 255 and ord(accel[5]) == 7 and ord(accel[4]) == 6 and ord(accel[3]) == 255 and ord(accel[2]) == 0 and ord(accel[1]) == 0 and ord(accel[0]) == 0:
  129.  
  130.         # accelerometer data, but it is bogus
  131.  
  132.         unknown = 0
  133.  
  134.         continue
  135.  
  136.     if ord(accel[6]) == 255 and ord(accel[5]) == 7 and ord(accel[4]) == 6 and ord(accel[3]) == 255:
  137.  
  138.         print "Accelerometer data: x: " + str(ord(accel[2])) + "\t y: " + str(ord(accel[1])) + "\t z: " + str(ord(accel[0]))
  139.  
  140.         unknown = 0
  141.  
  142.  
  143.  
  144.     if unknown == 1:
  145.  
  146.         print "Unknown data: 6: " + str(ord(accel[6])) + "\t5: " + str(ord(accel[5])) + "\t4: " + str(ord(accel[4])) + "\t3: " + str(ord(accel[3])) + "\t2: " + str(ord(accel[2])) + "\t1: " + str(ord(accel[1])) + "\t0: " + str(ord(accel[0]))
  147.  
  148.         continue
  149.  
  150.  
  151.  
  152.     #This actually writes the data to the CSV file.
  153.  
  154.     accelcsvfile.writerow([str(ord(accel[2])),str(ord(accel[1])),str(ord(accel[0])),str(M1),str(M2),str(S1)])
  155.  
  156. ser.close()
  157.  
Advertisement
Add Comment
Please, Sign In to add comment