Sample Scripts

Official forum for open source FreePIE discussion and development.
Post Reply
User avatar
brantlew
Petrif-Eyed
Posts: 2221
Joined: Sat Sep 17, 2011 9:23 pm
Location: Menlo Park, CA

Sample Scripts

Post by brantlew »

Since there really is not much documentation for FreePIE, I think the next best thing is just to supply working sample scripts.

This first script is for simple two-handed Wiimote button control of SkyRim. Note: this is only button control so don't expect to be swinging your Wiimotes to emulate sword-play just yet :)

Code: Select all

MOUSE_SPEED = 3;

-- Right Wiimote

-- mouse cursor/ aiming
if (wiimote[0]:getRight()) then
   mouse.DeltaX = MOUSE_SPEED;
elseif (wiimote[0]:Left()) then
   mouse.DeltaX = -MOUSE_SPEED;
end

if (wiimote[0]:getUp()) then
   mouse.DeltaY = -MOUSE_SPEED;
elseif (wiimote[0]:getDown()) then
   mouse.DeltaY = MOUSE_SPEED;
end

-- right attack
mouse.LeftButton = wiimote[0]:getB();

-- jump
keyboard:setKey(Key.Space, wiimote[0]:getA());

-- crouch/sneak toggle
keyboard:setKey(Key.LeftControl, wiimote[0]:getMinus());

-- run toggle
keyboard:setKey(Key.CapsLock, wiimote[0]:getPlus());

-- inventory
keyboard:setKey(Key.Tab, wiimote[0]:getHome());

-- Enter
keyboard:setKey(Key.Return, wiimote[0]:getOne());

-- Esc
keyboard:setKey(Key.Escape, wiimote[0]:getTwo());


-- Left Wiimote

-- character motion
keyboard:setKey(Key.W, wiimote[1]:getUp());
keyboard:setKey(Key.A, wiimote[1]:getLeft());
keyboard:setKey(Key.S, wiimote[1]:getDown());
keyboard:setKey(Key.D, wiimote[1]:getRight());
   

-- left attack
mouse.RightButton = wiimote[1]:getB();

-- use
keyboard:setKey(Key.E, wiimote[1]:getA());

-- put away weapon
keyboard:setKey(Key.R, wiimote[1]:getMinus());

-- shout
keyboard:setKey(Key.Z, wiimote[1]:getPlus());
Last edited by brantlew on Thu Jul 12, 2012 8:50 pm, edited 3 times in total.
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Sample Scripts

Post by CyberVillain »

Freetrack mouse Emulation, also checkout the curve plugin, perfect when using optical tracking
(Define a curve in the GUI, give it a name and access it from script like yaw = cuveName.getY(rawYaw))

Code: Select all

def update():
	yaw = math.degrees(freeTrack.yaw)
	roll = math.degrees(freeTrack.roll)
	pitch = math.degrees(freeTrack.pitch)
	 
	deltaYaw = filters.delta(yaw)
	deltaPitch = filters.delta(pitch)   
     
	if (enabled and hotkey):
		mouse.deltaX = deltaYaw*multiply
		mouse.deltaY = -deltaPitch*multiply

if starting:
	enabled = False
	multiply = 20
	freeTrack.update += update

hotkey = mouse.rightButton
toggle = keyboard.getPressed(Key.Z)

if toggle:
	enabled = not enabled
Last edited by CyberVillain on Mon Oct 29, 2012 4:35 pm, edited 2 times in total.
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Sample Scripts

Post by CyberVillain »

PPJoy joystick emulation

Code: Select all

def update():
	x = math.degrees(freeTrack.yaw) * ratio
	y = math.degrees(freeTrack.pitch) * ratio
	
	ppJoy[0].setAxis(AxisTypes.X, x)
	ppJoy[0].setAxis(AxisTypes.Y, y)
	
if starting:
	maxAngle = 180
	axisRange = 1000
	ratio = axisRange / maxAngle 
	freeTrack.update += update
Last edited by CyberVillain on Tue Oct 30, 2012 7:01 am, edited 2 times in total.
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Sample Scripts

Post by CyberVillain »

