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

moving a simple object

asked 2014-01-24 04:12:45 -0500

robot_arm gravatar image

updated 2014-01-24 04:13:50 -0500

Hi All, I want to move an object, for example a box in 3D space, using ROS and visualize its movements in RVIZ. for this reason I wrote this node,

#include <ros/ros.h>
#include "geometry_msgs/PoseStamped.h"
#include "geometry_msgs/Pose.h"
// publisher handles
ros::Publisher goal_pub;
void publish_goal()
{ 
  geometry_msgs::PoseStamped msg;
  msg.header.frame_id = "base_link";
  msg.pose.position.x += 2; 
  msg.pose.position.y = 20; 
  msg.pose.position.z = 50;   
  msg.pose.orientation.w = 0; 
  msg.pose.orientation.x = -2.0; 
  msg.pose.orientation.y = 0; 
  msg.pose.orientation.z = 0;  
  goal_pub.publish(msg);
}
int main(int argc, char** argv) 
{
    ros::init(argc, argv, "move_sphere");
    ros::NodeHandle nh;
    ros::Rate loop_rate(100);
    goal_pub = nh.advertise<geometry_msgs::PoseStamped>("pose", 1);
    while (ros::ok()) 
    {
      publish_goal();
      ros::spinOnce();
      loop_rate.sleep();
    }
}

but the object don't move. anyone can tell me what is the mistake in code.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2014-01-24 04:26:18 -0500

Wolf gravatar image

updated 2014-01-27 00:03:44 -0500

Your code always publishes the same pos:

  geometry_msgs::PoseStamped msg;

msg contains default values( all zeros, false for bool )

  msg.pose.position.x += 2;

msg.pose.position.x is now (always) 0+2, i. e. 2.!! It does not increment (I guess that'd be your intention).

If you wish to do that define msg in global space, initialize at beginning of main and than increment in your loop like this:

#include <ros/ros.h>
#include "geometry_msgs/PoseStamped.h"
#include "geometry_msgs/Pose.h"
// publisher handles
ros::Publisher goal_pub;
geometry_msgs::PoseStamped msg;

void publish_goal()
{ 
  msg.pose.position.x += 2; 

  goal_pub.publish(msg);
}

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

  msg.header.frame_id = "base_link";
  msg.pose.position.x = 2; 
  msg.pose.position.y = 20; 
  msg.pose.position.z = 50;   
  msg.pose.orientation.w = 0; 
  msg.pose.orientation.x = -2.0; 
  msg.pose.orientation.y = 0; 
  msg.pose.orientation.z = 0;  

    ros::init(argc, argv, "move_sphere");
    ros::NodeHandle nh;
    ros::Rate loop_rate(100);
    goal_pub = nh.advertise<geometry_msgs::PoseStamped>("pose", 1);
    while (ros::ok()) 
    {
      publish_goal();
      ros::spinOnce();
      loop_rate.sleep();
    }
}
edit flag offensive delete link more

Comments

1

Nope, messages are initialized with default values (0.0 for floats, `false` for bools etc.) in their constructor. Other than that, this answer is correct (if you always re-initialize the msg, you'll always get the same results, so nothing moves).

Martin Günther gravatar image Martin Günther  ( 2014-01-26 21:18:19 -0500 )edit

@robot_arm: There are a couple more problems with your code, but they don't stop the object from moving: 1. the quaternion isn't valid; try orientation.x = 1.0. 2. You should set header.stamp = ros::Time::now(). 3. Do you really want to move the object by 2 * 100 meters a second? Try 0.02.

Martin Günther gravatar image Martin Günther  ( 2014-01-26 21:28:27 -0500 )edit

Thanks Martin, you're right. I edited the answer accordingly.

Wolf gravatar image Wolf  ( 2014-01-27 00:04:21 -0500 )edit

that's weird, I corrected the program and I did exaclty what you are suggested to. but nothing move. when I type rostopic info /pose, I got the following information: rostopic info pose Type: geometry_msgs/PoseStamped Publishers: * /move_sphere (http://d:48494/) Subscribers: None it seems that there is no node subscribed to this topic, why ?

robot_arm gravatar image robot_arm  ( 2014-01-27 02:38:03 -0500 )edit
0

answered 2014-01-24 07:07:38 -0500

robot_arm gravatar image

thanks wolf for your reply,

but still the object is not moving, it is in the initial position. how to move it ! do am I using the right topic to publish the position goal to be reached by the box.

edit flag offensive delete link more

Comments

What "object" are you trying to move? Is it listening on the topic "pose" for position information?

Tim Sweet gravatar image Tim Sweet  ( 2014-01-25 06:51:43 -0500 )edit

the object is defined in an urdf file, it's a simple box or sphere. with this program I want to move it to another position, but it doesn't move to the goal position. when I type rostopic info /pose, I got the following information: rostopic info pose Type: geometry_msgs/PoseStamped Publishers: * /move_sphere (http://d:48494/) Subscribers: None it seems that there is no node subscribed to this topic, why ?

robot_arm gravatar image robot_arm  ( 2014-01-25 10:54:03 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-01-24 04:12:45 -0500

Seen: 2,233 times

Last updated: Jan 27 '14