WorldViz User Forum

WorldViz User Forum (
-   Vizard (
-   -   random speed and associated coding (

Saz 05-19-2010 04:24 AM

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

import viz
#Import vizjoy module
import vizjoy 
import time
import math
import vizact
import vizinfo
import random

viz.displaymode(1024, 768)
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

#Add a joystick
joy = vizjoy.add()


sky = viz.add(viz.ENVIRONMENT_MAP,'sky.jpg')

skybox = viz.add('skydome.dlc')

#add the road texture
road = viz.add('roadld3.png')
#road2 = viz.add('roadsd.png')

#add the ground
ground = viz.add('tut_ground.wrl')

#create a texture quad to add the road texture to
#and place it over ground
quad = viz.addTexQuad()

road_position = 0
ground_position = 50
#add road and ground if getting near the end of road
def addRoad():
        global road_position, ground_position
        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:
                #add 50 meters of ground
                global ground_position
                groundcopy = ground.copy()
                ground_position +=50
                #add 50 meters of road
                for i in range(1,50):
                        quadcopy = quad.copy()
                        road_position +=1

#call a timer every frame to check if road needs to be added
vizact.ontimer(0, addRoad)

#change the road texture on keypress
#vizact.onkeydown('1', quad.texture,road2)
#vizact.onkeydown('2', quad.texture,road)
vizact.onmousedown(viz.MOUSEBUTTON_LEFT, quad.texture,road)
#vizact.onmousedown(viz.MOUSEBUTTON_LEFT, quad.texture,road2)

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
    #Move the viewpoint left/right based on x-axis value
    if abs(x) > 0.001: #Make sure value is above a certain threshold
    #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

#define random speed scalar constant for changing gain on accelerator
        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 = viz.addTextbox(parent = viz.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):
        #filename = 'adapt_.txt' + str(subject)
        publishPath = viz.getOption('Y:\backup\Vizard30\resources\road\_.txt','')
        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')
        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!

Saz 05-20-2010 04:03 AM

Having carried out some testing on the code it seems that the program isn't changing the mph coding to reflect the associated change in accelerator gain.

Is the code in the wrong postition? Or have I just cocked it up completely with incorrect coding?

All times are GMT -7. The time now is 09:18 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2021, vBulletin Solutions, Inc.
Copyright 2002-2018 WorldViz LLC