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

GetElement from sdf of type std::vector<double>

asked 2022-03-04 23:58:30 -0600

KhalidOwlWalid gravatar image

I want to declare a vector of double in my gazebo.xacro file and then get the element from that file to my script.

The way I do it for, say int type is (eg cameraWidth)

 <gazebo>
    <plugin name="simulate_bounding_boxes" filename="libgazebo_simulate_bounding_boxes.so">
      <targetFrame>zed_right_camera_optical_frame</targetFrame>
      <sourceFrame>base_footprint</sourceFrame>
      <cameraInfo>zed/right/camera_info</cameraInfo>
      <cameraWidth>1280</cameraWidth>
      <cameraHeight>720</cameraHeight>
      <simulateBoundingBoxesTopicName>simulate_bounding_boxes</simulateBoundingBoxesTopicName>
      <customCameraInfo>custom_camera_info</customCameraInfo>
      <D>0.0 0.0 0.0 0.0 0.0</D>
    </plugin>
  </gazebo>

In my plugin script;

this->width = getIntParameter(_sdf, "width", 1280, "Width of camera");
...
...
int GazeboSimulateBoundingBoxes::getIntParameter(sdf::ElementPtr _sdf, const char *element,
                                                    int default_value,
                                                    const char *default_description) {
if (!_sdf->HasElement(element)) {
  RCLCPP_DEBUG(this->rosnode_->get_logger(),
                "state_ground_truth plugin missing <%s>, defaults to %s", element,
                default_description);
  return default_value;
} else {
  return _sdf->GetElement(element)->Get<int>();
}
}

But how do I do this for a std::vector<double> type? I tried to do the following. I include my vector of doubles in the plugin parameter

 <vector>0.0 0.0 0.0 0.0 0.0</vector>

Then, I create a function for it where;

std::vector<double> GazeboSimulateBoundingBoxes::getVectorParameter(sdf::ElementPtr _sdf, const char *element,
                                                    std::vector<double> default_value,
                                                    const char *default_description) {
if (!_sdf->HasElement(element)) {
  RCLCPP_DEBUG(this->rosnode_->get_logger(),
                "state_ground_truth plugin missing <%s>, defaults to %s", element,
                default_description);
  return default_value;
} else {
  return _sdf->GetElement(element)->Get<std::vector<double> >();
}
}

but I am getting this error;

/usr/include/sdformat-9.7/sdf/Param.hh: In instantiation of ‘bool sdf::v9::Param::Get(T&) const [with T = std::vector<double, std::allocator<double> >]’:
/usr/include/sdformat-9.7/sdf/Element.hh:620:7:   required from ‘std::pair<T, bool> sdf::v9::Element::Get(const string&, const T&) const [with T = std::vector<double, std::allocator<double> >; std::string = std::__cxx11::basic_string<char>]’
/usr/include/sdformat-9.7/sdf/Element.hh:595:24:   required from ‘T sdf::v9::Element::Get(const string&) const [with T = std::vector<double, std::allocator<double> >; std::string = std::__cxx11::basic_string<char>]’
/home/shenowlshoot/eufs-master/src/eufs_sim/eufs_plugins/gazebo_simulate_bounding_boxes/src/gazebo_simulate_bounding_boxes.cpp:295:63:   required from here
/usr/include/sdformat-9.7/sdf/Param.hh:388:13: error: no match for ‘operator>>’ (operand types are ‘std::stringstream’ {aka ‘std::__cxx11::basic_stringstream<char>’} and ‘std::vector<double, std::allocator<double> >’)
  388 |         tmp >> _value;
      |         ~~~~^~~~~~~~~

The error is even longer than this, but I believe this is the most important one.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2023-03-20 12:43:16 -0600

Dharmin B. gravatar image

Unfortunately only the following types are supported

bool,
char,
std::string,
int,
std::uint64_t,
unsigned int,
double,
float,
sdf::Time,
ignition::math::Angle, 
ignition::math::Color,  
ignition::math::Vector2i,
ignition::math::Vector2d,
ignition::math::Vector3d,
ignition::math::Quaterniond,
ignition::math::Pose3d

If you check the file from the error message "Param.hh", it uses a function "ValueFromStringImpl" which supports only these types. :(

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-03-04 23:58:30 -0600

Seen: 364 times

Last updated: Mar 20 '23