writing joystick position to a data file   
		
		
		Hi there, 
I'm after creating data files for each of my subjects that gives me the position of the joystick as they are driving down my virtual road. I have the following code:
 
	Code: 
	
 import viz 
#Import vizjoy module 
import vizjoy   
import time 
import math 
viz.go(viz.FULLSCREEN) 
viz.mouse(viz.OFF) 
 
 
subject = viz.input('What is the participant number?') 
 
#define speed 
MOVE_SPEED = 0.5 #scalar constant for speed gain 
 
#Add a joystick  
joy = vizjoy.add()  
 
viz.clearcolor(0.5,0.5,1) 
viz.MainView.setPosition(0,.5,0) 
 
sky = viz.add(viz.ENVIRONMENT_MAP,'sky.jpg') 
 
skybox = viz.add('skydome.dlc') 
skybox.texture(sky) 
 
#there are two road textures with different line count in the middle 
#road = viz.add('road.jpg') 
road = viz.add('roadld.jpg') 
#road2 = viz.add('road2.jpg') 
 
 
#add the ground 
ground = viz.add('tut_ground.wrl') 
ground.setPosition(0,0,25) 
 
#create a texture quad to add the road texture to 
#and place it over ground 
quad = viz.addTexQuad() 
quad.setScale(1,2,1) 
quad.setPosition(0,.1,0) 
quad.setEuler(0,90,0) 
quad.texture(road) 
 
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() 
                groundcopy.setPosition([0,0,ground_position]) 
                ground_position +=50 
                 
                #add 50 meters of road 
                for i in range(1,50): 
                        quadcopy = quad.copy() 
                        quadcopy.setPosition([0,.1,road_position]) 
                        quadcopy.setEuler(0,90,0) 
                        road_position +=1 
 
addRoad() 
#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.001: #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) 
 
def mytimer( num ): 
  print joy.getPosition() 
viz.callback( viz.TIMER_EVENT, mytimer ) 
viz.starttimer( 0, 0.5, viz.FOREVER )  
 
 
#write out to a file 
start_time = time.time() 
speed_data = open('speed_' + str(subject),'w') 
# Define a function that saves data 
# Define a function that saves data 
def SaveData(currenttime, key): 
    # Create the output string 
    out = str(currenttime) + '\t' + key + '\n'                
    # Write the string to the output file 
    file.write(out)                                      
    # Makes sure the file data is really written to the harddrive 
    file.flush()                                         
    print out 
    
# Define a function that is called every time a keyboard button is pressed 
def mykeyboard(key):           
    # Calls the function SaveData and hands it the current time and key 
    SaveData(viz.tick(),key) 
 
# Defines a callback for keyboard events 
viz.callback(viz.KEYBOARD_EVENT, mykeyboard) 
 but obviously the "key" coding for the SaveData and "mykeyboard" part is not going to work but I can't find out the equivalent code to insert for the joystick position. It outputs the joystick position to the screen ok so I just want it to write to a file
 
Any advice is greatly appreciated!  
	 |