Skip to content

Commit 022f24f

Browse files
authored
Add holonomic motion for turtlesim (ros#94)
the turtle can move forward by linear.x and linear.y
1 parent c076583 commit 022f24f

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

turtlesim/include/turtlesim/turtle.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class Turtle
7676
QPointF pos_;
7777
qreal orient_;
7878

79-
qreal lin_vel_;
79+
qreal lin_vel_x_;
80+
qreal lin_vel_y_;
8081
qreal ang_vel_;
8182
bool pen_on_;
8283
QPen pen_;

turtlesim/src/turtle.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ Turtle::Turtle(const ros::NodeHandle& nh, const QImage& turtle_image, const QPoi
4444
, turtle_image_(turtle_image)
4545
, pos_(pos)
4646
, orient_(orient)
47-
, lin_vel_(0.0)
47+
, lin_vel_x_(0.0)
48+
, lin_vel_y_(0.0)
4849
, ang_vel_(0.0)
4950
, pen_on_(true)
5051
, pen_(QColor(DEFAULT_PEN_R, DEFAULT_PEN_G, DEFAULT_PEN_B))
@@ -66,7 +67,8 @@ Turtle::Turtle(const ros::NodeHandle& nh, const QImage& turtle_image, const QPoi
6667
void Turtle::velocityCallback(const geometry_msgs::Twist::ConstPtr& vel)
6768
{
6869
last_command_time_ = ros::WallTime::now();
69-
lin_vel_ = vel->linear.x;
70+
lin_vel_x_ = vel->linear.x;
71+
lin_vel_y_ = vel->linear.y;
7072
ang_vel_ = vel->angular.z;
7173
}
7274

@@ -145,7 +147,8 @@ bool Turtle::update(double dt, QPainter& path_painter, const QImage& path_image,
145147

146148
if (ros::WallTime::now() - last_command_time_ > ros::WallDuration(1.0))
147149
{
148-
lin_vel_ = 0.0;
150+
lin_vel_x_ = 0.0;
151+
lin_vel_y_ = 0.0;
149152
ang_vel_ = 0.0;
150153
}
151154

@@ -154,8 +157,10 @@ bool Turtle::update(double dt, QPainter& path_painter, const QImage& path_image,
154157
orient_ = orient_ + ang_vel_ * dt;
155158
// Keep orient_ between -pi and +pi
156159
orient_ -= 2*PI * std::floor((orient_ + PI)/(2*PI));
157-
pos_.rx() += std::cos(orient_) * lin_vel_ * dt;
158-
pos_.ry() += - std::sin(orient_) * lin_vel_ * dt;
160+
pos_.rx() += std::cos(orient_) * lin_vel_x_ * dt
161+
- std::sin(orient_) * lin_vel_y_ * dt;
162+
pos_.ry() -= std::cos(orient_) * lin_vel_y_ * dt
163+
+ std::sin(orient_) * lin_vel_x_ * dt;
159164

160165
// Clamp to screen size
161166
if (pos_.x() < 0 || pos_.x() > canvas_width ||
@@ -172,7 +177,7 @@ bool Turtle::update(double dt, QPainter& path_painter, const QImage& path_image,
172177
p.x = pos_.x();
173178
p.y = canvas_height - pos_.y();
174179
p.theta = orient_;
175-
p.linear_velocity = lin_vel_;
180+
p.linear_velocity = std::sqrt(lin_vel_x_ * lin_vel_x_ + lin_vel_y_ * lin_vel_y_);
176181
p.angular_velocity = ang_vel_;
177182
pose_pub_.publish(p);
178183

0 commit comments

Comments
 (0)