It is currently Mon Apr 22, 2019 1:03 am



Reply to topic  [ 20 posts ] 
 Sample Scripts 
Author Message
Petrif-Eyed
User avatar

Joined: Sat Sep 17, 2011 9:23 pm
Posts: 2218
Location: Menlo Park, CA
Reply with quote
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:
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.



Thu Jun 14, 2012 8:15 am
Profile
Petrif-Eyed

Joined: Mon Jun 22, 2009 8:36 am
Posts: 2160
Location: Stockholm, Sweden
Reply with quote
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:
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

_________________
FreePIE
My blog


Last edited by CyberVillain on Mon Oct 29, 2012 4:35 pm, edited 2 times in total.



Fri Jun 15, 2012 1:22 am
Profile
Petrif-Eyed

Joined: Mon Jun 22, 2009 8:36 am
Posts: 2160
Location: Stockholm, Sweden
Reply with quote
PPJoy joystick emulation

Code:
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

_________________
FreePIE
My blog


Last edited by CyberVillain on Tue Oct 30, 2012 7:01 am, edited 2 times in total.



Tue Jun 26, 2012 2:07 pm
Profile
Petrif-Eyed

Joined: Mon Jun 22, 2009 8:36 am
Posts: 2160
Location: Stockholm, Sweden
Reply with quote
Updated my samples to FreePIE 0.3.115.0 syntax

_________________
FreePIE
My blog


Thu Jul 12, 2012 6:13 pm
Profile
Petrif-Eyed

Joined: Mon Jun 22, 2009 8:36 am
Posts: 2160
Location: Stockholm, Sweden
Reply with quote
android > freeTrack sample, with recenter function (Ctrl+C)

Code:
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

_________________
FreePIE
My blog


Last edited by CyberVillain on Sat Nov 03, 2012 1:41 pm, edited 2 times in total.



Sat Jul 14, 2012 10:42 am
Profile
Golden Eyed Wiseman! (or woman!)

Joined: Fri Jun 08, 2012 8:18 pm
Posts: 1329
Reply with quote
This is lua? I guess what I wanted to do really could all be done in script.


Sat Sep 15, 2012 6:31 pm
Profile
Petrif-Eyed

Joined: Mon Jun 22, 2009 8:36 am
Posts: 2160
Location: Stockholm, Sweden
Reply with quote
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

_________________
FreePIE
My blog


Sun Sep 16, 2012 4:28 am
Profile
Sharp Eyed Eagle!
User avatar

Joined: Tue Aug 21, 2012 6:51 am
Posts: 401
Reply with quote
These scripts are now all python scripts right?
I would like to have an Android > FreeTrack script


Sat Nov 03, 2012 11:46 am
Profile
Petrif-Eyed

Joined: Mon Jun 22, 2009 8:36 am
Posts: 2160
Location: Stockholm, Sweden
Reply with quote
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

_________________
FreePIE
My blog


Sat Nov 03, 2012 1:06 pm
Profile
Petrif-Eyed

Joined: Mon Jun 22, 2009 8:36 am
Posts: 2160
Location: Stockholm, Sweden
Reply with quote
now all mine are updated to Python

_________________
FreePIE
My blog


Sat Nov 03, 2012 1:41 pm
Profile
Petrif-Eyed

Joined: Mon Jun 22, 2009 8:36 am
Posts: 2160
Location: Stockholm, Sweden
Reply with quote
Added a script examples page to the new FreePIE homepage

http://andersmalmgren.github.com/FreePIE/samples.html

_________________
FreePIE
My blog


Fri Nov 09, 2012 9:29 am
Profile
One Eyed Hopeful

Joined: Tue Nov 27, 2012 1:48 pm
Posts: 43
Reply with quote
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:
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.



Thu Nov 29, 2012 7:04 am
Profile
Petrif-Eyed

Joined: Mon Jun 22, 2009 8:36 am
Posts: 2160
Location: Stockholm, Sweden
Reply with quote
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

_________________
FreePIE
My blog


Thu Nov 29, 2012 10:06 am
Profile
One Eyed Hopeful

Joined: Tue Nov 27, 2012 1:48 pm
Posts: 43
Reply with quote
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)


Thu Nov 29, 2012 10:10 am
Profile
One Eyed Hopeful

Joined: Mon Dec 10, 2012 10:23 pm
Posts: 4
Reply with quote
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!


Mon Dec 10, 2012 10:28 pm
Profile
Petrif-Eyed

Joined: Mon Jun 22, 2009 8:36 am
Posts: 2160
Location: Stockholm, Sweden
Reply with quote
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?

_________________
FreePIE
My blog


Tue Dec 11, 2012 2:40 am
Profile
One Eyed Hopeful

Joined: Tue Nov 27, 2012 1:48 pm
Posts: 43
Reply with quote
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:
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-razor-9dof-ahrs/wiki/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.googlecode.com/files/ejml-0.21.jar) to get the enhanced magneto calibration to work, but otherwise it's pretty good.


Tue Dec 11, 2012 4:48 am
Profile
One Eyed Hopeful

Joined: Mon Dec 10, 2012 10:23 pm
Posts: 4
Reply with quote
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.


Tue Dec 11, 2012 9:58 am
Profile
Petrif-Eyed

Joined: Mon Jun 22, 2009 8:36 am
Posts: 2160
Location: Stockholm, Sweden
Reply with quote
jimNY lets take this talk to the new Freetrack thread not to cramp the Sample thread

_________________
FreePIE
My blog


Tue Dec 11, 2012 2:07 pm
Profile
One Eyed Hopeful

Joined: Tue Nov 27, 2012 1:48 pm
Posts: 43
Reply with quote
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:
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)


Tue Dec 11, 2012 5:43 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 20 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by STSoftware.