Advertisement
Guest User

custom_gazebo.cpp

a guest
Jun 4th, 2025
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #include "custom_sensor_plugin/custom_sensor.hpp"
  2. #include <pluginlib/class_list_macros.hpp>
  3. #include <gz/plugin/Register.hh>
  4.  
  5. namespace custom_sensor
  6. {
  7.  
  8. CallbackReturn CustomSensor::on_init(const hardware_interface::HardwareInfo & info)
  9. {
  10. if (info.sensors.empty()) {
  11. RCLCPP_ERROR(rclcpp::get_logger("CustomSensor"), "No Sensor Configuration Found");
  12. return CallbackReturn::ERROR;
  13. }
  14. if (info.name != "custom_sensor") {
  15. RCLCPP_ERROR(rclcpp::get_logger("CustomSensor"),
  16. "Expected 'custom_sensor' but got '%s'", info.name.c_str());
  17. return CallbackReturn::ERROR;
  18. }
  19. RCLCPP_INFO(rclcpp::get_logger("CustomSensor"),
  20. "on_init received name = '%s'", info.name.c_str());
  21. info_ = info;
  22. data_values_.resize(info_.sensors[0].state_interfaces.size(), 0.0);
  23. return CallbackReturn::SUCCESS;
  24. }
  25.  
  26. CallbackReturn CustomSensor::on_configure(const rclcpp_lifecycle::State & /*previous_state*/)
  27. {
  28. RCLCPP_INFO(rclcpp::get_logger("CustomSensor"), "System successfully configured!");
  29. return CallbackReturn::SUCCESS;
  30. }
  31.  
  32. std::vector<hardware_interface::StateInterface> CustomSensor::export_state_interfaces()
  33. {
  34. std::vector<hardware_interface::StateInterface> state_interfaces;
  35. const auto & sensor_info = info_.sensors[0];
  36. for (size_t i = 0; i < sensor_info.state_interfaces.size(); ++i) {
  37. state_interfaces.emplace_back(
  38. sensor_info.name,
  39. sensor_info.state_interfaces[i].name,
  40. &data_values_[i]);
  41. }
  42. return state_interfaces;
  43. }
  44.  
  45. hardware_interface::return_type CustomSensor::read(
  46. const rclcpp::Time & /*time*/, const rclcpp::Duration & /*period*/)
  47. {
  48. // For testing, assign a fixed value
  49. for (auto & val : data_values_) {
  50. val = 5.0;
  51. }
  52. return hardware_interface::return_type::OK;
  53. }
  54.  
  55. bool CustomSensor::initSim(
  56. rclcpp::Node::SharedPtr & model_nh,
  57. std::map<std::string, gz::sim::Entity> & entities,
  58. const hardware_interface::HardwareInfo & hardware_info,
  59. gz::sim::EntityComponentManager & ecm,
  60. int & update_rate)
  61. {
  62.  
  63. }
  64.  
  65. update_rate = 0;
  66. return true;
  67. }
  68.  
  69. } // namespace custom_sensor
  70.  
  71. PLUGINLIB_EXPORT_CLASS(
  72. custom_sensor::CustomSensor,
  73. gz_ros2_control::GazeboSimSystemInterface)
  74.  
  75. IGNITION_ADD_PLUGIN(
  76. custom_sensor::CustomSensor,
  77. gz::sim::System,
  78. gz_ros2_control::GazeboSimSystemInterface
  79. )
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement