Tutorials/1.3/plugins/camera plugin


 * NOTE:** This tutorial is not working at the moment


 * 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:


 * 1) include "gazebo.hh"
 * 2) include "plugins/CameraPlugin.hh"
 * 3) include "common/common.hh"
 * 4) include "transport/transport.hh"

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:


 * 1) include 
 * 2) include "gazebo.hh"
 * 3) include "physics/physics.hh"
 * 4) include "common/common.hh"
 * 5) include "transport/transport.hh"

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:

  0.1 0.1 0.1 1      0 0 0 1       false

0 0 -9.8         quick 0.01 100 1.3

 true 1.0            1             0             0             1             0             1               0 0 1               1000 1000                 0 0          false 0 0 1              100 100             Gazebo/Black

0 1.5 0 0 0 0          1.0             1             0             0             1             0             1

1 1 1               0 0

1 1 1             Gazebo/WoodPallet

 true</cast_shadows> 0 0 10 0 0 0       .8 .8 .8 1        1 .1 .1 1         10  0.8  0.01  0.0         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]