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:
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 !
You should use an actual ROS message instead of a custom class for data that you'd like to publish/subscribe to. See http://wiki.ros.org/ROS/Tutorials/CreatingMsgAndSrv for how to create a new message that you can then use when publishing data. Using a ROS message also means that existing ROS tools such as rostopic
and entire language bindings such as rospy
for Python or rosjava
for Java will be able to easily interoperate with your C++ node.
For this particular case, you can simply use the standard ROS message for a 3D point, geometry_msgs/Point. You can find a fairly comprehensive list of the standardized messages in the common_msgs documentation. Using a standardized message has a number of benefits over creating your own message with essentially the same content - the biggest is that there are many tools or other pieces of software in the ROS ecosystem that you may want to take advantage of later, which will be a fairly painless process if you are already using standarized message types.
If you really have a need to use your custom Position class (without simply converting it to a ROS message), you can look at the roscpp serialization documentation. I would strongly suggest against serializing your custom class instead of using either an actual ROS message that you create or a standardized message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With