Updated my samples to FreePIE 0.3.115.0 syntax
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Sample Scripts

Post by CyberVillain »

android > freeTrack sample, with recenter function (Ctrl+C)

Code: Select all

def update(): 
   global yaw 
   yaw = android.yaw
   global pitch 
   pitch = -android.roll
   global roll 
   roll = android.pitch

if starting:
   centerYaw = 0
   centerPitch = 0
   centerRoll = 0
   
   yaw = 0
   pitch = 0
   roll = 0
   android.update += update

freetrack.yaw  = math.degrees(yaw - centerYaw)
freetrack.pitch =  math.degrees(pitch - centerPitch)
freetrack.roll =  math.degrees(roll - centerRoll)

if keyboard.getKeyDown(Key.LeftControl) and keyboard.getPressed(Key.C):
   centerYaw = yaw
   centerPitch = pitch
   centerRoll = roll
Last edited by CyberVillain on Sat Nov 03, 2012 1:41 pm, edited 2 times in total.
MSat
Golden Eyed Wiseman! (or woman!)
Posts: 1329
Joined: Fri Jun 08, 2012 8:18 pm

Re: Sample Scripts

Post by MSat »

This is lua? I guess what I wanted to do really could all be done in script.
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Sample Scripts

Post by CyberVillain »

MSat wrote:This is lua? I guess what I wanted to do really could all be done in script.
Yupp, the idea is that changing and manipulating a input before sending it to a emulated input is done in Lua directly in the FreePIE GUI
User avatar
mahler
Sharp Eyed Eagle!
Posts: 401
Joined: Tue Aug 21, 2012 6:51 am

Re: Sample Scripts

Post by mahler »

These scripts are now all python scripts right?
I would like to have an Android > FreeTrack script
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Sample Scripts

Post by CyberVillain »

mahler wrote:These scripts are now all python scripts right?
I would like to have an Android > FreeTrack script
All mine but the last one is, can update it when I get home. It's the one you want btw
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Sample Scripts

Post by CyberVillain »

now all mine are updated to Python
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Sample Scripts

Post by CyberVillain »

Added a script examples page to the new FreePIE homepage

http://andersmalmgren.github.com/FreePIE/samples.html
logicalChimp
One Eyed Hopeful
Posts: 43
Joined: Tue Nov 27, 2012 1:48 pm

Re: Sample Scripts

Post by logicalChimp »

For anyone else that is contemplating making a virtual desktop using a HMD + IMU with Infinite Screen (http://ynea.futureware.at/cgi-bin/infinite_screen.pl - scroll to the bottom to download v1.53, the only version with the IP interface), the following script demonstrates how to send data to Infinite Screen via its IP Interface (using an Android IMU for input):

Code: Select all

import telnetlib
import re

def update():
   yaw = math.degrees(android.googleYaw)
   pitch = math.degrees(android.googlePitch)
    
   deltaYaw = filters.delta(yaw)
   deltaPitch = filters.delta(pitch)   
   
   deltax = int(deltaYaw*multiplyYaw)
   deltay = int(deltaPitch*multiplyPitch)
   
   #send screen position change to Infinite Screen, and consume response
   tn.write('POS:'+str(deltax)+','+str(deltay)+'\n')   
   pos,match,text = tn.expect([ptn])
      
if starting:
   multiplyYaw = 200 #separate yaw/pitch for future flexibility
   multiplyPitch = 200

   #open connection to Infinite Screen
   tn = telnetlib.Telnet()   
   tn.open('127.0.0.1', 5000)

   #Consume initial welcome message
   ptn = re.compile('^.*$')
   pos,match,text = tn.expect([ptn])
   
   #hook up 'update' handler
   android.update += update

if keyboard.getKeyDown(Key.LeftControl) and keyboard.getPressed(Key.C):
   tn.write('CLOSE\n')
   pos,match,text = tn.expect([ptn])
Note that, at the moment, the connection doesn't get closed (even if you hit 'stop script'). The solution is to force the socket to close within Infinite Screen.

You'll probably also need to tweak the multiplier values to match the sensitivity of your IMU :D

If anyone knows how to do a 'stopping' equivalent to 'if starting:' so that I can do clean up, please post :)
Last edited by logicalChimp on Thu Nov 29, 2012 3:55 pm, edited 1 time in total.
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Sample Scripts

