I have written the following program to move the BB8 robot in a square. I couldn't make it move in square. I presume that it is because of my bad understanding of sleep(). Here is my code :

#include "geometry_msgs/Twist.h"
#include "ros/ros.h"
#include "std_srvs/Empty.h"
class MoveBB8

  ros::NodeHandle nh_;
  ros::Rate *rate;
  ros::Rate *rate1;
  ros::ServiceServer srv_perform_square_;
  ros::Publisher pub_cmd_vel_;

  MoveBB8() {
    rate = new ros::Rate(0.25);
    rate1 = new ros::Rate(0.5);
    srv_perform_square_ =
        nh_.advertiseService("/perform_square", &MoveBB8::my_callback, this);
    pub_cmd_vel_ = nh_.advertise<geometry_msgs::Twist>("/cmd_vel", 1);

  geometry_msgs::Twist movestraight() {
    ROS_INFO("moving straight");
    geometry_msgs::Twist vel;
    vel.linear.x = 0.25;
    vel.angular.z = 0;

    return vel;

  geometry_msgs::Twist turnright() {
    ROS_INFO("turning right");
    geometry_msgs::Twist vel;
    vel.linear.x = 0;
    vel.angular.z = 0.25;

    return vel;

  geometry_msgs::Twist stop() {
    geometry_msgs::Twist vel;
    vel.linear.x = 0;
    vel.angular.z = 0;

    return vel;
  void moveinsquare() {

    geometry_msgs::Twist vel;
    int i = 0;
    while (i < 4) {
      vel = movestraight();

      vel = stop();

      vel = turnright();

      vel = stop();
    vel.linear.x = 0;
    vel.angular.z = 0;
  bool my_callback(std_srvs::Empty::Request &req,
                   std_srvs::Empty::Response &res) {
    ROS_INFO("my_callback called");
    return true;

int main(int argc, char **argv) {
  ros::init(argc, argv, "MoveBB8Server");
  ROS_INFO("Node Initialized");

  MoveBB8 moveBB8;
  ROS_INFO("moveBB8 instanciated");

  while (ros::ok()) {

    return 0;

I get the following output, I wrote some ROS_INFO to help me debug. I can see that my_callback() is not called though I have written

srv_perform_square_ =
            nh_.advertiseService("/perform_square", &MoveBB8::my_callback, this);

in my constructor. Also the complete code of my server runs without service call. I know I'm doing something wrong here, but I don't realize what is wrong. Please help me out on this. I'm stuck here for like more than a day

alt text

asked 24 May, 09:23

eashwar.erahan's gravatar image

accept rate: 0%

Hi @eashwar.erahan,

Please have a look at the following:

About the BB8 not moving in a square,

  • yes, you're not using rate properly. I recommend you don't use rate at all as this is not a good use case for it. What you just need to do is to sleep x amount of seconds before taking the next turn (that is move straight for x seconds, stop, turn right, stop, move straight again for x seconds again, stop, turn left, etc.
  • Also, (1) you need to implement your code so that the robot actually moves after turning (2) you can just remove the while loop.

About the service callback not getting called,

Try adding the word static to the callback function, i.e static bool .... Could you try and let us know how it goes? Also, I don't think the & preceding the function name is necessary.

permanent link

answered 27 May, 10:08

bayodesegun%40bayodesegun.com's gravatar image

accept rate: 52%

edited 27 May, 11:16

I have never come accross the sleep command in cpp nor the header file necessary for it in ROS. Could you tell me the header file and the function to be included in the program to implement sleep

(27 May, 10:15) eashwar.erahan

You can use something like this to sleep in ROS, no need to include another header

ros::Duration(0.5).sleep(); // sleep for half a second

(27 May, 11:01) bayodesegun@...

I update the answer and added some hint about the callback not getting called.

(27 May, 11:21) bayodesegun@...
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 24 May, 09:23

question was seen: 223 times

last updated: 27 May, 11:21

powered by OSQA