Python code
import serial #Import Serial Library
from visual import * #Import all the vPython library
arduinoSerialData = serial.Serial('COM3', 9600) #Create an object for the Serial port. Adjust 'com11' to whatever port your arduino is sending to.
measuringRod = cylinder( axis=(0,0,10),radius= .5, length=6, color=color.yellow, pos=(-3,0,0))
lengthLabel = label(pos=(0,0,5), text='Target Distance is: ', box=false, height=10)
orientation = 0
x = 0
y = 1
z = 2
while (1==1): #Create a loop that continues to read and display the data
rate(20)#Tell vpython to run this loop 20 times a second
if (arduinoSerialData.inWaiting()>0): #Check to see if a data point is available on the serial port
myData = arduinoSerialData.readline() #Read the distance measure as a string
myData = myData.strip()
mylist = myData.split(',')
ax = mylist[x]
ay = mylist[y]
az = mylist[z]
#print ax,ay,az #Print the measurement to confirm things are working
#newAngle = float(myData) #convert reading to a floating point number
orientation_x = (radians(float(ax))*2/19)
orientation_y = (radians(float(ay))*2/19)
orientation_z = (radians(float(az))*2/19)
measuringRod.rotate(angle = orientation_x, axis = vector(1,0,0))#Change the length of your measuring rod to your last measurement
measuringRod.rotate(angle = orientation_y, axis = vector(0,1,0))#Change the length of your measuring rod to your last measurement
measuringRod.rotate(angle = orientation_z, axis = vector(0,0,1))#Change the length of your measuring rod to your last measurement
print ax,ay,az
#myLabel= 'Target angle is: ' + myData #Create label by appending string myData to string
#lengthLabel.text = myLabel #display updated myLabel
Comments
Post a Comment