rm_controllers
Loading...
Searching...
No Matches
gimbal_base.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 1/16/21.
36//
37
38#pragma once
39
40#include <effort_controllers/joint_velocity_controller.h>
41#include <controller_interface/multi_interface_controller.h>
42#include <hardware_interface/joint_command_interface.h>
43#include <hardware_interface/imu_sensor_interface.h>
44#include <rm_common/hardware_interface/robot_state_interface.h>
45#include <rm_common/filters/filters.h>
46#include <rm_msgs/GimbalCmd.h>
47#include <rm_msgs/TrackData.h>
48#include <rm_msgs/GimbalDesError.h>
49#include <rm_msgs/GimbalPosState.h>
50#include <rm_gimbal_controllers/GimbalBaseConfig.h>
53#include <tf2_eigen/tf2_eigen.h>
54#include <Eigen/Eigen>
55#include <control_toolbox/pid.h>
56#include <urdf/model.h>
57#include <dynamic_reconfigure/server.h>
58#include <realtime_tools/realtime_publisher.h>
59#include <unordered_map>
60#include <nav_msgs/Odometry.h>
61#include <tf2_geometry_msgs/tf2_geometry_msgs.h>
62#include <std_msgs/Float32MultiArray.h>
63
65{
71
73{
74public:
75 explicit ChassisVel(const ros::NodeHandle& nh)
76 {
77 double num_data;
78 nh.param("num_data", num_data, 20.0);
79 nh.param("debug", is_debug_, true);
80 linear_ = std::make_shared<Vector3WithFilter<double>>(num_data);
81 angular_ = std::make_shared<Vector3WithFilter<double>>(num_data);
82 if (is_debug_)
83 {
84 real_pub_.reset(new realtime_tools::RealtimePublisher<geometry_msgs::Twist>(nh, "real", 1));
85 filtered_pub_.reset(new realtime_tools::RealtimePublisher<geometry_msgs::Twist>(nh, "filtered", 1));
86 }
87 }
88 std::shared_ptr<Vector3WithFilter<double>> linear_;
89 std::shared_ptr<Vector3WithFilter<double>> angular_;
90 void update(double linear_vel[3], double angular_vel[3], double period)
91 {
92 if (period < 0)
93 return;
94 if (period > 0.1)
95 {
96 linear_->clear();
97 angular_->clear();
98 }
99 linear_->input(linear_vel);
100 angular_->input(angular_vel);
101 if (is_debug_ && loop_count_ % 10 == 0)
102 {
103 if (real_pub_->trylock())
104 {
105 real_pub_->msg_.linear.x = linear_vel[0];
106 real_pub_->msg_.linear.y = linear_vel[1];
107 real_pub_->msg_.linear.z = linear_vel[2];
108 real_pub_->msg_.angular.x = angular_vel[0];
109 real_pub_->msg_.angular.y = angular_vel[1];
110 real_pub_->msg_.angular.z = angular_vel[2];
111
112 real_pub_->unlockAndPublish();
113 }
114 if (filtered_pub_->trylock())
115 {
116 filtered_pub_->msg_.linear.x = linear_->x();
117 filtered_pub_->msg_.linear.y = linear_->y();
118 filtered_pub_->msg_.linear.z = linear_->z();
119 filtered_pub_->msg_.angular.x = angular_->x();
120 filtered_pub_->msg_.angular.y = angular_->y();
121 filtered_pub_->msg_.angular.z = angular_->z();
122
123 filtered_pub_->unlockAndPublish();
124 }
125 }
126 loop_count_++;
127 }
128
129private:
130 bool is_debug_{};
131 int loop_count_{};
132 std::shared_ptr<realtime_tools::RealtimePublisher<geometry_msgs::Twist>> real_pub_{}, filtered_pub_{};
133};
134
135class Controller : public controller_interface::MultiInterfaceController<rm_control::RobotStateInterface,
136 hardware_interface::ImuSensorInterface,
137 hardware_interface::EffortJointInterface>
138{
139public:
140 Controller() = default;
141 bool init(hardware_interface::RobotHW* robot_hw, ros::NodeHandle& root_nh, ros::NodeHandle& controller_nh) override;
142 void starting(const ros::Time& time) override;
143 void update(const ros::Time& time, const ros::Duration& period) override;
144 void setDes(const ros::Time& time, double yaw_des, double pitch_des, double traject_yaw_des, bool update_yaw = true,
145 bool update_pitch = true);
146
147private:
148 void rate(const ros::Time& time, const ros::Duration& period);
149 void track(const ros::Time& time);
150 void direct(const ros::Time& time);
151 void traj(const ros::Time& time);
152 bool setDesIntoLimit(double& angle, const urdf::JointConstSharedPtr& joint_urdf, bool update, double current_angle);
153 void moveJoint(const ros::Time& time, const ros::Duration& period);
154 void updateChassisVel();
155 void updateBallisticSolution(const ros::Time& time);
156 double gravityFeedForward(const ros::Time& time);
157 double updateCompensation(double chassis_vel_angular_z);
158 void commandCB(const rm_msgs::GimbalCmdConstPtr& msg);
159 void trackCB(const rm_msgs::TrackDataConstPtr& msg);
160 void ballisticSolverRequestCB(const std_msgs::BoolConstPtr& msg);
161 void reconfigCB(rm_gimbal_controllers::GimbalBaseConfig& config, uint32_t);
162 std::string getGimbalFrameID(std::unordered_map<int, urdf::JointConstSharedPtr> joint_urdfs);
163 std::string getBaseFrameID(std::unordered_map<int, urdf::JointConstSharedPtr> joint_urdfs);
164
165 rm_control::RobotStateHandle robot_state_handle_;
166 hardware_interface::ImuSensorHandle imu_sensor_handle_;
167 std::unordered_map<int, std::unique_ptr<effort_controllers::JointVelocityController>> ctrls_;
168 std::unordered_map<int, std::unique_ptr<control_toolbox::Pid>> pid_pos_;
169 std::unordered_map<int, urdf::JointConstSharedPtr> joint_urdfs_;
170 std::unordered_map<int, bool> pos_des_in_limit_;
171 bool has_imu_ = true;
172 double ballistic_yaw_{}, ballistic_pitch_{};
173
174 std::shared_ptr<BulletSolver> bullet_solver_;
175 std::shared_ptr<BallisticSolver> ballistic_solver_;
176
177 // ROS Interface
178 ros::Time last_publish_time_{};
179 ros::Time last_ballistic_publish_time_{};
180 std::unordered_map<int, std::unique_ptr<realtime_tools::RealtimePublisher<rm_msgs::GimbalPosState>>> pos_state_pub_;
181 std::shared_ptr<realtime_tools::RealtimePublisher<rm_msgs::GimbalDesError>> error_pub_;
182 std::shared_ptr<realtime_tools::RealtimePublisher<std_msgs::Float32MultiArray>> ballistic_solution_pub_;
183 ros::Subscriber cmd_gimbal_sub_;
184 ros::Subscriber data_track_sub_;
185 ros::Subscriber ballistic_solver_request_sub_;
186 realtime_tools::RealtimeBuffer<rm_msgs::GimbalCmd> cmd_rt_buffer_;
187 realtime_tools::RealtimeBuffer<rm_msgs::TrackData> track_rt_buffer_;
188 realtime_tools::RealtimeBuffer<std_msgs::Bool> ballistic_track_rt_buffer_;
189
190 rm_msgs::GimbalCmd cmd_gimbal_;
191 rm_msgs::TrackData data_track_;
192 std::string gimbal_des_frame_id_{}, imu_name_{}, gimbal_traject_des_frame_id_;
193 double publish_rate_{};
194 bool state_changed_{};
195 int loop_count_{};
196
197 // Transform
198 geometry_msgs::TransformStamped odom2gimbal_des_, odom2gimbal_, odom2base_, last_odom2base_, base2gimbal_,
199 odom2gimbal_traject_des_;
200
201 // Gravity Compensation
202 geometry_msgs::Vector3 mass_origin_;
203 double gravity_{};
204 bool enable_gravity_compensation_{};
205
206 // Chassis
207 std::shared_ptr<ChassisVel> chassis_vel_;
208 double chassis_compensation_{};
209
210 bool dynamic_reconfig_initialized_{};
211 GimbalConfig config_{};
212 realtime_tools::RealtimeBuffer<GimbalConfig> config_rt_buffer_;
213 dynamic_reconfigure::Server<rm_gimbal_controllers::GimbalBaseConfig>* d_srv_{};
214
215 enum
216 {
217 RATE,
218 TRACK,
219 DIRECT,
220 TRAJ
221 };
222 int state_ = RATE;
223 bool start_ = false;
224 double pos_real[3]{ 0. };
225 double pos_des[3]{ 0. }, vel_des[3]{ 0. }, angle_error[3]{ 0. }, traject_pos_des[3]{ 0. },
226 traject_angle_error[3]{ 0. };
227};
228
229} // namespace rm_gimbal_controllers
Definition gimbal_base.h:73
ChassisVel(const ros::NodeHandle &nh)
Definition gimbal_base.h:75
std::shared_ptr< Vector3WithFilter< double > > angular_
Definition gimbal_base.h:89
void update(double linear_vel[3], double angular_vel[3], double period)
Definition gimbal_base.h:90
std::shared_ptr< Vector3WithFilter< double > > linear_
Definition gimbal_base.h:88
Definition gimbal_base.h:138
void setDes(const ros::Time &time, double yaw_des, double pitch_des, double traject_yaw_des, bool update_yaw=true, bool update_pitch=true)
Definition gimbal_base.cpp:204
bool init(hardware_interface::RobotHW *robot_hw, ros::NodeHandle &root_nh, ros::NodeHandle &controller_nh) override
Definition gimbal_base.cpp:49
void starting(const ros::Time &time) override
Definition gimbal_base.cpp:156
void update(const ros::Time &time, const ros::Duration &period) override
Definition gimbal_base.cpp:163
Definition ballistic_solver.h:22
Definition gimbal_base.h:67
double accel_yaw_
Definition gimbal_base.h:68
double chassis_comp_a_
Definition gimbal_base.h:69
double chassis_comp_c_
Definition gimbal_base.h:69
double chassis_comp_d_
Definition gimbal_base.h:69
double chassis_comp_b_
Definition gimbal_base.h:69
double yaw_k_v_
Definition gimbal_base.h:68
double pitch_k_v_
Definition gimbal_base.h:68
double accel_pitch_
Definition gimbal_base.h:68