Having issues while running magfit script

Hi guys,
when I am trying to run command for magfit, I am facing this issue, (refer below image)

command --->>   magfit_WMM.py --cmot --elliptical --mag 1 --reduce 10 /home/orion/ardupilot/modules/mavlink/pymavlink/tools/test1_grey_vtol_qhover_160323.bin

LOGS—

orion@asus:~/ardupilot/modules/mavlink/pymavlink/tools$ magfit_WMM.py --cmot --elliptical --mag 1 --reduce 10 /home/orion/ardupilot/modules/mavlink/pymavlink/tools/test1_grey_vtol_qhover_160323.bin
Processing log /home/orion/ardupilot/modules/mavlink/pymavlink/tools/test1_grey_vtol_qhover_160323.bin
Attitude source XKF1
Extracted 513 points
Current: Vector3(-178.19, -198.93, 191.47) diag: Vector3(0.98, 1.02, 0.99) offdiag: Vector3(-0.02, -0.00, -0.00) cmot_mode: 0.0 cmot: Vector3(0.10, -1.07, 0.56) scale: 1.00
Traceback (most recent call last):
  File "/home/orion/.local/bin/magfit_WMM.py", line 491, in <module>
    magfit(args.log)
  File "/home/orion/.local/bin/magfit_WMM.py", line 399, in magfit
    c = fit_WWW()
  File "/home/orion/.local/bin/magfit_WMM.py", line 222, in fit_WWW
    (p,err,iterations,imode,smode) = optimize.fmin_slsqp(wmm_error, p, bounds=bounds, full_output=True, iter=args.iter)
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/slsqp.py", line 207, in fmin_slsqp
    constraints=cons, **opts)
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/slsqp.py", line 370, in _minimize_slsqp
    raise ValueError("Objective function must return a scalar")
ValueError: Objective function must return a scalar

I tried with mavproxy>tools also, even there I was facing errors, (I followed this video - Demonstration of MagFit utility within MAVExplorer.py - YouTube) from official documentation.
documentation link - Refining Calibration Parameters using a Flight Log — Copter documentation

Logs -

Attitude source ATT mtypes=['GPS', 'MAG', 'ATT', 'BAT']
Extracted 54 points
Current: Vector3(-178.19, -198.93, 191.47) diag: Vector3(0.98, 1.02, 0.99) offdiag: Vector3(-0.02, -0.00, -0.00) cmot: Vector3(0.00, 0.00, 0.00) scale: 1.00
Traceback (most recent call last):
  File "/home/orion/.local/lib/python2.7/site-packages/MAVProxy/modules/lib/magfit.py", line 686, in OnButton
    self.callbacks[event.GetId()]()
  File "/home/orion/.local/lib/python2.7/site-packages/MAVProxy/modules/lib/magfit.py", line 691, in run
    magfit(self.mlog, self.timestamp_in_range)
  File "/home/orion/.local/lib/python2.7/site-packages/MAVProxy/modules/lib/magfit.py", line 366, in magfit
    c = fit_WWW()
  File "/home/orion/.local/lib/python2.7/site-packages/MAVProxy/modules/lib/magfit.py", line 183, in fit_WWW
    (p,err,iterations,imode,smode) = optimize.fmin_slsqp(wmm_error, p, bounds=bounds, full_output=True)
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/slsqp.py", line 207, in fmin_slsqp
    constraints=cons, **opts)
  File "/usr/lib/python2.7/dist-packages/scipy/optimize/slsqp.py", line 370, in _minimize_slsqp
    raise ValueError("Objective function must return a scalar")
ValueError: Objective function must return a scalar

Please suggest any solutions, Thanks in advance!

It appears you are attempting to run the script with Python 2, but it is a Python 3 script.

Hi @Yuri_Rage, Thanks for your reply!

I tried with python3 command also, I am having the same issue as you see in the 2nd method.

logs----

