18 #include <gazebo/common/Assert.hh> 19 #include <gazebo/common/Console.hh> 20 #include <gazebo/physics/Joint.hh> 21 #include <gazebo/physics/Model.hh> 26 : WorldPlugin(), gzNode(new
gazebo::transport::Node()) {
32 GZ_ASSERT(_world,
"ScoringPlugin::Load(): NULL world pointer");
33 GZ_ASSERT(_sdf,
"ScoringPlugin::Load(): NULL _sdf pointer");
41 gzerr <<
"Scoring disabled" << std::endl;
66 #if GAZEBO_MAJOR_VERSION >= 8 67 std::string worldName = this->
world->Name();
69 std::string worldName = this->
world->GetName();
71 std::string collisionTopic =
72 std::string(
"/gazebo/") + worldName + std::string(
"/physics/contacts");
76 if (
char* env_dbg = std::getenv(
"VRX_DEBUG"))
78 if (std::string(env_dbg) ==
"false")
83 gzwarn <<
"VRX_DEBUG enviornment variable not set, defaulting to true" 87 this->
gzNode->Advertise<gazebo::msgs::ServerControl>
88 (
"/gazebo/server/control");
101 this->
score = _newScore;
142 #if GAZEBO_MAJOR_VERSION >= 8 158 #if GAZEBO_MAJOR_VERSION >= 8 235 gzerr <<
"Unable to release [" << jointName <<
"]" << std::endl;
238 this->lockJointNames.clear();
240 gzmsg <<
"Vehicle released" << std::endl;
246 gzmsg <<
"OnReady" << std::endl;
252 gzmsg <<
"OnRunning" << std::endl;
278 for (
unsigned int i = 0; i < _contacts->contact_size(); ++i) {
279 std::string wamvCollisionStr1 = _contacts->contact(i).collision1();
280 std::string wamvCollisionStr2 = _contacts->contact(i).collision2();
281 std::string wamvCollisionSubStr1 =
282 wamvCollisionStr1.substr(0, wamvCollisionStr1.find(
"lump"));
283 std::string wamvCollisionSubStr2 =
284 wamvCollisionStr2.substr(0, wamvCollisionStr2.find(
"lump"));
287 wamvCollisionSubStr1 ==
"wamv::base_link::base_link_fixed_joint_" ||
288 wamvCollisionSubStr2 ==
"wamv::base_link::base_link_fixed_joint_";
293 if (isWamvHit && this->
debug) {
295 this->
contactMsg.collision1 = _contacts->contact(i).collision1();
296 this->
contactMsg.collision2 = _contacts->contact(i).collision2();
300 if (isWamvHit && isHitBufferPassed) {
303 <<
"] New collision counted between [" 304 << _contacts->contact(i).collision1() <<
"] and [" 305 << _contacts->contact(i).collision2() <<
"]" << std::endl;
308 #if GAZEBO_MAJOR_VERSION >= 8 314 _contacts->contact(i).collision1() +
315 std::string(
" || ") + _contacts->contact(i).collision2());
327 if (!this->
sdf->HasElement(
"vehicle"))
329 gzerr <<
"Unable to find <vehicle> element in SDF." << std::endl;
335 if (!this->
sdf->HasElement(
"task_name"))
337 gzerr <<
"Unable to find <task_name> element in SDF." << std::endl;
340 this->
taskName = this->
sdf->Get<std::string>(
"task_name");
343 if (this->
sdf->HasElement(
"task_info_topic"))
347 if (this->
sdf->HasElement(
"contact_debug_topic"))
349 (
"contact_debug_topic");
352 if (this->
sdf->HasElement(
"initial_state_duration"))
354 double value = this->
sdf->Get<
double>(
"initial_state_duration");
357 gzerr <<
"<initial_state_duration> value should not be negative." 365 if (this->
sdf->HasElement(
"ready_state_duration"))
367 double value = this->
sdf->Get<
double>(
"ready_state_duration");
370 gzerr <<
"<ready_state_duration> value should not be negative." 379 if (this->
sdf->HasElement(
"running_state_duration"))
381 double value = this->
sdf->Get<
double>(
"running_state_duration");
384 gzerr <<
"<running_state_duration> value should not be negative." 392 if (this->
sdf->HasElement(
"collision_buffer"))
404 if (this->
sdf->HasElement(
"release_joints"))
406 auto releaseJointsElem = this->
sdf->GetElement(
"release_joints");
409 if (!releaseJointsElem->HasElement(
"joint"))
411 gzerr <<
"Unable to find <joint> element in SDF." << std::endl;
415 auto jointElem = releaseJointsElem->GetElement(
"joint");
421 if (!jointElem->HasElement(
"name"))
423 gzerr <<
"Unable to find <name> element in SDF." << std::endl;
427 const std::string jointName = jointElem->Get<std::string>(
"name");
431 jointElem = jointElem->GetNextElement(
"joint");
440 if (
char* env = std::getenv(
"VRX_EXIT_ON_COMPLETION"))
442 if (std::string(env) ==
"true")
445 gazebo::msgs::ServerControl
msg;
455 gzerr <<
"VRX_EXIT_ON_COMPLETION not set" 456 <<
" will not shutdown on ScoringPlugin::Exit()" 459 "not shutdown on ScoringPlugin::Exit()");
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 publish(const boost::shared_ptr< M > &message) const
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.
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.
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.
ROSCPP_DECL void shutdown()
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.
#define ROS_ERROR_STREAM(args)
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.