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


mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:08