Tutorials/1.0/plugins/camera plugin


 * Prerequisite**: See the Model Manipulation Plugin tutorial.


 * Code:** `/examples/plugins/camera/`


 * Description:** How to access data from a camera sensor using a plugin.

The CameraPlugin class is given as a convenience tool when accessing camera sensors. Here we will create a new plugin `CameraDump` that inherits from `CameraPlugin` class:

#include "gazebo.hh" #include "plugins/CameraPlugin.hh" #include "common/common.h"       #include "transport/transport.h"        namespace gazebo {            class CameraDump : public CameraPlugin {            public: CameraDump : CameraPlugin,saveCount(0) {} public: void Load(sensors::SensorPtr _parent, sdf::ElementPtr _sdf) {             // Don't forget to load the camera plugin CameraPlugin::Load(_parent,_sdf); }            // Update the controller public: void OnNewFrame(const unsigned char *_image,                unsigned int _width, unsigned int _height, unsigned int _depth,                 const std::string &_format) {             char tmp[1024]; snprintf(tmp, sizeof(tmp), "/tmp/%s-%04d.jpg",                 this->parentSensor->GetCamera->GetName.c_str, this->saveCount); if (this->saveCount < 10) {                  this->parentSensor->GetCamera->SaveFrame(                    _image, _width, _height, _depth, _format, tmp); gzmsg << "Saving frame [" << this->saveCount << "] as [" << tmp << "]\n"; this->saveCount++; }            }                     private: int saveCount; };         // Register this plugin with the simulator GZ_REGISTER_SENSOR_PLUGIN(CameraDump) }


 * 1) Compiling Camera Plugin ###

Assuming the reader has gone through the Simple World tutorial and the Model Manipulation Plugin tutorial. All that needs to be done in addition is save the above code as ~/gazebo_plugin_tutorial/camera_dump.cc and add the following lines to ~/gazebo_plugin_tutorial/CMakeLists.txt

add_library(camera_dump SHARED camera_dump.cc) target_link_libraries(camera_dump ${GAZEBO_libraries} CameraPlugin)

Compiling this code will result in a shared library, `~/gazebo_plugin_tutorial/build/libcamera_dump.so`, that can be inserted in a Gazebo simulation.


 * 1) Running Camera Plugin ###

To test the camera plugin that we have created above, let's build a world with two objects, one stationary (`box2`) and the another has a camera attached (`box`). The camera is set to revolve around the stationary object using a slightly modified Model Manipulation Plugin.


 * 1) Modified Model Manipulation Plugin ####

Here's the slightly modified model manipulation pluugin that makes the camera move in a circular path:

#include  #include "gazebo.hh" #include "physics/physics.h"       #include "common/common.h"        #include "transport/transport.h"        namespace gazebo {              class CameraMove : public ModelPlugin {                public: CameraMove : ModelPlugin {} public: void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf) {                // Get a pointer to the model this->model = _model; // Listen to the update event. This event is broadcast every // simulation iteration. this->updateConnection = event::Events::ConnectWorldUpdateStart(                 boost::bind(&CameraMove::OnUpdate, this)); }                  // OnUpdate will not be executed unless it's explicitly connected by            //   an event, e.g. use event::Events::ConnectWorldUpdateStart public: void OnUpdate {              math::Vector3 v(0.03, 0, 0); math::Pose pose = this->model->GetWorldPose; v = pose.rot * v;             // Apply a small linear velocity to the model. this->model->SetLinearVel(v); this->model->SetAngularVel(math::Vector3(0, 0, 0.01)); }            // Pointer to the model private: physics::ModelPtr model; // Pointer to the update event connection private: event::ConnectionPtr updateConnection; };         // Register this plugin with the simulator GZ_REGISTER_MODEL_PLUGIN(CameraMove) }

Again, assuming the reader has gone through the Simple World tutorial and the Model Manipulation Plugin tutorial. Save the above code as ~/gazebo_plugin_tutorial/camera_move.cc and add yet another two lines to ~/gazebo_plugin_tutorial/CMakeLists.txt

add_library(camera_move SHARED camera_move.cc) target_link_libraries(camera_move ${GAZEBO_libraries})

Compiling this code will result in a shared library, `~/gazebo_plugin_tutorial/build/libcamera_move.so`, that can be inserted in a Gazebo simulation.

Finally, here's the world file that uses both of the above plugins:

              <sensor name='cam_sensor1' type='camera' always_on='1' update_rate='10'> <origin pose='0 0 0 0 0 1.57'/> <horizontal_fov angle='1.57'/> <image width='640' height='480' format='R8G8B8'/> <clip near='0.5' far='5'/> <plugin name='camera_dump' filename='libcamera_dump.so'/> <plugin name="camera_move" filename="libcamera_move.so"/> <origin pose="0 1.5 0 0 0 0"/> <origin pose="0 0 0.5 0 0 0"/> <inertial mass="1.0"> <inertia ixx="1" ixy="0" ixz="0" iyy="1" iyz="0" izz="1"/> <material script="Gazebo/WoodPallet"/> <light type="directional" name="sun" cast_shadows="true"> <diffuse rgba=".8 .8 .8 1"/> <specular rgba=".1 .1 .1 1"/> <attenuation range="10" constant="0.8" linear="0.01" quadratic="0.0"/> <direction xyz="0 .5 -.5"/>

Save the world file as `~/gazebo_plugin_tutorial/camera.world` and start the simulation:

gzserver ~/gazebo_plugin_tutorial/camera.world

You should see the following outputs:

$ gzserver ~/gazebo_plugin_tutorial/camera.world Gazebo multi-robot simulator, version 1.0.0-RC2 Copyright (C) 2011 Nate Koenig, John Hsu, Andrew Howard, and contributors. Released under the Apache 2 License. http://gazebosim.org Msg Waiting for master Msg Connected to gazebo master @ http://localhost:11345 Msg Saving frame [0] as [/tmp/default::cam1_sensor(0)-0000.jpg] Msg Saving frame [1] as [/tmp/default::cam1_sensor(0)-0001.jpg] Msg Saving frame [2] as [/tmp/default::cam1_sensor(0)-0002.jpg] Msg Saving frame [3] as [/tmp/default::cam1_sensor(0)-0003.jpg] Msg Saving frame [4] as [/tmp/default::cam1_sensor(0)-0004.jpg] Msg Saving frame [5] as [/tmp/default::cam1_sensor(0)-0005.jpg] Msg Saving frame [6] as [/tmp/default::cam1_sensor(0)-0006.jpg] Msg Saving frame [7] as [/tmp/default::cam1_sensor(0)-0007.jpg] Msg Saving frame [8] as [/tmp/default::cam1_sensor(0)-0008.jpg] Msg Saving frame [9] as [/tmp/default::cam1_sensor(0)-0009.jpg]