Skip to content

Commit eb2b180

Browse files
committed
Update: move code to a dedicated method.
1 parent ca50bde commit eb2b180

File tree

1 file changed

+128
-108
lines changed

1 file changed

+128
-108
lines changed

MinIMU-9-test.py

Lines changed: 128 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -43,116 +43,136 @@
4343
import os
4444
import time
4545

46-
grad2rad = 3.141592 / 180.0
47-
48-
# Check your COM port and baud rate
49-
ser = serial.Serial(port='COM',baudrate=115200, timeout=1)
50-
51-
# Main scene
52-
scene = canvas(title="Platform")
53-
caption = "<b>Pololu MinIMU-9 + Arduino AHRS</b>"
54-
caption += """
46+
DEVICE = "/dev/ttyACM" # could be "COM", "/dev/ttyUSB", ...
47+
48+
49+
def launch():
50+
grad2rad = 3.141592 / 180.0
51+
# find the device
52+
ser = None
53+
maxi = 10
54+
it = 0
55+
while it < maxi:
56+
try:
57+
dev = DEVICE + str(it)
58+
if os.path.exists(dev):
59+
ser = serial.Serial(port=dev, baudrate=115200, timeout=1)
60+
print(dev + " exists !")
61+
break
62+
else:
63+
print("device %s does not exists" % dev)
64+
except serial.serialutil.SerialException:
65+
print("Serial device triggered an exception :-(")
66+
it += 1
67+
if not ser:
68+
return 1
69+
70+
# Main scene
71+
scene = canvas(title="Platform")
72+
caption = "<b>Pololu MinIMU-9 + Arduino AHRS</b>"
73+
caption += """
5574
Click to toggle between pausing or running.
5675
Right button drag or Ctrl-drag to rotate "camera" to view scene.
5776
To zoom, drag with mid1e button or Alt/Option depressed, or use scroll wheel.
5877
On a two-button mouse, midd1e is left + right.
5978
Touch screen: pinch/extend to zoom, swipe or two-finger rotate."""
60-
scene.caption = caption
61-
scene.forward = vector(0, -1, -0.25)
62-
63-
# Second scene (Roll, Pitch, Yaw)
64-
scene2 = canvas(title='Rotation axis')
65-
scene.width = 500
66-
scene.y = 200
67-
68-
scene2.select()
69-
# Roll, Pitch, Yaw
70-
cil_roll = cylinder(pos=vector(-0.4, 0, 0), axis=vector(0.2, 0, 0), radius=0.01, color=color.red)
71-
cil_roll2 = cylinder(pos=vector(-0.4, 0, 0), axis=vector(-0.2, 0, 0), radius=0.01, color=color.red)
72-
cil_pitch = cylinder(pos=vector(0.1, 0, 0), axis=vector(0.2, 0, 0), radius=0.01, color=color.green)
73-
cil_pitch2 = cylinder(pos=vector(0.1, 0, 0), axis=vector(-0.2, 0, 0), radius=0.01, color=color.green)
74-
# cil_course = cylinder(pos=(0.6,0,0),axis=(0.2,0,0),radius=0.01,color=color.blue)
75-
# cil_course2 = cylinder(pos=(0.6,0,0),axis=(-0.2,0,0),radius=0.01,color=color.blue)
76-
arrow_course = arrow(pos=vector(0.6, 0, 0), color=color.cyan, axis=vector(-0.2, 0, 0), shaftwidth=0.02,
77-
fixedwidth=1)
78-
79-
# Roll,Pitch,Yaw labels
80-
label(pos=vector(-0.4, 0.3, 0), text="Roll", box=0, opacity=0, billboard=True)
81-
label(pos=vector(0.1, 0.3, 0), text="Pitch", box=0, opacity=1, billboard=True)
82-
label(pos=vector(0.55, 0.3, 0), text="Yaw", box=0, opacity=0, billboard=True)
83-
label(pos=vector(0.6, 0.22, 0), text="N", color=color.yellow, box=0, opacity=0)
84-
label(pos=vector(0.6, -0.22, 0), text="S", color=color.yellow, box=0, opacity=0)
85-
label(pos=vector(0.38, 0, 0), text="W", color=color.yellow, box=0, opacity=0)
86-
label(pos=vector(0.82, 0, 0), text="E", box=0, opacity=0, color=color.yellow)
87-
label(pos=vector(0.75, 0.15, 0), height=7, text="NE", box=0, color=color.yellow)
88-
label(pos=vector(0.45, 0.15, 0), height=7, text="NW", box=0, color=color.yellow)
89-
label(pos=vector(0.75, -0.15, 0), height=7, text="SE", box=0, color=color.yellow)
90-
label(pos=vector(0.45, -0.15, 0), height=7, text="SW", box=0, color=color.yellow)
91-
92-
l_1 = label(pos=vector(-0.4, 0.22, 0), text="-", box=0, opacity=0)
93-
l_2 = label(pos=vector(0.1, 0.22, 0), text="-", box=0, opacity=0)
94-
l_3 = label(pos=vector(0.7, 0.3, 0), text="-", box=0, opacity=0)
95-
96-
# Main scene objects
97-
scene.select()
98-
# Reference axis (x,y,z)
99-
arrow(color=color.green, axis=vector(1, 0, 0), shaftwidth=0.02, fixedwidth=1)
100-
arrow(color=color.green, axis=vector(0, -1, 0), shaftwidth=0.02, fixedwidth=1)
101-
arrow(color=color.green, axis=vector(0, 0, -1), shaftwidth=0.02, fixedwidth=1)
102-
# labels
103-
# label(pos=vector(0, 0, 0.8), text="Pololu MinIMU-9 + Arduino AHRS", box=0, opacity=0)
104-
label(pos=vector(1, 0, 0), text="X", box=0, opacity=0)
105-
label(pos=vector(0, -1, 0), text="Y", box=0, opacity=0)
106-
label(pos=vector(0, 0, -1), text="Z", box=0, opacity=0)
107-
# IMU object
108-
platform = box(length=1, height=0.05, width=1, color=color.blue)
109-
p_line = box(length=1, height=0.08, width=0.1, color=color.yellow)
110-
plat_arrow = arrow(color=color.green, axis=vector(1, 0, 0), shaftwidth=0.06, fixedwidth=1)
111-
112-
f = open("Serial"+str(time.time())+".txt", 'w')
113-
114-
roll = 0
115-
pitch = 0
116-
yaw = 0
117-
118-
time.sleep(0.2)
119-
rate(25)
120-
while True:
121-
line = ser.readline()
122-
line = line.decode('utf-8')
123-
if line.find("=ANG:") != -1: # filter out incomplete (invalid) lines
124-
line = line.replace("=ANG:", "") # Delete "!ANG:"
125-
f.write(line) # Write to the output log file
126-
words = line.split(",") # Fields split
127-
if len(words) > 2:
128-
try:
129-
roll = float(words[0]) * grad2rad
130-
pitch = float(words[1]) * grad2rad
131-
yaw = float(words[2]) * grad2rad
132-
except (TypeError, IndexError) as ex:
133-
print("Invalid line")
134-
135-
axis = vector(math.cos(pitch) * math.cos(yaw), -math.cos(pitch) * math.sin(yaw), math.sin(pitch))
136-
up = vector(math.sin(roll) * math.sin(yaw) + math.cos(roll) * math.sin(pitch) * math.cos(yaw),
137-
math.sin(roll) * math.cos(yaw) - math.cos(roll) * math.sin(pitch) * math.sin(yaw),
138-
- math.cos(roll) * math.cos(pitch))
139-
platform.axis = axis
140-
platform.up = up
141-
platform.length = 1.0
142-
platform.width = 0.65
143-
plat_arrow.axis = axis
144-
plat_arrow.up = up
145-
plat_arrow.length = 0.8
146-
p_line.axis = axis
147-
p_line.up = up
148-
cil_roll.axis = vector(0.2 * math.cos(roll), 0.2 * math.sin(roll), 0)
149-
cil_roll2.axis = vector(-0.2 * math.cos(roll), -0.2 * math.sin(roll), 0)
150-
cil_pitch.axis = vector(0.2 * math.cos(pitch), 0.2 * math.sin(pitch), 0)
151-
cil_pitch2.axis = vector(-0.2 * math.cos(pitch), -0.2 * math.sin(pitch), 0)
152-
arrow_course.axis = vector(0.2 * math.sin(yaw), 0.2 * math.cos(yaw), 0)
153-
l_1.text = str(float(words[0]))
154-
l_2.text = str(float(words[1]))
155-
l_3.text = str(float(words[2]))
156-
sleep(0.05)
157-
ser.close()
158-
f.close()
79+
scene.caption = caption
80+
81+
scene.forward = vector(0, -1, -0.25)
82+
83+
# # Second scene (Roll, Pitch, Yaw)
84+
scene2 = canvas(title='Rotation axis')
85+
scene2.select()
86+
scene.width = 500
87+
scene.y = 200
88+
89+
# Roll, Pitch, Yaw
90+
cil_roll = cylinder(pos=vector(-0.4, 0, 0), axis=vector(0.2, 0, 0), radius=0.01, color=color.red)
91+
cil_roll2 = cylinder(pos=vector(-0.4, 0, 0), axis=vector(-0.2, 0, 0), radius=0.01, color=color.red)
92+
cil_pitch = cylinder(pos=vector(0.1, 0, 0), axis=vector(0.2, 0, 0), radius=0.01, color=color.green)
93+
cil_pitch2 = cylinder(pos=vector(0.1, 0, 0), axis=vector(-0.2, 0, 0), radius=0.01, color=color.green)
94+
# cil_course = cylinder(pos=(0.6,0,0),axis=(0.2,0,0),radius=0.01,color=color.blue)
95+
# cil_course2 = cylinder(pos=(0.6,0,0),axis=(-0.2,0,0),radius=0.01,color=color.blue)
96+
arrow_course = arrow(pos=vector(0.6, 0, 0), color=color.cyan, axis=vector(-0.2, 0, 0), shaftwidth=0.02,
97+
fixedwidth=1)
98+
99+
# Roll,Pitch,Yaw labels
100+
label(pos=vector(-0.4, 0.3, 0), text="Roll", box=0, opacity=0, billboard=True)
101+
label(pos=vector(0.1, 0.3, 0), text="Pitch", box=0, opacity=1, billboard=True)
102+
label(pos=vector(0.55, 0.3, 0), text="Yaw", box=0, opacity=0, billboard=True)
103+
label(pos=vector(0.6, 0.22, 0), text="N", color=color.yellow, box=0, opacity=0)
104+
label(pos=vector(0.6, -0.22, 0), text="S", color=color.yellow, box=0, opacity=0)
105+
label(pos=vector(0.38, 0, 0), text="W", color=color.yellow, box=0, opacity=0)
106+
label(pos=vector(0.82, 0, 0), text="E", box=0, opacity=0, color=color.yellow)
107+
label(pos=vector(0.75, 0.15, 0), height=7, text="NE", box=0, color=color.yellow)
108+
label(pos=vector(0.45, 0.15, 0), height=7, text="NW", box=0, color=color.yellow)
109+
label(pos=vector(0.75, -0.15, 0), height=7, text="SE", box=0, color=color.yellow)
110+
label(pos=vector(0.45, -0.15, 0), height=7, text="SW", box=0, color=color.yellow)
111+
112+
l_1 = label(pos=vector(-0.4, 0.22, 0), text="-", box=0, opacity=0)
113+
l_2 = label(pos=vector(0.1, 0.22, 0), text="-", box=0, opacity=0)
114+
l_3 = label(pos=vector(0.7, 0.3, 0), text="-", box=0, opacity=0)
115+
116+
# Main scene objects
117+
scene.select()
118+
# Reference axis (x,y,z)
119+
arrow(color=color.green, axis=vector(1, 0, 0), shaftwidth=0.02, fixedwidth=1)
120+
arrow(color=color.green, axis=vector(0, -1, 0), shaftwidth=0.02, fixedwidth=1)
121+
arrow(color=color.green, axis=vector(0, 0, -1), shaftwidth=0.02, fixedwidth=1)
122+
# labels
123+
# label(pos=vector(0, 0, 0.8), text="Pololu MinIMU-9 + Arduino AHRS", box=0, opacity=0)
124+
label(pos=vector(1, 0, 0), text="X", box=0, opacity=0)
125+
label(pos=vector(0, -1, 0), text="Y", box=0, opacity=0)
126+
label(pos=vector(0, 0, -1), text="Z", box=0, opacity=0)
127+
# IMU object
128+
platform_mimi = box(length=1, height=0.05, width=1, color=color.blue)
129+
p_line = box(length=1, height=0.08, width=0.1, color=color.yellow)
130+
plat_arrow = arrow(color=color.green, axis=vector(1, 0, 0), shaftwidth=0.06, fixedwidth=1)
131+
roll = 0
132+
pitch = 0
133+
yaw = 0
134+
135+
time.sleep(0.2)
136+
rate(25)
137+
while True:
138+
line = ser.readline()
139+
line = line.decode('utf-8')
140+
if line.find("=ANG:") != -1: # filter out incomplete (invalid) lines
141+
line = line.replace("=ANG:", "") # Delete "!ANG:"
142+
words = line.split(",") # Fields split
143+
if len(words) > 2:
144+
try:
145+
roll = float(words[0]) * grad2rad
146+
pitch = float(words[1]) * grad2rad
147+
yaw = float(words[2]) * grad2rad
148+
except (TypeError, IndexError) as ex:
149+
print("Invalid line")
150+
151+
axis = vector(math.cos(pitch) * math.cos(yaw), -math.cos(pitch) * math.sin(yaw), math.sin(pitch))
152+
up = vector(math.sin(roll) * math.sin(yaw) + math.cos(roll) * math.sin(pitch) * math.cos(yaw),
153+
math.sin(roll) * math.cos(yaw) - math.cos(roll) * math.sin(pitch) * math.sin(yaw),
154+
- math.cos(roll) * math.cos(pitch))
155+
platform_mimi.axis = axis
156+
platform_mimi.up = up
157+
platform_mimi.length = 1.0
158+
platform_mimi.width = 0.65
159+
plat_arrow.axis = axis
160+
plat_arrow.up = up
161+
plat_arrow.length = 0.8
162+
p_line.axis = axis
163+
p_line.up = up
164+
cil_roll.axis = vector(0.2 * math.cos(roll), 0.2 * math.sin(roll), 0)
165+
cil_roll2.axis = vector(-0.2 * math.cos(roll), -0.2 * math.sin(roll), 0)
166+
cil_pitch.axis = vector(0.2 * math.cos(pitch), 0.2 * math.sin(pitch), 0)
167+
cil_pitch2.axis = vector(-0.2 * math.cos(pitch), -0.2 * math.sin(pitch), 0)
168+
arrow_course.axis = vector(0.2 * math.sin(yaw), 0.2 * math.cos(yaw), 0)
169+
l_1.text = str(float(words[0]))
170+
l_2.text = str(float(words[1]))
171+
l_3.text = str(float(words[2]))
172+
sleep(0.05)
173+
174+
ser.close()
175+
176+
177+
if __name__ == '__main__':
178+
launch()

0 commit comments

Comments
 (0)