21 #include <boost/thread/thread.hpp> 22 #include <boost/bind.hpp> 26 #include <sensor_msgs/Image.h> 29 #include <geometry_msgs/Point32.h> 30 #include <sensor_msgs/ChannelFloat32.h> 35 #include <gazebo/physics/World.hh> 36 #include <gazebo/physics/HingeJoint.hh> 37 #include <gazebo/sensors/Sensor.hh> 38 #include <gazebo/common/Exception.hh> 39 #include <gazebo/sensors/CameraSensor.hh> 40 #include <gazebo/sensors/SensorTypes.hh> 41 #include <gazebo/rendering/Camera.hh> 42 #include <gazebo/rendering/Distortion.hh> 62 gazebo_plugins::GazeboRosCameraConfig &config, uint32_t level)
66 ROS_INFO_NAMED(
"camera_utils",
"Reconfigure request for the gazebo ros camera_: %s. New rate: %.2f",
88 const std::string &_camera_name_suffix,
89 double _hack_baseline)
95 this->
Load(_parent, _sdf, _camera_name_suffix);
105 sdf::ElementPtr _sdf,
106 const std::string &_camera_name_suffix)
109 std::string world_name = _parent->WorldName();
112 this->
world_ = physics::get_world(world_name);
121 std::stringstream ss;
125 if (this->
sdf->HasElement(
"imageTopicName"))
129 if (this->
sdf->HasElement(
"triggerTopicName"))
133 if (this->
sdf->HasElement(
"cameraInfoTopicName"))
135 this->
sdf->Get<std::string>(
"cameraInfoTopicName");
137 if (!this->
sdf->HasElement(
"cameraName"))
138 ROS_DEBUG_NAMED(
"camera_utils",
"Camera plugin missing <cameraName>, default to empty");
146 if (!this->
sdf->HasElement(
"frameName"))
147 ROS_DEBUG_NAMED(
"camera_utils",
"Camera plugin missing <frameName>, defaults to /world");
151 if (!this->
sdf->HasElement(
"updateRate"))
153 ROS_DEBUG_NAMED(
"camera_utils",
"Camera plugin missing <updateRate>, defaults to unlimited (0).");
159 if (!this->
sdf->HasElement(
"CxPrime"))
161 ROS_DEBUG_NAMED(
"camera_utils",
"Camera plugin missing <CxPrime>, defaults to 0");
167 if (!this->
sdf->HasElement(
"Cx"))
169 ROS_DEBUG_NAMED(
"camera_utils",
"Camera plugin missing <Cx>, defaults to 0");
173 this->
cx_ = this->
sdf->Get<
double>(
"Cx");
175 if (!this->
sdf->HasElement(
"Cy"))
177 ROS_DEBUG_NAMED(
"camera_utils",
"Camera plugin missing <Cy>, defaults to 0");
181 this->
cy_ = this->
sdf->Get<
double>(
"Cy");
183 if (!this->
sdf->HasElement(
"focalLength"))
185 ROS_DEBUG_NAMED(
"camera_utils",
"Camera plugin missing <focalLength>, defaults to 0");
191 if (!this->
sdf->HasElement(
"hackBaseline"))
193 ROS_DEBUG_NAMED(
"camera_utils",
"Camera plugin missing <hackBaseline>, defaults to 0");
199 if (!this->
sdf->HasElement(
"distortionK1"))
201 ROS_DEBUG_NAMED(
"camera_utils",
"Camera plugin missing <distortionK1>, defaults to 0");
207 if (!this->
sdf->HasElement(
"distortionK2"))
209 ROS_DEBUG_NAMED(
"camera_utils",
"Camera plugin missing <distortionK2>, defaults to 0");
215 if (!this->
sdf->HasElement(
"distortionK3"))
217 ROS_DEBUG_NAMED(
"camera_utils",
"Camera plugin missing <distortionK3>, defaults to 0");
223 if (!this->
sdf->HasElement(
"distortionT1"))
225 ROS_DEBUG_NAMED(
"camera_utils",
"Camera plugin missing <distortionT1>, defaults to 0");
231 if (!this->
sdf->HasElement(
"distortionT2"))
233 ROS_DEBUG_NAMED(
"camera_utils",
"Camera plugin missing <distortionT2>, defaults to 0");
239 if (!this->
sdf->HasElement(
"borderCrop"))
241 ROS_DEBUG_NAMED(
"camera_utils",
"Camera plugin missing <borderCrop>, defaults to true");
269 gzerr <<
"Not loading plugin since ROS hasn't been " 270 <<
"properly initialized. Try starting gazebo with ros plugin:\n" 271 <<
" gazebo -s libgazebo_ros_api_plugin.so\n";
291 boost::trim_right_if(this->
tf_prefix_,boost::is_any_of(
"/"));
295 ROS_INFO_NAMED(
"camera_utils",
"Camera Plugin (ns = %s) <tf_prefix_>, set to \"%s\"",
302 new dynamic_reconfigure::Server<gazebo_plugins::GazeboRosCameraConfig>
304 dynamic_reconfigure::Server<gazebo_plugins::GazeboRosCameraConfig>
311 ROS_WARN_NAMED(
"camera_utils",
"dynamic reconfigure is not enabled for this image topic [%s]" 312 " becuase <cameraName> is not specified",
330 ros::AdvertiseOptions::create<sensor_msgs::CameraInfo>(
356 ros::SubscribeOptions::create<std_msgs::Empty>(
376 const std_msgs::Empty::ConstPtr &dummy)
385 #if GAZEBO_MAJOR_VERSION >= 7 386 this->
camera_->SetHFOV(ignition::math::Angle(hfov->data));
388 this->
camera_->SetHFOV(gazebo::math::Angle(hfov->data));
395 const std_msgs::Float64::ConstPtr& update_rate)
452 else if (this->
format_ ==
"R8G8B8" || this->
format_ ==
"RGB_INT8")
457 else if (this->
format_ ==
"B8G8R8" || this->
format_ ==
"BGR_INT8")
462 else if (this->
format_ ==
"R16G16B16" || this->
format_ ==
"RGB_INT16")
467 else if (this->
format_ ==
"BAYER_RGGB8")
469 ROS_INFO_NAMED(
"camera_utils",
"bayer simulation maybe computationally expensive.");
473 else if (this->
format_ ==
"BAYER_BGGR8")
475 ROS_INFO_NAMED(
"camera_utils",
"bayer simulation maybe computationally expensive.");
479 else if (this->
format_ ==
"BAYER_GBRG8")
481 ROS_INFO_NAMED(
"camera_utils",
"bayer simulation maybe computationally expensive.");
485 else if (this->
format_ ==
"BAYER_GRBG8")
487 ROS_INFO_NAMED(
"camera_utils",
"bayer simulation maybe computationally expensive.");
502 this->
cx_ = (
static_cast<double>(this->
width_) + 1.0) /2.0;
504 this->
cy_ = (
static_cast<double>(this->
height_) + 1.0) /2.0;
507 double hfov = this->
camera_->HFOV().Radian();
508 double computed_focal_length =
509 (
static_cast<double>(this->
width_)) /
510 (2.0 * tan(hfov / 2.0));
519 if (!ignition::math::equal(this->
focal_length_, computed_focal_length))
521 ROS_WARN_NAMED(
"camera_utils",
"The <focal_length>[%f] you have provided for camera_ [%s]" 522 " is inconsistent with specified image_width [%d] and" 523 " HFOV [%f]. Please double check to see that" 524 " focal_length = width_ / (2.0 * tan(HFOV/2.0))," 525 " the explected focal_lengtth value is [%f]," 526 " please update your camera_ model description accordingly.",
529 computed_focal_length);
534 sensor_msgs::CameraInfo camera_info_msg;
536 camera_info_msg.header.frame_id = this->
frame_name_;
538 camera_info_msg.height = this->
height_;
539 camera_info_msg.width = this->
width_;
541 #if ROS_VERSION_MINIMUM(1, 3, 0) 542 camera_info_msg.distortion_model =
"plumb_bob";
543 camera_info_msg.D.resize(5);
548 if(this->
camera_->LensDistortion())
563 camera_info_msg.K[1] = 0.0;
564 camera_info_msg.K[2] = this->
cx_;
565 camera_info_msg.K[3] = 0.0;
567 camera_info_msg.K[5] = this->
cy_;
568 camera_info_msg.K[6] = 0.0;
569 camera_info_msg.K[7] = 0.0;
570 camera_info_msg.K[8] = 1.0;
572 camera_info_msg.R[0] = 1.0;
573 camera_info_msg.R[1] = 0.0;
574 camera_info_msg.R[2] = 0.0;
575 camera_info_msg.R[3] = 0.0;
576 camera_info_msg.R[4] = 1.0;
577 camera_info_msg.R[5] = 0.0;
578 camera_info_msg.R[6] = 0.0;
579 camera_info_msg.R[7] = 0.0;
580 camera_info_msg.R[8] = 1.0;
584 camera_info_msg.P[1] = 0.0;
585 camera_info_msg.P[2] = this->
cx_;
587 camera_info_msg.P[4] = 0.0;
589 camera_info_msg.P[6] = this->
cy_;
590 camera_info_msg.P[7] = 0.0;
591 camera_info_msg.P[8] = 0.0;
592 camera_info_msg.P[9] = 0.0;
593 camera_info_msg.P[10] = 1.0;
594 camera_info_msg.P[11] = 0.0;
609 common::Time &last_update_time)
623 boost::mutex::scoped_lock lock(this->
lock_);
632 this->
skip_*this->
width_, reinterpret_cast<const void*>(_src));
674 camera_info_publisher.
publish(camera_info_msg);
682 static const double timeout = 0.001;
virtual bool CanTriggerCamera()
const std::string BAYER_GRBG8
ros::Subscriber trigger_subscriber_
std::string image_topic_name_
ROS image topic name.
#define ROS_INFO_NAMED(name,...)
void publish(const boost::shared_ptr< M > &message) const
boost::mutex lock_
A mutex to lock access to fields that are used in ROS message callbacks.
std::string getPrefixParam(ros::NodeHandle &nh)
common::Time sensor_update_time_
#define ROS_WARN_NAMED(name,...)
dynamic_reconfigure::Server< gazebo_plugins::GazeboRosCameraConfig > * dyn_srv_
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
boost::shared_ptr< camera_info_manager::CameraInfoManager > camera_info_manager_
std::string robot_namespace_
for setting ROS name space
ROSCPP_DECL bool isInitialized()
boost::shared_ptr< bool > was_active_
Keep track when we activate this camera through ros subscription, was it already active? resume state when unsubscribed.
image_transport::ImageTransport * itnode_
boost::thread deferred_load_thread_
image_transport::Publisher image_pub_
void Load(sensors::SensorPtr _parent, sdf::ElementPtr _sdf, const std::string &_camera_name_suffix="")
Load the plugin.
Publisher advertise(const std::string &base_topic, uint32_t queue_size, bool latch=false)
~GazeboRosCameraUtils()
Destructor.
std::string camera_name_
ROS camera name.
void SetHFOV(const std_msgs::Float64::ConstPtr &hfov)
: Camera modification functions
ros::CallbackQueue camera_queue_
virtual void TriggerCamera()
boost::thread callback_queue_thread_
boost::shared_ptr< int > image_connect_count_
Keep track of number of image connections.
const std::string BAYER_GBRG8
void TriggerCameraInternal(const std_msgs::Empty::ConstPtr &dummy)
std::string resolve(const std::string &prefix, const std::string &frame_name)
sensor_msgs::Image image_msg_
ROS image message.
#define ROS_DEBUG_NAMED(name,...)
ros::Publisher camera_info_pub_
camera info
std::string tf_prefix_
tf prefix
event::ConnectionPtr OnLoad(const boost::function< void()> &)
void publish(const sensor_msgs::Image &message) const
bool initialized_
True if camera util is initialized.
common::Time last_info_update_time_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
double update_rate_
update rate of this sensor
std::string frame_name_
ROS frame transform name to use in the image message header. This should typically match the link nam...
std::string camera_info_topic_name_
void PutCameraData(const unsigned char *_src)
Put camera data to the ROS topic.
const std::string BAYER_BGGR8
rendering::CameraPtr camera_
GazeboRosCameraUtils()
Constructor.
uint32_t getNumSubscribers() const
void SetUpdateRate(const std_msgs::Float64::ConstPtr &update_rate)
ros::NodeHandle * rosnode_
A pointer to the ROS node. A node will be instantiated if it does not exist.
common::Time last_update_time_
std::string trigger_topic_name_
ROS trigger topic name.
event::EventT< void()> load_event_
#define ROS_ERROR_NAMED(name,...)
void configCallback(gazebo_plugins::GazeboRosCameraConfig &config, uint32_t level)
const std::string BAYER_RGGB8
std::string GetRobotNamespace(const sensors::SensorPtr &parent, const sdf::ElementPtr &sdf, const char *pInfo=NULL)
Reads the name space tag of a sensor plugin.
sensors::SensorPtr parentSensor_
std::string type_
size of image buffer
boost::shared_ptr< void > VoidPtr
boost::shared_ptr< boost::mutex > image_connect_count_lock_
A mutex to lock access to image_connect_count_.