Output joystick data to file

Official forum for open source FreePIE discussion and development.
Post Reply
samworthington
One Eyed Hopeful
Posts: 12
Joined: Tue Jun 16, 2015 4:49 am

Output joystick data to file

Post 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
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Output joystick data to file

Post 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
samworthington
One Eyed Hopeful
Posts: 12
Joined: Tue Jun 16, 2015 4:49 am

Re: Output joystick data to file

Post 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
FrenchyKiel
One Eyed Hopeful
Posts: 47
Joined: Thu Oct 03, 2013 7:10 am

Re: Output joystick data to file

Post 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
samworthington
One Eyed Hopeful
Posts: 12
Joined: Tue Jun 16, 2015 4:49 am

Re: Output joystick data to file

Post 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()
FrenchyKiel
One Eyed Hopeful
Posts: 47
Joined: Thu Oct 03, 2013 7:10 am

Re: Output joystick data to file

Post 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
samworthington
One Eyed Hopeful
Posts: 12
Joined: Tue Jun 16, 2015 4:49 am

Re: Output joystick data to file

Post 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()
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Output joystick data to file

Post 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()
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Output joystick data to file

Post 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()
samworthington
One Eyed Hopeful
Posts: 12
Joined: Tue Jun 16, 2015 4:49 am

Re: Output joystick data to file

Post 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.
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Output joystick data to file

Post 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
samworthington
One Eyed Hopeful
Posts: 12
Joined: Tue Jun 16, 2015 4:49 am

Re: Output joystick data to file

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

Return to “FreePIE”