World_services.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2023 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
8  +-------------------------------------------------------------------------+ */
9 
10 #include <mrpt/core/lock_helper.h>
11 #include <mvsim/World.h>
12 
13 #if MVSIM_HAS_ZMQ && MVSIM_HAS_PROTOBUF
14 #include <mvsim/mvsim-msgs/GenericAnswer.pb.h>
15 #include <mvsim/mvsim-msgs/SrvGetPose.pb.h>
16 #include <mvsim/mvsim-msgs/SrvGetPoseAnswer.pb.h>
17 #include <mvsim/mvsim-msgs/SrvSetControllerTwist.pb.h>
18 #include <mvsim/mvsim-msgs/SrvSetControllerTwistAnswer.pb.h>
19 #include <mvsim/mvsim-msgs/SrvSetPose.pb.h>
20 #include <mvsim/mvsim-msgs/SrvSetPoseAnswer.pb.h>
21 #include <mvsim/mvsim-msgs/SrvShutdown.pb.h>
22 #include <mvsim/mvsim-msgs/SrvShutdownAnswer.pb.h>
23 #endif
24 
25 #include <algorithm> // count()
26 #include <map>
27 #include <stdexcept>
28 
29 using namespace mvsim;
30 
31 #if MVSIM_HAS_ZMQ && MVSIM_HAS_PROTOBUF
32 
33 mvsim_msgs::SrvSetPoseAnswer World::srv_set_pose(
34  const mvsim_msgs::SrvSetPose& req)
35 {
36  mvsim_msgs::SrvSetPoseAnswer ans;
37  ans.set_objectisincollision(false);
38 
39  const auto sId = req.objectid();
40 
41  auto lckListObjs = mrpt::lockHelper(getListOfSimulableObjectsMtx());
42  if (auto itV = simulableObjects_.find(sId); itV != simulableObjects_.end())
43  {
44  if (req.has_relativeincrement() && req.relativeincrement())
45  {
46  auto p = mrpt::poses::CPose3D(itV->second->getPose());
47  p = p + mrpt::poses::CPose3D(
48  req.pose().x(), req.pose().y(), req.pose().z(),
49  req.pose().yaw(), req.pose().pitch(),
50  req.pose().roll());
51  itV->second->setPose(p.asTPose());
52 
53  auto* absPose = ans.mutable_objectglobalpose();
54  absPose->set_x(p.x());
55  absPose->set_y(p.y());
56  absPose->set_z(p.z());
57  absPose->set_yaw(p.yaw());
58  absPose->set_pitch(p.pitch());
59  absPose->set_roll(p.roll());
60  }
61  else
62  {
63  itV->second->setPose(
64  {req.pose().x(), req.pose().y(), req.pose().z(),
65  req.pose().yaw(), req.pose().pitch(), req.pose().roll()});
66  }
67  ans.set_success(true);
68  ans.set_objectisincollision(itV->second->hadCollision());
69  itV->second->resetCollisionFlag();
70  }
71  else
72  {
73  ans.set_success(false);
74  }
75  return ans;
76 }
77 
78 mvsim_msgs::SrvGetPoseAnswer World::srv_get_pose(
79  const mvsim_msgs::SrvGetPose& req)
80 {
81  auto lckCopy = mrpt::lockHelper(copy_of_objects_dynstate_mtx_);
82 
83  mvsim_msgs::SrvGetPoseAnswer ans;
84  const auto sId = req.objectid();
85  ans.set_objectisincollision(false);
86 
87  if (auto itV = copy_of_objects_dynstate_pose_.find(sId);
88  itV != copy_of_objects_dynstate_pose_.end())
89  {
90  ans.set_success(true);
91  const mrpt::math::TPose3D p = itV->second;
92  auto* po = ans.mutable_pose();
93  po->set_x(p.x);
94  po->set_y(p.y);
95  po->set_z(p.z);
96  po->set_yaw(p.yaw);
97  po->set_pitch(p.pitch);
98  po->set_roll(p.roll);
99 
100  const auto t = copy_of_objects_dynstate_twist_.at(sId);
101  auto* tw = ans.mutable_twist();
102  tw->set_vx(t.vx);
103  tw->set_vy(t.vy);
104  tw->set_vz(0);
105  tw->set_wx(0);
106  tw->set_wy(0);
107  tw->set_wz(t.omega);
108 
109  ans.set_objectisincollision(
110  copy_of_objects_had_collision_.count(sId) != 0);
111  }
112  else
113  {
114  ans.set_success(false);
115  }
116 
117  lckCopy.unlock();
118 
119  {
120  const auto lckPhys = mrpt::lockHelper(reset_collision_flags_mtx_);
121  reset_collision_flags_.insert(sId);
122  }
123  return ans;
124 }
125 
126 mvsim_msgs::SrvSetControllerTwistAnswer World::srv_set_controller_twist(
127  const mvsim_msgs::SrvSetControllerTwist& req)
128 {
129  std::lock_guard<std::mutex> lck(simulationStepRunningMtx_);
130 
131  mvsim_msgs::SrvSetControllerTwistAnswer ans;
132  ans.set_success(false);
133 
134  const auto sId = req.objectid();
135 
136  auto lckListObjs = mrpt::lockHelper(getListOfSimulableObjectsMtx());
137 
138  auto itV = simulableObjects_.find(sId);
139  if (itV == simulableObjects_.end())
140  {
141  ans.set_errormessage("objectId not found");
142  return ans;
143  }
144 
145  auto veh = std::dynamic_pointer_cast<VehicleBase>(itV->second);
146  if (!veh)
147  {
148  ans.set_errormessage("objectId is not of VehicleBase type");
149  return ans;
150  }
151 
152  mvsim::ControllerBaseInterface* controller = veh->getControllerInterface();
153  if (!controller)
154  {
155  ans.set_errormessage(
156  "objectId vehicle seems not to have any controller");
157  return ans;
158  }
159 
160  const mrpt::math::TTwist2D t(
161  req.twistsetpoint().vx(), req.twistsetpoint().vy(),
162  req.twistsetpoint().wz());
163 
164  const bool ctrlAcceptTwist = controller->setTwistCommand(t);
165  if (!ctrlAcceptTwist)
166  {
167  ans.set_errormessage(
168  "objectId vehicle controller did not accept the twist "
169  "command");
170  return ans;
171  }
172 
173  ans.set_success(true);
174  return ans;
175 }
176 
177 mvsim_msgs::SrvShutdownAnswer World::srv_shutdown(
178  [[maybe_unused]] const mvsim_msgs::SrvShutdown& req)
179 {
180  mvsim_msgs::SrvShutdownAnswer ans;
181  ans.set_accepted(true);
182 
183  this->simulator_must_close(true);
184 
185  return ans;
186 }
187 
188 #endif // MVSIM_HAS_ZMQ && MVSIM_HAS_PROTOBUF
189 
191 {
192 #if MVSIM_HAS_ZMQ && MVSIM_HAS_PROTOBUF
193  // global services:
194  client_
195  .advertiseService<mvsim_msgs::SrvSetPose, mvsim_msgs::SrvSetPoseAnswer>(
196  "set_pose", [this](const auto& req) { return srv_set_pose(req); });
197 
198  client_
199  .advertiseService<mvsim_msgs::SrvGetPose, mvsim_msgs::SrvGetPoseAnswer>(
200  "get_pose", [this](const auto& req) { return srv_get_pose(req); });
201 
203  mvsim_msgs::SrvSetControllerTwist,
204  mvsim_msgs::SrvSetControllerTwistAnswer>(
205  "set_controller_twist",
206  [this](const auto& req) { return srv_set_controller_twist(req); });
207 
209  mvsim_msgs::SrvShutdown, mvsim_msgs::SrvShutdownAnswer>(
210  "shutdown", [this](const auto& req) { return srv_shutdown(req); });
211 
212 #endif
213 }
bool simulator_must_close() const
Definition: World.h:242
SimulableList simulableObjects_
Definition: World.h:564
virtual bool setTwistCommand([[maybe_unused]] const mrpt::math::TTwist2D &t)
std::mutex simulationStepRunningMtx_
Definition: World.h:570
geometry_msgs::TransformStamped t
void advertiseService(const std::string &serviceName, const std::function< OUTPUT_MSG_T(const INPUT_MSG_T &)> &callback)
Definition: Client.h:193
auto & getListOfSimulableObjectsMtx()
Definition: World.h:318
std::recursive_mutex copy_of_objects_dynstate_mtx_
Definition: World.h:625
void internal_advertiseServices()
std::map< std::string, mrpt::math::TPose3D > copy_of_objects_dynstate_pose_
Updated in internal_one_step()
Definition: World.h:622
std::set< std::string > copy_of_objects_had_collision_
Definition: World.h:624
std::mutex reset_collision_flags_mtx_
Definition: World.h:628
std::map< std::string, mrpt::math::TTwist2D > copy_of_objects_dynstate_twist_
Definition: World.h:623
mvsim::Client client_
Definition: World.h:400
std::set< std::string > reset_collision_flags_
Definition: World.h:627


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:22