Subversion Repositories Code-Repo

Rev

Rev 223 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 223 Rev 224
Line 16... Line 16...
16
	try:
16
	try:
17
		ser.open()
17
		ser.open()
18
		print("Connected to serial port.")
18
		print("Connected to serial port.")
19
		# Loop reading in data from the sensor
19
		# Loop reading in data from the sensor
20
		while(1):
20
		while(1):
21
			s = ser.read(19)
21
			s = ser.read(21)
22
			# Ensure that the data properly ends in a newline
22
			# Ensure that the data properly ends in a newline
23
			if s[18] == ord('\n'):
23
			if s[20] == ord('\n'):
24
				# Read in accelerometer data and convert from two's compliment
24
				# Read in accelerometer data and convert from two's compliment
25
				A_X = ((s[1] << 8) | s[0])
25
				A_X = ((s[1] << 8) | s[0])
26
				A_Y = ((s[3] << 8) | s[2])
26
				A_Y = ((s[3] << 8) | s[2])
27
				A_Z = ((s[5] << 8) | s[4])
27
				A_Z = ((s[5] << 8) | s[4])
28
				A_X_N = twos_comp(A_X, 16) >> 4
28
				A_X_N = twos_comp(A_X, 16) >> 4
Line 43... Line 43...
43
				M_Z = ((s[17] << 8) | s[16])
43
				M_Z = ((s[17] << 8) | s[16])
44
				M_X_N = twos_comp(M_X, 16)
44
				M_X_N = twos_comp(M_X, 16)
45
				M_Y_N = twos_comp(M_Y, 16)
45
				M_Y_N = twos_comp(M_Y, 16)
46
				M_Z_N = twos_comp(M_Z, 16)
46
				M_Z_N = twos_comp(M_Z, 16)
47
 
47
 
-
 
48
				# Read in battery status
-
 
49
				B_H = s[18]
-
 
50
				B_L = s[19]
-
 
51
 
48
				# Print out the processed data
52
				# Print out the processed data
49
				print("A: %-6i %-6i %-6i    G: %-6i %-6i %-6i    M: %-6i %-6i %-6i" % \
53
				print("A: %-6i %-6i %-6i   G: %-6i %-6i %-6i   M: %-6i %-6i %-6i   B: %u.%u" % \
50
					(A_X_N, A_Y_N, A_Z_N, G_X_N, G_Y_N, G_Z_N, M_X_N, M_Y_N, M_Z_N))
54
					(A_X_N, A_Y_N, A_Z_N, G_X_N, G_Y_N, G_Z_N, M_X_N, M_Y_N, M_Z_N, B_H, B_L))
51
			else:
55
			else:
52
				break
56
				break
53
	except:
57
	except:
54
		pass
58
		pass
55
	ser.close()
59
	ser.close()
56
60