Failed to use if condition to process the received message in my callback function
Hi all, I am using a node to subscribe Twist message and convert it into Timestamped message. Also, I want to make sure all the message that I assigned to Timestamped message is smaller than 0.05 and please take a look at the following snippet of code for this purpose. However, when I print the message with ROS_ERROR() , the messages still are not all smaller than 0.05. Who could you please tell me why? Thanks a lot.
void joyCallback(const geometry_msgs::Twist::ConstPtr& msg)
{
// Cartesian jogging with the axes
geometry_msgs::TwistStamped twist;
twist.header.stamp = ros::Time::now();
if (msg->linear.x < 0.05 ){
twist.twist.linear.x = msg->linear.x;}
if (msg->linear.x >= 0.05 ){
twist.twist.linear.x = 0.05;}
if (msg->linear.y < 0.05 ){
twist.twist.linear.y = msg->linear.y;}
if (msg->linear.y >= 0.05 ){
twist.twist.linear.y = 0.05;}
if (msg->linear.z < 0.05 ){
twist.twist.linear.z = msg->linear.z;}
if (msg->linear.z >= 0.05 ){
twist.twist.linear.z = 0.05;}
ROS_ERROR("Hello %s", "World");
ROS_ERROR("Hello %f", twist.twist.linear.x);
ROS_ERROR("Hello %f", twist.twist.linear.y);
ROS_ERROR("Hello %f", twist.twist.linear.z);
twist.twist.angular.x = 0;
twist.twist.angular.y = 0;
twist.twist.angular.z = 0;
twist_pub_.publish(twist);
}
Can you provide the input
msg
and the results of the prints?You could use the
std::min()
function here as it does what you are trying to accomplish:twist.twist.linear.x = std::min(msg->linear.x, 0.05)
https://stackoverflow.com/questions/1...
+1 for the
std::min()
but if you don't want to use it you could at least useif else
instead of two consecutiveif
.