scoring_plugin.cc
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 #include <gazebo/common/Assert.hh>
19 #include <gazebo/common/Console.hh>
20 #include <gazebo/physics/Joint.hh>
21 #include <gazebo/physics/Model.hh>
23 
25 void ScoringPlugin::Load(gazebo::physics::WorldPtr _world,
26  sdf::ElementPtr _sdf)
27 {
28  GZ_ASSERT(_world, "ScoringPlugin::Load(): NULL world pointer");
29  GZ_ASSERT(_sdf, "ScoringPlugin::Load(): NULL _sdf pointer");
30 
31  this->world = _world;
32  this->sdf = _sdf;
33 
34  // SDF.
35  if (!this->ParseSDFParameters())
36  {
37  gzerr << "Scoring disabled" << std::endl;
38  return;
39  }
40 
41  this->readyTime.Set(this->initialStateDuration);
42  this->runningTime = this->readyTime + this->readyStateDuration;
43  this->finishTime = this->runningTime + this->runningStateDuration;
44 
45  // Prepopulate the task msg.
46  this->taskMsg.name = this->taskName;
47  this->taskMsg.ready_time.fromSec(this->readyTime.Double());
48  this->taskMsg.running_time.fromSec(this->runningTime.Double());
49  this->UpdateTaskMessage();
50 
51  // Initialize ROS transport.
52  this->rosNode.reset(new ros::NodeHandle());
53  this->taskPub = this->rosNode->advertise<vmrc_gazebo::Task>(this->topic, 100);
54 
55  this->updateConnection = gazebo::event::Events::ConnectWorldUpdateBegin(
56  std::bind(&ScoringPlugin::Update, this));
57 }
58 
60 double ScoringPlugin::Score() const
61 {
62  return this->score;
63 }
64 
66 void ScoringPlugin::SetScore(double _newScore)
67 {
68  this->score = _newScore;
69 }
70 
72 std::string ScoringPlugin::TaskName() const
73 {
74  return this->taskName;
75 }
76 
78 std::string ScoringPlugin::TaskState() const
79 {
80  return this->taskState;
81 }
82 
84 gazebo::common::Time ScoringPlugin::ElapsedTime() const
85 {
86  return this->elapsedTime;
87 }
88 
90 gazebo::common::Time ScoringPlugin::RemainingTime() const
91 {
92  return this->remainingTime;
93 }
94 
97 {
98  if (this->taskState == "finished")
99  return;
100 
101  this->taskState = "finished";
102  this->OnFinished();
103 }
104 
107 {
108  // The vehicle might not be ready yet, let's try to get it.
109  if (!this->vehicleModel)
110  this->vehicleModel = this->world->GetModel(this->vehicleName);
111 
112  this->UpdateTime();
113  this->UpdateTaskState();
114  this->PublishStats();
115 }
116 
119 {
120  this->currentTime = this->world->GetSimTime();
121 
122  if (this->taskState == "running")
123  {
124  this->elapsedTime = this->currentTime - this->runningTime;
125  this->remainingTime = this->finishTime - this->currentTime;
126  this->timedOut = this->remainingTime <= gazebo::common::Time::Zero;
127  }
128 }
129 
132 {
133  if (this->taskState == "initial" &&
134  this->currentTime >= this->readyTime)
135  {
136  this->taskState = "ready";
137  this->ReleaseVehicle();
138  this->OnReady();
139  return;
140  }
141 
142  if (this->taskState == "ready" &&
143  this->currentTime >= this->runningTime)
144  {
145  this->taskState = "running";
146  this->OnRunning();
147  return;
148  }
149 
150  if (this->taskState == "running" && this->timedOut)
151  {
152  this->taskState = "finished";
153  this->OnFinished();
154  return;
155  }
156 }
157 
160 {
161  this->taskMsg.state = this->taskState;
162  this->taskMsg.elapsed_time.fromSec(this->elapsedTime.Double());
163  this->taskMsg.remaining_time.fromSec(this->remainingTime.Double());
164  this->taskMsg.timed_out = this->timedOut;
165  this->taskMsg.score = this->score;
166 }
167 
170 {
171  this->UpdateTaskMessage();
172 
173  // We publish stats at 1Hz.
174  if (this->currentTime - this->lastStatsSent >= gazebo::common::Time(1, 0))
175  {
176  this->taskPub.publish(this->taskMsg);
177  this->lastStatsSent = this->currentTime;
178  }
179 }
180 
183 {
184  if (!this->vehicleModel || this->lockJointNames.empty())
185  return;
186 
187  for (auto jointName : this->lockJointNames)
188  {
189  auto joint = this->vehicleModel->GetJoint(jointName);
190  if (joint)
191  joint->Detach();
192  else
193  gzerr << "Unable to release [" << jointName << "]" << std::endl;
194  }
195 
196  this->lockJointNames.clear();
197 
198  gzmsg << "Vehicle released" << std::endl;
199 }
200 
203 {
204 }
205 
208 {
209 }
210 
213 {
214 }
215 
218 {
219  // This is a required element.
220  if (!this->sdf->HasElement("vehicle"))
221  {
222  gzerr << "Unable to find <vehicle> element in SDF." << std::endl;
223  return false;
224  }
225  this->vehicleName = this->sdf->Get<std::string>("vehicle");
226 
227  // This is a required element.
228  if (!this->sdf->HasElement("task_name"))
229  {
230  gzerr << "Unable to find <task_name> element in SDF." << std::endl;
231  return false;
232  }
233  this->taskName = this->sdf->Get<std::string>("task_name");
234 
235  // This is an optional element.
236  if (this->sdf->HasElement("topic"))
237  this->topic = this->sdf->Get<std::string>("topic");
238 
239  // This is an optional element.
240  if (this->sdf->HasElement("initial_state_duration"))
241  {
242  double value = this->sdf->Get<double>("initial_state_duration");
243  if (value < 0)
244  {
245  gzerr << "<initial_state_duration> value should not be negative."
246  << std::endl;
247  return false;
248  }
249  this->initialStateDuration = value;
250  }
251 
252  // This is an optional element.
253  if (this->sdf->HasElement("ready_state_duration"))
254  {
255  double value = this->sdf->Get<double>("ready_state_duration");
256  if (value < 0)
257  {
258  gzerr << "<ready_state_duration> value should not be negative."
259  << std::endl;
260  return false;
261  }
262 
263  this->readyStateDuration = value;
264  }
265 
266  // This is an optional element.
267  if (this->sdf->HasElement("running_state_duration"))
268  {
269  double value = this->sdf->Get<double>("running_state_duration");
270  if (value < 0)
271  {
272  gzerr << "<running_state_duration> value should not be negative."
273  << std::endl;
274  return false;
275  }
276  this->runningStateDuration = value;
277  }
278 
279  return this->ParseJoints();
280 }
281 
284 {
285  // Optional element.
286  if (this->sdf->HasElement("release_joints"))
287  {
288  auto releaseJointsElem = this->sdf->GetElement("release_joints");
289 
290  // We need at least one joint.
291  if (!releaseJointsElem->HasElement("joint"))
292  {
293  gzerr << "Unable to find <joint> element in SDF." << std::endl;
294  return false;
295  }
296 
297  auto jointElem = releaseJointsElem->GetElement("joint");
298 
299  // Parse a new joint to be released.
300  while (jointElem)
301  {
302  // The joint's name.
303  if (!jointElem->HasElement("name"))
304  {
305  gzerr << "Unable to find <name> element in SDF." << std::endl;
306  return false;
307  }
308 
309  const std::string jointName = jointElem->Get<std::string>("name");
310  this->lockJointNames.push_back(jointName);
311 
312  // Parse the next gate.
313  jointElem = jointElem->GetNextElement("joint");
314  }
315  }
316 
317  return true;
318 }
void UpdateTime()
Update all time-related variables.
std::string TaskName() const
Get the task name.
double Score() const
Get the current score.
void publish(const boost::shared_ptr< M > &message) const
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().
gazebo::common::Time currentTime
Current time (simulation).
void UpdateTaskMessage()
Update the task stats message.
std::string TaskState() const
Get the task state.
gazebo::common::Time remainingTime
Remaining time since the start of the task (running state).
vmrc_gazebo::Task taskMsg
The next task message to be published.
void Load(gazebo::physics::WorldPtr _world, sdf::ElementPtr _sdf)
double runningStateDuration
Duration (seconds) of the running state (max task time).
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.
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.
std::unique_ptr< ros::NodeHandle > rosNode
ROS node handle.
void Update()
Callback executed at every world update.
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.
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.
gazebo::event::ConnectionPtr updateConnection
Pointer to the update event connection.
gazebo::common::Time RemainingTime() const
Remaining time in the running state.
std::string topic
Topic where the task stats are published.
double readyStateDuration
Duration (seconds) of the ready state.
std::string taskName
The name of the task.
virtual void OnFinished()
Callback executed when the task state transition into "finished".
bool timedOut
Whether the current task has timed out or not.
bool ParseJoints()
Parse the joints section of the SDF block.
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.
double score
The score.


vmrc_gazebo
Author(s): Brian Bingham , Carlos Aguero
autogenerated on Sun Feb 17 2019 03:14:16