4 estimate COMPASS_MOT_* parameters for throttle based compensation 6 from __future__
import print_function
7 from builtins
import range
10 from argparse
import ArgumentParser
11 parser = ArgumentParser(description=__doc__)
12 parser.add_argument(
"--condition",dest=
"condition", default=
None, help=
"select packets by condition")
13 parser.add_argument(
"logs", metavar=
"LOG", nargs=
"+")
15 args = parser.parse_args()
17 from pymavlink
import mavutil
25 errout = (0.3 - r) * 100
28 errout = (r - 2.0) * 100
31 (baseline_field,baseline_throttle) = data[0]
35 cmag = mag + corr * math.pow(throttle,r)
36 err = (cmag - baseline_field).length()
42 from scipy
import optimize
44 p0 = [0.0, 0.0, 0.0, 1.0]
45 p1, ier = optimize.leastsq(mag_error, p0[:], args=(data))
46 if not ier
in [1, 2, 3, 4]:
47 raise RuntimeError(
"Unable to find solution")
48 return (
Vector3(p1[0], p1[1], p1[2]), p1[3])
51 '''find best magnetometer offset fit to a log file''' 53 print(
"Processing log %s" % filename)
54 mlog = mavutil.mavlink_connection(filename)
61 m = mlog.recv_match(condition=args.condition)
64 if m.get_type() ==
"MAG":
65 mag =
Vector3(m.MagX, m.MagY, m.MagZ)
66 data.append((mag, throttle))
69 if m.get_type() ==
"CTUN":
72 print(
"Extracted %u data points" % len(data))
75 print(
"Fit : %s r: %s" % (cmot,r))
78 errors =
mag_error((cmot.x,cmot.y,cmot.z,r), data)
80 import matplotlib.pyplot
as plt
81 plt.plot(x, errors,
'bo-')
82 x1,x2,y1,y2 = plt.axis()
83 plt.axis((x1,x2,0,y2))
88 for filename
in args.logs: