Failed to use if condition to process the received message in my callback function

asked 2019-04-17 02:13:50 -0500

stevensu1838 gravatar image

updated 2019-04-17 02:32:25 -0500

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);
  }
edit retag flag offensive close merge delete

Comments

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...

Isha Dijcks gravatar image Isha Dijcks  ( 2019-04-17 03:54:50 -0500 )edit

+1 for the std::min() but if you don't want to use it you could at least use if else instead of two consecutive if.

Delb gravatar image Delb  ( 2019-04-18 02:38:10 -0500 )edit