orion@asus:~/ardupilot/modules/mavlink/pymavlink/tools$ python3 magfit_WMM.py --cmot --elliptical --mag 1 --reduce 10 /home/orion/ardupilot/modules/mavlink/pymavlink/tools/test1_grey_vtol_qhover_160323.bin
Processing log /home/orion/ardupilot/modules/mavlink/pymavlink/tools/test1_grey_vtol_qhover_160323.bin
Attitude source XKF1
Extracted 513 points
Current: Vector3(-178.19, -198.93, 191.47) diag: Vector3(0.98, 1.02, 0.99) offdiag: Vector3(-0.02, -0.00, -0.00) cmot: Vector3(0.00, 0.00, 0.00) scale: 1.00
Traceback (most recent call last):
  File "magfit_WMM.py", line 458, in <module>
    magfit(args.log)
  File "magfit_WMM.py", line 366, in magfit
    c = fit_WWW()
  File "magfit_WMM.py", line 198, in fit_WWW
    (p,err,iterations,imode,smode) = optimize.fmin_slsqp(wmm_error, p, bounds=bounds, full_output=True)
  File "/home/orion/.local/lib/python3.8/site-packages/scipy/optimize/_slsqp_py.py", line 206, in fmin_slsqp
    res = _minimize_slsqp(func, x0, args, jac=fprime, bounds=bounds,
  File "/home/orion/.local/lib/python3.8/site-packages/scipy/optimize/_slsqp_py.py", line 374, in _minimize_slsqp
    sf = _prepare_scalar_function(func, x, jac=jac, args=args, epsilon=eps,
  File "/home/orion/.local/lib/python3.8/site-packages/scipy/optimize/_optimize.py", line 263, in _prepare_scalar_function
    sf = ScalarFunction(fun, x0, args, grad, hess,
  File "/home/orion/.local/lib/python3.8/site-packages/scipy/optimize/_differentiable_functions.py", line 158, in __init__
    self._update_fun()
  File "/home/orion/.local/lib/python3.8/site-packages/scipy/optimize/_differentiable_functions.py", line 251, in _update_fun
    self._update_fun_impl()
  File "/home/orion/.local/lib/python3.8/site-packages/scipy/optimize/_differentiable_functions.py", line 155, in update_fun
    self.f = fun_wrapped(self.x)
  File "/home/orion/.local/lib/python3.8/site-packages/scipy/optimize/_differentiable_functions.py", line 137, in fun_wrapped
    fx = fun(np.copy(x), *args)
  File "magfit_WMM.py", line 154, in wmm_error
    yaw = get_yaw(ATT,MAG,BAT,c)
  File "magfit_WMM.py", line 111, in get_yaw
    yaw = math.degrees(math.atan2(-headY,headX)) + declination
TypeError: unsupported operand type(s) for +: 'float' and 'NoneType'

Screenshot of the error.

You might try:

Tools/environment_install/install-prereqs-ubuntu.sh -y

Hi @Yuri_Rage, I already have installed the latest version, I am able to build the quadplane firmware for my aircraft also.

It looks like an issue with your scipy module. Possibly out of date.

Maybe
pip3 install scipy --update

Hi @Yuri_Rage, I was using the log that didn’t have GPS Fix, nut now i tried with complete log with 3D fix,

but while running this command -

python3 magfit_WMM.py --cmot --elliptical --mag 1 --reduce 10 /home/orion/ardupilot/modules/mavlink/pymavlink/tools/Run_06_Q_hover_RTL_crash_08032023_17-59.bin

I’m getting this iteration limit error, any idea how to fix this, and thanks for your help!

LOGS

orion@asus:~/ardupilot/modules/mavlink/pymavlink/tools$ python3 magfit_WMM.py --cmot --elliptical --mag 1 --reduce 10 /home/orion/ardupilot/modules/mavlink/pymavlink/tools/Run_06_Q_hover_RTL_crash_08032023_17-59.bin
Processing log /home/orion/ardupilot/modules/mavlink/pymavlink/tools/Run_06_Q_hover_RTL_crash_08032023_17-59.bin
Attitude source XKF1
Earth field: Vector3(404.95, -7.57, 100.12)  strength 417 declination -1.1 degrees
Extracted 157 points
Current: Vector3(-201.04, -173.82, 153.85) diag: Vector3(0.98, 1.02, 1.01) offdiag: Vector3(-0.02, 0.00, -0.00) cmot: Vector3(0.00, 0.00, 0.00) scale: 1.00
Iteration limit reached    (Exit mode 9)
            Current function value: 7.779545378135729
            Iterations: 100
            Function evaluations: 1437
            Gradient evaluations: 100
Fit failed: Iteration limit reached

With only 157 points to analyze, it shouldn’t be a huge task. I’d suggest trying yet another log file.

Hi @Yuri_Rage, Did it with Mavexplorer and it worked!
Thank you for your time.

1 Like

Hey @Yuri_Rage I saw your zeroturn mower on youtube 2 years ago and asked if you were interested in consulting to try to make an easy mower to possibly sell it. I took a break from the idea but now I am exploring it again and I finally found you here. I wonder if we can talk and exchange ideas.thanks