@@ -92,6 +92,8 @@ void Kinematics::initROS(ros::NodeHandle *n) {
9292 ROS_INFO (" Kinematics: rivz enabled" );
9393 rviz_uav_marker_pub_ = n->advertise <visualization_msgs::Marker>(" /uav_marker" , 1 );
9494 }
95+
96+ n->param (" /Debug/Kinematics/velocity_debug" , enable_velocity_debug_, false );
9597}
9698
9799void Kinematics::setPose (const std::string& source_frame, const ros::Time& update_time, const geometry_msgs::Pose& p, int debug) {
@@ -166,13 +168,46 @@ bool Kinematics::getPoseStamped(std::string& target_frame_id, geometry_msgs::Po
166168
167169void Kinematics::setPosition (const ros::Time& update_time, double x, double y, double z, int debug) {
168170 checkPosition (x, y, z, debug);
171+
172+ // Calculate speed
173+ double delta_t = update_time.toSec () - update_time_.toSec ();
174+
175+ if (update_cnt_ <= 0 || delta_t <= 0.0 )
176+ {
177+ position_.x = x;
178+ position_.y = y;
179+ position_.z = z;
180+
181+ setLinearVelocity (0.0 , 0.0 , 0.0 );
182+ return ;
183+ }
184+
185+ double B = 1.0 - speed_filter_;
186+
187+ Eigen::Vector3d vel;
188+
189+ vel[0 ] = (x - position_.x ) / delta_t ;
190+ vel[1 ] = (y - position_.y ) / delta_t ;
191+ vel[2 ] = (z - position_.z ) / delta_t ;
192+
193+ vel = (vel * speed_filter_) + (B * velLinear_);
194+ velLinear_ = vel;
195+
169196 position_.x = x;
170197 position_.y = y;
171198 position_.z = z;
172199
173- // SSS Calculate speed
174-
175200 update_time_ = update_time;
201+
202+ if (enable_velocity_debug_)
203+ {
204+ Eigen::Vector3d vel_debug;
205+ getLinearVelocity (drone_ENU, vel_debug);
206+ ROS_INFO (" Kinematics Velocity standard[%s : %0.3f %0.3f, %0.3f] body[%s : %0.3f %0.3f, %0.3f]" ,
207+ frame_id_.c_str (), velLinear_[0 ], velLinear_[1 ], velLinear_[2 ],
208+ drone_ENU, vel_debug[0 ], vel_debug[1 ], vel_debug[2 ]);
209+
210+ }
176211}
177212
178213#define MAX_POS_ERR 1.0
@@ -234,14 +269,34 @@ void Kinematics::setVelocity(const Eigen::Vector3d& linear,
234269 setAngularVelocity (angular);
235270}
236271
237- void Kinematics::setAngularVelocity (const Eigen::Vector3d& vel) {
238- velAngular_ = vel;
239- }
272+ bool Kinematics::getLinearVelocity (std::string target_frame_id, Eigen::Vector3d& velocity) {
273+
274+ geometry_msgs::TransformStamped ts;
275+ if (!transformer_.getTransform (target_frame_id, frame_id_, ts, true ))
276+ {
277+ ROS_ERROR (" Kinematics::getLinearVelocity : No transform from[%s] to[%s]" , frame_id_.c_str (), target_frame_id.c_str ());
278+ return false ;
279+ }
280+
281+ Eigen::Quaterniond rot;
282+ rot.x () = ts.transform .rotation .x ;
283+ rot.y () = ts.transform .rotation .y ;
284+ rot.z () = ts.transform .rotation .z ;
285+ rot.w () = ts.transform .rotation .w ;
240286
241- void Kinematics::setAngularVelocity (const geometry_msgs::Vector3& vel) {
242- velAngular_[0 ] = vel.x ;
243- velAngular_[1 ] = vel.y ;
244- velAngular_[2 ] = vel.z ;
287+ velocity = rot * velLinear_;
288+
289+ // ps.pose.position.x = ts.transform.translation.x;
290+ // ps.pose.position.y = ts.transform.translation.y;
291+ // ps.pose.position.z = ts.transform.translation.z;
292+ // ps.pose.orientation.x = ts.transform.rotation.x;
293+ // ps.pose.orientation.y = ts.transform.rotation.y;
294+ // ps.pose.orientation.z = ts.transform.rotation.z;
295+ // ps.pose.orientation.w = ts.transform.rotation.w;
296+ // ps.header.stamp = update_time_;
297+ // ps.header.frame_id = target_frame_id;
298+
299+ return true ;
245300}
246301
247302void Kinematics::setAngularVelocity (double x, double y, double z) {
@@ -250,16 +305,6 @@ void Kinematics::setAngularVelocity(double x, double y, double z) {
250305 velAngular_[2 ] = z;
251306}
252307
253- void Kinematics::setLinearVelocity (const Eigen::Vector3d& vel) {
254- velLinear_ = vel;
255- }
256-
257- void Kinematics::setLinearVelocity (const geometry_msgs::Vector3& vel) {
258- velLinear_[0 ] = vel.x ;
259- velLinear_[1 ] = vel.y ;
260- velLinear_[2 ] = vel.z ;
261- }
262-
263308void Kinematics::setLinearVelocity (double x, double y, double z) {
264309 velLinear_[0 ] = x;
265310 velLinear_[1 ] = y;
0 commit comments