Swarm API

RobotPlugin.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
20 
21 #ifndef __SWARM_ROBOT_PLUGIN_HH__
22 #define __SWARM_ROBOT_PLUGIN_HH__
23 
24 #include <functional>
25 #include <map>
26 #include <mutex>
27 #include <string>
28 #include <vector>
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>
41 #include <sdf/sdf.hh>
42 
43 #include "msgs/datagram.pb.h"
44 #include "msgs/neighbor_v.pb.h"
45 
46 namespace swarm
47 {
50  typedef std::map<std::string, ignition::math::Pose3d> ObjPose_M;
51 
53  class ImageData
54  {
56  public: ObjPose_M objects;
57  };
58 
59 
106  class IGNITION_VISIBLE RobotPlugin : public gazebo::ModelPlugin
107  {
109  public: enum VehicleType
110  {
112  GROUND = 0,
113 
115  ROTOR = 1,
116 
118  FIXED_WING = 2
119  };
120 
122  public: RobotPlugin();
123 
125  public: virtual ~RobotPlugin();
126 
131  protected: virtual void Load(sdf::ElementPtr _sdf);
132 
137  protected: virtual void Update(const gazebo::common::UpdateInfo &_info);
138 
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),
168  C *_obj,
169  const std::string &_address,
170  const int _port = kDefaultPort)
171  {
172  // Sanity check: Make sure that you use your local address or multicast.
173  if ((_address != this->kMulticast) && (_address != this->Host()))
174  {
175  gzerr << "[" << this->Host() << "] Bind() error: Address ["
176  << _address << "] is not your local address" << std::endl;
177  return false;
178  }
179 
180  // Mapping the "unicast socket" to a topic name.
181  const std::string unicastTopic =
182  "/swarm/" + _address + "/" + std::to_string(_port);
183 
184  if (!this->node.Subscribe(unicastTopic,
185  &RobotPlugin::OnMsgReceived, this))
186  {
187  gzerr << "RobotPlugin::Bind() error: Subscribe() returned an "
188  << "error while subscribing the unicast/multicast address"
189  << std::endl;
190  return false;
191  }
192 
193  // Register the user callback using the topic name as the key.
194  this->callbacks[unicastTopic] = std::bind(_cb, _obj,
195  std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
196  std::placeholders::_4);
197 
198  // Only enable broadcast if the address is a regular unicast address.
199  if (_address != this->kMulticast)
200  {
201  const std::string bcastTopic =
202  "/swarm/broadcast/" + std::to_string(_port);
203 
204  if (!this->node.Subscribe(bcastTopic,
205  &RobotPlugin::OnMsgReceived, this))
206  {
207  gzerr << "RobotPlugin::Bind() error: Subscribe() returned an "
208  << "error while subscribing the broadcast address"
209  << std::endl;
210  return false;
211  }
212 
213  // Register the user callback using the broadcast topic as the key.
214  this->callbacks[bcastTopic] = std::bind(_cb, _obj,
215  std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
216  std::placeholders::_4);
217  }
218 
219  return true;
220  }
221 
234  protected: bool SendTo(const std::string &_data,
235  const std::string &_dstAddress,
236  const uint32_t _port = kDefaultPort);
237 
242  protected: std::string Host() const;
243 
247  protected: std::vector<std::string> Neighbors() const;
248 
253  protected: VehicleType Type() const;
254 
258  protected: std::string Name() const;
259 
276  protected: bool SetLinearVelocity(
277  const ignition::math::Vector3d &_velocity);
278 
296  protected: bool SetLinearVelocity(const double _x,
297  const double _y, const double _z);
298 
315  protected: bool SetAngularVelocity(
316  const ignition::math::Vector3d &_velocity);
317 
335  protected: bool SetAngularVelocity(const double _x, const double _y,
336  const double _z);
337 
364  protected: bool Imu(ignition::math::Vector3d &_linVel,
365  ignition::math::Vector3d &_angVel,
366  ignition::math::Quaterniond &_orient) const;
367 
376  protected: bool Bearing(ignition::math::Angle &_bearing) const;
377 
384  protected: bool Pose(double &_latitude,
385  double &_longitude,
386  double &_altitude) const;
387 
395  protected: bool Image(ImageData &_img) const;
396 
403  protected: void SearchArea(double &_minLatitude,
404  double &_maxLatitude,
405  double &_minLongitude,
406  double &_maxLongitude);
407 
410  protected: double BatteryStartCapacity() const;
411 
414  protected: double BatteryCapacity() const;
415 
418  protected: double BatteryConsumption() const;
419 
424  protected: double BatteryConsumptionFactor() const;
425 
429  protected: double ExpectedBatteryLife() const;
430 
434  protected: ignition::math::Pose3d CameraToWorld(
435  const ignition::math::Pose3d &_poseinCamera) const;
436 
440  private: virtual void Loop(const gazebo::common::UpdateInfo &_info);
441 
442  // Documentation Inherited.
443  private: virtual void Load(gazebo::physics::ModelPtr _model,
444  sdf::ElementPtr _sdf);
445 
454  private: void OnMsgReceived(const std::string &_topic,
455  const msgs::Datagram &_msg);
456 
464  private: void OnNeighborsReceived(const std::string &_topic,
465  const msgs::Neighbor_V &_msg);
466 
469  private: void AdjustPose();
470 
475  private: void TerrainLookup(const ignition::math::Vector3d &_pos,
476  ignition::math::Vector3d &_terrainPos,
477  ignition::math::Vector3d &_norm) const;
478 
480  private: void UpdateSensors();
481 
483  private: void UpdateBattery();
484 
489  using Callback_t =
490  std::function<void(const std::string &_srcAddress,
491  const std::string &_dstAddress,
492  const uint32_t _dstPort,
493  const std::string &_data)>;
494 
497  protected: const std::string kBroadcast = "broadcast";
498 
501  protected: const std::string kMulticast = "multicast";
502 
504  protected: const std::string kBoo = "boo";
505 
507  protected: static const uint32_t kDefaultPort = 4100;
508 
510  protected: static const uint32_t kBooPort = 4200;
511 
513  protected: static const uint32_t kMtu = 1500;
514 
516  private: std::vector<std::string> neighbors;
517 
519  private: std::vector<double> neighborProbabilities;
520 
522  private: ignition::transport::Node node;
523 
524  // The gazebo transport node. Used for debugging, see source.
525  // private: gazebo::transport::NodePtr gzNode;
526 
527  // Used to publish markers, Used for debugging, see source.
528  // private: gazebo::transport::PublisherPtr markerPub;
529 
532  private: std::map<std::string, Callback_t> callbacks;
533 
535  private: gazebo::physics::ModelPtr model;
536 
538  private: std::string address;
539 
541  private: gazebo::event::ConnectionPtr updateConnection;
542 
544  private: VehicleType type;
545 
547  private: gazebo::sensors::GpsSensorPtr gps;
548 
550  private: gazebo::sensors::ImuSensorPtr imu;
551 
553  private: gazebo::sensors::LogicalCameraSensorPtr camera;
554 
556  private: double searchMinLatitude, searchMaxLatitude,
557  searchMinLongitude, searchMaxLongitude;
558 
560  private: mutable std::mutex mutex;
561 
563  private: gazebo::physics::HeightmapShapePtr terrain;
564 
567  private: ignition::math::Vector2d terrainScaling;
568 
570  private: ignition::math::Vector3d terrainSize;
571 
573  private: double modelHeight2;
574 
576  private: ignition::math::Vector3d linearVelocity;
577 
579  private: ignition::math::Vector3d angularVelocity;
580 
582  private: ignition::math::Quaterniond orientation;
583 
585  private: ignition::math::Angle bearing;
586 
588  private: double startCapacity;
589 
591  private: double capacity;
592 
594  private: double consumption;
595 
598  private: double consumptionFactor;
599 
601  private: gazebo::physics::WorldPtr world;
602 
604  friend class BooPlugin;
605  };
606 }
607 #endif
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