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

Sequence movement with delay

asked 2014-06-11 05:01:07 -0500

nadal11 gravatar image

updated 2014-06-11 05:01:45 -0500

Hi there,

I am trying to put a delay between a movement of turtlebot so that I can execute the movement step by step. However, the behavior of the turtlebot is different from what I expected. It's only execute the first movement and skip the other command. Could anybody help me out here? Here I attached my sample of program. Thank you.

#include <ros/ros.h>
#include <stdlib.h>
#include <iostream.h>
#include <geometry_msgs/Twist.h>
#include <geometry_msgs/Pose.h>
#include <numeric>

using namespace std;

ros::Publisher pub_vel;
geometry_msgs::Twist cmdvel;
geometry_msgs::Pose Curr_Loc;

void odomCB(const nav_msgs::Odometry::ConstPtr& msg)
{
    Curr_Loc = msg->pose.pose;
    double x=msg->pose.pose.position.x;
    double y=msg->pose.pose.position.y;
    double Z=msg->pose.pose.orientation.z;
    double W=msg->pose.pose.orientation.w;

    cout<<"Current location: "<<x<<", "<<y<<"  Orientation  "<<Z<<", "<<W<<endl;

    cmdvel.angular.z=0.0;
    cmdvel.linear.x=0.2;
    usleep(300000); //delay
    cmdvel.angular.z=-0.2;
    cmdvel.linear.x=0.0;
    usleep(300000); //delay

pub_vel.publish(cmdvel);
}

int main(int argc, char** argv)
{
    ros::init(argc, argv, "sequence move");
    ros::NodeHandle n;

    pub_vel=n.advertise<geometry_msgs::Twist>("cmd_vel",1);
    pub_vel.publish(cmdvel);

    ros::Subscriber Sub_Odom=n.subscribe("odom",1,odomCB);  //subcribe
    ros::spin();
    return 0;
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-06-11 05:45:37 -0500

dornhege gravatar image

You are just setting values to some internal struct when setting cmdvel and waiting between that. This doesn't affect the robot at all. Only the final publish call will actually send something to the robot.

You are also sending those out of a callback. You'll probably want to restructure your program, so that you can send comamnds continuously from a main loop to keep the robot moving.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-06-11 05:01:07 -0500

Seen: 236 times

Last updated: Jun 11 '14