rm_controllers
Loading...
Searching...
No Matches
chassis_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 huakang on 2021/3/21.
36//
37
38#pragma once
39
40#include <controller_interface/multi_interface_controller.h>
41#include <hardware_interface/joint_command_interface.h>
42#include <rm_common/hardware_interface/robot_state_interface.h>
43#include <realtime_tools/realtime_publisher.h>
44#include <rm_common/filters/filters.h>
45#include <effort_controllers/joint_velocity_controller.h>
46#include <rm_msgs/ChassisCmd.h>
47#include <geometry_msgs/TwistStamped.h>
48#include <geometry_msgs/Vector3Stamped.h>
49#include <nav_msgs/Odometry.h>
50#include <tf2_geometry_msgs/tf2_geometry_msgs.h>
51#include <rm_chassis_controllers/PowerLimitConfig.h>
52#include <rm_common/ros_utilities.h>
53#include <rm_common/math_utilities.h>
54#include <rm_common/ori_tool.h>
55#include <tf2_geometry_msgs/tf2_geometry_msgs.h>
56#include <angles/angles.h>
57#include <hardware_interface/imu_sensor_interface.h>
58#include <cmath>
59#include <string>
60#include <std_msgs/Float64.h>
61#include <rm_msgs/PowerManagementSampleAndStatusData.h>
62
64{
65struct Command
66{
67 geometry_msgs::Twist cmd_vel_;
68 rm_msgs::ChassisCmd cmd_chassis_;
69 ros::Time stamp_;
70};
71
73{
74 double vel_coeff{};
75 double effort_coeff{};
76 double power_offset{};
77 double max_power{};
78 double power_in[4]{};
79 double cmd_power{};
81 double power_limit[4]{};
82 double ratio{ 1.0 };
83 double err[4]{};
84 double err_upper{};
85 double err_lower{};
86 double err_sum{};
87 double omiga[4]{};
88 double torque[4]{};
89 double K{};
90 double K_angle[4]{};
91};
92
93template <typename... T>
94class ChassisBase : public controller_interface::MultiInterfaceController<T...>
95{
96public:
97 ChassisBase() = default;
109 bool init(hardware_interface::RobotHW* robot_hw, ros::NodeHandle& root_nh, ros::NodeHandle& controller_nh) override;
121 void update(const ros::Time& time, const ros::Duration& period) override;
122
123protected:
128 void raw();
136 virtual void follow(const ros::Time& time, const ros::Duration& period);
144 void twist(const ros::Time& time, const ros::Duration& period);
145 virtual void moveJoint(const ros::Time& time, const ros::Duration& period) = 0;
146 virtual geometry_msgs::Twist odometry() = 0;
152 void updateOdom(const ros::Time& time, const ros::Duration& period);
155 void recovery();
160 void tfVelToBase(const std::string& from);
165 virtual void powerLimit();
166 virtual void updatePowerStatus();
171 void cmdChassisCallback(const rm_msgs::ChassisCmdConstPtr& msg);
176 void cmdVelCallback(const geometry_msgs::Twist::ConstPtr& msg);
177
178 void initialize_parameters(ros::NodeHandle& controller_nh);
179 void slamCallback(const nav_msgs::Odometry::ConstPtr& msg);
180 void localizationCallback(const geometry_msgs::TransformStamped::ConstPtr& msg);
181 void capacityCallback(const rm_msgs::PowerManagementSampleAndStatusData::ConstPtr& msg);
182
183 rm_control::RobotStateHandle robot_state_handle_{};
184 hardware_interface::EffortJointInterface* effort_joint_interface_{};
185 std::vector<hardware_interface::JointHandle> joint_handles_{};
186 realtime_tools::RealtimeBuffer<Command> cmd_rt_buffer_{};
187 realtime_tools::RealtimeBuffer<nav_msgs::Odometry> slam_rt_buffer_{};
188 realtime_tools::RealtimeBuffer<geometry_msgs::TransformStamped> localization_rt_buffer_{};
189 std::unique_ptr<realtime_tools::RealtimePublisher<nav_msgs::Odometry>> odom_pub_;
190 std::unique_ptr<realtime_tools::RealtimePublisher<std_msgs::Float64>> cpower_pub_; // command power publisher
191 std::unique_ptr<realtime_tools::RealtimePublisher<std_msgs::Float64>> epower_pub_; // estimated power publisher
192 std::unique_ptr<realtime_tools::RealtimePublisher<std_msgs::Float64>> chassis_power_pub_; // chassis power publisher
193
194 rm_common::TfRtBroadcaster brcst4global_map2robot_odom_{};
195 rm_common::TfRtBroadcaster brcst4global_map2camera_init_{};
196 rm_common::TfRtBroadcaster brcst4robot_odom2robot_base_{};
197
198 geometry_msgs::TransformStamped global_map2robot_odom_{};
199 geometry_msgs::TransformStamped global_map2camera_init_{};
200 geometry_msgs::TransformStamped robot_odom2robot_base_{};
201 geometry_msgs::TransformStamped robot_base2lidar_base_{};
202
203 tf2::Transform T_global_map2robot_odom_{};
205 tf2::Transform T_lidar_odom2lidar_base_{};
206 tf2::Transform T_robot_base2lidar_base_{};
207 tf2::Transform T_global_map2lidar_odom_{};
208
209 ros::Subscriber cmd_vel_sub_;
210 ros::Subscriber cmd_chassis_sub_;
211 ros::Subscriber slam_sub_;
212 ros::Subscriber localization_sub_;
213 ros::Subscriber capacity_sub_;
214
215 std::unique_ptr<RampFilter<double>> ramp_x_{ nullptr };
216 std::unique_ptr<RampFilter<double>> ramp_y_{ nullptr };
217 std::unique_ptr<RampFilter<double>> ramp_w_{ nullptr };
218
219 double publish_rate_{ 100.0 };
220 bool publish_map_tf_{ false };
221 bool publish_odom_tf_{ false };
223
224 double roll_{ 0.0 };
225 double pitch_{ 0.0 };
226 double yaw_{ 0.0 };
227 double wheel_radius_{ 0.02 };
228 double twist_angular_{ M_PI / 6 };
229 double max_odom_vel_{ 10.0 };
230 double timeout_{ 0.1 };
231 double chassis_power_{ 0.0 };
233 bool use_rls_{ false };
234 bool use_K_angle_{ false };
235
236 bool odom_initialized_{ false };
237 bool slam_updated_{ false };
239 bool state_changed_{ true };
240 int state_{ RAW };
241
242 std::string follow_source_frame_{};
244 std::string global_map_frame_id_{ "map" };
245 std::string robot_odom_frame_id_{ "odom" };
246 std::string robot_base_frame_id_{ "base_link" };
247 std::string lidar_base_frame_id_{ "livox_frame" };
248 std::string slam_topic_{ "/slam/odometry" };
249 std::string localization_topic_{ "/shinji/result" };
250
251 std::string capacity_topic_{ "/rm_referee/power_management/sample_and_status" };
252
254 geometry_msgs::Vector3 vel_cmd_{}; // x, y
255 std::array<double, 36> twist_covariance_{};
256 control_toolbox::Pid pid_follow_{};
257
260
261 enum
262 {
265 TWIST
266 };
267};
268
269} // namespace rm_chassis_controllers
Definition chassis_base.h:95
std::unique_ptr< realtime_tools::RealtimePublisher< nav_msgs::Odometry > > odom_pub_
Definition chassis_base.h:189
double roll_
Definition chassis_base.h:224
double publish_rate_
Definition chassis_base.h:219
@ FOLLOW
Definition chassis_base.h:264
@ RAW
Definition chassis_base.h:263
@ TWIST
Definition chassis_base.h:265
double timeout_
Definition chassis_base.h:230
double max_odom_vel_
Definition chassis_base.h:229
ros::Subscriber cmd_chassis_sub_
Definition chassis_base.h:210
tf2::Transform T_robot_odom_2robot_base_
Definition chassis_base.h:204
virtual void updatePowerStatus()
Definition chassis_base.cpp:472
double pitch_
Definition chassis_base.h:225
geometry_msgs::TransformStamped robot_base2lidar_base_
Definition chassis_base.h:201
geometry_msgs::TransformStamped robot_odom2robot_base_
Definition chassis_base.h:200
bool odom_initialized_
Definition chassis_base.h:236
double yaw_
Definition chassis_base.h:226
double twist_angular_
Definition chassis_base.h:228
virtual geometry_msgs::Twist odometry()=0
std::string lidar_base_frame_id_
Definition chassis_base.h:247
bool publish_map_tf_
Definition chassis_base.h:220
std::unique_ptr< RampFilter< double > > ramp_w_
Definition chassis_base.h:217
geometry_msgs::TransformStamped global_map2robot_odom_
Definition chassis_base.h:198
tf2::Transform T_robot_base2lidar_base_
Definition chassis_base.h:206
ros::Subscriber capacity_sub_
Definition chassis_base.h:213
std::string robot_base_frame_id_
Definition chassis_base.h:246
std::string capacity_topic_
Definition chassis_base.h:251
control_toolbox::Pid pid_follow_
Definition chassis_base.h:256
double chassis_power_
Definition chassis_base.h:231
std::string command_source_frame_
Definition chassis_base.h:243
void updateOdom(const ros::Time &time, const ros::Duration &period)
Init frame on base_link. Integral vel to current_pos_ and angle.
Definition chassis_base.cpp:297
void localizationCallback(const geometry_msgs::TransformStamped::ConstPtr &msg)
Definition chassis_base.cpp:512
std::string localization_topic_
Definition chassis_base.h:249
virtual void powerLimit()
To limit the chassis power according to current power limit.
Definition chassis_base.cpp:467
void tfVelToBase(const std::string &from)
Transform tf velocity to base link frame.
Definition chassis_base.cpp:477
ros::Time last_publish_time_
Definition chassis_base.h:253
std::string slam_topic_
Definition chassis_base.h:248
bool slam_updated_
Definition chassis_base.h:237
bool gravity_estimation_offset_
Definition chassis_base.h:222
rm_common::TfRtBroadcaster brcst4robot_odom2robot_base_
Definition chassis_base.h:196
void recovery()
Set chassis velocity to zero.
Definition chassis_base.cpp:459
bool publish_odom_tf_
Definition chassis_base.h:221
void update(const ros::Time &time, const ros::Duration &period) override
Receive real_time command from manual. Execute different action according to current mode....
Definition chassis_base.cpp:153
bool use_K_angle_
Definition chassis_base.h:234
void cmdChassisCallback(const rm_msgs::ChassisCmdConstPtr &msg)
Write current command from rm_msgs::ChassisCmd.
Definition chassis_base.cpp:490
rm_common::TfRtBroadcaster brcst4global_map2robot_odom_
Definition chassis_base.h:194
virtual void follow(const ros::Time &time, const ros::Duration &period)
The mode FOLLOW: chassis will follow gimbal.
Definition chassis_base.cpp:214
bool localization_updated_
Definition chassis_base.h:238
double wheel_radius_
Definition chassis_base.h:227
tf2::Transform T_global_map2robot_odom_
Definition chassis_base.h:203
tf2::Transform T_global_map2lidar_odom_
Definition chassis_base.h:207
void raw()
Definition chassis_base.cpp:284
bool use_rls_
Definition chassis_base.h:233
realtime_tools::RealtimeBuffer< geometry_msgs::TransformStamped > localization_rt_buffer_
Definition chassis_base.h:188
bool capacity_update_flag_
Definition chassis_base.h:232
void slamCallback(const nav_msgs::Odometry::ConstPtr &msg)
Definition chassis_base.cpp:505
PowerLimitor wheel_power_limitor_
Definition chassis_base.h:259
rm_common::TfRtBroadcaster brcst4global_map2camera_init_
Definition chassis_base.h:195
tf2::Transform T_lidar_odom2lidar_base_
Definition chassis_base.h:205
void initialize_parameters(ros::NodeHandle &controller_nh)
Definition chassis_base.cpp:46
std::string follow_source_frame_
Definition chassis_base.h:242
rm_control::RobotStateHandle robot_state_handle_
Definition chassis_base.h:183
std::unique_ptr< realtime_tools::RealtimePublisher< std_msgs::Float64 > > cpower_pub_
Definition chassis_base.h:190
hardware_interface::EffortJointInterface * effort_joint_interface_
Definition chassis_base.h:184
std::vector< hardware_interface::JointHandle > joint_handles_
Definition chassis_base.h:185
int state_
Definition chassis_base.h:240
std::unique_ptr< realtime_tools::RealtimePublisher< std_msgs::Float64 > > epower_pub_
Definition chassis_base.h:191
std::array< double, 36 > twist_covariance_
Definition chassis_base.h:255
std::unique_ptr< realtime_tools::RealtimePublisher< std_msgs::Float64 > > chassis_power_pub_
Definition chassis_base.h:192
ros::Subscriber localization_sub_
Definition chassis_base.h:212
void capacityCallback(const rm_msgs::PowerManagementSampleAndStatusData::ConstPtr &msg)
Definition chassis_base.cpp:519
geometry_msgs::Vector3 vel_cmd_
Definition chassis_base.h:254
Command cmd_struct_
Definition chassis_base.h:258
geometry_msgs::TransformStamped global_map2camera_init_
Definition chassis_base.h:199
std::string robot_odom_frame_id_
Definition chassis_base.h:245
std::string global_map_frame_id_
Definition chassis_base.h:244
realtime_tools::RealtimeBuffer< nav_msgs::Odometry > slam_rt_buffer_
Definition chassis_base.h:187
ros::Subscriber cmd_vel_sub_
Definition chassis_base.h:209
realtime_tools::RealtimeBuffer< Command > cmd_rt_buffer_
Definition chassis_base.h:186
void twist(const ros::Time &time, const ros::Duration &period)
The mode TWIST: Just moving chassis.
Definition chassis_base.cpp:243
virtual void moveJoint(const ros::Time &time, const ros::Duration &period)=0
bool init(hardware_interface::RobotHW *robot_hw, ros::NodeHandle &root_nh, ros::NodeHandle &controller_nh) override
Get and check params for covariances. Setup odometry realtime publisher + odom message constant field...
Definition chassis_base.cpp:70
void cmdVelCallback(const geometry_msgs::Twist::ConstPtr &msg)
Write current command from geometry_msgs::Twist.
Definition chassis_base.cpp:497
std::unique_ptr< RampFilter< double > > ramp_y_
Definition chassis_base.h:216
ros::Subscriber slam_sub_
Definition chassis_base.h:211
std::unique_ptr< RampFilter< double > > ramp_x_
Definition chassis_base.h:215
bool state_changed_
Definition chassis_base.h:239
Definition active_suspension.h:16
Definition chassis_base.h:66
rm_msgs::ChassisCmd cmd_chassis_
Definition chassis_base.h:68
geometry_msgs::Twist cmd_vel_
Definition chassis_base.h:67
ros::Time stamp_
Definition chassis_base.h:69
Definition chassis_base.h:73
double err_lower
Definition chassis_base.h:85
double ratio
Definition chassis_base.h:82
double omiga[4]
Definition chassis_base.h:87
double err_sum
Definition chassis_base.h:86
double cmd_power
Definition chassis_base.h:79
double err[4]
Definition chassis_base.h:83
double power_in[4]
Definition chassis_base.h:78
double power_limit[4]
Definition chassis_base.h:81
double torque[4]
Definition chassis_base.h:88
double power_offset
Definition chassis_base.h:76
double effort_coeff
Definition chassis_base.h:75
double K
Definition chassis_base.h:89
double K_angle[4]
Definition chassis_base.h:90
double estimated_power
Definition chassis_base.h:80
double vel_coeff
Definition chassis_base.h:74
double max_power
Definition chassis_base.h:77
double err_upper
Definition chassis_base.h:84