WorldUtils.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 João Borrego
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 
28 // Gazebo
29 #include <gazebo/common/Events.hh>
30 #include "gazebo/common/Plugin.hh"
31 #include <gazebo/msgs/msgs.hh>
32 #include "gazebo/physics/physics.hh"
33 #include <gazebo/transport/transport.hh>
34 
35 // Mutex
36 #include <mutex>
37 
38 #include <iostream>
39 #include <iomanip>
40 #include <sstream>
41 #include <list>
42 #include <string>
43 #include <regex>
44 // Boost
45 #include <boost/algorithm/string/predicate.hpp>
46 #include <boost/lexical_cast.hpp>
47 
48 // Custom messages
49 #include "world_utils_request.pb.h"
50 #include "world_utils_response.pb.h"
51 
52 // Õbjects with pending move operations
53 #include "MoveObject.hh"
54 
55 namespace WorldUtils {
56 
58 #define REQUEST_TOPIC "~/gap/world_utils"
59 #define RESPONSE_TOPIC "~/gap/world_utils/response"
61 
62 // Ease of use macros
63 
64 // Request
65 
67 #define SPAWN gap::msgs::WorldUtilsRequest::SPAWN
68 #define MOVE gap::msgs::WorldUtilsRequest::MOVE
70 #define REMOVE gap::msgs::WorldUtilsRequest::REMOVE
72 #define PHYSICS gap::msgs::WorldUtilsRequest::PHYSICS
74 #define PAUSE gap::msgs::WorldUtilsRequest::PAUSE
76 #define STATUS gap::msgs::WorldUtilsRequest::STATUS
78 
80 #define SPHERE gap::msgs::Object::SPHERE
81 #define CYLINDER gap::msgs::Object::CYLINDER
83 #define BOX gap::msgs::Object::BOX
85 #define CUSTOM gap::msgs::Object::CUSTOM
87 #define CUSTOM_LIGHT gap::msgs::Object::CUSTOM_LIGHT
89 #define MODEL gap::msgs::Object::MODEL
91 
92 // Response
93 
95 #define INFO gap::msgs::WorldUtilsResponse::INFO
96 #define SUCCESS gap::msgs::WorldUtilsResponse::SUCCESS
98 
99 // Regex patterns
100 
101 // Do not raise documentation warning
103 
105 #define REGEX_XML_SCRIPT "<script>[\\s\\S]*?<\\/script>"
106 #define REGEX_XML_POSE "<pose>[\\s\\S]*?<\\/pose>"
108 
110 
111 }
112 
113 namespace gazebo {
114 
116  typedef const boost::shared_ptr<const gap::msgs::WorldUtilsRequest>
119  typedef const boost::shared_ptr<const gap::msgs::WorldUtilsResponse>
121 
134  class WorldUtils : public WorldPlugin {
135 
137  public: std::mutex mutex;
138 
140  private: physics::WorldPtr world;
142  private: event::ConnectionPtr updateConnection;
143 
145  private: transport::NodePtr node;
147  private: transport::SubscriberPtr sub;
149  private: transport::PublisherPtr pub;
150 
152  private: transport::PublisherPtr request_pub;
154  private: transport::SubscriberPtr response_sub;
156  private: transport::PublisherPtr light_pub;
157 
158  // Regex patterns
159 
161  private: std::regex script_reg;
163  private: std::regex pose_reg;
164 
165  // Counters for automatic naming
166 
168  private: int sphere_counter {0};
170  private: int cylinder_counter {0};
172  private: int box_counter {0};
174  private: int light_counter {0};
175 
177  private: std::queue<MoveObject> move_queue;
178 
179  // Public methods
180 
182  public: WorldUtils();
183 
187  public: void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf);
188 
190  public: void onUpdate();
191 
192  // Private methods
193 
196  private: void onRequest(WorldUtilsRequestPtr &_msg);
197 
199  private: void clearWorld();
200 
204  private: void clearMatching(const std::string & match, const bool is_light);
205 
214  private: const std::string genSphere(
215  const std::string &model_name,
216  const double mass,
217  const double radius,
218  const ignition::math::Vector3d position,
219  const ignition::math::Quaterniond orientation);
220 
230  private: const std::string genCylinder(
231  const std::string &model_name,
232  const double mass,
233  const double radius,
234  const double length,
235  const ignition::math::Vector3d position,
236  const ignition::math::Quaterniond orientation);
237 
246  private: const std::string genBox(
247  const std::string &model_name,
248  const double mass,
249  const ignition::math::Vector3d size,
250  const ignition::math::Vector3d position,
251  const ignition::math::Quaterniond orientation);
252  };
253 }
Definition: WorldUtils.hh:55
transport::PublisherPtr pub
A publisher to the reply topic.
Definition: WorldUtils.hh:149
Definition: CameraUtils.cc:30
transport::SubscriberPtr sub
A subscriber to the request topic.
Definition: WorldUtils.hh:147
transport::PublisherPtr request_pub
A publisher to the gazebo request topic.
Definition: WorldUtils.hh:152
const boost::shared_ptr< const gap::msgs::WorldUtilsRequest > WorldUtilsRequestPtr
Shared pointer declaration for request message type.
Definition: WorldUtils.hh:117
event::ConnectionPtr updateConnection
Connection to World Update events.
Definition: WorldUtils.hh:142
std::queue< MoveObject > move_queue
Queue of objects with pending move actions.
Definition: WorldUtils.hh:177
transport::PublisherPtr light_pub
A publisher to light modify topic.
Definition: WorldUtils.hh:156
transport::SubscriberPtr response_sub
A subscriber to the gazebo response topic.
Definition: WorldUtils.hh:154
const boost::shared_ptr< const gap::msgs::WorldUtilsResponse > WorldUtilsResponsePtr
Pointer to WorldUtils request message.
Definition: scene_example.hh:158
Move Object class.
std::regex script_reg
Regex for applying custom material.
Definition: WorldUtils.hh:161
std::regex pose_reg
Regex for applying custom pose.
Definition: WorldUtils.hh:163
std::mutex mutex
Mutex for safe data access.
Definition: WorldUtils.hh:137
transport::NodePtr node
A node used for transport.
Definition: WorldUtils.hh:145
physics::WorldPtr world
A pointer to the world.
Definition: WorldUtils.hh:140