gtsam/python/gtsam_examples/ImuFactorExample.py

64 lines
1.6 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
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)
# Calculate time to do 1 loop
self.T = 2 * math.pi / w
def plot(self, t, pose):
# plot IMU
plt.figure(1)
2016-01-27 05:19:25 +08:00
times = np.arange(0, 10, 0.1)
shape = len(times), 1
labels = list('xyz')
colors = list('rgb')
plt.clf()
2016-01-27 06:41:55 +08:00
for i, (label, color) in enumerate(zip(labels, colors)):
plt.subplot(3, 1, i + 1)
2016-01-27 05:19:25 +08:00
imu = np.random.randn(len(times), 1)
plt.plot(times, imu, color=color)
# plt.axis([tmin, tmax, min,max])
plt.xlabel(label)
2016-01-27 06:41:55 +08:00
# plot ground truth
fig = plt.figure(2)
ax = fig.gca(projection='3d')
p = pose.translation()
ax.scatter(p.x(), p.y(), p.z())
2016-01-27 05:19:25 +08:00
plt.pause(0.1)
def run(self):
2016-01-27 06:41:55 +08:00
for t in np.arange(0, self.T / 2, 1):
pose = self.scenario.pose(t)
self.plot(t, pose)
plt.ioff()
plt.show()
2016-01-27 05:19:25 +08:00
if __name__ == '__main__':
ImuFactorExample().run()