PDA

View Full Version : Converting floats


starlingstm
12-20-2011, 01:55 PM
I'm working on a project that instruct students on how to connect a 6 Bar Quick Return using ARtoolkit. So far I'm able to track the marker and receive/print the position. My goal: when the students match the assigned location of a marker, their prompted to move to the next connection. I wrote an 'if' statement with assigned location for the x,y,z coordinates, but I need to know how to equate the x,y,z coordinates with a pre-set number. Below is my coding. I don't get an error message, the code skips down to the else statement. Some how I need to convert the float to integers or vise versa.

import viz
import vizact
import vizshape
viz.go()

ar = viz.add('artoolkit.dle')

camera = ar.addWebCamera()
board = camera.addMatrixMarker(0,width=1000)
camera.setGlobalMarker(board)



box = camera.addMatrixMarker(1,width=1000)


def showData():
for number in range(5):
box_pos = box.getPosition(viz.REL_GLOBAL)
for pos in box_pos:
if box_pos [0] == -2.0 and box_pos[1] == -3.0 and box_pos[2] == 14.0:
print box_pos, 'Box is connected properly; Move to the next connection'

else:
print box_pos



vizact.ontimer(2,showData)

farshizzo
12-20-2011, 03:47 PM
You can use the round function to round a floating point number to the nearest integer value:value = 5.3

if round(value) == 5.0:
print 'Is equal to 5'
If you want the integer component without rounding, then you can use the int function.

starlingstm
12-23-2011, 11:36 AM
Thanks, that corrected my issue. However this only works for a stationary camera, but my project involves the use of a HMD mounted camerea and this causes the positions and angles to change as the camera moves. How should I go about solving this problem. I have posted the most recent coding below:

import viz
import vizact
import vizshape
viz.go()

ar = viz.add('artoolkit.dle')

camera = ar.addWebCamera()
board = camera.addMatrixMarker(0,width=1000)
camera.setGlobalMarker(board)



box = camera.addMatrixMarker(1,width=1000)

arm = camera.addMatrixMarker(2,width=1000)

def showData():
for number in range(5):
box_pos = box.getPosition(viz.ABS_GLOBAL)
for pos in box_pos:
if round(box_pos [0]) == -3.0 and round(box_pos[1]) == 5.0 and round(box_pos[2]) == 5.0:
print box_pos, 'great job'

else:
print box_pos



vizact.ontimer(2,showData)

Jeff
12-23-2011, 02:58 PM
The artoolkit plug-in should support multiple cameras. Perhaps you can have one stationary camera that's just used for getting the position of the marker. You can call ar.addCamera for each camera you have attached.

starlingstm
01-04-2012, 05:35 PM
Thanks, I was trying to apply the INT function, but the ROUND function works great! I'm struggling now with ending a loop within a function. I've tried to apply the BREAK and SYS Exit functions but its yielding the results I'm seeking.

Goal: When the first function is true, the student must reply accordingly and this part of the program will no execute. The student moves to the second function and when its true, student will reply and this also will no longer execute... and so on with the remaining functions. Instead, when the program is ran it continues to loop through each function.

import viz
import vizact
import vizshape
viz.go()

ar = viz.add('artoolkit.dle')
#Setting Board as Reference Marker
camera = ar.addWebCamera()
board = camera.addMatrixMarker(0,width=1000)
camera.setGlobalMarker(board)


#Assigning Bars to Markers
bar_a = camera.addMatrixMarker(1,width=1000)
bar_b = camera.addMatrixMarker(2,width=1000)
bar_c = camera.addMatrixMarker(3,width=1000)
bar_d = camera.addMatrixMarker(4,width=1000)
bar_e = camera.addMatrixMarker(5,width=1000)
bar_f = camera.addMatrixMarker(6,width=1000)





#Connecting Bar A
def barA():
for number in range(1):
bar_a_pos = bar_a.getPosition(viz.ABS_GLOBAL)
bar_a_ang = bar_a.getEuler(viz.ABS_GLOBAL)
for pos in bar_a_pos and bar_a_ang:
print 'You now have bar a'
if round(bar_a_pos [0]) <= -1.0 and round(bar_a_pos[0]) >= -4.0 and round(bar_a_pos[1]) >= 5.0 and round(bar_a_pos[1]) <= 8.0 and round(bar_a_pos[2]) >= 5.0 and round(bar_a_pos[2]) <= 8.0:
if round(bar_a_ang [0]) <= -39.0 and round(bar_a_ang[0]) >= -42.0 and round(bar_a_ang[1]) >= 5.0 and round(bar_a_ang[1]) <= 8.0 and round(bar_a_ang[2]) >= 3.0 and round(bar_a_ang[2]) <= 6.0:
input("Bar A has been place correctly, Mount with screws then type done")
return
else:
print 'Check position or orientation for Bar A', bar_a_pos, bar_a_ang
return


#Printing Bar A position and orientation
vizact.ontimer(2,barA)
#Connecting Bar B
def barB():
for number in range(1):
bar_b_pos = bar_b.getPosition(viz.ABS_GLOBAL)
bar_b_ang = bar_b.getEuler(viz.ABS_GLOBAL)
for pos in bar_b_pos and bar_b_ang:
if round(bar_b_pos [0]) >= -5.0 and round(bar_b_pos[0]) <= -2.0 and round(bar_b_pos[1]) >= 0.0 and round(bar_b_pos[1]) <= 3.0 and round(bar_b_pos[2]) >= 0.0 and round(bar_b_pos[2]) <= 3.0:
if round(bar_b_ang [0]) >= 173.0 and round(bar_b_ang[0]) <= 176.0 and round(bar_b_ang[1]) >= 6.0 and round(bar_b_ang[1]) <= 9.0 and round(bar_b_ang[2]) >= -5.0 and round(bar_b_ang[2]) <= -2.0:
input("Bar B has been placed correctly, Mount with screws then type done")
return
else:
print 'Check position or orientation for Bar B', bar_b_pos, bar_b_ang
return


