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

Revision history [back]

click to hide/show revision 1
initial version

Unfortunately there isn't a concise way to do that. The message classes aren't implemented with these constructors for various reasons -- some context here: https://github.com/ros/ros_comm/issues/148

I've ended up writing some utility functions that let me do things like this, but I don't know if I recommend that since it's just extra tech debt in (probably) your test code:

#include "geometry_msgs/msg/point.hpp"
#include "geometry_msgs/msg/pose.hpp"
#include "geometry_msgs/msg/quaternion.hpp"

namespace geometry_msgs::msg {
auto make_point(double x, double y, double z) {
  geometry_msgs::msg::Point p;
  p.x = x; p.y = y; p.z = z;
  return p;
}

auto make_quaternion(double x = 0.0, double y = 0.0, double z = 0.0, double w = 1.0) {
  geometry_msgs::msg::Quaternion q;
  q.x = x; q.y = y; q.z = z; q.w = w;
  return q;
}

auto make_pose(Point p, Quaternion q) {
  geometry_msgs::msg::Pose pose;
  pose.position = p;
  pose.orientation = q;
  return pose;
}
}

{
  using namespace geometry_msgs::msg;
  Pose pose = make_pose(make_point(1, 2, 3), make_quaternion(1, 0, 0, 0));
}

Honestly, someone should just make a ROS package with interfaces like this, at least for the common message types...

Unfortunately there isn't a concise way to do that. The message classes aren't implemented with these constructors for various reasons -- some context (from ROS 1) here: https://github.com/ros/ros_comm/issues/148

I've ended up writing some utility functions that let me do things like this, but I don't know if I recommend that since it's just extra tech debt in (probably) your test code:

#include "geometry_msgs/msg/point.hpp"
#include "geometry_msgs/msg/pose.hpp"
#include "geometry_msgs/msg/quaternion.hpp"

namespace geometry_msgs::msg {
auto make_point(double x, double y, double z) {
  geometry_msgs::msg::Point p;
  p.x = x; p.y = y; p.z = z;
  return p;
}

auto make_quaternion(double x = 0.0, double y = 0.0, double z = 0.0, double w = 1.0) {
  geometry_msgs::msg::Quaternion q;
  q.x = x; q.y = y; q.z = z; q.w = w;
  return q;
}

auto make_pose(Point p, Quaternion q) {
  geometry_msgs::msg::Pose pose;
  pose.position = p;
  pose.orientation = q;
  return pose;
}
}

{
  using namespace geometry_msgs::msg;
  Pose pose = make_pose(make_point(1, 2, 3), make_quaternion(1, 0, 0, 0));
}

Honestly, someone should just make a ROS package with interfaces like this, at least for the common message types...

Unfortunately there isn't a concise way to do that. The message classes aren't implemented with these constructors for various reasons -- some context (from ROS 1) here: https://github.com/ros/ros_comm/issues/148

I've ended up writing some utility functions that let me do things like this, but I don't know if I recommend that since it's just extra tech debt in (probably) your test code:

#include "geometry_msgs/msg/point.hpp"
#include "geometry_msgs/msg/pose.hpp"
#include "geometry_msgs/msg/quaternion.hpp"

namespace geometry_msgs::msg {
auto make_point(double x, double y, double z) {
  geometry_msgs::msg::Point Point p;
  p.x = x; p.y = y; p.z = z;
  return p;
}

auto make_quaternion(double x = 0.0, double y = 0.0, double z = 0.0, double w = 1.0) {
  geometry_msgs::msg::Quaternion Quaternion q;
  q.x = x; q.y = y; q.z = z; q.w = w;
  return q;
}

auto make_pose(Point p, Quaternion q) {
  geometry_msgs::msg::Pose Pose pose;
  pose.position = p;
  pose.orientation = q;
  return pose;
}
}

{
  using namespace geometry_msgs::msg;
  Pose pose = make_pose(make_point(1, 2, 3), make_quaternion(1, 0, 0, 0));
}

Honestly, someone should just make a ROS package with interfaces like this, at least for the common message types...