ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

holding a twist message on cmd_vel node

asked 2014-06-27 11:33:43 -0500

choog gravatar image

updated 2014-06-27 14:25:32 -0500

Hi again,

I have come up with some code that allows for a turtlebot 2, to go in a straight path for 5 seconds. My problem is that it sends the velocity command for a split fraction of time, Im not sure what it is. I am looking for a way to hold a published command on a topic for a period of time. What is the easiest way to hold a command on a topic for a certain period of time? I guess I'm looking for some command that is like; ros::topic.hold(time);

#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <time.h>
#include <iostream>

using namespace std;

int main(int argc, char** argv){

  ros::init(argc, argv, "gs_now");
  ros::NodeHandle nh;

  ros::Publisher pub = nh.advertise<geometry_msgs::Twist>(
    "/cmd_vel_mux/input/navi", 1);
  ros::Rate rate(1);  
  geometry_msgs::Twist vel;
  vel.angular.z = 0.0;
  vel.linear.x = 0.1;

  clock_t t, l;

  t = clock();

  while(ros::ok()){
     l = clock() - t;

     if( ((double)l / ((double)CLOCKS_PER_SEC)) == 1.000){
        pub.publish(vel);
        ROS_INFO_STREAM("Sending random velocity command:"
      << " linear=" << vel.linear.x
      << " angular=" << vel.angular.z
      << " at time =" << ((double)l / ((double)CLOCKS_PER_SEC)));
     }
     else if(((double)l / ((double)CLOCKS_PER_SEC)) == 2.000){
       pub.publish(vel);
       ROS_INFO_STREAM("Sending random velocity command:"
      << " linear=" << vel.linear.x
      << " angular=" << vel.angular.z
      << " at time =" << ((double)l / ((double)CLOCKS_PER_SEC)));
     }
     else if(((double)l / ((double)CLOCKS_PER_SEC)) == 3.000){
       pub.publish(vel);
       ROS_INFO_STREAM("Sending random velocity command:"
      << " linear=" << vel.linear.x
      << " angular=" << vel.angular.z
      << " at time =" << ((double)l / ((double)CLOCKS_PER_SEC)));
     }
     else if(((double)l / ((double)CLOCKS_PER_SEC)) == 4.000){
       pub.publish(vel);
       ROS_INFO_STREAM("Sending random velocity command:"
      << " linear=" << vel.linear.x
      << " angular=" << vel.angular.z
      << " at time =" << ((double)l / ((double)CLOCKS_PER_SEC)));
     }
    else if(((double)l / ((double)CLOCKS_PER_SEC)) == 5.000){
       ros::shutdown();
    }

       //rate.sleep();  
  }

}

Thank you.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2014-06-28 09:52:44 -0500

dornhege gravatar image

What you want to do does not work. Messages/commands are not send to a topic, they are send over a topic to a node, where the topic is only a common name to connect over. There is simply nothing in between that could "hold" a message, besides your node.

So you have to make sure that you send the message in a high enough frequency, so the robot keeps on moving. This could be achieved easily with your code structure if you just change your == to <=. This will also fix this code which compares doubles with == that probably wouldn't have worked anyways. It's also a good idea to use ros::Time instead of clock cycles.

edit flag offensive delete link more

Comments

Thank you I edited the title of my question but I forgot to change the content. I wish there was a way to hold the message at the node. Why is ros::time better?

choog gravatar image choog  ( 2014-06-28 14:56:46 -0500 )edit

Using ros::TIme is the trick to reading time efficiently thank you.

choog gravatar image choog  ( 2014-06-30 09:31:29 -0500 )edit
0

answered 2014-06-27 11:49:58 -0500

Erwan R. gravatar image

Hello,

In my opinion, you could just check for the time ; after enough has elapsed, you can send another command or a "stop command" with speed equals to zero. The speed sent to the command topic is kept until the control node stops or receives a different command (at least on the PR2, but I don't think it's different for the TurtleBot).

edit flag offensive delete link more

Comments

The turtlebot does not keep the command. Basically if I just send it a command it will only hold the command for a split second. Like if I publish a command to move in X direction at .2 m/s. It only jerks for a split second.

choog gravatar image choog  ( 2014-06-27 11:54:34 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-06-27 11:33:43 -0500

Seen: 2,386 times

Last updated: Jun 28 '14