Skip to content

Commit d122cd2

Browse files
committed
Add option to change material color from ROS.
Forward port of gazebosim#486. * Message and bridge for MaterialColor. This allows bridging MaterialColor from ROS to GZ and is important for allowing simulation users to create status lights. Signed-off-by: Benjamin Perseghetti <[email protected]>
1 parent 78a2d1c commit d122cd2

File tree

9 files changed

+161
-0
lines changed

9 files changed

+161
-0
lines changed

ros_gz_bridge/include/ros_gz_bridge/convert/ros_gz_interfaces.hpp

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <gz/msgs/float_v.pb.h>
2626
#include <gz/msgs/gui_camera.pb.h>
2727
#include <gz/msgs/light.pb.h>
28+
#include <gz/msgs/material_color.pb.h>
2829
#include <gz/msgs/param.pb.h>
2930
#include <gz/msgs/param_v.pb.h>
3031
#include <gz/msgs/sensor_noise.pb.h>
@@ -43,6 +44,7 @@
4344
#include <ros_gz_interfaces/msg/float32_array.hpp>
4445
#include <ros_gz_interfaces/msg/gui_camera.hpp>
4546
#include <ros_gz_interfaces/msg/light.hpp>
47+
#include <ros_gz_interfaces/msg/material_color.hpp>
4648
#include <ros_gz_interfaces/msg/param_vec.hpp>
4749
#include <ros_gz_interfaces/msg/sensor_noise.hpp>
4850
#include <ros_gz_interfaces/msg/string_vec.hpp>
@@ -153,6 +155,18 @@ convert_gz_to_ros(
153155
const gz::msgs::Light & gz_msg,
154156
ros_gz_interfaces::msg::Light & ros_msg);
155157

