View Single Post
#1
05-19-2010, 04:24 AM
 Saz Member Join Date: Nov 2008 Posts: 36
random speed and associated coding

Hi there,

I want the code to sample a random accelerator gain from a predetermined list, but when the gain changes, the coding for the indicated speed in mph also has to change, to give the correct speed in the text box.

I put this together so far
Code:
```import viz
#Import vizjoy module
import vizjoy
import time
import math
import vizact
import vizinfo
import random

viz.displaymode(1024, 768)
viz.go(viz.FULLSCREEN)
viz.mouse(viz.OFF)
vizact.ontimer(40,viz.quit) #runs file for 40 secs

subject = viz.input('What is the participant number?')

MOVE_SPEED = 0.25 #initial speed scalar
#define random speed scalar constant for changing gain on accelerator

viz.clearcolor(0.5,0.5,1)
viz.MainView.setPosition(0,.5,0)

skybox.texture(sky)

ground.setPosition(0,0,25)

#and place it over ground

ground_position = 50

viewpoint_pos = viz.MainView.getPosition()
#check to see how close the viewpoint is to the end of the road
if road_position - viewpoint_pos[2] < 50:

global ground_position
groundcopy = ground.copy()
groundcopy.setPosition([0,0,ground_position])
ground_position +=50

for i in range(1,50):

#call a timer every frame to check if road needs to be added

#change the road texture on keypress

def UpdateJoystick():
#Get the joystick position
x,y,z = joy.getPosition()
#Get the twist of the joystick
#twist = joy.getTwist()
#Move the viewpoint forward/backward based on y-axis value
if abs(y) > 0.0001: #Make sure value is above a certain threshold
viz.MainView.move(0,0,-y*MOVE_SPEED,viz.BODY_ORI)
#Move the viewpoint left/right based on x-axis value
if abs(x) > 0.001: #Make sure value is above a certain threshold
viz.MainView.move(x*0.1,0,0,viz.BODY_ORI)
#Turn the viewpoint left/right based on twist value
#if abs(twist) > 0.001: #Make sure value is above a certain threshold
#    viz.MainView.rotate(0,1,0,twist,viz.BODY_ORI,viz.RELATIVE_WORLD)

#UpdateJoystick every frame
vizact.ontimer(0,UpdateJoystick)

#define random speed scalar constant for changing gain on accelerator
def MOVE_SPEEDSC():
global MOVE_SPEED, speedm, speedcon
MOVE_SPEED = random.choice([0.15,0.2,0.25,0.3])
vizact.ontimer(5, MOVE_SPEEDSC)

#add textbox and set position on screen
text_speed.setPosition(.15, .9 )
text_speed.fontSize (30)

old_posZ = 0
def showSpeed():

global old_posZ, speed, speedm, speedcon

current_pos = viz.MainView.getPosition()
current_posZ = current_pos[2]
distance = abs(current_posZ - old_posZ)

#get speed in meters/sec
speed = (distance/viz.elapsed())

#get speed in mph

#speedm = 4*(speed*3600)/1609.3

#chnage multiplier to reflect gain change
if MOVE_SPEED == 0.15:
speedm = 4*(speed*3600)/1609.3
speedm = 6.666666667*(speed*3600)/1609.3
if MOVE_SPEED == 0.2:
speedm = 5*(speed*3600)/1609.3
if MOVE_SPEED == 0.25:
speedm = 4*(speed*3600)/1609.3
if MOVE_SPEED == 0.3:
speedm = 3.33333333*(speed*3600)/1609.3

old_posZ = current_posZ

#enter speed in textbox rounded to 1 digit after decimal
text_speed.message(str(round(speedm,1))+ 'MPH')

vizact.ontimer(0.25, showSpeed)

#Open file in path

filename = 'gainchangeprac_.txt' + str(subject)
def getPublishedPath(filename):
if publishPath:
return '/'.join([publishPath,filename])
return filename

file = open(viz.res.getPublishedPath(filename), 'w')

def mytimer (num):

#joy_pos = str(joy.getPosition())
out = (str(round(speedm,1)) + '\n')
file.write(out)
file.flush()
print out
viz.callback( viz.TIMER_EVENT, mytimer )
viz.starttimer( 0, 0.25, viz.FOREVER ) #capturing 4 times a sec```
And it all appears to be working ok, but is difficult to verify for sure. Would you be able to cast your expert eye over this and let me know if I'm on the right track please?
Any help greatly appreciated!