22 #include <gazebo/common/Assert.hh> 23 #include <gazebo/common/Console.hh> 24 #include <gazebo/common/Events.hh> 25 #include <ignition/math/config.hh> 26 #include <ignition/math/Matrix4.hh> 27 #include <ignition/math/Pose3.hh> 28 #include <gazebo/physics/Link.hh> 29 #include <gazebo/physics/Model.hh> 30 #include <gazebo/transport/transport.hh> 40 #define DO_EXPAND(VAL) VAL ## 1 41 #define EXPAND(VAL) DO_EXPAND(VAL) 42 #if (EXPAND(IGNITION_MATH_MAJOR_VERSION) == 1) 43 #define MAJOR_VERSION 3 44 #define MINOR_VERSION 0 46 #define MAJOR_VERSION IGNITION_MATH_MAJOR_VERSION 47 #define MINOR_VERSION IGNITION_MATH_MINOR_VERSION 50 #if MAJOR_VERSION == 2 && MINOR_VERSION < 3 51 #define ign_math_vector3d_zero ignition::math::Vector3d(0, 0, 0) 53 #define ign_math_vector3d_zero ignition::math::Vector3d::Zero 60 const double& _duration,
61 const std::string& _type,
62 const std::string& _name,
63 const ignition::math::Pose3d& _trialPose,
64 const gazebo::physics::WorldPtr _world)
71 #if GAZEBO_MAJOR_VERSION >= 8 78 #if GAZEBO_MAJOR_VERSION >= 8 88 std::string rtn =
"\nname: ";
93 rtn += std::to_string(this->
time);
94 rtn +=
"\nduration: ";
95 rtn += std::to_string(this->
duration);
97 rtn += std::to_string(this->
error);
105 this->
error = std::min(2.0, std::min(this->
error, _error));
114 #if GAZEBO_MAJOR_VERSION >= 8 115 ignition::math::Pose3d framePose(
116 _frame->WorldPose().Pos(),
117 ignition::math::Quaterniond(0.0, 0.0,
118 _frame->WorldPose().Rot().Yaw()));
120 ignition::math::Pose3d framePose(
121 _frame->GetWorldPose().pos.Ign(),
122 ignition::math::Quaterniond(0.0, 0.0,
123 _frame->GetWorldPose().rot.Ign().Yaw()));
125 ignition::math::Matrix4d transMat(framePose);
126 ignition::math::Matrix4d pose_local(this->
trialPose);
128 this->
modelPtr->SetWorldPose((transMat * pose_local).
Pose());
132 gzmsg <<
"PerceptionScoringPlugin: spawning " << this->
name << std::endl;
142 gzmsg <<
"PerceptionScoringPlugin: despawning " << this->
name << std::endl;
150 gzmsg <<
"PerceptionScoringPlugin loaded" << std::endl;
160 sdf::ElementPtr _sdf)
165 this->world = _world;
168 if (_sdf->HasElement(
"loop_forever"))
170 sdf::ElementPtr loopElem = _sdf->GetElement(
"loop_forever");
171 this->loopForever = loopElem->Get<
bool>();
174 if (_sdf->HasElement(
"frame"))
176 this->frameName = _sdf->Get<std::string>(
"frame");
179 if (!_sdf->HasElement(
"object_sequence"))
181 gzerr <<
"PerceptionScoringPlugin: Unable to find <object_sequence> " 186 sdf::ElementPtr sequence = _sdf->GetElement(
"object_sequence");
188 sdf::ElementPtr objectElem = NULL;
189 if (sequence->HasElement(
"object"))
191 objectElem = sequence->GetElement(
"object");
197 if (!objectElem->HasElement(
"time"))
199 gzerr <<
"PerceptionScoringPlugin: Unable to find <time> in object\n";
200 objectElem = objectElem->GetNextElement(
"object");
203 sdf::ElementPtr timeElement = objectElem->GetElement(
"time");
204 double time = timeElement->Get<
double>();
207 if (objectElem->HasElement(
"duration"))
209 sdf::ElementPtr durationElement = objectElem->GetElement(
"duration");
210 duration = durationElement->Get<
double>();
214 if (!objectElem->HasElement(
"type"))
216 gzerr <<
"PerceptionScoringPlugin: Unable to find <type> in object.\n";
217 objectElem = objectElem->GetNextElement(
"object");
220 sdf::ElementPtr typeElement = objectElem->GetElement(
"type");
221 std::string
type = typeElement->Get<std::string>();
224 if (!objectElem->HasElement(
"name"))
226 gzerr <<
"PerceptionScoringPlugin: Unable to find <name> in object.\n";
227 objectElem = objectElem->GetNextElement(
"object");
230 sdf::ElementPtr nameElement = objectElem->GetElement(
"name");
231 std::string
name = nameElement->Get<std::string>();
234 if (!objectElem->HasElement(
"pose"))
236 gzerr <<
"PerceptionScoringPlugin: Unable to find <pose> in object.\n";
237 objectElem = objectElem->GetNextElement(
"object");
240 sdf::ElementPtr poseElement = objectElem->GetElement(
"pose");
241 ignition::math::Pose3d pose = poseElement->Get<ignition::math::Pose3d>();
245 this->objects.push_back(obj);
247 objectElem = objectElem->GetNextElement(
"object");
250 #if GAZEBO_MAJOR_VERSION >= 8 251 this->lastUpdateTime = this->world->SimTime();
253 this->lastUpdateTime = this->world->GetSimTime();
257 if (_sdf->HasElement(
"robot_namespace"))
258 this->ns = _sdf->GetElement(
"robot_namespace")->Get<std::string>();
261 this->objectTopic =
"/vrx/perception/landmark";
262 if (_sdf->HasElement(
"landmark_topic"))
264 this->objectTopic = _sdf->GetElement(
"landmark_topic")->Get<std::string>();
269 this->connection = gazebo::event::Events::ConnectWorldUpdateEnd(
276 for (
auto& obj : this->objects)
281 #if GAZEBO_MAJOR_VERSION >= 8 282 obj.time += this->world->SimTime().Double();
284 obj.time += this->world->GetSimTime().Double();
287 gzmsg <<
"Object population restarted" << std::endl;
294 if (!this->frameName.empty())
297 #if GAZEBO_MAJOR_VERSION >= 8 299 this->world->EntityByName(this->frameName);
302 this->world->GetEntity(this->frameName);
307 gzwarn << std::string(
"The frame '") << this->frameName
308 <<
"' does not exist" << std::endl;
311 if (!this->frame->HasType(gazebo::physics::Base::LINK) &&
312 !this->frame->HasType(gazebo::physics::Base::MODEL))
314 gzwarn <<
"'frame' tag must list the name of a link or model" 320 for (
auto& obj : this->objects)
323 if (this->ElapsedTime() > obj.time &&
324 this->ElapsedTime() < obj.time + obj.duration &&
328 this->attemptBal += 1;
329 obj.StartTrial(this->frame);
331 "New Attempt Balance: %d", this->attemptBal);
334 if (this->ElapsedTime() > obj.time + obj.duration &&
338 if (this->attemptBal > 0)
339 this->attemptBal -= 1;
341 this->objectsDespawned += 1;
345 this->SetScore(this->Score() + obj.error);
348 "New Attempt Balance: %d", this->attemptBal);
352 if (this->objectsDespawned == this->objects.size() &&
353 this->TaskState() !=
"finished")
356 for (
auto& obj : this->objects)
362 this->SetScore(this->Score() / this->objects.size());
366 if (this->loopForever)
368 this->objectsDespawned = 0;
380 const geographic_msgs::GeoPoseStamped::ConstPtr &_msg)
383 if (this->attemptBal == 0)
386 "Attempt Balance is 0, no attempts currently allowed. Ignoring.");
392 this->attemptBal -= 1;
394 "New Attempt Balance: %d", this->attemptBal);
396 for (
auto& obj : this->objects)
399 if (obj.type == _msg->header.frame_id)
406 ignition::math::Vector3d scVec(_msg->pose.position.latitude,
407 _msg->pose.position.longitude, 0);
408 #if GAZEBO_MAJOR_VERSION >= 8 409 ignition::math::Vector3d cartVec =
410 this->world->SphericalCoords()->LocalFromSpherical(scVec);
412 ignition::math::Vector3d cartVec =
413 this->world->GetSphericalCoordinates()->LocalFromSpherical(scVec);
416 #if GAZEBO_MAJOR_VERSION >= 8 417 ignition::math::Pose3d truePose = obj.modelPtr->WorldPose();
419 ignition::math::Pose3d truePose = obj.modelPtr->GetWorldPose().Ign();
422 obj.SetError(
sqrt(
pow(cartVec.X() - truePose.Pos().X(), 2)+
423 pow(cartVec.Y() - truePose.Pos().Y(), 2)));
431 gzmsg <<
"OnRunning" << std::endl;
441 this->objectSub = this->nh.subscribe(this->objectTopic, 1,
#define ROS_INFO_NAMED(name,...)
std::string name
PerceptionObject type.
#define ROS_WARN_NAMED(name,...)
GZ_REGISTER_WORLD_PLUGIN(UsvWindPlugin)
virtual void Load(gazebo::physics::WorldPtr _world, sdf::ElementPtr _sdf)
void OnUpdate()
Update the plugin.
#define ign_math_vector3d_zero
std::string type
PerceptionObject type.
ROSCPP_DECL bool isInitialized()
void Restart()
Restart the object population list.
void SetError(const double &_error)
set the error of this boject if this object is active and this is the lowest seen error ...
A plugin that allows models to be spawned at a given location in a specific simulation time and then ...
void Load(gazebo::physics::WorldPtr _world, sdf::ElementPtr _sdf)
double duration
amount of time in which the object should be spawned.
virtual ~PerceptionScoringPlugin()
Destructor.
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
void EndTrial()
move the object back to its original location and make inactive
Class to store information about each object to be populated.
PerceptionScoringPlugin()
Constructor.
INLINE Rall1d< T, V, S > pow(const Rall1d< T, V, S > &arg, double m)
double error
error associated with the guess of a moel
void OnAttempt(const geographic_msgs::GeoPoseStamped::ConstPtr &_msg)
void StartTrial(const gazebo::physics::EntityPtr &_frame)
move the object to where it is supposed to be relative to the frame
ignition::math::Pose3d trialPose
Pose in which the object should be placed in wam-v's frame.
void ReleaseVehicle() override
Tries to release the vehicle in case is locked.
double time
Simulation time in which the object should be spawned.
void OnRunning() override
Callback executed when the task state transition into "running".
gazebo::physics::EntityPtr modelPtr
ModelPtr to the model that this object is representing.
PerceptionObject(const double &_time, const double &_duration, const std::string &_type, const std::string &_name, const ignition::math::Pose3d &_trialPose, const gazebo::physics::WorldPtr _world)
constructor of perception object
ignition::math::Pose3d origPose
Pose in which the object should be placed in global frame.
bool active
bool to tell weather or not the object is open for attempts