Skip to content

Commit afc306d

Browse files
authored
Add parameter to set expire duration of cmd_vel (openspur#55)
1 parent 9e5a86e commit afc306d

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

doc/ypspur_ros.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ This topic name is configurable through `ad?_name` parameter.
6060

6161
Path to your vehicle parameter file for yp-spur.
6262

63+
### ~/cmd_vel_expire [double: -1.0]
64+
65+
Expire duration of cmd_vel. After this duration since receiving cmd_vel, velocity and angular velocity is set to zero.
66+
Negative or zero value disables this feature.
67+
6368
### ~/ad?_enable [bool: false]
6469

6570
### ~/ad?_name [string: "ad" + i]

src/ypspur_ros.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ class YpspurRosNode
159159
int device_error_state_prev_;
160160
ros::Time device_error_state_time_;
161161

162-
geometry_msgs::Twist cmd_vel_;
162+
geometry_msgs::Twist::ConstPtr cmd_vel_;
163+
ros::Time cmd_vel_time_;
164+
ros::Duration cmd_vel_expire_;
163165

164166
int control_mode_;
165167

@@ -180,7 +182,8 @@ class YpspurRosNode
180182
}
181183
void cbCmdVel(const geometry_msgs::Twist::ConstPtr &msg)
182184
{
183-
cmd_vel_ = *msg;
185+
cmd_vel_ = msg;
186+
cmd_vel_time_ = ros::Time::now();
184187
if (control_mode_ == ypspur_ros::ControlMode::VELOCITY)
185188
{
186189
YP::YPSpur_vel(msg->linear.x, msg->angular.z);
@@ -464,6 +467,11 @@ class YpspurRosNode
464467
pnh_.param("ypspur_bin", ypspur_bin_, std::string("ypspur-coordinator"));
465468
pnh_.param("param_file", param_file_, std::string(""));
466469
pnh_.param("tf_time_offset", tf_time_offset_, 0.0);
470+
471+
double cmd_vel_expire_s;
472+
pnh_.param("cmd_vel_expire", cmd_vel_expire_s, -1.0);
473+
cmd_vel_expire_ = ros::Duration(cmd_vel_expire_s);
474+
467475
std::string ad_mask("");
468476
ads_.resize(ad_num_);
469477
for (int i = 0; i < ad_num_; i++)
@@ -829,12 +837,23 @@ class YpspurRosNode
829837
ros::Rate loop(params_["hz"]);
830838
while (!g_shutdown)
831839
{
832-
auto now = ros::Time::now();
833-
float dt = 1.0 / params_["hz"];
840+
const auto now = ros::Time::now();
841+
const float dt = 1.0 / params_["hz"];
842+
843+
if (cmd_vel_ && cmd_vel_expire_ > ros::Duration(0))
844+
{
845+
if (cmd_vel_time_ + cmd_vel_expire_ < now)
846+
{
847+
// cmd_vel is too old and expired
848+
cmd_vel_ = nullptr;
849+
if (control_mode_ == ypspur_ros::ControlMode::VELOCITY)
850+
YP::YPSpur_vel(0.0, 0.0);
851+
}
852+
}
834853

835854
if (mode_ == DIFF)
836855
{
837-
double x, y, yaw, v, w;
856+
double x, y, yaw, v(0), w(0);
838857
double t;
839858

840859
if (!simulate_control_)
@@ -847,8 +866,11 @@ class YpspurRosNode
847866
else
848867
{
849868
t = ros::Time::now().toSec();
850-
v = cmd_vel_.linear.x;
851-
w = cmd_vel_.angular.z;
869+
if (cmd_vel_)
870+
{
871+
v = cmd_vel_->linear.x;
872+
w = cmd_vel_->angular.z;
873+
}
852874
yaw = tf::getYaw(odom.pose.pose.orientation) + dt * w;
853875
x = odom.pose.pose.position.x + dt * v * cosf(yaw);
854876
y = odom.pose.pose.position.y + dt * v * sinf(yaw);

0 commit comments

Comments
 (0)