Page 1 of 1

Output joystick data to file

Posted: Fri Jun 19, 2015 5:35 am
by samworthington
Is it possible to use FreePIE to write the data from a 4-axis joystick to a text file? Preferably with rX, rY and rZ values comma separated and each time-stamp on a new line?

Many thanks,

Sam

Re: Output joystick data to file

Posted: Fri Jun 19, 2015 6:50 am
by CyberVillain
Google System.IO

There are a lot of classes in that names space that could be used for this. I'm on the run so can't write a concretecexample example

Re: Output joystick data to file

Posted: Fri Jun 19, 2015 7:00 am
by samworthington
That sounds promising. I'm a mechanical engineer so all this coding business is magic to me. If you could put an example together when you've made good your escape, that would be really appreciated.

Sam

Re: Output joystick data to file

Posted: Sat Jun 20, 2015 9:23 am
by FrenchyKiel
you could use this code

Code: Select all

from ctypes import windll

if starting:
	system.setThreadTiming(TimingTypes.HighresSystemTimer)
	system.threadExecutionInterval = 30
	l = 0
	t = windll.kernel32.GetTickCount() 
	file = open("d:\\newfile.txt", "w")
	
l = windll.kernel32.GetTickCount() - t
t = windll.kernel32.GetTickCount()

x = joystick[1].x
y = joystick[1].y 
file.write(str(t) + "," + str(x) + "," + str(y) + "\n")

if keyboard.getPressed(Key.D):
	file.close()
all coding about variable t and l and the fisrt line is not necessary

you could adapt the code as you want

once you type ket D, the file is close and you ll have an error I/O because the program try to write in a file closed..

Thierry

Re: Output joystick data to file

Posted: Sun Jun 21, 2015 11:18 am
by samworthington
Thank you very much for this.

I've modified the code as follows, but I'm just struggling a bit getting it to work. It gives me the error "An item with the same key has already been added"

if starting:
system.setThreadTiming(TimingTypes.HighresSystemTimer)
system.threadExecutionInterval = 30
file = open("c:\\newfile.txt", "w")

l = windll.kernel32.GetTickCount() - t
t = windll.kernel32.GetTickCount()

x = joystick[1].x
y = joystick[1].y
file.write(str(t) + "," + str(x) + "," + str(y) + "\n")

if keyboard.getPressed(Key.D):
file.close()

Re: Output joystick data to file

Posted: Sun Jun 21, 2015 12:59 pm
by FrenchyKiel
its not good to write directly under c: in win7 or win 8 problem of rights, maybe it couldnt overwrite your file

try to write your file under mydocuments

Re: Output joystick data to file

Posted: Mon Jun 22, 2015 3:13 am
by samworthington
I've tried the following instead but still get the same error.

if starting:
system.setThreadTiming(TimingTypes.HighresSystemTimer)
system.threadExecutionInterval = 30
file = open("C:\Users\User\Documents\newfile.txt", "w")

l = windll.kernel32.GetTickCount() - t
t = windll.kernel32.GetTickCount()

x = joystick[1].x
y = joystick[1].y
file.write(str(t) + "," + str(x) + "," + str(y) + "\n")

if keyboard.getPressed(Key.D):
file.close()

Re: Output joystick data to file

Posted: Tue Jun 23, 2015 1:13 am
by CyberVillain
This worked for me

Code: Select all

from ctypes import windll

if starting:
   system.setThreadTiming(TimingTypes.HighresSystemTimer)
   system.threadExecutionInterval = 30
   l = 0
   t = windll.kernel32.GetTickCount() 
   file = open("c:\\temp\\freepie.txt", "w")
   x = 0
   
l = windll.kernel32.GetTickCount() - t
t = windll.kernel32.GetTickCount()

x = x + 1
y = x + 1

file.write(str(t) + "," + str(x) + "," + str(y) + "\n")

if stopping:
   file.close()

Re: Output joystick data to file

Posted: Tue Jun 23, 2015 1:16 am
by CyberVillain
btw, if you want a high rate on your scrpt and a little lower rate on the disk IO you can do

Code: Select all

from ctypes import windll

if starting:
   system.setThreadTiming(TimingTypes.HighresSystemTimer)
   system.threadExecutionInterval = 1
   l = 0
   t = windll.kernel32.GetTickCount() 
   file = open("c:\\temp\\freepie.txt", "w")
   x = 0
   onSaveFile = True
   
l = windll.kernel32.GetTickCount() - t
t = windll.kernel32.GetTickCount()


if filters.stopWatch(onSaveFile, 30):
	x = x + 1
	y = x + 1
	file.write(str(t) + "," + str(x) + "," + str(y) + "\n")

if stopping:
   file.close()

Re: Output joystick data to file

Posted: Tue Jun 23, 2015 2:15 am
by samworthington
Thank you, but I think I must be doing something wrong. The script runs but just gives me a sequence of linearly increasing numbers:

84239026,1,2
84239057,2,3
84239089,3,4
84239120,4,5
84239151,5,6
84239182,6,7
84239213,7,8
84239245,8,9
84239276,9,10
...

Which bit of code specifies reading the joystick position?

I tried changing lines 17 and 18 to:
x = joystick[1].x
y = joystick[1].y
But then get the "An item with the same key has already been added" error.

Re: Output joystick data to file

Posted: Tue Jun 23, 2015 6:41 am
by CyberVillain
I dont have a jotstick at work so replaced that. Cant really tell why it gives that error, I get

index out of range: 1 since I do not have a joystick

Re: Output joystick data to file

Posted: Mon Aug 17, 2015 4:02 am
by samworthington
I've still been trying to get this to work but havn't got anywhere. I just get the error "An item with the same key has already been added" Does anyone have any suggestions?

Thanks,

Sam

Here is the code:

from ctypes import windll

if starting:
system.setThreadTiming(TimingTypes.HighresSystemTimer)
system.threadExecutionInterval = 1
l = 0
t = windll.kernel32.GetTickCount()
file = open("c:\\temp\\freepie.txt", "w")
x = 0
onSaveFile = True

l = windll.kernel32.GetTickCount() - t
t = windll.kernel32.GetTickCount()


if filters.stopWatch(onSaveFile, 30):
x = joystick[1].x
y = joystick[1].y
file.write(str(t) + "," + str(x) + "," + str(y) + "\n")

if stopping:
file.close()