scoring_plugin.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 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 
18 #ifndef VRX_GAZEBO_SCORING_PLUGIN_HH_
19 #define VRX_GAZEBO_SCORING_PLUGIN_HH_
20 
21 #include <ros/ros.h>
22 #include <gazebo/msgs/gz_string.pb.h>
23 #include <memory>
24 #include <string>
25 #include <vector>
26 #include <gazebo/common/Events.hh>
27 #include <gazebo/common/Plugin.hh>
28 #include <gazebo/common/Time.hh>
29 #include <gazebo/physics/World.hh>
30 #include <sdf/sdf.hh>
31 #include <gazebo/transport/transport.hh>
32 #include "vrx_gazebo/Task.h"
33 #include "vrx_gazebo/Contact.h"
34 
112 
113 class ScoringPlugin : public gazebo::WorldPlugin
114 {
116  public: ScoringPlugin();
117 
119  public: void Exit();
120 
121  // Documentation inherited.
122  protected: void Load(gazebo::physics::WorldPtr _world,
123  sdf::ElementPtr _sdf);
124 
127  protected: double Score() const;
128 
131  protected: void SetScore(double _newScore);
132 
135  protected: std::string TaskName() const;
136 
139  protected: std::string TaskState() const;
140 
143  protected: gazebo::common::Time ElapsedTime() const;
144 
147  protected: gazebo::common::Time RemainingTime() const;
148 
151  protected: void Finish();
152 
154  protected: virtual void ReleaseVehicle();
155 
157  protected: void SetTimeoutScore(double _timeoutScore);
158 
160  protected: double GetTimeoutScore();
161 
163  protected: double GetRunningStateDuration();
164 
166  private: void Update();
167 
169  private: void UpdateTime();
170 
172  private: void UpdateTaskState();
173 
175  private: void UpdateTaskMessage();
176 
178  private: void PublishStats();
179 
181  private: virtual void OnReady();
182 
184  private: virtual void OnRunning();
185 
187  private: virtual void OnFinished();
188 
190  private: virtual void OnCollision();
191 
194  private: void OnCollisionMsg(ConstContactsPtr &_contacts);
195 
199  private: bool ParseSDFParameters();
200 
204  private: bool ParseJoints();
205 
207  protected: gazebo::physics::WorldPtr world;
208 
210  protected: std::string taskName = "undefined";
211 
213  protected: std::string vehicleName;
214 
216  protected: gazebo::physics::ModelPtr vehicleModel;
217 
219  protected: gazebo::common::Time lastCollisionTime;
220 
222  private: gazebo::transport::NodePtr gzNode;
223 
225  private: gazebo::transport::SubscriberPtr collisionSub;
226 
228  private: gazebo::transport::PublisherPtr serverControlPub;
229 
231  private: gazebo::event::ConnectionPtr updateConnection;
232 
234  private: std::string taskInfoTopic = "/vrx/task/info";
235 
237  private: bool debug = true;
238 
240  private: std::string contactDebugTopic = "/vrx/debug/contact";
241 
243  private: double score = 0.0;
244 
246  private: sdf::ElementPtr sdf;
247 
249  private: double initialStateDuration = 30.0;
250 
252  private: double readyStateDuration = 60.0;
253 
255  protected: double runningStateDuration = 300.0;
256 
258  private: gazebo::common::Time readyTime;
259 
261  private: gazebo::common::Time runningTime;
262 
264  private: gazebo::common::Time finishTime;
265 
267  private: gazebo::common::Time currentTime;
268 
269  // \brief Elapsed time since the start of the task (running state).
270  private: gazebo::common::Time elapsedTime;
271 
273  private: gazebo::common::Time remainingTime;
274 
276  private: float CollisionBuffer = 3.0;
277 
279  private: int collisionCounter = 0;
280 
282  private: std::vector<std::string> collisionList;
283 
285  private: std::vector<gazebo::common::Time> collisionTimestamps;
286 
288  private: bool timedOut = false;
289 
291  private: gazebo::common::Time lastStatsSent = gazebo::common::Time::Zero;
292 
294  private: std::string taskState = "initial";
295 
297  protected: vrx_gazebo::Task taskMsg;
298 
300  private: vrx_gazebo::Contact contactMsg;
301 
303  private: std::vector<std::string> lockJointNames;
304 
306  private: std::unique_ptr<ros::NodeHandle> rosNode;
307 
310 
313 
315  private: double timeoutScore = -1.0;
316 };
317 
318 #endif
void UpdateTime()
Update all time-related variables.
std::string TaskName() const
Get the task name.
gazebo::common::Time lastCollisionTime
Last collision time.
double Score() const
Get the current score.
void SetTimeoutScore(double _timeoutScore)
Set the score in case of timeout.
virtual void ReleaseVehicle()
Tries to release the vehicle in case is locked.
std::vector< std::string > lockJointNames
The name of the joints to be dettached during ReleaseVehicle().
float CollisionBuffer
Collision buffer.
std::string contactDebugTopic
Topic where debug collision is published.
gazebo::common::Time currentTime
Current time (simulation).
bool debug
Bool flag for debug.
void UpdateTaskMessage()
Update the task stats message.
std::vector< gazebo::common::Time > collisionTimestamps
Collisions timestamps.
std::string TaskState() const
Get the task state.
gazebo::common::Time remainingTime
Remaining time since the start of the task (running state).
double timeoutScore
Score in case of timeout - added for Navigation task.
void Load(gazebo::physics::WorldPtr _world, sdf::ElementPtr _sdf)
double runningStateDuration
Duration (seconds) of the running state (max task time).
void Exit()
Shutdown Gazebo and ROS.
void Finish()
Finish the current task. This will set the "finished" flag in the task message to true...
ros::Publisher taskPub
Publisher for the task state.
virtual void OnRunning()
Callback executed when the task state transition into "running".
void UpdateTaskState()
Update the state of the current task.
std::vector< std::string > collisionList
Collision list.
gazebo::common::Time ElapsedTime() const
Elapsed time in the running state.
bool ParseSDFParameters()
Parse all SDF parameters.
std::string taskState
The task state.
gazebo::common::Time elapsedTime
void PublishStats()
Publish the task stats over a ROS topic.
gazebo::transport::NodePtr gzNode
gazebo node pointer
std::unique_ptr< ros::NodeHandle > rosNode
ROS node handle.
vrx_gazebo::Contact contactMsg
ROS Contact Msg.
void Update()
Callback executed at every world update.
vrx_gazebo::Task taskMsg
The next task message to be published.
gazebo::common::Time runningTime
Absolute time specifying the start of the running state.
gazebo::common::Time readyTime
Absolute time specifying the start of the ready state.
A plugin that provides common functionality to any scoring plugin. This plugin defines four different...
gazebo::physics::WorldPtr world
A world pointer.
gazebo::common::Time finishTime
Absolute time specifying the start of the finish state.
gazebo::physics::ModelPtr vehicleModel
Pointer to the vehicle to score.
sdf::ElementPtr sdf
Pointer to the SDF plugin element.
double initialStateDuration
Duration (seconds) of the initial state.
gazebo::common::Time lastStatsSent
Time at which the last message was sent.
double GetTimeoutScore()
Get the timeoutScore.
gazebo::event::ConnectionPtr updateConnection
Pointer to the update event connection.
gazebo::common::Time RemainingTime() const
Remaining time in the running state.
gazebo::transport::SubscriberPtr collisionSub
Collision detection node subscriber.
void OnCollisionMsg(ConstContactsPtr &_contacts)
Callback function when collision occurs in the world.
double readyStateDuration
Duration (seconds) of the ready state.
std::string taskName
The name of the task.
std::string taskInfoTopic
Topic where the task stats are published.
virtual void OnFinished()
Callback executed when the task state transition into "finished".
bool timedOut
Whether the current task has timed out or not.
ros::Publisher contactPub
Publisher for the collision.
ScoringPlugin()
Class constructor.
bool ParseJoints()
Parse the joints section of the SDF block.
gazebo::transport::PublisherPtr serverControlPub
gazebo server control publisher
int collisionCounter
Collisions counter.
void SetScore(double _newScore)
Set the score.
virtual void OnReady()
Callback executed when the task state transition into "ready".
std::string vehicleName
The name of the vehicle to score.
virtual void OnCollision()
Callback executed when a collision is detected for the WAMV.
double GetRunningStateDuration()
Get running duration.
double score
The score.


vrx_gazebo
Author(s): Brian Bingham , Carlos Aguero
autogenerated on Thu May 7 2020 03:54:56