Getting Magnetic Sensor Info using Python / SL4A

1,099 views
Skip to first unread message

electricpete

unread,
Oct 7, 2012, 3:40:00 AM10/7/12
to tas...@googlegroups.com

I used the following script  to attempt to retrieve magnetic sensor information:

import android, time
droid = android.Android()
droid.startSensingTimed(1, 250)
time.sleep(1)
s5 = droid.sensorsReadMagnetometer().result
droid.stopSensing()
droid.setClipboard(s5)

 
I’m no Python programmer, but it seems to me a straightforward combination of these two script examples:
1 – script example for retrieving sensor info into Python variablesL
http://www.mithril.com.au/android/doc/SensorManagerFacade.html
 
2 - script from Baudi for putting something into the clipboard:
http://tasker.wdfiles.com/local--files/filetoclip/CopyFileToClipboard.py
 
The problem is, the clipboard comes up empty.
I don’t think it’s my installation... I can run the example scripts like “hello_world.py” just fine either from tasker or from sl4a
I put my above script (magnetic.py) into the same directory as “hello_world.py” and it doesn’t work in either tasker or from sl4a
I also tried starting a separate compass application “super compass” (in order to ensure the sensor was putting out values) and then repeated the test... same result.
 
Any ideas why this didn’t work?  Is there another way to pass a Python variable back to Tasker other than thru the clipboard?

electricpete

unread,
Oct 7, 2012, 12:14:36 PM10/7/12
to tas...@googlegroups.com
I started the command prompt by Running SL4A and selecting View / Interpreters / Python

Then I ran the program myself.  Cut/paste of the seesion is as follows:

>>> import android, time
>>> droid = android.Android()
>>> droid.startSensingTimed(1,250)

Result(id=1, result=None, error=None)
>>> time.sleep(1)
>>> s5 = droid.sensorsReadMagnetometer().result
>>> droid.setClipboard(s5)
com.googlecode.android_scripting.rpc.RpcError: A
rgument 1 should be of type String.
Result(id=3, result=None, error=u'com.googlecode
.android_scripting.rpc.RpcError: Argument 1 shou
ld be of type String.')
>>> s5
[-24.239999999999998, -47.340000000000003, -9.77
99999999999994]
>>>


What you'll notice is:
1 - There is an error message indicating that setclipboard wants a string.
2 - the variable s5 was indeed populated with expected vector of three numbers indicating field strength in three axes.

******So my question is, how do I get the value of s5 into Tasker, if I can't use the setclipboard command?
Thanks in advance for any suggestions or hints.

baudi

unread,
Oct 7, 2012, 1:53:34 PM10/7/12
to tas...@googlegroups.com
I've written a Tasker python helper called tasker.py. It has a few handy features, such as the ability to call one function to retrieve and create all the parameters passed to your python script, set a tasker variable, and redirect standard error to a tasker variable.

To use it, do this.

import tasker

# If you want to redirect Stderr to a Tasker variable
sys.stderr = tasker.Stderr(__file__)
# To retrieve and create all passed params. Refer to them as t.MyPassedParamNameWithoutThe% 
p = tasker.Params()
# To create and set a Tasker variable
t = tasker.Task()
t.set_var_now( paramName, paramValue)

In your case, you'd use
t.set_var_now("%S5", s5) 
Which would set a global Tasker variable named %S5 to the value of the sensor.

Or you could just use a python function to convert s5 to a string and then use the clipboard. Actually, you may have to do that with t.set_var_now, but I'm not sure.

I've attached a copy of tasker.py.



tasker.py

baudi

unread,
Oct 7, 2012, 1:56:20 PM10/7/12
to tas...@googlegroups.com
Typo in one spot.

Refer to them as p.MyPassedParamNameWithoutThe%   (not t....)

electricpete

unread,
Oct 7, 2012, 6:31:29 PM10/7/12
to tas...@googlegroups.com
Thanks Baudi, I was hoping you'd reply.

The str() function did in fact work as you suggested:

import android
import time
droid = android.Android()
droid.startSensingTimed(3, 250)
time.sleep(1)
s5 = droid.sensorsReadMagnetometer().result
droid.stopSensing()
s5str = str(s5)
droid.setClipboard(s5str)
droid.makeToast("completed putting mag field into clipboard")

Your other routines look really useful. It would certainly be great to be able to see the error messages without having to step through at the command prompt.
I wasn't quite able to get them to work. The urgency has waned now that I tried str but I'll defimitely study them some more when I get a chance. Would definitely be a good thing to end up on the wiki.

Thanks again for your help on this thread as well as the other great stuff you've posted here and on the wiki.  Very useful stuff!!!!!

electricpete

unread,
Oct 7, 2012, 8:35:55 PM10/7/12
to tas...@googlegroups.com

If anyone’s interested, the final code I used to determine heading as angle measured CW from North:

 Tasker Routine:

A1: Run SL4A Script [ Name:ReadOrientation.py Terminal:Off Pass Variables: ]

A2: Wait [ MS:0 Seconds:4 Minutes:0 Hours:0 Days:0 ]

A3: Variable Set [ Name:%Heading To:%CLIP Do Maths:Off Append:Off ]

A4: Variable Split [ Name:%Heading Splitter:[ Delete Base:On ]

A5: Variable Split [ Name:%Heading2 Splitter:, Delete Base:Off ]

A6: Variable Set [ Name:%HeadingDeg To:%Heading21 * 180/3.14159 Do Maths:On Append:Off ]

A7: Popup [ Title: Text:%HeadingDeg Background Image: Layout:Popup Timeout (Seconds):17 Show Over Keyguard:On ]

 

The python script ReadOrientation.py is as follows:

import android

import time

droid = android.Android()

droid.startSensingTimed(1, 250)

time.sleep(1)

s5 = droid.sensorsReadOrientation().result

droid.stopSensing()

s5str = str(s5)

droid.setClipboard(s5str)

droid.makeToast("completed putting orientation into clipboard")

 

Notes:

1 – I changed sensorsReadMagnetometer().result to sensorsReadOrientation().result.  The latter is the one that is most suitable for telling us the heading.

2 – The “wait” statement is necessary because otherwise Tasker proceeds without waiting for the SL4A script to finish and you’ll get an old value from the previous time the routine was run. It’s odd that the steps aren’t sequential but apparenlty Tasker just launches the script and doesn’t wait for it to finish before proceeding to the next step

3 – The heading is based on the position of the phone, but is valid as you change positions of the phone.  Let me say it another way.  Assune say you’re standing facing a given direction with your phone directly in front of you.  Then it tells you the direction that you are facing and it gives the same correct direction regardless of whether you are holding the phone perpendicular to your body (phone face up) or parallel to your body (phone facing you).

baudi

unread,
Oct 7, 2012, 9:00:52 PM10/7/12
to tas...@googlegroups.com
You probably had a problem with tasker.py because you need to set the preference Misc / Allow External Access. Forgot to mention that.


10amla

unread,
Oct 8, 2012, 4:05:57 AM10/8/12
to tas...@googlegroups.com
On note 2, what I do is load something known into the clipboard and use 'wait until' that info is changed.
Reply all
Reply to author
Forward
0 new messages