config_handler.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "saturn_controller/config_handler.hpp"
  2. #include <spdlog/spdlog.h>
  3. #include <spdlog/async.h>
  4. #include <spdlog/sinks/rotating_file_sink.h>
  5. #include <spdlog/sinks/stdout_color_sinks.h>
  6. #include <spdlog/spdlog.h>
  7. namespace saturn_ros2 {
  8. ConfigHandler::ConfigHandler(std::shared_ptr<rclcpp::Node> node)
  9. : node_(node) {
  10. loadConfig();
  11. }
  12. template <typename T>
  13. T declareOrGetParam(rclcpp::Node::SharedPtr node_, const std::string &param_name, T default_value)
  14. {
  15. T output_value;
  16. try
  17. {
  18. if (node_->has_parameter(param_name))
  19. node_->get_parameter<T>(param_name, output_value);
  20. else
  21. output_value = node_->declare_parameter<T>(param_name, default_value);
  22. }
  23. catch (const rclcpp::exceptions::InvalidParameterTypeException &e)
  24. {
  25. RCLCPP_WARN_STREAM(node_->get_logger(), "InvalidParameterTypeException(" << param_name << "): " << e.what());
  26. RCLCPP_ERROR_STREAM(node_->get_logger(), "Error getting parameter \'" << param_name << "\', check parameter type in YAML file");
  27. throw e;
  28. }
  29. RCLCPP_INFO_STREAM(node_->get_logger(), "Found parameter - " << param_name << ": " << output_value);
  30. return output_value;
  31. }
  32. void ConfigHandler::loadConfig() {
  33. // 声明参数
  34. motion_computer_ip_address_ = declareOrGetParam<std::string>(
  35. node_, "motion_computer_ip_address", "192.168.144.103");
  36. main_timer_frequency_ =
  37. declareOrGetParam<float>(node_, "main_timer_frequency", 50.0);
  38. imu_timer_frequency_ =
  39. declareOrGetParam<float>(node_, "imu_timer_frequency", 200.0);
  40. legged_odom_timer_frequency_ =
  41. declareOrGetParam<float>(node_, "legged_odom_timer_frequency", 100.0);
  42. joints_state_timer_frequency_ =
  43. declareOrGetParam<float>(node_, "joints_state_timer_frequency", 5.0);
  44. }
  45. } // namespace saturn_ros2