WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Old 12-15-2008, 05:57 AM
Saz Saz is offline
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:
import viz
#Import vizjoy module
import vizjoy  
import time
import math

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() 


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

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

#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')

#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.001: #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

#UpdateJoystick every frame

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
    # Makes sure the file data is really written to the harddrive
    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

# 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
Old 12-15-2008, 04: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

joy_pos = str(joy.getPosition())
# Create the output string
out = str('current position' + '\t' + joy_pos + '\n')
# Write the string to the output file
# Makes sure the file data is really written to the harddrive
Reply With Quote
Old 12-16-2008, 02:23 AM
Saz Saz is offline
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
Old 12-17-2008, 05:18 AM
Saz Saz is offline
Join Date: Nov 2008
Posts: 36
joystick position

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

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 11:06 AM
Flagging the Data Elittdogg Vizard 5 04-11-2008 11:40 AM
Writing text files with executable adimov Vizard 1 03-21-2008 03:21 PM
Get position data in an own little program Researcher Precision Position Tracker (PPT) 2 02-01-2006 02:55 AM
problem with joystick data and input dialog box baf1 Vizard 4 03-03-2005 08:00 AM

All times are GMT -7. The time now is 07:41 AM.

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