21 #ifndef __SWARM_ROBOT_PLUGIN_HH__
22 #define __SWARM_ROBOT_PLUGIN_HH__
29 #include <gazebo/transport/TransportTypes.hh>
30 #include <gazebo/common/Console.hh>
31 #include <gazebo/common/Events.hh>
32 #include <gazebo/common/Plugin.hh>
33 #include <gazebo/common/UpdateInfo.hh>
34 #include <gazebo/physics/PhysicsTypes.hh>
35 #include <gazebo/sensors/sensors.hh>
36 #include <ignition/math/Angle.hh>
37 #include <ignition/math/Pose3.hh>
38 #include <ignition/math/Quaternion.hh>
39 #include <ignition/math/Vector3.hh>
40 #include <ignition/transport.hh>
43 #include "msgs/datagram.pb.h"
44 #include "msgs/neighbor_v.pb.h"
50 typedef std::map<std::string, ignition::math::Pose3d> ObjPose_M;
131 protected:
virtual void Load(sdf::ElementPtr _sdf);
137 protected:
virtual void Update(
const gazebo::common::UpdateInfo &_info);
163 protected:
template<
typename C>
164 bool Bind(
void(C::*_cb)(
const std::string &_srcAddress,
165 const std::string &_dstAddress,
166 const uint32_t _dstPort,
167 const std::string &_data),
169 const std::string &_address,
170 const int _port = kDefaultPort)
173 if ((_address != this->kMulticast) && (_address != this->Host()))
175 gzerr <<
"[" << this->Host() <<
"] Bind() error: Address ["
176 << _address <<
"] is not your local address" << std::endl;
181 const std::string unicastTopic =
182 "/swarm/" + _address +
"/" + std::to_string(_port);
184 if (!this->node.Subscribe(unicastTopic,
185 &RobotPlugin::OnMsgReceived,
this))
187 gzerr <<
"RobotPlugin::Bind() error: Subscribe() returned an "
188 <<
"error while subscribing the unicast/multicast address"
194 this->callbacks[unicastTopic] = std::bind(_cb, _obj,
195 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
196 std::placeholders::_4);
199 if (_address != this->kMulticast)
201 const std::string bcastTopic =
202 "/swarm/broadcast/" + std::to_string(_port);
204 if (!this->node.Subscribe(bcastTopic,
205 &RobotPlugin::OnMsgReceived,
this))
207 gzerr <<
"RobotPlugin::Bind() error: Subscribe() returned an "
208 <<
"error while subscribing the broadcast address"
214 this->callbacks[bcastTopic] = std::bind(_cb, _obj,
215 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
216 std::placeholders::_4);
234 protected:
bool SendTo(
const std::string &_data,
235 const std::string &_dstAddress,
236 const uint32_t _port = kDefaultPort);
242 protected: std::string Host()
const;
247 protected: std::vector<std::string> Neighbors()
const;
253 protected: VehicleType Type()
const;
258 protected: std::string Name()
const;
276 protected:
bool SetLinearVelocity(
277 const ignition::math::Vector3d &_velocity);
296 protected:
bool SetLinearVelocity(
const double _x,
297 const double _y,
const double _z);
315 protected:
bool SetAngularVelocity(
316 const ignition::math::Vector3d &_velocity);
335 protected:
bool SetAngularVelocity(
const double _x,
const double _y,
364 protected:
bool Imu(ignition::math::Vector3d &_linVel,
365 ignition::math::Vector3d &_angVel,
366 ignition::math::Quaterniond &_orient)
const;
376 protected:
bool Bearing(ignition::math::Angle &_bearing)
const;
384 protected:
bool Pose(
double &_latitude,
386 double &_altitude)
const;
395 protected:
bool Image(
ImageData &_img)
const;
403 protected:
void SearchArea(
double &_minLatitude,
404 double &_maxLatitude,
405 double &_minLongitude,
406 double &_maxLongitude);
410 protected:
double BatteryStartCapacity()
const;
414 protected:
double BatteryCapacity()
const;
418 protected:
double BatteryConsumption()
const;
424 protected:
double BatteryConsumptionFactor()
const;
429 protected:
double ExpectedBatteryLife()
const;
434 protected: ignition::math::Pose3d CameraToWorld(
435 const ignition::math::Pose3d &_poseinCamera)
const;
440 private:
virtual void Loop(
const gazebo::common::UpdateInfo &_info);
443 private:
virtual void Load(gazebo::physics::ModelPtr _model,
444 sdf::ElementPtr _sdf);
454 private:
void OnMsgReceived(
const std::string &_topic,
455 const msgs::Datagram &_msg);
464 private:
void OnNeighborsReceived(
const std::string &_topic,
465 const msgs::Neighbor_V &_msg);
469 private:
void AdjustPose();
475 private:
void TerrainLookup(
const ignition::math::Vector3d &_pos,
476 ignition::math::Vector3d &_terrainPos,
477 ignition::math::Vector3d &_norm)
const;
480 private:
void UpdateSensors();
483 private:
void UpdateBattery();
490 std::function<void(
const std::string &_srcAddress,
491 const std::string &_dstAddress,
492 const uint32_t _dstPort,
493 const std::string &_data)>;
497 protected:
const std::string kBroadcast =
"broadcast";
501 protected:
const std::string kMulticast =
"multicast";
504 protected:
const std::string kBoo =
"boo";
507 protected:
static const uint32_t kDefaultPort = 4100;
510 protected:
static const uint32_t kBooPort = 4200;
513 protected:
static const uint32_t kMtu = 1500;
516 private: std::vector<std::string> neighbors;
519 private: std::vector<double> neighborProbabilities;
522 private: ignition::transport::Node node;
532 private: std::map<std::string, Callback_t> callbacks;
535 private: gazebo::physics::ModelPtr model;
538 private: std::string address;
541 private: gazebo::event::ConnectionPtr updateConnection;
547 private: gazebo::sensors::GpsSensorPtr gps;
550 private: gazebo::sensors::ImuSensorPtr imu;
553 private: gazebo::sensors::LogicalCameraSensorPtr camera;
556 private:
double searchMinLatitude, searchMaxLatitude,
557 searchMinLongitude, searchMaxLongitude;
560 private:
mutable std::mutex mutex;
563 private: gazebo::physics::HeightmapShapePtr terrain;
567 private: ignition::math::Vector2d terrainScaling;
570 private: ignition::math::Vector3d terrainSize;
573 private:
double modelHeight2;
576 private: ignition::math::Vector3d linearVelocity;
579 private: ignition::math::Vector3d angularVelocity;
582 private: ignition::math::Quaterniond orientation;
585 private: ignition::math::Angle bearing;
588 private:
double startCapacity;
591 private:
double capacity;
594 private:
double consumption;
598 private:
double consumptionFactor;
601 private: gazebo::physics::WorldPtr world;
bool Bind(void(C::*_cb)(const std::string &_srcAddress, const std::string &_dstAddress, const uint32_t _dstPort, const std::string &_data), C *_obj, const std::string &_address, const int _port=kDefaultPort)
This method can bind a local address and a port to a virtual socket.
Definition: RobotPlugin.hh:164
A class that encapsulates image data.
Definition: RobotPlugin.hh:53
Definition: BooPlugin.hh:37
Class that drives the behavior of the base of operations (BOO).
Definition: BooPlugin.hh:57
A Model plugin that is the base class for all agent plugins in a swarm.
Definition: RobotPlugin.hh:106
VehicleType
The type of vehicle.
Definition: RobotPlugin.hh:109
ObjPose_M objects
Map of detected objects.
Definition: RobotPlugin.hh:56