calibration_queue.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2021, Qiayuan Liao
5  * All rights reserved.
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  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * 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  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without 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
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *******************************************************************************/
33 
34 //
35 // Created by qiayuan on 5/27/21.
36 //
37 
38 #pragma once
39 
42 
43 namespace rm_common
44 {
46 {
47 public:
49  {
50  ROS_ASSERT(rpc_value.hasMember("start_controllers"));
51  ROS_ASSERT(rpc_value.hasMember("stop_controllers"));
52  ROS_ASSERT(rpc_value.hasMember("services_name"));
53  ROS_ASSERT(rpc_value["start_controllers"].getType() == XmlRpc::XmlRpcValue::TypeArray);
54  ROS_ASSERT(rpc_value["stop_controllers"].getType() == XmlRpc::XmlRpcValue::TypeArray);
55  ROS_ASSERT(rpc_value["services_name"].getType() == XmlRpc::XmlRpcValue::TypeArray);
56  start_controllers = getControllersName(rpc_value["start_controllers"]);
57  stop_controllers = getControllersName(rpc_value["stop_controllers"]);
58  for (int i = 0; i < rpc_value["services_name"].size(); ++i)
59  {
60  query_services.push_back(new QueryCalibrationServiceCaller(nh, rpc_value["services_name"][i]));
61  }
62  }
63  void setCalibratedFalse()
64  {
65  for (auto& service : query_services)
66  service->getService().response.is_calibrated = false;
67  }
68  bool isCalibrated()
69  {
70  bool is_calibrated = true;
71  for (auto& service : query_services)
72  is_calibrated &= service->isCalibrated();
73  return is_calibrated;
74  }
75  void callService()
76  {
77  for (auto& service : query_services)
78  service->callService();
79  }
80  std::vector<std::string> start_controllers, stop_controllers;
81  std::vector<QueryCalibrationServiceCaller*> query_services;
82 
83 private:
84  static std::vector<std::string> getControllersName(XmlRpc::XmlRpcValue& rpc_value)
85  {
86  std::vector<std::string> controllers;
87  for (int i = 0; i < rpc_value.size(); ++i)
88  {
89  controllers.push_back(rpc_value[i]);
90  }
91  return controllers;
92  }
93 };
94 
95 class CalibrationQueue
96 {
97 public:
98  explicit CalibrationQueue(XmlRpc::XmlRpcValue& rpc_value, ros::NodeHandle& nh, ControllerManager& controller_manager)
99  : controller_manager_(controller_manager), switched_(false)
100  {
101  // Don't calibration if using simulation
102  ros::NodeHandle nh_global;
103  bool use_sim_time;
104  nh_global.param("use_sim_time", use_sim_time, false);
105  if (use_sim_time || rpc_value.getType() != XmlRpc::XmlRpcValue::TypeArray)
106  return;
107  for (int i = 0; i < rpc_value.size(); ++i)
108  calibration_services_.emplace_back(rpc_value[i], nh);
111  // Start with calibrated, you should use reset() to start calibration.
112  }
113  void reset()
114  {
115  if (calibration_services_.empty())
116  return;
118  switched_ = false;
119  for (auto service : calibration_services_)
120  service.setCalibratedFalse();
121  }
122  void update(const ros::Time& time, bool flip_controllers)
123  {
124  if (calibration_services_.empty())
125  return;
127  return;
128  if (switched_)
129  {
130  if (calibration_itr_->isCalibrated())
131  {
132  if (flip_controllers)
136  switched_ = false;
137  }
138  else if ((time - last_query_).toSec() > .2)
139  {
140  last_query_ = time;
141  calibration_itr_->callService();
142  }
143  }
144  else
145  {
146  // Switch controllers
147  switched_ = true;
149  {
152  }
153  }
154  }
155  void update(const ros::Time& time)
156  {
157  update(time, true);
158  }
159  bool isCalibrated()
160  {
161  return calibration_itr_ == calibration_services_.end();
162  }
163  void stopController()
164  {
165  if (calibration_services_.empty())
166  return;
169  }
170  void stop()
171  {
172  if (switched_)
173  {
175  switched_ = false;
176  }
177  }
178 
179 private:
181  std::vector<CalibrationService> calibration_services_;
182  std::vector<CalibrationService>::iterator calibration_itr_;
183  ControllerManager& controller_manager_;
184  bool switched_;
185 };
186 } // namespace rm_common
XmlRpc::XmlRpcValue::size
int size() const
rm_common::CalibrationService::isCalibrated
bool isCalibrated()
Definition: calibration_queue.h:130
rm_common::CalibrationQueue::update
void update(const ros::Time &time, bool flip_controllers)
Definition: calibration_queue.h:153
rm_common::CalibrationQueue::stop
void stop()
Definition: calibration_queue.h:201
rm_common::CalibrationQueue::calibration_services_
std::vector< CalibrationService > calibration_services_
Definition: calibration_queue.h:212
rm_common::CalibrationQueue::switched_
bool switched_
Definition: calibration_queue.h:215
rm_common::CalibrationQueue::CalibrationQueue
CalibrationQueue(XmlRpc::XmlRpcValue &rpc_value, ros::NodeHandle &nh, ControllerManager &controller_manager)
Definition: calibration_queue.h:129
rm_common::CalibrationQueue::reset
void reset()
Definition: calibration_queue.h:144
rm_common::CalibrationQueue::last_query_
ros::Time last_query_
Definition: calibration_queue.h:211
rm_common::CalibrationService::stop_controllers
std::vector< std::string > stop_controllers
Definition: calibration_queue.h:142
rm_common::CalibrationQueue::controller_manager_
ControllerManager & controller_manager_
Definition: calibration_queue.h:214
rm_common::QueryCalibrationServiceCaller
Definition: service_caller.h:192
rm_common::CalibrationQueue::isCalibrated
bool isCalibrated()
Definition: calibration_queue.h:190
service_caller.h
rm_common::ControllerManager::stopControllers
void stopControllers(const std::vector< std::string > &controllers)
Definition: controller_manager.h:189
rm_common::CalibrationService::CalibrationService
CalibrationService(XmlRpc::XmlRpcValue &rpc_value, ros::NodeHandle &nh)
Definition: calibration_queue.h:110
rm_common::CalibrationService::query_services
std::vector< QueryCalibrationServiceCaller * > query_services
Definition: calibration_queue.h:143
rm_common
Definition: calibration_queue.h:43
rm_common::ControllerManager::startControllers
void startControllers(const std::vector< std::string > &controllers)
Definition: controller_manager.h:184
XmlRpc::XmlRpcValue::getType
const Type & getType() const
rm_common::CalibrationService::getControllersName
static std::vector< std::string > getControllersName(XmlRpc::XmlRpcValue &rpc_value)
Definition: calibration_queue.h:146
XmlRpc::XmlRpcValue::TypeArray
TypeArray
XmlRpc::XmlRpcValue::hasMember
bool hasMember(const std::string &name) const
rm_common::CalibrationQueue::calibration_itr_
std::vector< CalibrationService >::iterator calibration_itr_
Definition: calibration_queue.h:213
ros::Time
rm_common::CalibrationService::callService
void callService()
Definition: calibration_queue.h:137
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
controller_manager.h
rm_common::CalibrationService
Definition: calibration_queue.h:76
ROS_ASSERT
#define ROS_ASSERT(cond)
rm_common::CalibrationQueue::stopController
void stopController()
Definition: calibration_queue.h:194
XmlRpc::XmlRpcValue
rm_common::CalibrationService::setCalibratedFalse
void setCalibratedFalse()
Definition: calibration_queue.h:125
ros::NodeHandle
ros::Time::now
static Time now()
rm_common::CalibrationService::start_controllers
std::vector< std::string > start_controllers
Definition: calibration_queue.h:142


rm_common
Author(s):
autogenerated on Sun Apr 6 2025 02:22:01