RigidJointFilter.cpp
Go to the documentation of this file.
2 
3 using namespace omip;
4 
7 {
8  // Initially the most probable joint type is rigid
11 }
12 
14 {
15 
16 }
17 
19  JointFilter(rigid_joint)
20 {
21 }
22 
24 {
26  // Initially the most probable joint type is rigid
28 }
29 
31 {
32  this->_rig_max_translation = max_trans;
33 }
34 
36 {
37  this->_rig_max_rotation = max_rot;
38 }
39 
41 {
42  this->_predicted_delta_pose_in_rrbf = Eigen::Twistd(0., 0., 0., 0., 0., 0.);
43 
44  Eigen::Displacementd predicted_delta = this->_predicted_delta_pose_in_rrbf.exp(1e-20);
45  Eigen::Displacementd T_rrbf_srbf_t0 = this->_srb_initial_pose_in_rrbf.exp(1.0e-20);
46  Eigen::Displacementd T_rrbf_srbf_t_next = predicted_delta * T_rrbf_srbf_t0;
47 
48  this->_srb_predicted_pose_in_rrbf = T_rrbf_srbf_t_next.log(1.0e-20);
49 }
50 
52 {
53  double p_one_meas_given_model_params = 0;
54  double p_all_meas_given_model_params = 0;
55 
56  double sigma_translation = 0.05;
57  double sigma_rotation = 0.2;
58 
59  double accumulated_error = 0.;
60 
61  double frame_counter = 0.;
62 
63  // Estimate the number of samples used for likelihood estimation
64  size_t trajectory_length = this->_delta_poses_in_rrbf.size();
65  size_t amount_samples = std::min(trajectory_length, (size_t)this->_likelihood_sample_num);
66 
67  // Estimate the delta for the iterator over the number of samples
68  // This makes that we take _likelihood_sample_num uniformly distributed over the whole trajectory, instead of the last _likelihood_sample_num
69  double delta_idx_samples = (double)std::max(1., (double)trajectory_length/(double)this->_likelihood_sample_num);
70 
71  size_t current_idx = 0;
72 
73  // We test the last window_length motions
74  for (size_t sample_idx = 0; sample_idx < amount_samples; sample_idx++)
75  {
76  current_idx = boost::math::round(sample_idx*delta_idx_samples);
77  Eigen::Displacementd rb2_last_delta_relative_displ = this->_delta_poses_in_rrbf.at(current_idx).exp(1e-12);
78  Eigen::Vector3d rb2_last_delta_relative_translation = rb2_last_delta_relative_displ.getTranslation();
79  Eigen::Quaterniond rb2_last_delta_relative_rotation = Eigen::Quaterniond(rb2_last_delta_relative_displ.qw(),
80  rb2_last_delta_relative_displ.qx(),
81  rb2_last_delta_relative_displ.qy(),
82  rb2_last_delta_relative_displ.qz());
83 
84  Eigen::Vector3d rigid_joint_translation = Eigen::Vector3d(0.,0.,0.);
85  Eigen::Displacementd rb2_last_delta_relative_displ_rigid_hyp = Eigen::Twistd(0.,
86  0.,
87  0.,
88  rigid_joint_translation.x(),
89  rigid_joint_translation.y(),
90  rigid_joint_translation.z()).exp(1e-12);
91  Eigen::Vector3d rb2_last_delta_relative_translation_rigid_hyp = rb2_last_delta_relative_displ_rigid_hyp.getTranslation();
92  Eigen::Quaterniond rb2_last_delta_relative_rotation_rigid_hyp = Eigen::Quaterniond(rb2_last_delta_relative_displ_rigid_hyp.qw(),
93  rb2_last_delta_relative_displ_rigid_hyp.qx(),
94  rb2_last_delta_relative_displ_rigid_hyp.qy(),
95  rb2_last_delta_relative_displ_rigid_hyp.qz());
96 
97  // Distance proposed by park and okamura in "Kinematic calibration using the product of exponentials formula"
98  double translation_error = (rb2_last_delta_relative_translation - rb2_last_delta_relative_translation_rigid_hyp).norm();
99 
100  if(translation_error > this->_rig_max_translation)
101  {
102  _motion_memory_prior = 0.0;
103  }
104 
105  Eigen::Quaterniond rotation_error = rb2_last_delta_relative_rotation.inverse() * rb2_last_delta_relative_rotation_rigid_hyp;
106  double rotation_error_angle = Eigen::Displacementd(0., 0., 0., rotation_error.w(),rotation_error.x(), rotation_error.y(),rotation_error.z()).log(1e-12).norm();
107 
108 
109  if(rotation_error_angle > this->_rig_max_rotation)
110  {
111  _motion_memory_prior = 0.0;
112  }
113 
114  accumulated_error += translation_error + fabs(rotation_error_angle);
115 
116  p_one_meas_given_model_params = (1.0/(sigma_translation*sqrt(2.0*M_PI)))*exp((-1.0/2.0)*pow(translation_error/sigma_translation, 2)) *
117  (1.0/(sigma_rotation*sqrt(2.0*M_PI)))*exp((-1.0/2.0)*pow(rotation_error_angle/sigma_rotation, 2));
118 
119  p_all_meas_given_model_params += (p_one_meas_given_model_params/(double)amount_samples);
120 
121  frame_counter++;
122  }
123 
124  this->_measurements_likelihood = p_all_meas_given_model_params;
125 }
126 
128 {
130 }
131 
133 {
134  // The delta in the pose of the SRB is the delta in the pose of the RRB (its velocity!)
135  Eigen::Twistd delta_rrb_in_sf = this->_rrb_current_vel_in_sf*(this->_loop_period_ns/1e9);
136  Eigen::Twistd delta_srb_in_sf = delta_rrb_in_sf;
137 
138  geometry_msgs::TwistWithCovariance hypothesis;
139 
140  hypothesis.twist.linear.x = delta_srb_in_sf.vx();
141  hypothesis.twist.linear.y = delta_srb_in_sf.vy();
142  hypothesis.twist.linear.z = delta_srb_in_sf.vz();
143  hypothesis.twist.angular.x = delta_srb_in_sf.rx();
144  hypothesis.twist.angular.y = delta_srb_in_sf.ry();
145  hypothesis.twist.angular.z = delta_srb_in_sf.rz();
146 
147  for (unsigned int i = 0; i < 6; i++)
148  {
149  for (unsigned int j = 0; j < 6; j++)
150  {
151  hypothesis.covariance[6 * i + j] = this->_rrb_vel_cov_in_sf(i, j)*(this->_loop_period_ns/1e9);
152  }
153  }
154 
155  return hypothesis;
156 }
157 
159 {
160  // The delta in the pose of the SRB is the delta in the pose of the RRB (its velocity!)
161  Eigen::Twistd delta_rrb_in_sf = this->_rrb_current_vel_in_sf;
162  Eigen::Twistd delta_srb_in_sf = delta_rrb_in_sf;
163 
164  geometry_msgs::TwistWithCovariance hypothesis;
165 
166  hypothesis.twist.linear.x = delta_srb_in_sf.vx();
167  hypothesis.twist.linear.y = delta_srb_in_sf.vy();
168  hypothesis.twist.linear.z = delta_srb_in_sf.vz();
169  hypothesis.twist.angular.x = delta_srb_in_sf.rx();
170  hypothesis.twist.angular.y = delta_srb_in_sf.ry();
171  hypothesis.twist.angular.z = delta_srb_in_sf.rz();
172 
173  for (unsigned int i = 0; i < 6; i++)
174  {
175  for (unsigned int j = 0; j < 6; j++)
176  {
177  hypothesis.covariance[6 * i + j] = this->_rrb_vel_cov_in_sf(i, j);
178  }
179  }
180 
181  return hypothesis;
182 }
183 
185 {
186  Eigen::Twistd delta_rrb_in_sf = this->_rrb_current_vel_in_sf*(this->_loop_period_ns/1e9);
187  Eigen::Twistd rrb_next_pose_in_sf = (delta_rrb_in_sf.exp(1e-12)*this->_rrb_current_pose_in_sf.exp(1e-12)).log(1e-12);
188 
189  //Eigen::Twistd rrb_next_pose_in_sf = this->_rrb_current_pose_in_sf + this->_rrb_current_vel_in_sf;
190  Eigen::Displacementd T_sf_rrbf_next = rrb_next_pose_in_sf.exp(1e-12);
191  Eigen::Displacementd T_rrbf_srbf_next = this->_srb_predicted_pose_in_rrbf.exp(1e-12);
192 
193  Eigen::Displacementd T_sf_srbf_next = T_rrbf_srbf_next*T_sf_rrbf_next;
194 
195  Eigen::Twistd srb_next_pose_in_sf = T_sf_srbf_next.log(1e-12);
196 
197  geometry_msgs::TwistWithCovariance hypothesis;
198 
199  hypothesis.twist.linear.x = srb_next_pose_in_sf.vx();
200  hypothesis.twist.linear.y = srb_next_pose_in_sf.vy();
201  hypothesis.twist.linear.z = srb_next_pose_in_sf.vz();
202  hypothesis.twist.angular.x = srb_next_pose_in_sf.rx();
203  hypothesis.twist.angular.y = srb_next_pose_in_sf.ry();
204  hypothesis.twist.angular.z = srb_next_pose_in_sf.rz();
205 
206  Eigen::Matrix<double,6,6> new_pose_covariance = this->_rrb_pose_cov_in_sf;
207  for (unsigned int i = 0; i < 6; i++)
208  {
209  for (unsigned int j = 0; j < 6; j++)
210  {
211  hypothesis.covariance[6 * i + j] = new_pose_covariance(i, j);
212  }
213  }
214 
215 #ifdef PUBLISH_PREDICTED_POSE_AS_PWC
216  // This is used to visualize the predictions based on the joint hypothesis
217  geometry_msgs::PoseWithCovarianceStamped pose_with_cov_stamped;
218  pose_with_cov_stamped.header.stamp = ros::Time::now();
219  pose_with_cov_stamped.header.frame_id = "camera_rgb_optical_frame";
220 
221  Eigen::Displacementd displ_from_twist = srb_next_pose_in_sf.exp(1e-12);
222  pose_with_cov_stamped.pose.pose.position.x = displ_from_twist.x();
223  pose_with_cov_stamped.pose.pose.position.y = displ_from_twist.y();
224  pose_with_cov_stamped.pose.pose.position.z = displ_from_twist.z();
225  pose_with_cov_stamped.pose.pose.orientation.x = displ_from_twist.qx();
226  pose_with_cov_stamped.pose.pose.orientation.y = displ_from_twist.qy();
227  pose_with_cov_stamped.pose.pose.orientation.z = displ_from_twist.qz();
228  pose_with_cov_stamped.pose.pose.orientation.w = displ_from_twist.qw();
229 
230  for (unsigned int i = 0; i < 6; i++)
231  for (unsigned int j = 0; j < 6; j++)
232  pose_with_cov_stamped.pose.covariance[6 * i + j] = hypothesis.covariance[6 * i + j];
233  _predicted_next_pose_publisher.publish(pose_with_cov_stamped);
234 #endif
235 
236  return hypothesis;
237 }
238 
239 std::vector<visualization_msgs::Marker> RigidJointFilter::getJointMarkersInRRBFrame() const
240 {
241  std::vector<visualization_msgs::Marker> rigid_markers;
242  // CONNECTION MARKER ///////////////////////////////////////////////////////////////////////////////////////////////////////////
243  visualization_msgs::Marker connection_marker;
244  connection_marker.ns = "kinematic_structure";
245  connection_marker.action = visualization_msgs::Marker::ADD;
246  connection_marker.type = visualization_msgs::Marker::ARROW;
247  connection_marker.id = 3 * this->_joint_id;
248  connection_marker.scale.x = 0.02f;
249  connection_marker.scale.y = 0.f;
250  connection_marker.scale.z = 0.f;
251  connection_marker.color.r = 0.f;
252  connection_marker.color.g = 0.f;
253  connection_marker.color.b = 1.f;
254  connection_marker.color.a = 1.f;
255  // Estimate position from supporting features:
256  Eigen::Displacementd current_ref_pose_displ = this->_rrb_current_pose_in_sf.exp(1e-12);
257  Eigen::Affine3d current_ref_pose;
258  current_ref_pose.matrix() = current_ref_pose_displ.toHomogeneousMatrix();
259  Eigen::Vector3d second_centroid_relative_to_ref_body = current_ref_pose.inverse() * this->_srb_centroid_in_sf;
260  geometry_msgs::Point pt1;
261  pt1.x = second_centroid_relative_to_ref_body.x();
262  pt1.y = second_centroid_relative_to_ref_body.y();
263  pt1.z = second_centroid_relative_to_ref_body.z();
264  connection_marker.points.push_back(pt1);
265  // The markers are now defined wrt to the reference frame and I want the rigid joint marker to go from the centroid of
266  // the second rigid body to the centroid of the reference rigid body
267  Eigen::Vector3d first_centroid_relative_to_ref_body = current_ref_pose.inverse() * this->_rrb_centroid_in_sf;
268  geometry_msgs::Point pt2;
269  pt2.x = first_centroid_relative_to_ref_body.x();
270  pt2.y = first_centroid_relative_to_ref_body.y();
271  pt2.z = first_centroid_relative_to_ref_body.z();
272  connection_marker.points.push_back(pt2);
273 
274  rigid_markers.push_back(connection_marker);
275 
276  // Delete other markers
277  visualization_msgs::Marker empty_marker;
278  empty_marker.pose.position.x = 0.;
279  empty_marker.pose.position.y = 0.;
280  empty_marker.pose.position.z = 0.;
281  empty_marker.header.frame_id = "camera_rgb_optical_frame";
282  empty_marker.type = visualization_msgs::Marker::SPHERE;
283  empty_marker.action = visualization_msgs::Marker::DELETE;
284  empty_marker.scale.x = 0.0001; //Using start and end points, scale.x is the radius of the array body
285  empty_marker.scale.y = 0.0001; //Using start and end points, scale.x is the radius of the array body
286  empty_marker.scale.z = 0.0001; //Using start and end points, scale.x is the radius of the array body
287  empty_marker.color.a = 0.3;
288  empty_marker.color.r = 0.0;
289  empty_marker.color.g = 0.0;
290  empty_marker.color.b = 1.0;
291  empty_marker.ns = "kinematic_structure";
292  empty_marker.id = 3 * this->_joint_id + 1;
293 
294  rigid_markers.push_back(empty_marker);
295 
296  empty_marker.id = 3 * this->_joint_id + 2;
297  rigid_markers.push_back(empty_marker);
298 
299  empty_marker.ns = "kinematic_structure_uncertainty";
300  empty_marker.id = 3 * this->_joint_id ;
301 
302  rigid_markers.push_back(empty_marker);
303 
304  empty_marker.id = 3* this->_joint_id + 1;
305 
306  rigid_markers.push_back(empty_marker);
307 
308  empty_marker.id = 3* this->_joint_id + 2;
309 
310  rigid_markers.push_back(empty_marker);
311 
312  return rigid_markers;
313 }
314 
316 {
317  return RIGID_JOINT;
318 }
319 
321 {
322  return std::string("RigidJointFilter");
323 }
324 
virtual void estimateUnnormalizedModelProbability()
virtual void setMaxTranslationRigid(double max_trans)
Eigen::Twistd _rrb_current_pose_in_sf
Definition: JointFilter.h:417
Eigen::Twistd _predicted_delta_pose_in_rrbf
Definition: JointFilter.h:439
virtual JointFilterType getJointFilterType() const
INLINE Rall1d< T, V, S > log(const Rall1d< T, V, S > &arg)
virtual geometry_msgs::TwistWithCovariance getPredictedSRBDeltaPoseWithCovInSensorFrame()
Generate a prediction about the change in pose of the second rigid body (SRB) and its covariance usin...
Eigen::Matrix< double, 6, 6 > _rrb_vel_cov_in_sf
Definition: JointFilter.h:421
virtual void estimateMeasurementHistoryLikelihood()
virtual void initialize()
JointFilterType
Definition: JointFilter.h:71
Eigen::Twistd _srb_initial_pose_in_rrbf
Definition: JointFilter.h:428
virtual void setMaxRotationRigid(double max_rot)
Eigen::Matrix< double, 6, 6 > _rrb_pose_cov_in_sf
Definition: JointFilter.h:419
Eigen::Twistd _rrb_current_vel_in_sf
Definition: JointFilter.h:420
Eigen::Vector3d _rrb_centroid_in_sf
Definition: JointFilter.h:385
double _model_prior_probability
Definition: JointFilter.h:348
virtual void predictMeasurement()
Eigen::Twistd _srb_predicted_pose_in_rrbf
Definition: JointFilter.h:434
virtual geometry_msgs::TwistWithCovariance getPredictedSRBPoseWithCovInSensorFrame()
double _measurements_likelihood
Definition: JointFilter.h:346
double _unnormalized_model_probability
Definition: JointFilter.h:351
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
virtual std::string getJointFilterTypeStr() const
virtual std::vector< visualization_msgs::Marker > getJointMarkersInRRBFrame() const
virtual geometry_msgs::TwistWithCovariance getPredictedSRBVelocityWithCovInSensorFrame()
Generate a prediction about the velocity of the second rigid body (SRB) and its covariance using the ...
INLINE Rall1d< T, V, S > exp(const Rall1d< T, V, S > &arg)
INLINE Rall1d< T, V, S > pow(const Rall1d< T, V, S > &arg, double m)
JointCombinedFilterId _joint_id
Definition: JointFilter.h:332
std::vector< Eigen::Twistd > _delta_poses_in_rrbf
Definition: JointFilter.h:440
static Time now()
Eigen::Vector3d _srb_centroid_in_sf
Definition: JointFilter.h:386
double _loop_period_ns
Definition: JointFilter.h:444


joint_tracker
Author(s): Roberto Martín-Martín
autogenerated on Mon Jun 10 2019 14:06:16