158+
template<>
159+
void
160+
convert_ros_to_gz(
161+
const ros_gz_interfaces::msg::MaterialColor & ros_msg,
162+
gz::msgs::MaterialColor & gz_msg);
163+
164+
template<>
165+
void
166+
convert_gz_to_ros(
167+
const gz::msgs::MaterialColor & gz_msg,
168+
ros_gz_interfaces::msg::MaterialColor & ros_msg);
169+
156170
template<>
157171
void
158172
convert_ros_to_gz(

ros_gz_bridge/ros_gz_bridge/mappings.py

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
Mapping('GuiCamera', 'GUICamera'),
6969
Mapping('JointWrench', 'JointWrench'),
7070
Mapping('Light', 'Light'),
71+
Mapping('MaterialColor', 'MaterialColor'),
7172
Mapping('ParamVec', 'Param'),
7273
Mapping('ParamVec', 'Param_V'),
7374
Mapping('SensorNoise', 'SensorNoise'),

ros_gz_bridge/src/convert/ros_gz_interfaces.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,64 @@ convert_gz_to_ros(
378378
ros_msg.intensity = gz_msg.intensity();
379379
}
380380

381+
template<>
382+
void
383+
convert_ros_to_gz(
384+
const ros_gz_interfaces::msg::MaterialColor & ros_msg,
385+
gz::msgs::MaterialColor & gz_msg)
386+
{
387+
using EntityMatch = gz::msgs::MaterialColor::EntityMatch;
388+
389+
switch (ros_msg.entity_match) {
390+
case ros_gz_interfaces::msg::MaterialColor::FIRST:
391+
gz_msg.set_entity_match(EntityMatch::MaterialColor_EntityMatch_FIRST);
392+
break;
393+
case ros_gz_interfaces::msg::MaterialColor::ALL:
394+
gz_msg.set_entity_match(EntityMatch::MaterialColor_EntityMatch_ALL);
395+
break;
396+
default:
397+
std::cerr << "Unsupported entity match type ["
398+
<< ros_msg.entity_match << "]\n";
399+
}
400+
401+
convert_ros_to_gz(ros_msg.header, (*gz_msg.mutable_header()));
402+
convert_ros_to_gz(ros_msg.entity, *gz_msg.mutable_entity());
403+
convert_ros_to_gz(ros_msg.ambient, *gz_msg.mutable_ambient());
404+
convert_ros_to_gz(ros_msg.diffuse, *gz_msg.mutable_diffuse());
405+
convert_ros_to_gz(ros_msg.specular, *gz_msg.mutable_specular());
406+
convert_ros_to_gz(ros_msg.emissive, *gz_msg.mutable_emissive());
407+
408+
gz_msg.set_shininess(ros_msg.shininess);
409+
}
410+
411+
template<>
412+
void
413+
convert_gz_to_ros(
414+
const gz::msgs::MaterialColor & gz_msg,
415+
ros_gz_interfaces::msg::MaterialColor & ros_msg)
416+
{
417+
using EntityMatch = gz::msgs::MaterialColor::EntityMatch;
418+
if (gz_msg.entity_match() == EntityMatch::MaterialColor_EntityMatch_FIRST) {
419+
ros_msg.entity_match = ros_gz_interfaces::msg::MaterialColor::FIRST;
420+
/* *INDENT-OFF* */
421+
} else if (gz_msg.entity_match() ==
422+
EntityMatch::MaterialColor_EntityMatch_ALL) {
423+
/* *INDENT-ON* */
424+
ros_msg.entity_match = ros_gz_interfaces::msg::MaterialColor::ALL;
425+
} else {
426+
std::cerr << "Unsupported EntityMatch [" <<
427+
gz_msg.entity_match() << "]" << std::endl;
428+
}
429+
convert_gz_to_ros(gz_msg.header(), ros_msg.header);
430+
convert_gz_to_ros(gz_msg.entity(), ros_msg.entity);
431+
convert_gz_to_ros(gz_msg.ambient(), ros_msg.ambient);
432+
convert_gz_to_ros(gz_msg.diffuse(), ros_msg.diffuse);
433+
convert_gz_to_ros(gz_msg.specular(), ros_msg.specular);
434+
convert_gz_to_ros(gz_msg.emissive(), ros_msg.emissive);
435+
436+
ros_msg.shininess = gz_msg.shininess();
437+
}
438+
381439
template<>
382440
void
383441
convert_ros_to_gz(

ros_gz_bridge/test/utils/gz_test_msg.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,35 @@ void compareTestMsg(const std::shared_ptr<gz::msgs::Light> & _msg)
13431343
EXPECT_FLOAT_EQ(expected_msg.intensity(), _msg->intensity());
13441344
}
13451345

1346+
void createTestMsg(gz::msgs::MaterialColor & _msg)
1347+
{
1348+
createTestMsg(*_msg.mutable_header());
1349+
createTestMsg(*_msg.mutable_entity());
1350+
createTestMsg(*_msg.mutable_ambient());
1351+
createTestMsg(*_msg.mutable_diffuse());
1352+
createTestMsg(*_msg.mutable_specular());
1353+
createTestMsg(*_msg.mutable_emissive());
1354+
1355+
_msg.set_shininess(1.0);
1356+
_msg.set_entity_match(gz::msgs::MaterialColor::EntityMatch::MaterialColor_EntityMatch_ALL);
1357+
}
1358+
1359+
void compareTestMsg(const std::shared_ptr<gz::msgs::MaterialColor> & _msg)
1360+
{
1361+
gz::msgs::MaterialColor expected_msg;
1362+
createTestMsg(expected_msg);
1363+
1364+
compareTestMsg(std::make_shared<gz::msgs::Header>(_msg->header()));
1365+
compareTestMsg(std::make_shared<gz::msgs::Entity>(_msg->entity()));
1366+
compareTestMsg(std::make_shared<gz::msgs::Color>(_msg->ambient()));
1367+
compareTestMsg(std::make_shared<gz::msgs::Color>(_msg->diffuse()));
1368+
compareTestMsg(std::make_shared<gz::msgs::Color>(_msg->specular()));
1369+
compareTestMsg(std::make_shared<gz::msgs::Color>(_msg->emissive()));
1370+
1371+
EXPECT_EQ(expected_msg.shininess(), _msg->shininess());
1372+
EXPECT_EQ(expected_msg.entity_match(), _msg->entity_match());
1373+
}
1374+
13461375
void createTestMsg(gz::msgs::GUICamera & _msg)
13471376
{
13481377
gz::msgs::Header header_msg;

ros_gz_bridge/test/utils/gz_test_msg.hpp

+9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <gz/msgs/laserscan.pb.h>
4444
#include <gz/msgs/light.pb.h>
4545
#include <gz/msgs/magnetometer.pb.h>
46+
#include <gz/msgs/material_color.pb.h>
4647
#include <gz/msgs/model.pb.h>
4748
#include <gz/msgs/navsat.pb.h>
4849
#include <gz/msgs/odometry.pb.h>
@@ -450,6 +451,14 @@ void createTestMsg(gz::msgs::Light & _msg);
450451
/// \param[in] _msg The message to compare.
451452
void compareTestMsg(const std::shared_ptr<gz::msgs::Light> & _msg);
452453

454+
/// \brief Create a message used for testing.
455+
/// \param[out] _msg The message populated.
456+
void createTestMsg(gz::msgs::MaterialColor & _msg);
457+
458+
/// \brief Compare a message with the populated for testing.
459+
/// \param[in] _msg The message to compare.
460+
void compareTestMsg(const std::shared_ptr<gz::msgs::MaterialColor> & _msg);
461+
453462
/// \brief Create a message used for testing.
454463
/// \param[out] _msg The message populated.
455464
void createTestMsg(gz::msgs::GUICamera & _msg);

ros_gz_bridge/test/utils/ros_test_msg.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,34 @@ void compareTestMsg(const std::shared_ptr<ros_gz_interfaces::msg::Light> & _msg)
621621
EXPECT_FLOAT_EQ(expected_msg.intensity, _msg->intensity);
622622
}
623623

624+
void createTestMsg(ros_gz_interfaces::msg::MaterialColor & _msg)
625+
{
626+
createTestMsg(_msg.header);
627+
createTestMsg(_msg.entity);
628+
createTestMsg(_msg.ambient);
629+
createTestMsg(_msg.diffuse);
630+
createTestMsg(_msg.specular);
631+
createTestMsg(_msg.emissive);
632+
_msg.shininess = 1.0;
633+
_msg.entity_match = ros_gz_interfaces::msg::MaterialColor::ALL;
634+
}
635+
636+
void compareTestMsg(const std::shared_ptr<ros_gz_interfaces::msg::MaterialColor> & _msg)
637+
{
638+
ros_gz_interfaces::msg::MaterialColor expected_msg;
639+
createTestMsg(expected_msg);
640+
641+
compareTestMsg(_msg->header);
642+
compareTestMsg(std::make_shared<ros_gz_interfaces::msg::Entity>(_msg->entity));
643+
compareTestMsg(std::make_shared<std_msgs::msg::ColorRGBA>(_msg->ambient));
644+
compareTestMsg(std::make_shared<std_msgs::msg::ColorRGBA>(_msg->diffuse));
645+
compareTestMsg(std::make_shared<std_msgs::msg::ColorRGBA>(_msg->specular));
646+
compareTestMsg(std::make_shared<std_msgs::msg::ColorRGBA>(_msg->emissive));
647+
648+
EXPECT_EQ(expected_msg.shininess, _msg->shininess);
649+
EXPECT_EQ(expected_msg.entity_match, _msg->entity_match);
650+
}
651+
624652
void createTestMsg(ros_gz_interfaces::msg::GuiCamera & _msg)
625653
{
626654
createTestMsg(_msg.header);

ros_gz_bridge/test/utils/ros_test_msg.hpp

+9
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <ros_gz_interfaces/msg/float32_array.hpp>
5858
#include <ros_gz_interfaces/msg/dataframe.hpp>
5959
#include <ros_gz_interfaces/msg/light.hpp>
60+
#include <ros_gz_interfaces/msg/material_color.hpp>
6061
#include <ros_gz_interfaces/msg/param_vec.hpp>
6162
#include <ros_gz_interfaces/msg/sensor_noise.hpp>
6263
#include <ros_gz_interfaces/msg/string_vec.hpp>
@@ -407,6 +408,14 @@ void createTestMsg(ros_gz_interfaces::msg::Light & _msg);
407408
/// \param[in] _msg The message to compare.
408409
void compareTestMsg(const std::shared_ptr<ros_gz_interfaces::msg::Light> & _msg);
409410

411+
/// \brief Create a message used for testing.
412+
/// \param[out] _msg The message populated.
413+
void createTestMsg(ros_gz_interfaces::msg::MaterialColor & _msg);
414+
415+
/// \brief Compare a message with the populated for testing.
416+
/// \param[in] _msg The message to compare.
417+
void compareTestMsg(const std::shared_ptr<ros_gz_interfaces::msg::MaterialColor> & _msg);
418+
410419
/// \brief Create a message used for testing.
411420
/// \param[out] _msg The message populated.
412421
void createTestMsg(ros_gz_interfaces::msg::Entity & _msg);

ros_gz_interfaces/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ set(msg_files
2727
"msg/GuiCamera.msg"
2828
"msg/JointWrench.msg"
2929
"msg/Light.msg"
30+
"msg/MaterialColor.msg"
3031
"msg/ParamVec.msg"
3132
"msg/SensorNoise.msg"
3233
"msg/StringVec.msg"
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Entities that match to apply material color: constant definition
2+
uint8 FIRST = 0
3+
uint8 ALL = 1
4+
5+
std_msgs/Header header # Optional header data
6+
ros_gz_interfaces/Entity entity # Entity to change material color
7+
std_msgs/ColorRGBA ambient # Ambient color
8+
std_msgs/ColorRGBA diffuse # Diffuse color
9+
std_msgs/ColorRGBA specular # Specular color
10+
std_msgs/ColorRGBA emissive # Emissive color
11+
float64 shininess # Specular exponent
12+
uint8 entity_match # Entities that match to apply material color

0 commit comments

Comments
 (0)