#Printing Bar B position and orientation
vizact.ontimer(2,barB)

#Connecting Bar
def barC():
for number in range(1):
bar_c_pos = bar_c.getPosition(viz.ABS_GLOBAL)
bar_c_ang = bar_c.getEuler(viz.ABS_GLOBAL)
for pos in bar_c_pos and bar_c_ang:
if round(bar_c_pos [0]) <= -6.0 and round(bar_c_pos[0]) >= -8.0 and round(bar_c_pos[1]) >= 1.0 and round(bar_c_pos[1]) <= 4.0 and round(bar_c_pos[2]) >= 0.0 and round(bar_c_pos[2]) <= 3.0:
if round(bar_c_ang [0]) >= 86.0 and round(bar_c_ang[0]) <= 89.0 and round(bar_c_ang[1]) <= -2.0 and round(bar_c_ang[1]) >= -6.0 and round(bar_c_ang[2]) <= -7.0 and round(bar_c_ang[2]) >= -10.0:
input("Bar C has been place correctly, Mount with screws then type done")
return
else:
print 'Check position or orientation for Bar C', bar_c_pos, bar_c_ang
return


#Printing Bar C position and orientation
vizact.ontimer(2,barC)

#Connecting Bar D
def barD():
for number in range(1):
bar_d_pos = bar_d.getPosition(viz.ABS_GLOBAL)
bar_d_ang = bar_d.getEuler(viz.ABS_GLOBAL)
for pos in bar_d_pos and bar_d_ang:
if round(bar_d_pos [0]) >= 1.0 and round(bar_d_pos[0]) <= 4.0 and round(bar_d_pos[1]) >= 0.0 and round(bar_d_pos[1]) <= 3.0 and round(bar_d_pos[2]) <= 0.0 and round(bar_d_pos[2]) >= -3.0:
if round(bar_d_ang [0]) >= 90.0 and round(bar_d_ang[0]) <= 93.0 and round(bar_d_ang[1]) >= 0.0 and round(bar_d_ang[1]) <= 3.0 and round(bar_d_ang[2]) <= 0.0 and round(bar_d_ang[2]) >= -3.0:
input("Bar D has been place correctly, Mount with screws then type done")
return
else:
print 'Check position or orientation for Bar D', bar_d_pos, bar_d_ang
return


#Printing Bar D position and orientation
vizact.ontimer(2,barD)

#Connecting Bar E
def barE():
for number in range(1):
bar_e_pos = bar_e.getPosition(viz.ABS_GLOBAL)
bar_e_ang = bar_e.getEuler(viz.ABS_GLOBAL)
for pos in bar_e_pos and bar_e_ang:
if round(bar_e_pos [0]) <= -1.0 and round(bar_e_pos[0]) >= -4.0 and round(bar_e_pos[1]) >= 5.0 and round(bar_e_pos[1]) <= 8.0 and round(bar_e_pos[2]) >= 5.0 and round(bar_e_pos[2]) <= 8.0:
if round(bar_e_ang [0]) <= -39.0 and round(bar_e_ang[0]) >= -42.0 and round(bar_e_ang[1]) >= 5.0 and round(bar_e_ang[1]) <= 8.0 and round(bar_e_ang[2]) >= 3.0 and round(bar_e_ang[2]) <= 6.0:
input("Bar E has been place correctly, Mount with screws then type done")
return
else:
print 'Check position or orientation for Bar E', bar_e_pos, bar_e_ang
return


#Printing Bar E position and orientation
vizact.ontimer(2,barE)

#Connecting Bar F
def barF():
for number in range(1):
bar_f_pos = bar_f.getPosition(viz.ABS_GLOBAL)
bar_f_ang = bar_f.getEuler(viz.ABS_GLOBAL)
for pos in bar_f_pos and bar_f_ang:
if round(bar_f_pos [0]) <= -1.0 and round(bar_f_pos[0]) >= -4.0 and round(bar_f_pos[1]) >= 5.0 and round(bar_f_pos[1]) <= 8.0 and round(bar_f_pos[2]) >= 5.0 and round(bar_f_pos[2]) <= 8.0:
if round(bar_f_ang [0]) <= -39.0 and round(bar_f_ang[0]) >= -42.0 and round(bar_f_ang[1]) >= 5.0 and round(bar_f_ang[1]) <= 8.0 and round(bar_f_ang[2]) >= 3.0 and round(bar_f_ang[2]) <= 6.0:
input("Bar A has been place correctly, Mount with screws then type done")
return
else:
print 'Check position or orientation for Bar F', bar_f_pos, bar_f_ang
return


#Printing Bar F position and orientation
vizact.ontimer(2,barF)

#Running Functions
barA()

barB()

barC()

barD()

barE()

barF()

Jeff
01-05-2012, 04:28 PM
You could rewrite your code to use a task function. Each of the functions you want to wait for could be a subtask. For more on this topic take a look at the viztask documentation.