Skip to content

Commit ca50bde

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.
1 parent fae87c3 commit ca50bde

File tree

1 file changed

+97
-87
lines changed

1 file changed

+97
-87
lines changed

MinIMU-9-test.py

Lines changed: 97 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -31,118 +31,128 @@
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+
import os
44+
import time
4345

44-
from time import time
45-
46-
grad2rad = 3.141592/180.0
46+
grad2rad = 3.141592 / 180.0
4747

4848
# Check your COM port and baud rate
49-
ser = serial.Serial(port='COM9',baudrate=115200, timeout=1)
49+
ser = serial.Serial(port='COM',baudrate=115200, timeout=1)
5050

5151
# 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)
52+
scene = canvas(title="Platform")
53+
caption = "<b>Pololu MinIMU-9 + Arduino AHRS</b>"
54+
caption += """
55+
Click to toggle between pausing or running.
56+
Right button drag or Ctrl-drag to rotate "camera" to view scene.
57+
To zoom, drag with mid1e button or Alt/Option depressed, or use scroll wheel.
58+
On a two-button mouse, midd1e is left + right.
59+
Touch screen: pinch/extend to zoom, swipe or two-finger rotate."""
60+
scene.caption = caption
61+
scene.forward = vector(0, -1, -0.25)
5762

5863
# 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
64+
scene2 = canvas(title='Rotation axis')
65+
scene.width = 500
66+
scene.y = 200
6367

6468
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)
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)
9095

9196
# Main scene objects
9297
scene.select()
9398
# 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)
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)
97102
# 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)
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)
102107
# IMU object
103108
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)
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)
106111

112+
f = open("Serial"+str(time.time())+".txt", 'w')
107113

108-
f = open("Serial"+str(time())+".txt", 'w')
114+
roll = 0
115+
pitch = 0
116+
yaw = 0
109117

110-
roll=0
111-
pitch=0
112-
yaw=0
113-
while 1:
118+
time.sleep(0.2)
119+
rate(25)
120+
while True:
114121
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
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
120127
if len(words) > 2:
121128
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
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()

0 commit comments

Comments
 (0)