|
| 1 | +#include "drake_ros/core/camera_info_system.h" |
| 2 | + |
| 3 | +#include <rclcpp/time.hpp> |
| 4 | + |
| 5 | +using drake_ros::core::CameraInfoSystem; |
| 6 | +using drake_ros::core::RosPublisherSystem; |
| 7 | + |
| 8 | +CameraInfoSystem::CameraInfoSystem() : camera_info(1, 1, 1, 1, 0.5, 0.5) { |
| 9 | + DeclareAbstractOutputPort("CameraInfoSystem", |
| 10 | + &CameraInfoSystem::CalcCameraInfo); |
| 11 | +} |
| 12 | + |
| 13 | +CameraInfoSystem::~CameraInfoSystem() {} |
| 14 | + |
| 15 | +void CameraInfoSystem::CalcCameraInfo( |
| 16 | + const drake::systems::Context<double>& context, |
| 17 | + sensor_msgs::msg::CameraInfo* output_value) const { |
| 18 | + rclcpp::Time now{0, 0, RCL_ROS_TIME}; |
| 19 | + now += rclcpp::Duration::from_seconds(context.get_time()); |
| 20 | + output_value->header.stamp = now; |
| 21 | + output_value->header.frame_id = "CartPole/camera_optical"; |
| 22 | + |
| 23 | + output_value->height = this->camera_info.height(); |
| 24 | + output_value->width = this->camera_info.width(); |
| 25 | + output_value->distortion_model = "plumb_bob"; |
| 26 | + |
| 27 | + // distortion isn't supported. Setting this values to zero |
| 28 | + output_value->d.push_back(0); |
| 29 | + output_value->d.push_back(0); |
| 30 | + output_value->d.push_back(0); |
| 31 | + output_value->d.push_back(0); |
| 32 | + output_value->d.push_back(0); |
| 33 | + |
| 34 | + output_value->r[0] = 1; |
| 35 | + output_value->r[1] = 0; |
| 36 | + output_value->r[2] = 0; |
| 37 | + output_value->r[3] = 0; |
| 38 | + output_value->r[4] = 1; |
| 39 | + output_value->r[5] = 0; |
| 40 | + output_value->r[6] = 0; |
| 41 | + output_value->r[7] = 0; |
| 42 | + output_value->r[8] = 1; |
| 43 | + |
| 44 | + // │ f_x 0 c_x │ |
| 45 | + // K = │ 0 f_y c_y │ |
| 46 | + // │ 0 0 1 │ |
| 47 | + |
| 48 | + // │ f_x 0 c_x Tx│ |
| 49 | + // P = │ 0 f_y c_y Ty│ |
| 50 | + // │ 0 0 1 0│ |
| 51 | + output_value->k[0] = this->camera_info.focal_x(); |
| 52 | + output_value->k[2] = this->camera_info.center_x(); |
| 53 | + output_value->k[4] = this->camera_info.focal_y(); |
| 54 | + output_value->k[5] = this->camera_info.center_y(); |
| 55 | + output_value->k[8] = 1.0; |
| 56 | + |
| 57 | + output_value->p[0] = this->camera_info.focal_x(); |
| 58 | + output_value->p[2] = this->camera_info.center_x(); |
| 59 | + output_value->p[3] = 0; |
| 60 | + output_value->p[5] = this->camera_info.focal_y(); |
| 61 | + output_value->p[6] = this->camera_info.center_y(); |
| 62 | + output_value->p[10] = 1.0; |
| 63 | +} |
| 64 | + |
| 65 | +void CameraInfoSystem::SetCameraInfo(const CameraInfo& _camera_info) { |
| 66 | + this->camera_info = _camera_info; |
| 67 | +} |
| 68 | + |
| 69 | +std::tuple<CameraInfoSystem*, RosPublisherSystem*> |
| 70 | +CameraInfoSystem::AddToBuilder( |
| 71 | + drake::systems::DiagramBuilder<double>* builder, DrakeRos* ros, |
| 72 | + const std::string& topic_name, const rclcpp::QoS& qos, |
| 73 | + const std::unordered_set<drake::systems::TriggerType>& publish_triggers, |
| 74 | + double publish_period) { |
| 75 | + auto* camera_info_system = builder->AddSystem<CameraInfoSystem>(); |
| 76 | + |
| 77 | + auto* pub_system = |
| 78 | + builder->AddSystem(RosPublisherSystem::Make<sensor_msgs::msg::CameraInfo>( |
| 79 | + topic_name, qos, ros, publish_triggers, publish_period)); |
| 80 | + |
| 81 | + builder->Connect(camera_info_system->get_output_port(), |
| 82 | + pub_system->get_input_port()); |
| 83 | + |
| 84 | + return {camera_info_system, pub_system}; |
| 85 | +} |
0 commit comments