Skip to content

Commit ea8857f

Browse files
committed
Update MinIMU-9-test.py to run with VPython 7 and python3
Before this commit, the python script was only compatible with older versions of Vpython (5.74) and python 2.7. This modified script can be run with latest pip version of Vpython and python 3.X. It was tested in a virtualenv on Linux: Python: 3.9.4 vpython: 7.6.1 This new version does basically the same thing than the previous one but in the navigator. The code was moved in a dedicated function.
1 parent fae87c3 commit ea8857f

File tree

1 file changed

+139
-109
lines changed

1 file changed

+139
-109
lines changed

MinIMU-9-test.py

Lines changed: 139 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -31,118 +31,148 @@
3131

3232
# This script needs VPython, pyserial and pywin modules
3333

34-
# First Install Python 2.6.4 (Python 2.7 also works)
34+
# First Install Python 3 (tested with Python 3.9 on linux but earlier versions on Windows should work)
35+
# This can be used in a Python/Conda virtualenv
3536
# Install pywin from http://sourceforge.net/projects/pywin32/
3637
# Install pyserial from http://sourceforge.net/projects/pyserial/files/
3738
# Install VPython from http://vpython.org/contents/download_windows.html
3839

39-
from visual import *
40+
from vpython import *
4041
import serial
41-
import string
4242
import math
43-
44-
from time import time
45-
46-
grad2rad = 3.141592/180.0
47-
48-
# Check your COM port and baud rate
49-
ser = serial.Serial(port='COM9',baudrate=115200, timeout=1)
50-
51-
# Main scene
52-
scene=display(title="Pololu MinIMU-9 + Arduino AHRS")
53-
scene.range=(1.2,1.2,1.2)
54-
#scene.forward = (0,-1,-0.25)
55-
scene.forward = (1,0,-0.25)
56-
scene.up=(0,0,1)
57-
58-
# Second scene (Roll, Pitch, Yaw)
59-
scene2 = display(title='Pololu MinIMU-9 + Arduino AHRS',x=0, y=0, width=500, height=200,center=(0,0,0), background=(0,0,0))
60-
scene2.range=(1,1,1)
61-
scene.width=500
62-
scene.y=200
63-
64-
scene2.select()
65-
#Roll, Pitch, Yaw
66-
cil_roll = cylinder(pos=(-0.4,0,0),axis=(0.2,0,0),radius=0.01,color=color.red)
67-
cil_roll2 = cylinder(pos=(-0.4,0,0),axis=(-0.2,0,0),radius=0.01,color=color.red)
68-
cil_pitch = cylinder(pos=(0.1,0,0),axis=(0.2,0,0),radius=0.01,color=color.green)
69-
cil_pitch2 = cylinder(pos=(0.1,0,0),axis=(-0.2,0,0),radius=0.01,color=color.green)
70-
#cil_course = cylinder(pos=(0.6,0,0),axis=(0.2,0,0),radius=0.01,color=color.blue)
71-
#cil_course2 = cylinder(pos=(0.6,0,0),axis=(-0.2,0,0),radius=0.01,color=color.blue)
72-
arrow_course = arrow(pos=(0.6,0,0),color=color.cyan,axis=(-0.2,0,0), shaftwidth=0.02, fixedwidth=1)
73-
74-
#Roll,Pitch,Yaw labels
75-
label(pos=(-0.4,0.3,0),text="Roll",box=0,opacity=0)
76-
label(pos=(0.1,0.3,0),text="Pitch",box=0,opacity=0)
77-
label(pos=(0.55,0.3,0),text="Yaw",box=0,opacity=0)
78-
label(pos=(0.6,0.22,0),text="N",box=0,opacity=0,color=color.yellow)
79-
label(pos=(0.6,-0.22,0),text="S",box=0,opacity=0,color=color.yellow)
80-
label(pos=(0.38,0,0),text="W",box=0,opacity=0,color=color.yellow)
81-
label(pos=(0.82,0,0),text="E",box=0,opacity=0,color=color.yellow)
82-
label(pos=(0.75,0.15,0),height=7,text="NE",box=0,color=color.yellow)
83-
label(pos=(0.45,0.15,0),height=7,text="NW",box=0,color=color.yellow)
84-
label(pos=(0.75,-0.15,0),height=7,text="SE",box=0,color=color.yellow)
85-
label(pos=(0.45,-0.15,0),height=7,text="SW",box=0,color=color.yellow)
86-
87-
L1 = label(pos=(-0.4,0.22,0),text="-",box=0,opacity=0)
88-
L2 = label(pos=(0.1,0.22,0),text="-",box=0,opacity=0)
89-
L3 = label(pos=(0.7,0.3,0),text="-",box=0,opacity=0)
90-
91-
# Main scene objects
92-
scene.select()
93-
# Reference axis (x,y,z)
94-
arrow(color=color.green,axis=(1,0,0), shaftwidth=0.02, fixedwidth=1)
95-
arrow(color=color.green,axis=(0,-1,0), shaftwidth=0.02 , fixedwidth=1)
96-
arrow(color=color.green,axis=(0,0,-1), shaftwidth=0.02, fixedwidth=1)
97-
# labels
98-
label(pos=(0,0,0.8),text="Pololu MinIMU-9 + Arduino AHRS",box=0,opacity=0)
99-
label(pos=(1,0,0),text="X",box=0,opacity=0)
100-
label(pos=(0,-1,0),text="Y",box=0,opacity=0)
101-
label(pos=(0,0,-1),text="Z",box=0,opacity=0)
102-
# IMU object
103-
platform = box(length=1, height=0.05, width=1, color=color.blue)
104-
p_line = box(length=1,height=0.08,width=0.1,color=color.yellow)
105-
plat_arrow = arrow(color=color.green,axis=(1,0,0), shaftwidth=0.06, fixedwidth=1)
106-
107-
108-
f = open("Serial"+str(time())+".txt", 'w')
109-
110-
roll=0
111-
pitch=0
112-
yaw=0
113-
while 1:
114-
line = ser.readline()
115-
if line.find("!ANG:") != -1: # filter out incomplete (invalid) lines
116-
line = line.replace("!ANG:","") # Delete "!ANG:"
117-
print line
118-
f.write(line) # Write to the output log file
119-
words = string.split(line,",") # Fields split
120-
if len(words) > 2:
121-
try:
122-
roll = float(words[0])*grad2rad
123-
pitch = float(words[1])*grad2rad
124-
yaw = float(words[2])*grad2rad
125-
except:
126-
print "Invalid line"
127-
128-
axis=(cos(pitch)*cos(yaw),-cos(pitch)*sin(yaw),sin(pitch))
129-
up=(sin(roll)*sin(yaw)+cos(roll)*sin(pitch)*cos(yaw),sin(roll)*cos(yaw)-cos(roll)*sin(pitch)*sin(yaw),-cos(roll)*cos(pitch))
130-
platform.axis=axis
131-
platform.up=up
132-
platform.length=1.0
133-
platform.width=0.65
134-
plat_arrow.axis=axis
135-
plat_arrow.up=up
136-
plat_arrow.length=0.8
137-
p_line.axis=axis
138-
p_line.up=up
139-
cil_roll.axis=(0.2*cos(roll),0.2*sin(roll),0)
140-
cil_roll2.axis=(-0.2*cos(roll),-0.2*sin(roll),0)
141-
cil_pitch.axis=(0.2*cos(pitch),0.2*sin(pitch),0)
142-
cil_pitch2.axis=(-0.2*cos(pitch),-0.2*sin(pitch),0)
143-
arrow_course.axis=(0.2*sin(yaw),0.2*cos(yaw),0)
144-
L1.text = str(float(words[0]))
145-
L2.text = str(float(words[1]))
146-
L3.text = str(float(words[2]))
147-
ser.close
148-
f.close
43+
import os
44+
import time
45+
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 += """
74+
Click to toggle between pausing or running.
75+
Right button drag or Ctrl-drag to rotate "camera" to view scene.
76+
To zoom, drag with mid1e button or Alt/Option depressed, or use scroll wheel.
77+
On a two-button mouse, midd1e is left + right.
78+
Touch screen: pinch/extend to zoom, swipe or two-finger rotate."""
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)