gtsam/python/gtsam_examples/ImuFactorExample.py

82 lines
2.5 KiB
Python
Raw Normal View History

2016-01-27 05:19:25 +08:00
"""
A script validating the ImuFactor prediction and inference.
"""
2016-01-27 06:41:55 +08:00
import math
2016-01-27 05:19:25 +08:00
import matplotlib.pyplot as plt
import numpy as np
2016-01-27 06:41:55 +08:00
from mpl_toolkits.mplot3d import Axes3D
import gtsam
from gtsam_utils import plotPose3
2016-01-27 06:41:55 +08:00
2016-01-27 05:19:25 +08:00
class ImuFactorExample(object):
def __init__(self):
2016-01-27 06:41:55 +08:00
# setup interactive plotting
2016-01-27 05:19:25 +08:00
plt.ion()
2016-01-27 06:41:55 +08:00
# Setup loop scenario
# Forward velocity 2m/s
# Pitch up with angular velocity 6 degree/sec (negative in FLU)
v = 2
w = math.radians(6)
W = np.array([0, -w, 0])
V = np.array([v, 0, 0])
self.scenario = gtsam.ConstantTwistScenario(W, V)
self.dt = 0.5
self.realTimeFactor = 10.0
2016-01-27 06:41:55 +08:00
# Calculate time to do 1 loop
self.radius = v / w
self.timeForOneLoop = 2 * math.pi / w
self.labels = list('xyz')
self.colors = list('rgb')
2016-01-27 06:41:55 +08:00
def plot(self, t, pose, omega_b, acceleration_n, acceleration_b):
# plot angular velocity
2016-01-27 06:41:55 +08:00
plt.figure(1)
for i, (label, color) in enumerate(zip(self.labels, self.colors)):
plt.subplot(3, 1, i + 1)
plt.scatter(t, omega_b[i], color=color, marker='.')
plt.xlabel(label)
# plot acceleration in nav
plt.figure(2)
for i, (label, color) in enumerate(zip(self.labels, self.colors)):
plt.subplot(3, 1, i + 1)
plt.scatter(t, acceleration_n[i], color=color, marker='.')
plt.xlabel(label)
# plot acceleration in body
plt.figure(3)
for i, (label, color) in enumerate(zip(self.labels, self.colors)):
2016-01-27 06:41:55 +08:00
plt.subplot(3, 1, i + 1)
plt.scatter(t, acceleration_b[i], color=color, marker='.')
2016-01-27 05:19:25 +08:00
plt.xlabel(label)
2016-01-27 06:41:55 +08:00
# plot ground truth
plotPose3(4, pose, 1.0)
ax = plt.gca()
ax.set_xlim3d(-self.radius, self.radius)
ax.set_ylim3d(-self.radius, self.radius)
ax.set_zlim3d(0, self.radius * 2)
2016-01-27 06:41:55 +08:00
plt.pause(self.dt / self.realTimeFactor)
2016-01-27 05:19:25 +08:00
def run(self):
# simulate the loop up to the top
for t in np.arange(0, self.timeForOneLoop, self.dt):
self.plot(t,
self.scenario.pose(t),
self.scenario.omega_b(t),
self.scenario.acceleration_n(t),
self.scenario.acceleration_b(t))
2016-01-27 06:41:55 +08:00
plt.ioff()
plt.show()
2016-01-27 05:19:25 +08:00
if __name__ == '__main__':
ImuFactorExample().run()