Advertisement
Guest User

Admar

a guest
Feb 3rd, 2010
818
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. #seabre986@gmail.com
  16.  
  17. #
  18.  
  19.  
  20.  
  21.  
  22.  
  23. import serial
  24.  
  25. import array
  26.  
  27. import csv
  28.  
  29.  
  30.  
  31. #Open a CSV file for writing
  32.  
  33. accelcsvfile = csv.writer(open('acceldata.csv', 'w'))
  34.  
  35.  
  36.  
  37. def startAccessPoint():
  38.  
  39.     return array.array('B', [0xFF, 0x07, 0x03]).tostring()
  40.  
  41.  
  42.  
  43. def accDataRequest():
  44.  
  45.     return array.array('B', [0xFF, 0x08, 0x07, 0x00, 0x00, 0x00, 0x00]).tostring()
  46.  
  47.  
  48.  
  49. #Open COM port 6 (check your system info to see which port
  50.  
  51. #yours is actually on.)
  52.  
  53. #argments are 5 (COM6), 115200 (bit rate), and timeout is set so
  54.  
  55. #the serial read function won't loop forever.
  56.  
  57. # ser = serial.Serial(5,115200,timeout=1)
  58.  
  59. ser = serial.Serial("/dev/ttyACM0",115200,timeout=1)
  60.  
  61.  
  62.  
  63. #Start access point
  64.  
  65. ser.write(startAccessPoint())
  66.  
  67.  
  68.  
  69. #Write the top row so you know what everything is.
  70.  
  71. accelcsvfile.writerow(["x","y","z", "M1", "M2", "S1"])
  72.  
  73.  
  74.  
  75. while True:
  76.  
  77.     #Send request for acceleration data
  78.  
  79.     ser.write(accDataRequest())
  80.  
  81.     accel = ser.read(7)
  82.  
  83.     M1 = 0
  84.  
  85.     M2 = 0
  86.  
  87.     S1 = 0
  88.  
  89.     unknown = 1 # indicate that the received data is of unkown type
  90.  
  91.  
  92.  
  93.     if len(accel) != 7:
  94.  
  95.         continue
  96.  
  97.     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:
  98.  
  99.     # bogus data?
  100.  
  101.         unknown = 0
  102.  
  103.         continue
  104.  
  105.     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:
  106.  
  107.     print "M1 pressed"
  108.  
  109.     M1 = 1
  110.  
  111.         unknown = 0
  112.  
  113.     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:
  114.  
  115.     print "M2 pressed"
  116.  
  117.     M2 = 1
  118.  
  119.         unknown = 0
  120.  
  121.     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:
  122.  
  123.     print "S1 pressed"
  124.  
  125.     S1 = 1
  126.  
  127.         unknown = 0
  128.  
  129.     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:
  130.  
  131.         # accelerometer data, but it is bogus
  132.  
  133.         unknown = 0
  134.  
  135.         continue
  136.  
  137.     if ord(accel[6]) == 255 and ord(accel[5]) == 7 and ord(accel[4]) == 6 and ord(accel[3]) == 255:
  138.  
  139.         print "Accelerometer data: x: " + str(ord(accel[2])) + "\t y: " + str(ord(accel[1])) + "\t z: " + str(ord(accel[0]))
  140.  
  141.         unknown = 0
  142.  
  143.  
  144.  
  145.     if unknown == 1:
  146.  
  147.         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]))
  148.  
  149.         continue
  150.  
  151.  
  152.  
  153.     #This actually writes the data to the CSV file.
  154.  
  155.     accelcsvfile.writerow([str(ord(accel[2])),str(ord(accel[1])),str(ord(accel[0])),str(M1),str(M2),str(S1)])
  156.  
  157. ser.close()
  158.  
Advertisement
RAW Paste Data Copied
Advertisement