Skip to content

Commit 420e215

Browse files
committed
robot marker publisher
1 parent b60b44b commit 420e215

File tree

4 files changed

+388
-0
lines changed

4 files changed

+388
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#ifndef CARTESIO_ROBOT_VIZ_H
2+
#define CARTESIO_ROBOT_VIZ_H
3+
4+
#include <rclcpp/rclcpp.hpp>
5+
#include <visualization_msgs/msg/marker_array.hpp>
6+
7+
#include <xbot2_interface/xbotinterface2.h>
8+
9+
10+
namespace XBot::Cartesian::Utils {
11+
12+
/**
13+
* @brief The RobotMarkerPublisher class is used to publish a marker array with all the meshes (collision & visual) of the robot
14+
* in a uniform color.
15+
*/
16+
class RobotMarkerPublisher
17+
{
18+
19+
public:
20+
21+
22+
typedef Eigen::Vector4d color; // rgba
23+
24+
/**
25+
* @brief RobotMarkerPublisher
26+
* @param model model of the robot to publish as marker
27+
* @param topic_name topic of the published marker
28+
* @param nh to retrieve the namespace of the topic
29+
* @param rgba color of the robot published
30+
*/
31+
RobotMarkerPublisher(ModelInterface::ConstPtr model,
32+
std::string topic_name,
33+
rclcpp::Node::SharedPtr node = nullptr,
34+
std::optional<color> rgba = std::nullopt);
35+
36+
RobotMarkerPublisher(ModelInterface::ConstPtr model,
37+
std::string topic_name,
38+
std::optional<color> rgba = std::nullopt);
39+
40+
/**
41+
* @brief setRGBA
42+
* @param rgba [Red, Green, Blue, Alpha]
43+
*/
44+
void setRGBA(const color& rgba);
45+
46+
/**
47+
* @brief publishMarkers of the robot with a time
48+
* @param time
49+
* @param color_override_map if a link is in the map it will be published in the specified color
50+
*/
51+
void publishMarkers(const rclcpp::Time& time,
52+
std::string base_link = "world",
53+
std::string frame_id = "world",
54+
std::map<std::string, color> color_override_map = std::map<std::string, color>());
55+
56+
/**
57+
* @brief getNode
58+
* @return
59+
*/
60+
rclcpp::Node& getNode();
61+
62+
private:
63+
64+
/**
65+
* @brief _reserved_color for the self collision
66+
*/
67+
const color _reserved_color;
68+
69+
XBot::ModelInterface::ConstPtr _model;
70+
rclcpp::Node::SharedPtr _node;
71+
rclcpp::Publisher<visualization_msgs::msg::MarkerArray>::SharedPtr _collision_robot_pub;
72+
color _rgba;
73+
74+
static Eigen::Affine3d toAffine3d(const urdf::Pose& p);
75+
76+
};
77+
78+
}
79+
80+
81+
#endif // ROBOT_VIZ_H

src/ros/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ set(CartesianInterfaceRos_SRC
9797
# JoyStick.cpp
9898
RosExecutor.cpp
9999
utils/RobotStatePublisher.cpp
100+
utils/RobotMarkerPublisher.cpp
100101
)
101102

