Re: Any way to set a variable to current accelerometer, gyro, and/or magnetometer values?

1,771 views
Skip to first unread message

Pent

unread,
Sep 9, 2012, 4:01:19 AM9/9/12
to Tasker

> values for a mobile device's accelerometer, gyro, and magnetometer.  The
> magnetometer, at least, appears to set %MFIELD, which I guess is the
> magnitude of the 3 components of the sensor's outputs?

No need to guess, it's described in the document you linked.

Gyroscope is totally not supported by Tasker.

Accelerometer has no corresponding variable(s).

Pent

Logan Park

unread,
Sep 9, 2012, 10:25:13 AM9/9/12
to tas...@googlegroups.com
Pent -- 

Thanks for an amazing app!  Worth the price of admission multiple times over.  My phone now does things that I have wanted it to do for a very long time.  

Based on the above answer, the best way to log accel/gyro/magnet values is then to find a third party app that might be able to get these values into Tasker?

Pent

unread,
Sep 9, 2012, 11:59:31 AM9/9/12
to Tasker
> Based on the above answer, the best way to log accel/gyro/magnet values is
> then to find a third party app that might be able to get these values into
> Tasker?

A condition plugin could theoretically do it: it can pass variable
values to the resultant Tasker task when it activates. But it's a very
(very) inefficient mechanism for sensors whose values change so quick.

Pent

10amla

unread,
Sep 9, 2012, 1:01:35 PM9/9/12
to tas...@googlegroups.com
Speaking to the accelerometer, Pent is correct; at any point in time you have 3 values and even at rest they will change.  Accelerometer Log, https://play.google.com/store/apps/details?id=RabiSoft.AccelerometerLog&feature=search_result#?t=W251bGwsMSwyLDEsIlJhYmlTb2Z0LkFjY2VsZXJvbWV0ZXJMb2ciXQ.. will give you a nice graph.  As far as capturing that data to tasker goes, I've used a SL4A script to get the data and return (in my case) a mode of transport value based on the standard deviation. 

baudi

unread,
Sep 9, 2012, 1:48:16 PM9/9/12
to tas...@googlegroups.com
I'd love to see that script. 

10amla

unread,
Sep 9, 2012, 3:23:51 PM9/9/12
to tas...@googlegroups.com
OK, I'll give it a try, it's in python.
 
droid = android.Android()
def meanstdv(x):
    from math import sqrt
    n, mean, std = len(x), 0, 0
    for a in x:
        mean = mean + a
    mean = mean / float(n)
    for a in x:
        std = std + (a - mean)**2
    std = sqrt(std / float(n-1))
    return mean, std
 
def getMOT(interval, duration):
    t = 0
    accX = []
    meanX = 0
    stdX = 0
    accY = []
    meanY = 0
    stdY = 0
    accZ = []
    meanZ = 0
    stdZ = 0
    mot = ""
    droid.startSensingTimed(2,interval)
    while (t < duration):
        acc = droid.sensorsReadAccelerometer().result
        print acc
        if acc[0] is not None:
            accX.append(acc[0])
            accY.append(acc[1])
            accZ.append(acc[2])
            t += 1
    droid.stopSensing()
    meanX, stdX = meanstdv(accX)
    meanY, stdY = meanstdv(accY)
    meanZ, stdZ = meanstdv(accZ)
    if stdY > 2:
        mot = "Walk"
    elif stdY > 0.5:
        mot = "Drive"
    else:
        mot = "Rest"
    print mot
    return mot
def confirmMOT(interval, duration):
    t = 0  
    MOTS = ['Rest', 'Drive', 'Walk']
    summot = 0
    temp_duration = round(duration/4)
    summot = MOTS.index(currMOT)
    while (t < 3):
        tempMOT = getMOT(interval, temp_duration)
        summot = summot + MOTS.index(tempMOT)
        sleep(5)
        t += 1
    avg_mot = summot/5
    raw_mot = int(round(avg_mot))
    mot = MOTS[raw_mot]
    return mot
interval = droid.prefGetValue('interval', 'prefMOT')[1]
duration = droid.prefGetValue('duration', 'prefMOT')[1]
# Need to pass prev_MOT as parameter
prevMOT = droid.getIntent().result[u'extras'][u'%prevMOT']
####Testing
#prevMOT = 'Walk'
#interval = 100
#duration = 50
####
currMOT = getMOT(interval, duration)
if currMOT <> prevMOT:
    currMOT = confirmMOT(interval, duration)
 
strMOT = currMOT
droid.setClipboard(strMOT)

baudi

unread,
Sep 9, 2012, 3:35:01 PM9/9/12
to tas...@googlegroups.com
Thanks! Playing with it now...

baudi

unread,
Sep 9, 2012, 3:51:56 PM9/9/12
to tas...@googlegroups.com
What values for interval and duration do you typically use?

10amla

unread,
Sep 9, 2012, 11:26:25 PM9/9/12
to tas...@googlegroups.com
100 for both that gives a total test time of 10 seconds (100ms = 1/10 of a second at 100 interations).

baudi

unread,
Sep 10, 2012, 9:53:17 AM9/10/12
to tas...@googlegroups.com
Thank you. I see that you only consider the Y acceleration when determining MOT. I assume you found that Y is sufficient in all situations. That's a pretty interesting result by itself, particularly since the orientation of the device might be very different in different situations (in your pocket, in a car dock, etc.).

10amla

unread,
Sep 10, 2012, 1:40:48 PM9/10/12
to tas...@googlegroups.com
So far the Y-coordinate does seem to be sufficient.  However, I'm still playing with it, off and on; it's a work in progress.  It would be quite easy to add in the others since the data's already there.  Most of my testing up to this point has been in ways I'd use it; on my desk(rest), in my pocket(walk), and on the car seat(driving).  I might try using the accelerometer log program to look at some other configurations such as docked(car or desk). 

Logan Park

unread,
Sep 11, 2012, 11:00:00 AM9/11/12
to tas...@googlegroups.com
Perfect-- this approack is great for what I'm hoping to capture.  I don't need a continuous stream of values, really only need to sample at sensible intervals to keep power down.  This should work fine for Tasker-based pedometry and stuff like that.  

Using this Python in SL4A, can we similarly access gyro and magnetometer values?  I'm at work, can't test ATM.

Thanks a ton for posting the script and method.

baudi

unread,
Sep 11, 2012, 11:06:22 AM9/11/12
to tas...@googlegroups.com

rockfan0005

unread,
Jul 4, 2013, 3:31:59 PM7/4/13
to tas...@googlegroups.com
Hi 10amla,

Thanks for posting this! I tried running your script, but each time I get the following error:

dlopen libpython2.6.so

java.lang.NullPointerException

Traceback (most recent call last):

File "/mnt/sdcard/sl4a/scripts/pythonaccelerometer.py", line 74, in <module>

currMOT = getMOT(interval, duration)

File "/mnt/sdcard/sl4a/scripts/pythonaccelerometer.py", line 39, in getMOT

meanX, stdX = meanstdv(accX)

File "/mnt/sdcard/sl4a/scripts/pythonaccelerometer.py", line 11, in meanstdv

mean = mean / float(n)

ZeroDivisionError: float division


Any idea  why I might be getting this. I'd love to play around with the script, but I am quite a novice w/ python. Any help would be greatly appreciated!


On Sunday, September 9, 2012 3:23:51 PM UTC-4, 10amla wrote:

Cyril Preiss

unread,
Dec 9, 2013, 7:48:46 AM12/9/13
to tas...@googlegroups.com
Hello, i'm trying to get accelerator values for my app.

i have installed Sl4 and run your script but nothing happens.

do you knwo what can be wrong ?

Le samedi 8 septembre 2012 22:07:43 UTC+2, Logan Park a écrit :
Hi, all, 

I have been looking through the list of variables exposed by Tasker, and I haven't seen a way yet to access the variable getters or values for a mobile device's accelerometer, gyro, and magnetometer.  The magnetometer, at least, appears to set %MFIELD, which I guess is the magnitude of the 3 components of the sensor's outputs?  I'd like to read each one.

Does Tasker support this yet?

I understand the consequences for battery life.

Thank you! 

Cyril Preiss

unread,
Dec 9, 2013, 7:57:16 AM12/9/13
to tas...@googlegroups.com
i'm stupid, i didn't see i had to pass interval and duration value...

but i don't see how to do that.Do i have to create variable with name %interval and %duration in tasker  ?

Bob Hansen

unread,
Dec 9, 2013, 9:29:03 AM12/9/13
to tas...@googlegroups.com
Yes. Here is another example of using Tasker and SL4A to study how to do it.

Rich D

unread,
Dec 9, 2013, 5:33:02 PM12/9/13
to Tasker Google Groups Post

> Hello, i'm trying to get accelerator values for my app.

I believe you can also get these values from the shell dumpsys

Cyril Preiss

unread,
Dec 10, 2013, 2:06:05 AM12/10/13
to tas...@googlegroups.com
Thank you.

Cyril Preiss

unread,
Dec 10, 2013, 2:06:21 AM12/10/13
to tas...@googlegroups.com
How do you do that ?

TomL

unread,
Dec 16, 2013, 1:35:04 PM12/16/13
to tas...@googlegroups.com
Your phone needs to be rooted.  From a rooted shell prompt, type:

dumpsys sensorservice



Tom

Régis Bouguin

unread,
Apr 3, 2014, 8:05:41 AM4/3/14
to tas...@googlegroups.com
Hi, to get sensor value, i use this SL4A script


import android, time

droid = android.Android()

class Task():
  SET_VARIABLE = 547
  def new_task(self):
    self.action_cnt = 0
    self.extras = {'version_number': '1.0', 'task_name': 'task' + str(time.time()), 'task_priority': 10 }
  def set_var(self, varname, value):
    self.action_cnt += 1
    self.extras['action' + str(self.action_cnt)] = {'action': self.SET_VARIABLE, 'arg:1': varname, 'arg:2': value, 'arg:3': False, 'arg:4': False, 'arg:5': False}
  def run_task(self):
    taskIntent = droid.makeIntent('net.dinglisch.android.tasker.ACTION_TASK', None, None, self.extras).result
    droid.sendBroadcastIntent(taskIntent)
  def set_var_now(self, varname, value):
    self.new_task()
    self.set_var(varname, value)
    self.run_task()


droid.wakeLockAcquirePartial()


temps = 20

droid.startSensingTimed(1,500)

while temps > 0:
  time.sleep(0.5)
  x, y, z = droid.sensorsReadOrientation().result
  temps = temps - 1
  if x is not None:
    break

droid.stopSensing()

t = Task()
t.new_task()
if x is not None:
  t.set_var("%ORIENTATION_X", str(x))
  t.set_var("%ORIENTATION_Y", str(y))
  t.set_var("%ORIENTATION_Z", str(z))


t.set_var("%ORIENTATION", "Done")

t.run_task()
droid.wakeLockRelease()

You get phone orientation in variable  %ORIENTATION_X, %ORIENTATION_Y, %ORIENTATION_Z in tasker
So you set %ORIENTATION to null, run SL4A script and wait for %ORIENTATION = Done

But i have some problem on android 4.4.2, often SL4A crash and i don't know why. Crash arrives only when i run script with phone locked.

if somebody have any idea ?
I search a solution

Régis Bouguin

unread,
Nov 27, 2014, 11:19:57 AM11/27/14
to tas...@googlegroups.com
This post is interesting for my problem.
May be the cause is time.spleep
http://stackoverflow.com/questions/2076381/making-an-android-python-service-to-run-in-suspend-state

I do test
Reply all
Reply to author
Forward
0 new messages