Skip to content

Commit d793291

Browse files
committed
feat: added speed calculation to Kinematics
1 parent 7e42347 commit d793291

File tree

2 files changed

+78
-26
lines changed

2 files changed

+78
-26
lines changed

include/vb_util_lib/kinematics.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,22 @@ class Kinematics {
121121
void setVelocity(const geometry_msgs::Vector3& linear, const geometry_msgs::Vector3& angular);
122122
void setVelocity(const Eigen::Vector3d& linear, const Eigen::Vector3d& angular);
123123

124-
void setAngularVelocity(const Eigen::Vector3d& vel);
125-
void setAngularVelocity(const geometry_msgs::Vector3& vel);
124+
void setAngularVelocity(const Eigen::Vector3d& vel) {
125+
setAngularVelocity(vel[0], vel[1], vel[2]);
126+
}
127+
void setAngularVelocity(const geometry_msgs::Vector3& vel) {
128+
setAngularVelocity(vel.x, vel.y, vel.z);
129+
}
126130
void setAngularVelocity(double x, double y, double z);
127131
const Eigen::Vector3d& getAngularVelocity() const {
128132
return velAngular_;
129133
}
130-
void setLinearVelocity(const Eigen::Vector3d& vel);
131-
void setLinearVelocity(const geometry_msgs::Vector3& vel);
134+
void setLinearVelocity(const Eigen::Vector3d& vel) {
135+
setLinearVelocity(vel[0], vel[1], vel[2]);
136+
}
137+
void setLinearVelocity(const geometry_msgs::Vector3& vel) {
138+
setLinearVelocity(vel.x, vel.y, vel.z);
139+
}
132140
void setLinearVelocity(double x, double y, double z);
133141
double getLinearVelocityMagnitude() {
134142
return velLinear_.norm();
@@ -140,9 +148,7 @@ class Kinematics {
140148
/**
141149
* Get the linear velocity with respect to some frame of reference
142150
*/
143-
const Eigen::Vector3d& getLinearVelocity(std::string frame) const {
144-
return velLinearFrame_;
145-
}
151+
bool getLinearVelocity(std::string target_frame_id, Eigen::Vector3d& velocity);
146152

147153
bool isPoseInitialized() const {
148154
return update_cnt_ > 0;
@@ -386,6 +392,7 @@ class Kinematics {
386392

387393
// Debug flag
388394
bool debug_ {false};
395+
bool enable_velocity_debug_ {false};
389396

390397
/**
391398
* RVIZ debug display

src/vb_util_lib/kinematics.cpp

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

9799
void 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

167169
void 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

247302
void 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-
263308
void Kinematics::setLinearVelocity(double x, double y, double z) {
264309
velLinear_[0] = x;
265310
velLinear_[1] = y;

0 commit comments

Comments
 (0)