Page 1 of 1

getAccel function not working mk2 v2.7.8

Posted: Tue Mar 17, 2015 2:17 am
by JMcDonough
I've been trying to learn how the lua scripting works, so I thought I'd try to start the logger based on an accel threshold.

I copied and pasted the example code:

Code: Select all

setTickRate(30)

function onTick()
 local arm = getGpio(0)
 local g = getAccel(1)
 if arm == 0 then
   stopLogging()
 end
 if arm == 1 and g < 0.1 then
   startLogging&#40;&#41;
 end
end
Then deleted the "arm" stuff and changed the threshold to g > 0.5

I then tipped the unit around to exceed the threshold, but did not see the logger come on.

Next I started using the print command to check other functions. getAnalog(7) will give me battery voltage and print it, but getAccel(0) will not return anything. I tried all four getAccel channels with no success.

Ideas?

Also, after pushing changes to the RCP, I have to reset it (pull usb power in this case) to get it to run the updated lua script. Is this normal?

Thanks!

Josh

Posted: Tue Mar 17, 2015 4:30 am
by Copper280z
Your conditionals are not going to work like that, you are never changing the state of arm and you never set it to 1, which you require to start logging. EDIT: I think I misunderstood what you were asking, can you post some of the code you were using that didn't do what you wanted?

Take a look at this, I think that should do what you want, I'm still learning the lua syntax as well.

Code: Select all

setTickRate&#40;30&#41; 

function onTick&#40;&#41; 
 local arm = false --getGPIO grabs the state of an input pin, which I don't think you want, unless you have a physical arming switch?
 local g = getAccel&#40;1&#41; 
 if arm == true and g < .3 then --if you are currently logging AND less than .3 G, then stop logging
   stopLogging&#40;&#41; 
 end 
 if arm == false and g > .5 then --if you are currently not logging AND greater than .5G, then start logging
   startLogging&#40;&#41; 
 end 
end

Posted: Tue Mar 17, 2015 12:34 pm
by JMcDonough
To clarify, here's what I actually tried:

Code: Select all

setTickRate&#40;30&#41; 

function onTick&#40;&#41; 
 local g = getAccel&#40;1&#41; 
 if g > 0.5 then 
   startLogging&#40;&#41; 
 end 
end
After failing to get it to start logging, I tried to simply print the current accel value. No luck there either. Next was to try to print another variable, battery voltage. That worked and leads me to believe something is wrong with the getAccel function. Unless there's something wrong with the above code, my next step is to re-write the firmware and see if that fixes it.

Posted: Tue Mar 17, 2015 10:02 pm
by JMcDonough
Tried re-flashing the firmware. Tried v2.7.6 then 2.7.8 again. No success. The function seems to stop at the g = getAccel(1) line. It will print text to the logfile portion of the screen prior to that line, but nothing after.

Any suggestions?

Posted: Wed Mar 18, 2015 12:14 am
by Copper280z
hmm, I'm not getting anything out of getAccel, getAccelRaw, or getGPIO, but I do get the expected value out of getButton.

Posted: Wed Mar 18, 2015 1:57 am
by JMcDonough
Glad to know it's not just me. Thanks for checking.

getAnalog(7) does return the battery voltage for me.

Anyone w/ a mk2 on v2.7.8 firmware using getAccel(channel) successfully?

Posted: Wed Mar 18, 2015 8:23 pm
by brentp
Hi all,

we'll double check this in the latest firmware and post a sample script for you to try. Sorry for the hassle!

Posted: Thu Mar 19, 2015 12:09 am
by rdoherty
Tested on 2.7.9, getAccel() doesn't return anything for me either.

Posted: Thu Mar 19, 2015 1:48 am
by brentp
Well, poop. With the MK2 and V2 firmware we changed it to getImu() and getImuRaw() and forgot to update the docs. This was to reflect the true nature of the device - not just an accelerometer.

This script should work for you:

Code: Select all

setTickRate&#40;10&#41;

function onTick&#40;&#41;
for i =0,5 do print&#40;getImu&#40;i&#41; ..' '&#41; end
println&#40;''&#41;
end
Apologies for the confusion! Let us know how it works for you.