Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
bounds.append((c.cmot.z, c.cmot.z))
else:
for i in range(3):
bounds.append((-args.max_cmot,args.max_cmot))
p = optimize.fmin_slsqp(wmm_error, p, bounds=bounds)
p = list(p)
c.offsets = Vector3(p.pop(0), p.pop(0), p.pop(0))
c.scaling = p.pop(0)
if args.elliptical:
c.diag = Vector3(p.pop(0), p.pop(0), p.pop(0))
c.offdiag = Vector3(p.pop(0), p.pop(0), p.pop(0))
else:
c.diag = Vector3(1.0, 1.0, 1.0)
c.offdiag = Vector3(0.0, 0.0, 0.0)
if args.cmot:
c.cmot = Vector3(p.pop(0), p.pop(0), p.pop(0))
else:
c.cmot = Vector3(0.0, 0.0, 0.0)
return c
Vector3( 0.618034, 0.000000, 0.000000)
),
Matrix3(
Vector3(-0.309017, -0.500000, -0.190983),
Vector3( 0.190983, 0.309017, -0.500000),
Vector3( 0.500000, -0.190983, 0.309017)
),
Matrix3(
Vector3( 0.309017, -0.500000, 0.190983),
Vector3( 0.000000, 0.000000, -0.618034),
Vector3( 0.309017, 0.500000, 0.190983)
),
Matrix3(
Vector3( 0.190983, -0.309017, -0.500000),
Vector3( 0.500000, 0.190983, 0.309017),
Vector3(-0.309017, 0.500000, -0.190983)
),
Matrix3(
Vector3( 0.500000, -0.190983, -0.309017),
Vector3( 0.000000, 0.618034, 0.000000),
Vector3(-0.500000, -0.190983, -0.309017)
),
Matrix3(
Vector3( 0.309017, 0.500000, -0.190983),
Vector3(-0.500000, 0.190983, 0.309017),
Vector3(-0.190983, -0.309017, -0.500000)
),
)
_mid_inverses = (
Matrix3(
Vector3(-0.000000, 1.000000, -0.618034),
corrected_attitude_combined = Attitude()
corrected_attitude_integrator = Tian_integrator()
corrected_attitude_integrator_combined = Tian_integrator(integrate_separately = False)
reference_attitude.add_arrows(Vector3(0,-3,0))
uncorrected_attitude_low.add_arrows(Vector3(0,-3,0), "no correction\nlow rate integration\n30hz software LPF @ 1khz\n(ardupilot 2015-02-18)")
reference_attitude.add_arrows(Vector3(0,-1,0))
uncorrected_attitude_high.add_arrows(Vector3(0,-1,0), "no correction\nhigh rate integration")
reference_attitude.add_arrows(Vector3(0,1,0))
corrected_attitude.add_arrows(Vector3(0,1,0), "Tian et al\nseparate integration")
reference_attitude.add_arrows(Vector3(0,3,0))
corrected_attitude_combined.add_arrows(Vector3(0,3,0), "Tian et al\ncombined_integration\n(proposed patch)")
#scene.scale = (0.3,0.3,0.3)
scene.fov = 0.001
scene.forward = (-0.5, -0.5, -1)
coning_frequency_hz = 50
coning_magnitude_rad_s = 2
label_text = (
"coning motion frequency %f hz\n"
"coning motion peak amplitude %f deg/s\n"
"thin arrows are reference attitude"
) % (coning_frequency_hz, degrees(coning_magnitude_rad_s))
label(pos = vpy_vec(Vector3(0,0,2)), text=label_text)
t = 0.0
dt_10000 = 0.0001
def mtl_to_material(mtl):
if mtl.name not in material_map:
m = Material(
ambient=Vector3(*mtl.Ka),
diffuse=Vector3(*mtl.Kd),
specular=Vector3(*mtl.Ks),
specular_exponent=mtl.Ns,
)
material_map[mtl.name] = m
return material_map[mtl.name]
break
if m.get_type() == "SENSOR_OFFSETS":
# update offsets that were used during this flight
offsets = Vector3(m.mag_ofs_x, m.mag_ofs_y, m.mag_ofs_z)
if m.get_type() == "RAW_IMU" and offsets is not None:
# extract one mag vector, removing the offsets that were
# used during that flight to get the raw sensor values
mag = Vector3(m.xmag, m.ymag, m.zmag) - offsets
data.append(mag)
print("Extracted %u data points" % len(data))
print("Current offsets: %s" % offsets)
# run the fitting algorithm
ofs = offsets
ofs = Vector3(0,0,0)
for r in range(args.repeat):
ofs = find_offsets(data, ofs)
print('Loop %u offsets %s' % (r, ofs))
sys.stdout.flush()
print("New offsets: %s" % ofs)
ATT = msg
if msg.get_type() == 'BAT':
BAT = msg
if msg.get_type() == mag_msg and ATT is not None:
if count % args.reduce == 0:
data.append((msg,ATT,BAT))
count += 1
old_corrections.offsets = Vector3(parameters.get('COMPASS_OFS%s_X' % mag_idx,0.0),
parameters.get('COMPASS_OFS%s_Y' % mag_idx,0.0),
parameters.get('COMPASS_OFS%s_Z' % mag_idx,0.0))
old_corrections.diag = Vector3(parameters.get('COMPASS_DIA%s_X' % mag_idx,1.0),
parameters.get('COMPASS_DIA%s_Y' % mag_idx,1.0),
parameters.get('COMPASS_DIA%s_Z' % mag_idx,1.0))
if old_corrections.diag == Vector3(0,0,0):
old_corrections.diag = Vector3(1,1,1)
old_corrections.offdiag = Vector3(parameters.get('COMPASS_ODI%s_X' % mag_idx,0.0),
parameters.get('COMPASS_ODI%s_Y' % mag_idx,0.0),
parameters.get('COMPASS_ODI%s_Z' % mag_idx,0.0))
if parameters.get('COMPASS_MOTCT',0) == 2:
# only support current based corrections for now
old_corrections.cmot = Vector3(parameters.get('COMPASS_MOT%s_X' % mag_idx,0.0),
parameters.get('COMPASS_MOT%s_Y' % mag_idx,0.0),
parameters.get('COMPASS_MOT%s_Z' % mag_idx,0.0))
old_corrections.scaling = parameters.get('COMPASS_SCALE%s' % mag_idx, None)
if old_corrections.scaling is None or old_corrections.scaling < 0.1:
force_scale = False
old_corrections.scaling = 1.0
else:
force_scale = True
# remove existing corrections
Vector3( 0.190983, -0.309017, 0.500000)
),
Matrix3(
Vector3(-0.500000, 0.190983, -0.309017),
Vector3( 0.000000, -0.618034, 0.000000),
Vector3( 0.500000, 0.190983, -0.309017)
),
Matrix3(
Vector3(-0.190983, -0.309017, -0.500000),
Vector3(-0.190983, -0.309017, 0.500000),
Vector3( 0.618034, 0.000000, 0.000000)
),
Matrix3(
Vector3(-0.309017, -0.500000, -0.190983),
Vector3( 0.190983, 0.309017, -0.500000),
Vector3( 0.500000, -0.190983, 0.309017)
),
Matrix3(
Vector3( 0.309017, -0.500000, 0.190983),
Vector3( 0.000000, 0.000000, -0.618034),
Vector3( 0.309017, 0.500000, 0.190983)
),
Matrix3(
Vector3( 0.190983, -0.309017, -0.500000),
Vector3( 0.500000, 0.190983, 0.309017),
Vector3(-0.309017, 0.500000, -0.190983)
),
Matrix3(
Vector3( 0.500000, -0.190983, -0.309017),
Vector3( 0.000000, 0.618034, 0.000000),
Vector3(-0.500000, -0.190983, -0.309017)
),
offsets = Vector3(0,0,0)
# now gather all the data
while True:
m = mlog.recv_match(condition=args.condition)
if m is None:
break
if m.get_type() == "SENSOR_OFFSETS":
# update current offsets
offsets = Vector3(m.mag_ofs_x, m.mag_ofs_y, m.mag_ofs_z)
if m.get_type() == "RAW_IMU":
mag = Vector3(m.xmag, m.ymag, m.zmag)
# add data point after subtracting the current offsets
data.append(mag - offsets + noise())
if m.get_type() == "MAG" and not args.mag2:
offsets = Vector3(m.OfsX,m.OfsY,m.OfsZ)
mag = Vector3(m.MagX,m.MagY,m.MagZ)
data.append(mag - offsets + noise())
if m.get_type() == "MAG2" and args.mag2:
offsets = Vector3(m.OfsX,m.OfsY,m.OfsZ)
mag = Vector3(m.MagX,m.MagY,m.MagZ)
data.append(mag - offsets + noise())
print("Extracted %u data points" % len(data))
print("Current offsets: %s" % offsets)
orig_data = data
data = select_data(data)
# remove initial outliers
data.sort(lambda a,b : radius_cmp(a,b,offsets))
vehicle_dcm.from_euler(att.roll, att.pitch, att.yaw)
rotmat_copter_gimbal = Matrix3()
rotmat_copter_gimbal.from_euler312(m.joint_roll, m.joint_el, m.joint_az)
gimbal_dcm = vehicle_dcm * rotmat_copter_gimbal
lat = gpi.lat * 1.0e-7
lon = gpi.lon * 1.0e-7
alt = gpi.relative_alt * 1.0e-3
# ground plane
ground_plane = Plane()
# the position of the camera in the air, remembering its a right
# hand coordinate system, so +ve z is down
camera_point = Vector3(0, 0, -alt)
# get view point of camera when not rotated
view_point = Vector3(1, 0, 0)
# rotate view_point to form current view vector
rot_point = gimbal_dcm * view_point
# a line from the camera to the ground
line = Line(camera_point, rot_point)
# find the intersection with the ground
pt = line.plane_intersection(ground_plane, forward_only=True)
if pt is None:
# its pointing up into the sky
return None
functions. Those files should be consulted for implementation details.
'''
import math
from pymavlink.rotmat import Matrix3, Vector3
# The golden number below was obtained from scipy.constants.golden. Let's use
# the literal value here as this is the only place that would require scipy
# module.
g = 1.618033988749895
_first_half = (
(Vector3(-g, 1, 0), Vector3(-1, 0,-g), Vector3(-g,-1, 0)),
(Vector3(-1, 0,-g), Vector3(-g,-1, 0), Vector3( 0,-g,-1)),
(Vector3(-g,-1, 0), Vector3( 0,-g,-1), Vector3( 0,-g, 1)),
(Vector3(-1, 0,-g), Vector3( 0,-g,-1), Vector3( 1, 0,-g)),
(Vector3( 0,-g,-1), Vector3( 0,-g, 1), Vector3( g,-1, 0)),
(Vector3( 0,-g,-1), Vector3( 1, 0,-g), Vector3( g,-1, 0)),
(Vector3( g,-1, 0), Vector3( 1, 0,-g), Vector3( g, 1, 0)),
(Vector3( 1, 0,-g), Vector3( g, 1, 0), Vector3( 0, g,-1)),
(Vector3( 1, 0,-g), Vector3( 0, g,-1), Vector3(-1, 0,-g)),
(Vector3( 0, g,-1), Vector3(-g, 1, 0), Vector3(-1, 0,-g)),
)
_second_half = tuple((-a, -b, -c) for a, b, c in _first_half)
triangles = _first_half + _second_half
radius = math.sqrt(1 + g**2)
# radius / (length of two vertices of an icosahedron triangle)
_midpoint_projection_scale = radius / (2 * g)
sections = ()