I set out to better understand how to leverage the MagFit tool and ended up with this script.
I’d always assumed that long, straight legs of varying headings were best for MagFit, thinking that method would stabilize a GPS heading track for use with the tool. The dev team corrected my assumption, and it turns out, it’s better to fly long, sweeping curves. They recommended a large figure-8 pattern while varying throttle to some extent. Many thanks to @peterbarker and @iampete for the discussion!
Before I describe the resulting script and its use, I should caution that this may not actually be the best method for using the MagFit tool, as the compass must be reasonably calibrated in the first place in order to fly nav missions at all. Still, I’ve achieved what I think are very good, consistent results by doing a basic compass calibration and then running MagFit against a flight where this script was employed.
First, upload the attached script, enable scripting, and reboot to initialize the script’s custom parameter set.
Connect to the Copter and look for the parameters prefixed “MAGH_” (for “MagFit-Helper”). Here’s a brief description of each:
- MAGH_ALT_DELTA: Altitude change to be commanded during the figure-8 pattern to capture mag field changes during throttle changes. Defaults to 10m. Starting altitude is based upon the entry waypoint’s altitude, and the Copter will climb this amount ABOVE that starting altitude on occasion.
- MAGH_B: geometric factor to adjust the width of the figure-8. Values of 1.2-1.5 should be more than adequate.
- MAGH_CMD: The SCRIPT_TIME command value used to initiate the scripted pattern. Defaults to 117.
- MAGH_COUNT: Number of times to repeat the figure-8. If battery failsafes are properly configured, you can make this number somewhat high so that the figure-8 will be repeated until failsafe.
-
MAGH_LOG_ENABLE: Enable a
MAGH.Active
value to be logged in dataflash for later consumption. Set 0 for no logging. - MAGH_MIN_SPEED: Starting speed (m/s) for the figure-8. Defaults to 1/2 of WPNAV_SPEED. Speed will slowly increment to WPNAV_SPEED as the pattern is repeated.
- MAGH_NUM_WP: The number of waypoints to be used in defining the figure-8. Defaults to 18. 16-24 should be adequate for most cases. Use more points for a smoother path at the cost of potentially slower speed during navigation.
- MAGH_USE_LOITER: Insert a LOITER_UNLIMITED waypoint prior to the figure-8 so that the new waypoints can be downloaded and verified prior to continuing. Set 0 to disable (not recommended). See the end of the writeup for a helper script to advance waypoints via an RC switch.
Once the script is loaded and active, create a flight plan that uses it, like this one. The figure-8 will be inserted between the points on either side of the SCRIPT_TIME command.
The SCRIPT_TIME command can be given two arguments for min speed and altitude delta, respectively, if you want to override MAGH_MIN_SPEED and MAGH_ALT_DELTA. Zero values (as shown) will let the parameter values remain in use.
Upon execution of the mission, several messages will be displayed via telemetry:
At this point, the figure-8 has been created, and the script is awaiting the user to set the waypoint beyond the loiter to commence the pattern. Download the mission to confirm obstacle clearance before proceeding, and then use the GCS to set the indicated waypoint to continue (or use the additional helper script below to use an RC switch for advancing the waypoint).
The figure-8 will then repeat MAGH_COUNT times, occasionally climbing and descending by MAGH_ALT_DELTA meters and incrementing speed toward WPNAV_SPEED.
Once complete, download the .bin log for analysis via MavExplorer (see the next post).
The ~200m long pattern consisting of 24 points depicted here should be more than adequate. I’ve achieved good success with patterns as small as ~50m and only 16 waypoints.
While it appears limited to use with Copter firmware at the moment, I don’t see any roadblocks to using the script on quadplanes (or similar) with no adaptation, and compass-based electric Rovers should be able to take advantage as well. Plane users may find that the number of waypoints is excessive (a simple “X” shaped pattern might be enough for a Plane), and I think there is room for optimization there. I did include the MAGH_MIN_SPEED parameter with Plane users in mind.
Scripts:
copter-magfit-helper.lua (10.9 KB)
If you’d like to advance past the LOITER_UNLIM via RC switch, you can use this one. Set RCx_OPTION=300 to a channel with a switch (preferably momentary). When that switch is activated during a nav mission, the vehicle will skip to the next waypoint.
advance-wp.lua (1.2 KB)
Bonus supernerd content:
The generated figure-8 is based upon the Lemniscate of Bernoulli and uses a scale factor (MAGH_B) in the y-direction. I arrived at the scaling technique by asking ChatGPT to craft MatLab scripts graphing various lemniscate figures.