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

moving mobile robot in zigzag way

asked 2014-09-25 10:29:46 -0500

RSA_kustar gravatar image

Hello,

I want to move the husky robot using a code that publishes the command velocities. the movement should be like zigzag. here is the code that I am trying but it is not working.. I did not knew how to do it. Any help please ?

//const double M_PI=3.14159265359 ;
 const double deg_to_rad = M_PI / 180.0 ;
class test_pf
 {
 public:
test_pf(ros::NodeHandle & n_) : n(n_)
{
    vel_pub = n.advertise<geometry_msgs::Twist>("/husky_ns/husky/cmd_vel", 100);
};

void move(double val)
{
    geometry_msgs::Twist msg;
            msg.linear.x = val;
            msg.linear.y =  0.0 ;
            msg.linear.z =  0.0;
            msg.angular.x = 0 ;
            msg.angular.y = 0 ;
            msg.angular.z = 0 ;
        vel_pub.publish(msg);
}

void turn(double val)
{
    geometry_msgs::Twist msg;
            msg.linear.x = 0.0;
            msg.linear.y =  0.0 ;
            msg.linear.z =  0.0;
            msg.angular.x = 0.0 ;
            msg.angular.y = 0.0 ;
            msg.angular.z = val ;
        vel_pub.publish(msg);
}

  private:
  // ROS
  ros::NodeHandle n;
  ros::Publisher vel_pub ;
 } ;


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

// ROS node
std:: cout << " MAIN " << std::endl ;
ros::init(argc, argv, "move_robot");
ros::NodeHandle n;
ros::NodeHandle n_priv("~");
ros::Rate loop_rate(50);

// create an object
test_pf move_robot_obj(n);



while(ros::ok())
{
    move_robot_obj.move(1.0);
    loop_rate.sleep();
    move_robot_obj.turn(4.0);
    loop_rate.sleep();
    move_robot_obj.move(1.0);
    loop_rate.sleep();
    move_robot_obj.turn(-4.0);
    loop_rate.sleep();
    }
  return 0;
 }
edit retag flag offensive close merge delete

Comments

What behavior do you see when you try it? Does anything interesting (e.g., a warning or error) show up in the console?

Morgan gravatar image Morgan  ( 2014-09-25 10:58:00 -0500 )edit

no error in console.. it keeps moving forward and I cant notice the turns but eventually the track is changing slowly downward in un-noticable way

RSA_kustar gravatar image RSA_kustar  ( 2014-09-25 11:06:13 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2014-10-09 03:41:50 -0500

RSA_kustar gravatar image

I solved it using a dummy way of pulling the CPU by using a for loop

  for (int i=0 ;i <99999; i++)
        {
            std::cout << "turn left" << std::endl ;
            move_robot_obj_1.turn(0.780);
        }
        loop_rate.sleep();

        for (int i=0 ;i <99999; i++)
        {
            std::cout << "move forward" << std::endl ;
              move_robot_obj_1.move(1.0);
        }
        loop_rate.sleep();
        for (int i=0 ;i <99999; i++)
        {
            std::cout << "turn right" << std::endl ;
            move_robot_obj_1.turn(-1.2);
        }
        loop_rate.sleep();

        for (int i=0 ;i <99999; i++)
        {
            std::cout << "move forward" << std::endl ;
            move_robot_obj_1.move(1.0);
        }

in the turn you should specify the angle in radian not degree and send it to function that define a geometry_msgs::Twist msg and fill it with the required information and publish it. Also while turning, I send a velocity in the forward direction, so the robot will move in a wave form as the following:

  void turn(double val)
  {
      geometry_msgs::Twist msg;
     msg.linear.x = 1.0;
     msg.linear.y =  0.0 ;
     msg.linear.z =  0.0;
     msg.angular.x = 0.0 ;
     msg.angular.y = 0.0 ;
     msg.angular.z = val ;
     vel_pub.publish(msg);
 }

The robot moved in a wave form. I know that it is not the best way to do it. So, if you have another way please let me know.

edit flag offensive delete link more
1

answered 2014-09-25 11:02:48 -0500

bvbdort gravatar image

updated 2014-09-25 11:05:12 -0500

use ros::Duration(1).sleep(); to wait for robot to execute move or turn for 1 second.

ros::Rate loop_rate(50); and loop_rate.sleep(); try to run the loop at 50hz i.e robot get <1/50 sec for move or turn

edit flag offensive delete link more

Comments

I tired that after each function I used ros::Duration(1).sleep(); abd before the end of the while(ro::ok) I used the loop_rate() with 50 Hz the problem is that it moves for a very very very short distance then it stop moving for a second then it turns for a short angel then it stop moving etc

RSA_kustar gravatar image RSA_kustar  ( 2014-09-25 11:23:10 -0500 )edit

ros::Duration(1).sleep(); it actually stops the movement not waiting for execution

RSA_kustar gravatar image RSA_kustar  ( 2014-09-25 11:26:07 -0500 )edit

since publishing velocities and sleeping is not problem. try ros::Rate loop_rate(0.125) i.e giving more time for execution.

bvbdort gravatar image bvbdort  ( 2014-09-25 11:43:21 -0500 )edit

did not work :( it is not moving for 1 meter just small distances

RSA_kustar gravatar image RSA_kustar  ( 2014-09-26 00:59:44 -0500 )edit

may be problem on other side with husky subscriber. You can test it by publishing twist message from commandline. http://wiki.ros.org/rostopic#rostopic...

bvbdort gravatar image bvbdort  ( 2014-09-26 03:05:42 -0500 )edit

@bvbdort, I used the the Twist msgs in the code to make the robot move and turn but not in this way .. I solved the problem by using dummy while loop as (pulling) in order to make it work.

RSA_kustar gravatar image RSA_kustar  ( 2014-10-09 03:35:09 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-09-25 10:29:46 -0500

Seen: 1,477 times

Last updated: Oct 09 '14