dynamics_handlers.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Roboception GmbH
3  * All rights reserved
4  *
5  * Author: Heiko Hirschmueller, Christian Emmerich
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its contributors
18  * may be used to endorse or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "device_nodelet.h"
35 
36 #include <rc_common_msgs/ReturnCodeConstants.h>
37 
38 namespace rc
39 {
40 namespace rcd = dynamics;
41 using rc_common_msgs::ReturnCodeConstants;
42 
43 // Anonymous namespace for local linkage
44 namespace
45 {
47 enum class DynamicsCmd
48 {
49  START = 0,
50  START_SLAM,
51  STOP,
52  STOP_SLAM,
53  RESTART,
54  RESTART_SLAM,
55  RESET_SLAM
56 };
57 
59 void handleDynamicsStateChangeRequest(rcd::RemoteInterface::Ptr dynIF, DynamicsCmd state,
60  rc_common_msgs::Trigger::Response& resp)
61 {
62  resp.return_code.value = ReturnCodeConstants::SUCCESS;
63  resp.return_code.message = "";
64 
65  std::string new_state;
66 
67  if (dynIF)
68  {
69  try
70  {
71  switch (state)
72  {
73  case DynamicsCmd::STOP:
74  new_state = dynIF->stop();
75  break;
76  case DynamicsCmd::STOP_SLAM:
77  new_state = dynIF->stopSlam();
78  break;
79  case DynamicsCmd::START:
80  new_state = dynIF->start();
81  break;
82  case DynamicsCmd::START_SLAM:
83  new_state = dynIF->startSlam();
84  break;
85  case DynamicsCmd::RESTART_SLAM:
86  new_state = dynIF->restartSlam();
87  break;
88  case DynamicsCmd::RESTART:
89  new_state = dynIF->restart();
90  break;
91  case DynamicsCmd::RESET_SLAM:
92  new_state = dynIF->resetSlam();
93  break;
94  default:
95  throw std::runtime_error("handleDynamicsStateChangeRequest: unrecognized state change request");
96  }
97  if (new_state == rcd::RemoteInterface::State::FATAL)
98  {
99  resp.return_code.value = ReturnCodeConstants::NOT_APPLICABLE;
100  resp.return_code.message = "rc_dynamics module is in " + new_state + " state. Check the log files.";
101  }
102  }
103  catch (std::exception& e)
104  {
105  resp.return_code.value = ReturnCodeConstants::INTERNAL_ERROR;
106  resp.return_code.message = std::string("Failed to change state of rcdynamics module: ") + e.what();
107  }
108  }
109  else
110  {
111  resp.return_code.value = ReturnCodeConstants::NOT_APPLICABLE;
112  resp.return_code.message = "rcdynamics remote interface not yet initialized!";
113  }
114 
115  std::stringstream msg;
116  msg << "rc_visard_driver: dynamics state change request returned with code: "
117  << resp.return_code.value << " msg: " << resp.return_code.message;
118  ROS_INFO_STREAM_COND(resp.return_code.value > ReturnCodeConstants::SUCCESS, msg.str());
119  ROS_ERROR_STREAM_COND(resp.return_code.value < ReturnCodeConstants::SUCCESS, msg.str());
120 }
121 }
122 
123 bool DeviceNodelet::dynamicsStart(rc_common_msgs::Trigger::Request& req, rc_common_msgs::Trigger::Response& resp)
124 {
125  handleDynamicsStateChangeRequest(dynamicsInterface, DynamicsCmd::START, resp);
126  return true;
127 }
128 
129 bool DeviceNodelet::dynamicsStartSlam(rc_common_msgs::Trigger::Request& req, rc_common_msgs::Trigger::Response& resp)
130 {
131  handleDynamicsStateChangeRequest(dynamicsInterface, DynamicsCmd::START_SLAM, resp);
132  return true;
133 }
134 
135 bool DeviceNodelet::dynamicsRestart(rc_common_msgs::Trigger::Request& req, rc_common_msgs::Trigger::Response& resp)
136 {
137  handleDynamicsStateChangeRequest(dynamicsInterface, DynamicsCmd::RESTART, resp);
138  return true;
139 }
140 
141 bool DeviceNodelet::dynamicsRestartSlam(rc_common_msgs::Trigger::Request& req, rc_common_msgs::Trigger::Response& resp)
142 {
143  handleDynamicsStateChangeRequest(dynamicsInterface, DynamicsCmd::RESTART_SLAM, resp);
144  return true;
145 }
146 
147 bool DeviceNodelet::dynamicsStop(rc_common_msgs::Trigger::Request& req, rc_common_msgs::Trigger::Response& resp)
148 {
149  handleDynamicsStateChangeRequest(dynamicsInterface, DynamicsCmd::STOP, resp);
150  return true;
151 }
152 
153 bool DeviceNodelet::dynamicsStopSlam(rc_common_msgs::Trigger::Request& req, rc_common_msgs::Trigger::Response& resp)
154 {
155  handleDynamicsStateChangeRequest(dynamicsInterface, DynamicsCmd::STOP_SLAM, resp);
156  return true;
157 }
158 
159 bool DeviceNodelet::dynamicsResetSlam(rc_common_msgs::Trigger::Request& req, rc_common_msgs::Trigger::Response& resp)
160 {
161  handleDynamicsStateChangeRequest(dynamicsInterface, DynamicsCmd::RESET_SLAM, resp);
162  return true;
163 }
164 
165 bool DeviceNodelet::getSlamTrajectory(rc_visard_driver::GetTrajectory::Request& req,
166  rc_visard_driver::GetTrajectory::Response& resp)
167 {
168  TrajectoryTime start(req.start_time.sec, req.start_time.nsec, req.start_time_relative);
169  TrajectoryTime end(req.end_time.sec, req.end_time.nsec, req.end_time_relative);
170 
171  auto pbTraj = dynamicsInterface->getSlamTrajectory(start, end);
172  resp.trajectory.header.frame_id = pbTraj.parent();
173  resp.trajectory.header.stamp.sec = pbTraj.timestamp().sec();
174  resp.trajectory.header.stamp.nsec = pbTraj.timestamp().nsec();
175 
176  for (auto pbPose : pbTraj.poses())
177  {
178  geometry_msgs::PoseStamped rosPose;
179  rosPose.header.frame_id = pbTraj.parent();
180  rosPose.header.stamp.sec = pbPose.timestamp().sec();
181  rosPose.header.stamp.nsec = pbPose.timestamp().nsec();
182  rosPose.pose.position.x = pbPose.pose().position().x();
183  rosPose.pose.position.y = pbPose.pose().position().y();
184  rosPose.pose.position.z = pbPose.pose().position().z();
185  rosPose.pose.orientation.x = pbPose.pose().orientation().x();
186  rosPose.pose.orientation.y = pbPose.pose().orientation().y();
187  rosPose.pose.orientation.z = pbPose.pose().orientation().z();
188  rosPose.pose.orientation.w = pbPose.pose().orientation().w();
189  resp.trajectory.poses.push_back(rosPose);
190  }
191 
192  // additionally publish extracted trajectory on topic
193  if (autopublishTrajectory)
194  {
195  trajPublisher.publish(resp.trajectory);
196  }
197 
198  return true;
199 }
200 
201 bool DeviceNodelet::saveSlamMap(rc_common_msgs::Trigger::Request& req, rc_common_msgs::Trigger::Response& resp)
202 {
203  if (dynamicsInterface)
204  {
205  try
206  {
207  rcd::RemoteInterface::ReturnCode rc = dynamicsInterface->saveSlamMap();
208  resp.return_code.value = rc.value;
209  resp.return_code.message = rc.message;
210  }
211  catch (std::exception& e)
212  {
213  resp.return_code.value = ReturnCodeConstants::INTERNAL_ERROR;
214  resp.return_code.message = std::string("Failed to save SLAM map: ") + e.what();
215  }
216  }
217  else
218  {
219  resp.return_code.value = ReturnCodeConstants::NOT_APPLICABLE;
220  resp.return_code.message = "rcdynamics remote interface not yet initialized!";
221  }
222 
223  std::stringstream msg;
224  msg << "rc_visard_driver: save slam map request returned with code: "
225  << resp.return_code.value << " msg: " << resp.return_code.message;
226  ROS_INFO_STREAM_COND(resp.return_code.value > ReturnCodeConstants::SUCCESS, msg.str());
227  ROS_ERROR_STREAM_COND(resp.return_code.value < ReturnCodeConstants::SUCCESS, msg.str());
228 
229  return true;
230 }
231 
232 bool DeviceNodelet::loadSlamMap(rc_common_msgs::Trigger::Request& req, rc_common_msgs::Trigger::Response& resp)
233 {
234  if (dynamicsInterface)
235  {
236  try
237  {
238  rcd::RemoteInterface::ReturnCode rc = dynamicsInterface->loadSlamMap();
239  resp.return_code.value = rc.value;
240  resp.return_code.message = rc.message;
241  }
242  catch (std::exception& e)
243  {
244  resp.return_code.value = ReturnCodeConstants::INTERNAL_ERROR;
245  resp.return_code.message = std::string("Failed to load SLAM map: ") + e.what();
246  }
247  }
248  else
249  {
250  resp.return_code.value = ReturnCodeConstants::NOT_APPLICABLE;
251  resp.return_code.message = "rcdynamics remote interface not yet initialized!";
252  }
253 
254  std::stringstream msg;
255  msg << "rc_visard_driver: load slam map request returned with code: "
256  << resp.return_code.value << " msg: " << resp.return_code.message;
257  ROS_INFO_STREAM_COND(resp.return_code.value > ReturnCodeConstants::SUCCESS, msg.str());
258  ROS_ERROR_STREAM_COND(resp.return_code.value < ReturnCodeConstants::SUCCESS, msg.str());
259 
260  return true;
261 }
262 
263 bool DeviceNodelet::removeSlamMap(rc_common_msgs::Trigger::Request& req, rc_common_msgs::Trigger::Response& resp)
264 {
265  if (dynamicsInterface)
266  {
267  try
268  {
269  rcd::RemoteInterface::ReturnCode rc = dynamicsInterface->removeSlamMap();
270  resp.return_code.value = rc.value;
271  resp.return_code.message = rc.message;
272  }
273  catch (std::exception& e)
274  {
275  resp.return_code.value = ReturnCodeConstants::INTERNAL_ERROR;
276  resp.return_code.message = std::string("Failed to remove SLAM map: ") + e.what();
277  }
278  }
279  else
280  {
281  resp.return_code.value = ReturnCodeConstants::NOT_APPLICABLE;
282  resp.return_code.message = "rcdynamics remote interface not yet initialized!";
283  }
284 
285  std::stringstream msg;
286  msg << "rc_visard_driver: remove slam map request returned with code: "
287  << resp.return_code.value << " msg: " << resp.return_code.message;
288  ROS_INFO_STREAM_COND(resp.return_code.value > ReturnCodeConstants::SUCCESS, msg.str());
289  ROS_ERROR_STREAM_COND(resp.return_code.value < ReturnCodeConstants::SUCCESS, msg.str());
290 
291  return true;
292 }
293 
294 } // namespace rc
msg
bool dynamicsStart(rc_common_msgs::Trigger::Request &req, rc_common_msgs::Trigger::Response &resp)
Start Stereo INS.
bool dynamicsStopSlam(rc_common_msgs::Trigger::Request &req, rc_common_msgs::Trigger::Response &resp)
Stop SLAM (keep Stereo INS running)
bool getSlamTrajectory(rc_visard_driver::GetTrajectory::Request &req, rc_visard_driver::GetTrajectory::Response &resp)
Get the Slam trajectory.
#define ROS_ERROR_STREAM_COND(cond, args)
bool dynamicsStartSlam(rc_common_msgs::Trigger::Request &req, rc_common_msgs::Trigger::Response &resp)
Start Stereo INS+SLAM.
DynamicsCmd
Commands taken by handleDynamicsStateChangeRequest()
bool dynamicsRestart(rc_common_msgs::Trigger::Request &req, rc_common_msgs::Trigger::Response &resp)
Restart Stereo INS.
bool removeSlamMap(rc_common_msgs::Trigger::Request &req, rc_common_msgs::Trigger::Response &resp)
Remove the onboard SLAM map.
#define ROS_INFO_STREAM_COND(cond, args)
bool dynamicsResetSlam(rc_common_msgs::Trigger::Request &req, rc_common_msgs::Trigger::Response &resp)
Reset SLAM (keep Stereo INS running)
bool dynamicsStop(rc_common_msgs::Trigger::Request &req, rc_common_msgs::Trigger::Response &resp)
Stop Stereo INS(+SLAM if running)
bool dynamicsRestartSlam(rc_common_msgs::Trigger::Request &req, rc_common_msgs::Trigger::Response &resp)
Restart Stereo INS+SLAM.
bool saveSlamMap(rc_common_msgs::Trigger::Request &req, rc_common_msgs::Trigger::Response &resp)
Save the onboard SLAM map.
bool loadSlamMap(rc_common_msgs::Trigger::Request &req, rc_common_msgs::Trigger::Response &resp)
Load the onboard SLAM map.


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Sat Feb 13 2021 03:42:55