rm_control
Loading...
Searching...
No Matches
controller_manager.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 astro on 2021/5/15.
36//
37#pragma once
38
40
41#include <algorithm>
42
43#include <ros/ros.h>
44#include <controller_manager_msgs/LoadController.h>
45
46namespace rm_common
47{
49{
50public:
51 explicit ControllerManager(ros::NodeHandle& nh)
52 : load_client_(nh.serviceClient<controller_manager_msgs::LoadController>("/controller_manager/load_controller"))
53 , switch_caller_(nh)
54 {
55 if (!nh.hasParam("controllers_list"))
56 ROS_ERROR("No controllers defined");
57 ROS_INFO("Waiting for load_controller service...");
58 load_client_.waitForExistence();
59 ros::NodeHandle nh_list(nh, "controllers_list");
60 XmlRpc::XmlRpcValue controllers;
61 if (nh_list.getParam("state_controllers", controllers))
62 for (int i = 0; i < controllers.size(); ++i)
63 {
64 state_controllers_.push_back(controllers[i]);
65 loadController(controllers[i]);
66 }
67 if (nh_list.getParam("main_controllers", controllers))
68 for (int i = 0; i < controllers.size(); ++i)
69 {
70 main_controllers_.push_back(controllers[i]);
71 loadController(controllers[i]);
72 }
73 if (nh_list.getParam("calibration_controllers", controllers))
74 for (int i = 0; i < controllers.size(); ++i)
75 {
76 calibration_controllers_.push_back(controllers[i]);
77 loadController(controllers[i]);
78 }
79 }
80 void update()
81 {
82 if (!switch_caller_.isCalling())
83 {
84 switch_caller_.startControllers(start_buffer_);
85 switch_caller_.stopControllers(stop_buffer_);
86 if (!start_buffer_.empty() || !stop_buffer_.empty())
87 {
88 switch_caller_.callService();
89 start_buffer_.clear();
90 stop_buffer_.clear();
91 }
92 }
93 }
94 void loadController(const std::string& controller)
95 {
96 controller_manager_msgs::LoadController load_controller;
97 load_controller.request.name = controller;
98 load_client_.call(load_controller);
99 if (load_controller.response.ok)
100 ROS_INFO("Loaded %s", controller.c_str());
101 else
102 ROS_ERROR("Fail to load %s", controller.c_str());
103 }
104 void startController(const std::string& controller)
105 {
106 if (std::find(start_buffer_.begin(), start_buffer_.end(), controller) == start_buffer_.end())
107 start_buffer_.push_back(controller);
108 // AVoid setting controller to start and stop in the same time
109 auto item = std::find(stop_buffer_.begin(), stop_buffer_.end(), controller);
110 if (item != stop_buffer_.end())
111 stop_buffer_.erase(item);
112 }
113 void stopController(const std::string& controller)
114 {
115 if (std::find(stop_buffer_.begin(), stop_buffer_.end(), controller) == stop_buffer_.end())
116 stop_buffer_.push_back(controller);
117 // AVoid setting controller to start and stop in the same time
118 auto item = std::find(start_buffer_.begin(), start_buffer_.end(), controller);
119 if (item != start_buffer_.end())
120 start_buffer_.erase(item);
121 }
122 void startControllers(const std::vector<std::string>& controllers)
123 {
124 for (const auto& controller : controllers)
125 startController(controller);
126 }
127 void stopControllers(const std::vector<std::string>& controllers)
128 {
129 for (const auto& controller : controllers)
130 stopController(controller);
131 }
133 {
134 startControllers(state_controllers_);
135 }
137 {
138 startControllers(main_controllers_);
139 }
141 {
142 stopControllers(main_controllers_);
143 }
145 {
146 startControllers(calibration_controllers_);
147 }
149 {
150 stopControllers(calibration_controllers_);
151 }
153 {
154 return switch_caller_.isCalling();
155 }
156
157private:
158 ros::ServiceClient load_client_;
159 std::vector<std::string> state_controllers_, main_controllers_, calibration_controllers_;
160 std::vector<std::string> start_buffer_, stop_buffer_;
161 SwitchControllersServiceCaller switch_caller_;
162};
163
164} // namespace rm_common
Definition controller_manager.h:49
void stopCalibrationControllers()
Definition controller_manager.h:148
void loadController(const std::string &controller)
Definition controller_manager.h:94
void stopMainControllers()
Definition controller_manager.h:140
void startCalibrationControllers()
Definition controller_manager.h:144
ControllerManager(ros::NodeHandle &nh)
Definition controller_manager.h:51
void startController(const std::string &controller)
Definition controller_manager.h:104
void update()
Definition controller_manager.h:80
void startStateControllers()
Definition controller_manager.h:132
bool isCalling()
Definition controller_manager.h:152
void startControllers(const std::vector< std::string > &controllers)
Definition controller_manager.h:122
void startMainControllers()
Definition controller_manager.h:136
void stopController(const std::string &controller)
Definition controller_manager.h:113
void stopControllers(const std::vector< std::string > &controllers)
Definition controller_manager.h:127
bool isCalling()
Definition service_caller.h:102
void callService()
Definition service_caller.h:91
Definition service_caller.h:137
void startControllers(const std::vector< std::string > &controllers)
Definition service_caller.h:145
void stopControllers(const std::vector< std::string > &controllers)
Definition service_caller.h:149
Definition calibration_queue.h:44