-
Notifications
You must be signed in to change notification settings - Fork 337
/
motion_model.py
executable file
·216 lines (173 loc) · 8.27 KB
/
motion_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""
* This file is part of PYSLAM
* This file contains a revised and fixed version of the class in https://github.com/uoip/stereo_ptam/blob/master/motion.py
*
* Copyright (C) 2016-present Luigi Freda <luigi dot freda at gmail dot com>
*
* PYSLAM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PYSLAM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PYSLAM. If not, see <http://www.gnu.org/licenses/>.
"""
import numpy as np
import g2o
class MotionModelBase(object):
def __init__(self,
timestamp=None,
initial_position=None,
initial_orientation=None,
initial_covariance=None):
self.timestamp = timestamp
if initial_position is not None:
self.position = initial_position
else:
self.position = np.zeros(3)
if initial_orientation is not None:
self.orientation = initial_orientation
else:
self.orientation = g2o.Quaternion()
self.orientation = initial_orientation
self.covariance = initial_covariance # pose covariance
self.is_ok = False
self.initialized = False
def current_pose(self):
'''
Get the current camera pose.
'''
return (g2o.Isometry3d(self.orientation, self.position), self.covariance)
def predict_pose(self, timestamp, prev_position=None, prev_orientation=None):
return None
def update_pose(self, timestamp, new_position, new_orientation, new_covariance=None):
return None
# correction= Tcw_old.inverse() * Tcw_new (transform from world_new to worl_old)
def apply_correction(self, correction): # corr: g2o.Isometry3d or matrix44
return None
# simple kinematic motion model without damping (does not actually use timestamps)
class MotionModel(MotionModelBase):
def __init__(self,
timestamp=None,
initial_position=None,
initial_orientation=None,
initial_covariance=None):
super().__init__(timestamp, initial_position, initial_orientation, initial_covariance)
self.delta_position = np.zeros(3) # delta translation
self.delta_orientation = g2o.Quaternion()
def predict_pose(self, timestamp, prev_position=None, prev_orientation=None):
'''
Predict the next camera pose.
'''
if prev_position is not None:
self.position = prev_position
if prev_orientation is not None:
self.orientation = prev_orientation
if not self.initialized:
return (g2o.Isometry3d(self.orientation, self.position), self.covariance)
orientation = self.delta_orientation * self.orientation
position = self.position + self.delta_orientation * self.delta_position
return (g2o.Isometry3d(orientation, position), self.covariance)
def update_pose(self, timestamp, new_position, new_orientation, new_covariance=None):
'''
Update the motion model when given a new camera pose.
'''
if self.initialized:
self.delta_position = new_position - self.position
self.delta_orientation = new_orientation * self.orientation.inverse()
self.delta_orientation.normalize()
self.timestamp = timestamp
self.position = new_position
self.orientation = new_orientation
self.covariance = new_covariance
self.initialized = True
# correction= Tcw_corrected * Tcw_uncorrected.inverse() (transform from camera_uncorrected to camera_corrected)
def apply_correction(self, correction): # corr: g2o.Isometry3d or matrix44
'''
Reset the model given a new camera pose.
Note: This method will be called when it happens an abrupt change in the pose (LoopClosing)
'''
if not isinstance(correction, g2o.Isometry3d):
correction = g2o.Isometry3d(correction)
current = g2o.Isometry3d(self.orientation, self.position)
current = correction * current
self.position = current.position()
self.orientation = current.orientation()
# correction= Tcw_corrected * Tcw_uncorrected.inverse() (transform from camera_uncorrected to camera_corrected)
self.delta_orientation = correction.orientation() * self.delta_orientation
self.delta_position = correction.orientation() * self.delta_position
# motion model with damping
class MotionModelDamping(MotionModelBase):
def __init__(self,
timestamp=None,
initial_position=None,
initial_orientation=None,
initial_covariance=None,
damping=0.95):
super().__init__(timestamp, initial_position, initial_orientation, initial_covariance)
self.v_linear = np.zeros(3) # linear velocity
self.v_angular_angle = 0
self.v_angular_axis = np.array([1, 0, 0])
self.damp = damping # damping factor
def predict_pose(self, timestamp, prev_position=None, prev_orientation=None):
'''
Predict the next camera pose.
'''
if prev_position is not None:
self.position = prev_position
if prev_orientation is not None:
self.orientation = prev_orientation
if not self.initialized:
return (g2o.Isometry3d(self.orientation, self.position), self.covariance)
dt = timestamp - self.timestamp
delta_angle = g2o.AngleAxis(
self.v_angular_angle * dt * self.damp,
self.v_angular_axis)
delta_orientation = g2o.Quaternion(delta_angle)
orientation = delta_orientation * self.orientation
position = self.position + delta_orientation * (self.v_linear * dt * self.damp)
return (g2o.Isometry3d(orientation, position), self.covariance)
def update_pose(self, timestamp, new_position, new_orientation, new_covariance=None):
'''
Update the motion model when given a new camera pose.
'''
if self.initialized:
dt = timestamp - self.timestamp
assert dt != 0
v_linear = (new_position - self.position) / dt
self.v_linear = v_linear
delta_q = new_orientation * self.orientation.inverse()
delta_q.normalize()
delta_angle = g2o.AngleAxis(delta_q)
angle = delta_angle.angle()
axis = delta_angle.axis()
if angle > np.pi:
axis = axis * -1
angle = 2 * np.pi - angle
self.v_angular_axis = axis
self.v_angular_angle = angle / dt
self.timestamp = timestamp
self.position = new_position
self.orientation = new_orientation
self.covariance = new_covariance
self.initialized = True
# correction= Tcw_corrected * Tcw_uncorrected.inverse() (transform from camera_uncorrected to camera_corrected)
def apply_correction(self, correction): # corr: g2o.Isometry3d or matrix44
'''
Reset the model given a new camera pose.
Note: This method will be called when it happens an abrupt change in the pose (LoopClosing)
'''
if not isinstance(correction, g2o.Isometry3d):
correction = g2o.Isometry3d(correction)
current = g2o.Isometry3d(self.orientation, self.position)
current = correction * current
self.position = current.position()
self.orientation = current.orientation()
# correction= Tcw_corrected * Tcw_uncorrected.inverse() (transform from camera_uncorrected to camera_corrected)
self.v_angular_axis = correction.orientation() * self.v_angular_axis
self.v_linear = correction.orientation() * self.v_linear