5

I am new to ROS...and trying to create a simple random number generator, which will publish the randomly generated values. For this I created a Position class :

include "stdlib.h"

namespace RandomPositionGenerator {


class Position {
private:
double x;
double y;
double z;
public:
Position();
void setPosition();
void getPosition(double &a, double &b, double &c);
};

Position::Position(){}

void Position::setPosition() {
x = rand();
y = rand();
z = rand();
}

void Position::getPosition(double &a, double &b, double &c) {
 a=x;
 b=y;
 c=z;

}

}

and used this class to create my publisher :

include "ros/ros.h"
include "std_msgs/String.h"
include "sstream"
include "Position.cpp"

/**
 * This method generates position coordinates at random.
**/
int main(int argc, char **argv)
{
    ros::init(argc, argv, "talker");

    ros::NodeHandle n;
    ros::Publisher position_pub = n.advertise<RandomPositionGenerator:: Position>      ("position", 50);
    ros::Rate loop_rate(10);

    double pos_x=0;
    double pos_y=0;
    double pos_z=0;

    while (ros::ok())
    {
   RandomPositionGenerator:: Position pos;
   pos.setPosition();
   pos.getPosition(pos_x,pos_y,pos_z);

       ROS_INFO("The co-ordinates are : x=",pos_x);

       position_pub.publish(pos);
       ros::spinOnce();
       loop_rate.sleep();

    }


   return 0;
}

And now I get the following errors:

  1. ‘__s_getDataType’ is not a member of ‘RandomPositionGenerator::Position’
  2. ‘__s_getMD5Sum’ is not a member of ‘RandomPositionGenerator::Position’
  3. ‘const class RandomPositionGenerator::Position’ has no member named ‘__getDataType’

and some more similar errors...Please correct me...I am not sure where am I going wrong, or if there is anything that I did right in this small bit of code !

4

1 回答 1

4

对于要发布/订阅的数据,您应该使用实际的 ROS 消息而不是自定义类。请参阅http://wiki.ros.org/ROS/Tutorials/CreatingMsgAndSrv了解如何创建新消息,然后您可以在发布数据时使用该消息。使用 ROS 消息还意味着现有的 ROS 工具(例如Python 或Javarostopic等整个语言绑定)将能够轻松地与您的 C++ 节点进行互操作。rospyrosjava

对于这种特殊情况,您可以简单地将标准 ROS 消息用于 3D 点,geometry_msgs/Point您可以在common_msgs文档中找到相当全面的标准化消息列表。与使用基本相同的内容创建自己的消息相比,使用标准化消息有许多好处 - 最大的是 ROS 生态系统中有许多工具或其他软件,您可能希望以后利用,这将是如果您已经在使用标准化的消息类型,这是一个相当轻松的过程。

如果您确实需要使用自定义的 Position 类(而不是简单地将其转换为 ROS 消息),您可以查看roscpp 序列化文档。我强烈建议不要序列化您的自定义类,而不是使用您创建的实际 ROS 消息或标准化消息。

于 2013-11-08T17:28:23.263 回答