WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1  
Old 12-15-2008, 06:57 AM
Saz Saz is offline
Member
 
Join Date: Nov 2008
Posts: 36
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!
Reply With Quote
  #2  
Old 12-15-2008, 05:10 PM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
You can get the joystick position and convert it to a string and output that to the file

Code:
joy_pos = str(joy.getPosition())
# Create the output string
out = str('current position' + '\t' + joy_pos + '\n')
# Write the string to the output file
file.write(out)                                     
# Makes sure the file data is really written to the harddrive
file.flush()
Reply With Quote
  #3  
Old 12-16-2008, 03:23 AM
Saz Saz is offline
Member
 
Join Date: Nov 2008
Posts: 36
joystick position

That's brilliant thanks for that Jeff, but when I run it and open the file, there is no output recorded at all. Would I have to incorporate a timer loop/callback event as well?
Reply With Quote
  #4  
Old 12-17-2008, 06:18 AM
Saz Saz is offline
Member
 
Join Date: Nov 2008
Posts: 36
joystick position

No worries Jeff - I've now worked out how to do it
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Crashing when I select Tracker option GoldenSun Vizard 4 07-01-2008 12:06 PM
Flagging the Data Elittdogg Vizard 5 04-11-2008 12:40 PM
Writing text files with executable adimov Vizard 1 03-21-2008 04:21 PM
Get position data in an own little program Researcher Precision Position Tracker (PPT) 2 02-01-2006 03:55 AM
problem with joystick data and input dialog box baf1 Vizard 4 03-03-2005 09:00 AM


All times are GMT -7. The time now is 08:35 AM.


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