|
| 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