Post by CyberVillain »

logicalChimp wrote:For anyone else that is contemplating making a virtual desktop using a HMD + IMU with Infinite Screen (http://ynea.futureware.at/cgi-bin/infinite_screen.pl - scroll to the bottom to download v1.53, the only version with the IP interface), the following script demonstrates how to send data to Infinite Screen via its IP Interface (using an Android IMU for input)
Nice work! We never designed the Python egine around the possiblity for socket code like this, nice that it works :D
Currently FreePIE does not support this, what about a new FreePIE object called system?

so you can do

system.starting += starting
and
system.stopping += stopping
logicalChimp
One Eyed Hopeful
Posts: 43
Joined: Tue Nov 27, 2012 1:48 pm

Re: Sample Scripts

Post by logicalChimp »

That would do, or even just setting a property when the user clicks 'stop script' (similar to how - I guess - the 'starting' property gets set.

In the meantime, when I get back to my other machine, I'll test adding a hot-key to terminate the connection (a la the ctrl-c for centering in one of the examples above) - this would at least provide a cleaner way to terminate the running script.

Edit: Added hotkey (CTRL-C) to close the connection (this also causes the script to stop running, because the update function tries to write to the closed connection)
jimNY
One Eyed Hopeful
Posts: 4
Joined: Mon Dec 10, 2012 10:23 pm

Re: Sample Scripts

Post by jimNY »

Hi, I'm working with the Sparkfun Razor IMU and I was wondering if there is a sample script that is compatible with it. Since I'm not that good at coding and have limited knowledge in python, I was wondering if someone could point me in the right direction to get it working with FreePIE. Thanks!
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Sample Scripts

Post by CyberVillain »

jimNY wrote:Hi, I'm working with the Sparkfun Razor IMU and I was wondering if there is a sample script that is compatible with it. Since I'm not that good at coding and have limited knowledge in python, I was wondering if someone could point me in the right direction to get it working with FreePIE. Thanks!
You can use most scripts here with the razor, just replace for example
freeTrack.yaw with ahrs.yaw, I do not remember right now if the ahrs uses Radians or Degrees so you might have to add or remove the usage of math.degrees. What are you trying todo?
logicalChimp
One Eyed Hopeful
Posts: 43
Joined: Tue Nov 27, 2012 1:48 pm

Re: Sample Scripts

Post by logicalChimp »

As per CyberVillains post, it's pretty simple to use (once you've got it calibrated, which took me several attempts).
Alternatively, working strictly from memory, here is a simple script just to see the input:

Code: Select all

def update():
    Diagnostics.watch(ahrs.yaw)
    Diagnostics.watch(ahrs.pitch)
    Diagnostics.watch(ahrs.roll)

if Starting:
    ahrs.update += update

Don't forget to go to the plugins menu, select the AHRS plugin, and check that it is using the correct COM port and update frequency (frequency was correct by default for me, but I needed to change the COM port).

If you've only just got the sparkfun, you might want to have a read of https://dev.qu.tu-berlin.de/projects/sf ... i/Tutorial - it contains details on calibrating the Sparkfun, and some links to tools for visualising the output / testing it works, etc. Note that it does skip a couple of bits (such as installing a Virtual Com Port driver if you're using the USB connector), and the need to download an extra library (http://efficient-java-matrix-library.go ... l-0.21.jar) to get the enhanced magneto calibration to work, but otherwise it's pretty good.
jimNY
One Eyed Hopeful
Posts: 4
Joined: Mon Dec 10, 2012 10:23 pm

Re: Sample Scripts

Post by jimNY »

CyberVillain wrote:
jimNY wrote:Hi, I'm working with the Sparkfun Razor IMU and I was wondering if there is a sample script that is compatible with it. Since I'm not that good at coding and have limited knowledge in python, I was wondering if someone could point me in the right direction to get it working with FreePIE. Thanks!
You can use most scripts here with the razor, just replace for example
freeTrack.yaw with ahrs.yaw, I do not remember right now if the ahrs uses Radians or Degrees so you might have to add or remove the usage of math.degrees. What are you trying todo?
I think the IMU uses degrees. I'm trying to get the razor to work with the DayZ game and control the free look with the IMU. I was able to get the freetrack sample script to run with the IMU without crashing and its detecting the input (as logicalChimp suggested) but inside the game the IMU is not controlling the free look after toggling it.
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Sample Scripts

Post by CyberVillain »

jimNY lets take this talk to the new Freetrack thread not to cramp the Sample thread
logicalChimp
One Eyed Hopeful
Posts: 43
Joined: Tue Nov 27, 2012 1:48 pm

Re: Sample Scripts

Post by logicalChimp »

Updated / full Sparkfun & Infinite Screen sample script. Because the Razor Sparkfun reports absolute position (that is, absolute degrees of current rotation, not a delta from the last reported position), and Infinite Screen only accepts relative movements (ie no 'go to 600,300', only 'move 600 left, 300 down' type commands), I've had to add a bit of cruft to track/calculate the diffs myself. I've also added extra cruft to provide a 'deadzone' on movement (so that tiny head motions don't cause the desktop to 'swim around'. This makes slow pans/scans a bit jerky, but it's fine for larger movements.

Lastly it uses the new DLLs in the 'Official FreePIE thread' to close the socket cleanly on 'stopping', and the new mousebutton tracker to allow Mouse3 (in conjunction with left control) to toggle the tracker on/off (so you can take the tracker off to go to the loo without messing up your desktop :D). Note that the script starts with the tracker disabled, allowing it to calculate the starting position without burgering up the desktop (because it is very unlikely you would start the script whilst staring perfectly at 0,0,0!)

So, without further ado:

Code: Select all

import telnetlib
import re

def update():
	global lastYaw
	global lastRoll
	global lastPitch
	
	yawDiff = ahrsImu.yaw - lastYaw
	rollDiff = ahrsImu.roll - lastRoll
	pitchDiff = ahrsImu.pitch - lastPitch
	
	lastRoll = ahrsImu.roll
	
	yawShift = 0
	if (yawDiff < -1):
		yawShift = yawDiff*40
		lastYaw = ahrsImu.yaw
	if (yawDiff > 1):
		yawShift = yawDiff*40
		lastYaw = ahrsImu.yaw
	
	pitchShift = 0
	if (pitchDiff <-0.6):
		pitchShift = pitchDiff*20
		lastPitch = ahrsImu.pitch
	if (pitchDiff > 0.6):
		pitchShift = pitchDiff*20
		lastPitch = ahrsImu.pitch

	#send screen position change to Infinite Screen, and consume response
	if UpdateScreenPosition == 1:
		cmd = 'POS:'+str(int(yawShift*-1))+','+str(int(pitchShift*-1))+'\n'
		diagnostics.watch(cmd)
		tn.write(cmd)
		pos,match,text = tn.expect([ptn])

if starting:
	lastYaw = ahrsImu.yaw
	lastRoll = ahrsImu.roll
	lastPitch = ahrsImu.pitch

	UpdateScreenPosition = 0

	global tn
	tn = telnetlib.Telnet()   
	tn.open('127.0.0.1', 5000)

	#Consume initial welcome message
	ptn = re.compile('^.*$')
	pos,match,text = tn.expect([ptn])

	ahrsImu.update += update

if stopping:
	tn.write('CLOSE\n')
	pos,match,text = tn.expect([ptn])
	diagnostics.debug("finished!")

if keyboard.getKeyDown(Key.LeftControl) and mouse.getPressed(3):
	UpdateScreenPosition = 1-UpdateScreenPosition
	diagnostics.debug("Head tracker toggled, is now ["+UpdateScreenPosition+"]")

All I need now is a HMD with better ergonomics (the HMZ-T2 is a neck-killer in stock form!) and a better resolution, and it'll be perfect :) (with the T2, I get about 4 screens horizontal, 3 screens vertical, so approx resolution of 5120x2160, but the active viewable area is still only 1280x720)
Post Reply

Return to “FreePIE”