102103
add_library(CartesianInterfaceRos STATIC
@@ -149,6 +150,19 @@ install(TARGETS marker_spawner
149150
DESTINATION lib/${PROJECT_NAME}
150151
)
151152

153+
# Collision marker publisher executable
154+
add_executable(collision_marker_publisher
155+
CollisionMarkerPublisherNode.cpp
156+
)
157+
158+
target_link_libraries(collision_marker_publisher
159+
CartesianInterfaceRos
160+
)
161+
162+
install(TARGETS collision_marker_publisher
163+
DESTINATION lib/${PROJECT_NAME}
164+
)
165+
152166
# Robot description publisher
153167
add_executable(robot_description_publisher
154168
RobotDescriptionPublisher.cpp
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include <cartesian_interface/ros/utils/RobotMarkerPublisher.h>
2+
#include <xbot2_interface/ros2/config_from_param.hpp>
3+
#include <sensor_msgs/msg/joint_state.hpp>
4+
#include <xbot2_interface/collision.h>
5+
6+
class CollisionMarkerPublisherNode : public rclcpp::Node
7+
{
8+
9+
public:
10+
11+
CollisionMarkerPublisherNode(const std::string& name) : Node(name)
12+
{
13+
}
14+
15+
void initialize()
16+
{
17+
auto cfg = XBot::ConfigOptionsFromParams(shared_from_this(), "", 5s);
18+
19+
_model = XBot::ModelInterface::getModel(cfg);
20+
21+
_marker_publisher = std::make_unique<XBot::Cartesian::Utils::RobotMarkerPublisher>(
22+
_model,
23+
"collision_markers",
24+
shared_from_this(),
25+
Eigen::Vector4d(0, 0, 0, 0)
26+
);
27+
28+
XBot::Collision::CollisionModel::Options opt;
29+
opt.assume_convex_meshes = declare_parameter("assume_convex_meshes", true);
30+
31+
_collision_model = std::make_shared<XBot::Collision::CollisionModel>(_model, opt);
32+
33+
_link_pairs = _collision_model->getCollisionPairs(true);
34+
35+
_collision_color << 1, 0, 0, .8;
36+
37+
_js_sub = create_subscription<sensor_msgs::msg::JointState>(
38+
"joint_states",
39+
rclcpp::QoS(1),
40+
std::bind(&CollisionMarkerPublisherNode::on_js_recv, this, std::placeholders::_1)
41+
);
42+
43+
44+
}
45+
46+
private:
47+
48+
49+
50+
void on_js_recv(sensor_msgs::msg::JointState::ConstSharedPtr js)
51+
{
52+
53+
if(js->name.size() != js->position.size())
54+
{
55+
RCLCPP_ERROR(this->get_logger(), "Received joint state with different name/position size");
56+
return;
57+
}
58+
59+
XBot::JointNameMap qmap;
60+
61+
for(size_t i=0; i<js->name.size(); ++i)
62+
{
63+
qmap[js->name[i]] = js->position[i];
64+
}
65+
66+
_model->setJointPosition(qmap);
67+
_model->update();
68+
_collision_model->update();
69+
70+
std::vector<int> pair_idx;
71+
72+
_collision_model->checkCollision(pair_idx, true);
73+
74+
std::map<std::string, Eigen::Vector4d> link_colors;
75+
for(auto idx : pair_idx)
76+
{
77+
link_colors[_link_pairs[idx].first] = _collision_color;
78+
link_colors[_link_pairs[idx].second] = _collision_color;
79+
80+
RCLCPP_WARN(this->get_logger(), "Collision detected between links %s and %s",
81+
_link_pairs[idx].first.c_str(), _link_pairs[idx].second.c_str());
82+
}
83+
84+
_marker_publisher->publishMarkers(get_clock()->now(), "world", "ci/world", link_colors);
85+
86+
87+
}
88+
89+
XBot::ModelInterface::Ptr _model;
90+
XBot::Collision::CollisionModel::Ptr _collision_model;
91+
XBot::Collision::CollisionModel::LinkPairVector _link_pairs;
92+
std::unique_ptr<XBot::Cartesian::Utils::RobotMarkerPublisher> _marker_publisher;
93+
Eigen::Vector4d _collision_color;
94+
rclcpp::Subscription<sensor_msgs::msg::JointState>::SharedPtr _js_sub;
95+
96+
97+
};
98+
99+
int main(int argc, char** argv)
100+
{
101+
rclcpp::init(argc, argv);
102+
103+
auto node = std::make_shared<CollisionMarkerPublisherNode>("collision_marker_publisher");
104+
105+
node->initialize();
106+
107+
RCLCPP_INFO(node->get_logger(), "Node initialized");
108+
109+
rclcpp::spin(node);
110+
rclcpp::shutdown();
111+
return 0;
112+
}

0 commit comments

Comments
 (0)