Lua Scripting Set EKF Origin

Hello all,

I’m currently trying to set my EKF origin and on mission planner map it cannot set it from there as it refuses the origin. My question is from the GitHub link posted will ahrs-set-origin.lua be the correct file to set the EKF by using coordinates and altitude?

Note: This is just for confirmation if this .lua file is the correct one for the function I’m looking for.

Also, would the altitude be set in m or feet? I’m currently assuming m.

These are some of the errors I got (I’m trying to fix requires position but now I also have PreArm: AHRS: EKF3 Yaw inconsistent, I’m not sure if it is because of the same reason):

The code I used was with my longitude and latitude as well as my altitude in m:

– example script for using “set_origin()”" and “initialised()”
– sets the ekf origin if not already set

function update ()

if not ahrs:initialised() then
    return update, 5000
end

origin = assert(not ahrs:get_origin(),"Refused to set EKF origin - already set")
location = Location() location:lat(-353632640) location:lng(1491652352) location:alt(58409)

if ahrs:set_origin(location) then
    gcs:send_text(6, string.format("Origin Set - Lat:%.7f Long:%.7f Alt:%.1f", location:lat()/10000000, location:lng()/10000000, location:alt()/100))
else
    gcs:send_text(0, "Refused to set EKF origin")
end

return

end

return update()

The script didn’t seem to do anything.

Hi @Sold,

I’m pretty confident that the script works in general but it may not appear to work because:

  1. the origin has already been set. Once the origin is set it cannot be moved.
  2. the EKF is using the GPS for its position (e.g. EK3_SRCx_POSXY = 3 (GPS))
  3. it is setting the origin but you’re not able to see the message because it happens before the ground station is connected.

I suspect the issue is 2. The constraint in the code is here and we could potentially remove it. I’ve even created a branch here that removes the constraint but we just need some justification for the change.

EDIT: I’ve created a PR to remove the constraint.

Looks like it’s #2 (from his parameter file). This script works in the simulator after setting this to 0. Default script coordinates set it to the well known Canberra Model Aircraft club…

1 Like

Hi rmackay9,

I’m not really familiar with Lua scripts as I’ve only really learnt about them yesterday. If the constraint was removed would that mean it should run without any problem? Would I just need to use the same code again once edited?

Hi Dave,

Do I keep this parameter as 3 or am I meant to disable it so EKF isn’t set by GPS.


Already addressed.

This is covered in the Non-GPS>Optical Flow configuration which you said you followed.

OpFlow

Hi Dave,

You’re right, it is set up for the optical flow and not for normal operation. Do you think I will have to put in the Lua code again without the constraint?

Yes indeed. I use it constantly with wheel encoders indoors. And using MP soft reset does it again, which is very convenient.

However, using OSD I never see those virtual coordinates:
image

1 Like

@rmackay9 Hi,

Does this mean I can now use the updated script which contains the change and it should work? Also, with the error SmartRTL: requires position or any position required can this not be bypassed if the gps is not being used?

Are position errors because of EKF origin was not set properly or is it related with GPS? In the case of EKF, shall I re-download the script to mission planner? Also initially when I put my drone down there is a lot of errors but eventually they go away is that fine or something I need to correct?

Is it possible to disable SmartRTL and the drone won’t need position since it is indoors.

The script has not changed. The PR is to change action in firmware. set EK3_SRC1_POSXY to 0 and move along. You will know if the script is doing it’s thing in Mission Planner when the home position Icon is where you set it to be in the script.

Thanks Dave. If you change home on mp or EKF on mp does that overwrite the scripts home/EKF?

Take some initiative, try things for yourself. You will never get there posting a question before every action you want to take.

You’re right, I will try it and see if it has any effect.