Skip to content

Commit 42ef992

Browse files
authored
Merge pull request #158 from liyixin135/Gimbal_cascade_pid
Gimbal cascade pid
2 parents 6b89a28 + 3cdb8d3 commit 42ef992

File tree

7 files changed

+203
-59
lines changed

7 files changed

+203
-59
lines changed

rm_gimbal_controllers/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ find_package(catkin REQUIRED
2424
visualization_msgs
2525
dynamic_reconfigure
2626
angles
27-
)
27+
)
2828

2929
generate_dynamic_reconfigure_options(
3030
cfg/BulletSolver.cfg
31+
cfg/GimbalBase.cfg
3132
)
3233

3334
###################################

rm_gimbal_controllers/cfg/BulletSolver.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ gen.add("g", double_t, 0, "Air resistance divided by mass", 9.8, 9.6, 10.0)
1414
gen.add("delay", double_t, 0, "Delay of bullet firing", 0.0, 0, 0.5)
1515
gen.add("dt", double_t, 0, "Iteration interval", 0.01, 0.0001, 0.1)
1616
gen.add("timeout", double_t, 0, "Flight time exceeded", 2.0, 0.5, 3.0)
17+
gen.add("angle1", double_t, 0, "Min switch angle", 15.0, 0.0, 90.0)
18+
gen.add("angle2", double_t, 0, "Min switch angle", 15.0, 0.0, 90.0)
1719

1820
exit(gen.generate(PACKAGE, "bullet_solver", "BulletSolver"))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python
2+
PACKAGE = "rm_gimbal_controllers"
3+
4+
from dynamic_reconfigure.parameter_generator_catkin import *
5+
6+
gen = ParameterGenerator()
7+
8+
gen.add("yaw_k_v_", double_t, 0, "Pid pitch position max output", 0.0, 0, 1.0)
9+
gen.add("pitch_k_v_", double_t, 0, "Pid pitch position max output", 0.0, 0, 1.0)
10+
11+
exit(gen.generate(PACKAGE, "gimbal_base", "GimbalBase"))

rm_gimbal_controllers/include/rm_gimbal_controllers/bullet_solver.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@
4646
#include <rm_common/hardware_interface/robot_state_interface.h>
4747
#include <rm_common/eigen_types.h>
4848
#include <rm_common/ros_utilities.h>
49+
#include <rm_msgs/GimbalDesError.h>
4950

5051
namespace rm_gimbal_controllers
5152
{
5253
struct Config
5354
{
5455
double resistance_coff_qd_10, resistance_coff_qd_15, resistance_coff_qd_16, resistance_coff_qd_18,
55-
resistance_coff_qd_30, g, delay, dt, timeout;
56+
resistance_coff_qd_30, g, delay, dt, timeout, angle1, angle2;
5657
};
5758

5859
class BulletSolver
@@ -76,13 +77,15 @@ class BulletSolver
7677
void getSelectedArmorPosAndVel(geometry_msgs::Point& armor_pos, geometry_msgs::Vector3& armor_vel,
7778
geometry_msgs::Point pos, geometry_msgs::Vector3 vel, double yaw, double v_yaw,
7879
double r1, double r2, double dz, int armors_num);
80+
void isShootAfterDelay(const ros::Time& time);
7981
void bulletModelPub(const geometry_msgs::TransformStamped& odom2pitch, const ros::Time& time);
8082
void reconfigCB(rm_gimbal_controllers::BulletSolverConfig& config, uint32_t);
8183
~BulletSolver() = default;
8284

8385
private:
8486
std::shared_ptr<realtime_tools::RealtimePublisher<visualization_msgs::Marker>> path_desire_pub_;
8587
std::shared_ptr<realtime_tools::RealtimePublisher<visualization_msgs::Marker>> path_real_pub_;
88+
std::shared_ptr<realtime_tools::RealtimePublisher<rm_msgs::GimbalDesError>> is_shoot_after_delay_pub_;
8689
realtime_tools::RealtimeBuffer<Config> config_rt_buffer_;
8790
dynamic_reconfigure::Server<rm_gimbal_controllers::BulletSolverConfig>* d_srv_{};
8891
Config config_{};
@@ -92,6 +95,7 @@ class BulletSolver
9295
double bullet_speed_{}, resistance_coff_{};
9396
int selected_armor_;
9497
bool track_target_;
98+
double is_shoot_after_delay_ = 1.;
9599

96100
geometry_msgs::Point target_pos_{};
97101
double fly_time_;

rm_gimbal_controllers/include/rm_gimbal_controllers/gimbal_base.h

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,32 @@
3838
#pragma once
3939

4040
#include <effort_controllers/joint_position_controller.h>
41+
#include <effort_controllers/joint_velocity_controller.h>
4142
#include <controller_interface/multi_interface_controller.h>
4243
#include <hardware_interface/joint_command_interface.h>
43-
#include <rm_common/hardware_interface/robot_state_interface.h>
4444
#include <hardware_interface/imu_sensor_interface.h>
45-
#include <realtime_tools/realtime_publisher.h>
45+
#include <rm_common/hardware_interface/robot_state_interface.h>
46+
#include <rm_common/filters/filters.h>
4647
#include <rm_msgs/GimbalCmd.h>
4748
#include <rm_msgs/TrackData.h>
4849
#include <rm_msgs/GimbalDesError.h>
49-
#include <dynamic_reconfigure/server.h>
50+
#include <rm_gimbal_controllers/GimbalBaseConfig.h>
5051
#include <rm_gimbal_controllers/bullet_solver.h>
5152
#include <tf2_eigen/tf2_eigen.h>
5253
#include <Eigen/Eigen>
53-
#include <rm_common/filters/filters.h>
54+
#include <control_toolbox/pid.h>
55+
#include <urdf/model.h>
56+
#include <dynamic_reconfigure/server.h>
57+
#include <realtime_tools/realtime_publisher.h>
5458

5559
namespace rm_gimbal_controllers
5660
{
61+
struct GimbalConfig
62+
{
63+
// Input feedforward
64+
double yaw_k_v_, pitch_k_v_;
65+
};
66+
5767
class ChassisVel
5868
{
5969
public:
@@ -139,40 +149,42 @@ class Controller : public controller_interface::MultiInterfaceController<rm_cont
139149
void updateChassisVel();
140150
void commandCB(const rm_msgs::GimbalCmdConstPtr& msg);
141151
void trackCB(const rm_msgs::TrackDataConstPtr& msg);
152+
void reconfigCB(rm_gimbal_controllers::GimbalBaseConfig& config, uint32_t);
142153

143154
rm_control::RobotStateHandle robot_state_handle_;
144155
hardware_interface::ImuSensorHandle imu_sensor_handle_;
145156
bool has_imu_ = true;
146-
effort_controllers::JointPositionController ctrl_yaw_, ctrl_pitch_;
157+
effort_controllers::JointVelocityController ctrl_yaw_, ctrl_pitch_;
158+
control_toolbox::Pid pid_yaw_pos_, pid_pitch_pos_;
147159

148160
std::shared_ptr<BulletSolver> bullet_solver_;
149161

150162
// ROS Interface
151163
ros::Time last_publish_time_{};
164+
std::unique_ptr<realtime_tools::RealtimePublisher<control_msgs::JointControllerState>> pid_yaw_pos_state_pub_,
165+
pid_pitch_pos_state_pub_;
152166
std::shared_ptr<realtime_tools::RealtimePublisher<rm_msgs::GimbalDesError>> error_pub_;
153167
ros::Subscriber cmd_gimbal_sub_;
154168
ros::Subscriber data_track_sub_;
155169
realtime_tools::RealtimeBuffer<rm_msgs::GimbalCmd> cmd_rt_buffer_;
156170
realtime_tools::RealtimeBuffer<rm_msgs::TrackData> track_rt_buffer_;
171+
urdf::JointConstSharedPtr pitch_joint_urdf_, yaw_joint_urdf_;
157172

158173
rm_msgs::GimbalCmd cmd_gimbal_;
159174
rm_msgs::TrackData data_track_;
160175
std::string gimbal_des_frame_id_{}, imu_name_{};
161176
double publish_rate_{};
162177
bool state_changed_{};
178+
int loop_count_{};
163179

164180
// Transform
165-
geometry_msgs::TransformStamped odom2gimbal_des_, odom2pitch_, odom2base_, last_odom2base_;
181+
geometry_msgs::TransformStamped odom2gimbal_des_, odom2pitch_, odom2base_, last_odom2base_, last_odom2gimbal_des_;
166182

167183
// Gravity Compensation
168184
geometry_msgs::Vector3 mass_origin_;
169185
double gravity_;
170186
bool enable_gravity_compensation_;
171187

172-
// Input feedforward
173-
double yaw_k_v_;
174-
double pitch_k_v_;
175-
176188
// Resistance compensation
177189
double yaw_resistance_;
178190
double velocity_saturation_point_, effort_saturation_point_;
@@ -181,6 +193,11 @@ class Controller : public controller_interface::MultiInterfaceController<rm_cont
181193
double k_chassis_vel_;
182194
std::shared_ptr<ChassisVel> chassis_vel_;
183195

196+
bool dynamic_reconfig_initialized_{};
197+
GimbalConfig config_{};
198+
realtime_tools::RealtimeBuffer<GimbalConfig> config_rt_buffer_;
199+
dynamic_reconfigure::Server<rm_gimbal_controllers::GimbalBaseConfig>* d_srv_{};
200+
184201
enum
185202
{
186203
RATE,

rm_gimbal_controllers/src/bullet_solver.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ BulletSolver::BulletSolver(ros::NodeHandle& controller_nh)
5252
.g = getParam(controller_nh, "g", 0.),
5353
.delay = getParam(controller_nh, "delay", 0.),
5454
.dt = getParam(controller_nh, "dt", 0.),
55-
.timeout = getParam(controller_nh, "timeout", 0.) };
55+
.timeout = getParam(controller_nh, "timeout", 0.),
56+
.angle1 = getParam(controller_nh, "angle1", 15.),
57+
.angle2 = getParam(controller_nh, "angle2", 15.) };
5658
max_track_target_vel_ = getParam(controller_nh, "max_track_target_vel", 5.0);
5759
config_rt_buffer_.initRT(config_);
5860

@@ -80,6 +82,7 @@ BulletSolver::BulletSolver(ros::NodeHandle& controller_nh)
8082
new realtime_tools::RealtimePublisher<visualization_msgs::Marker>(controller_nh, "model_desire", 10));
8183
path_real_pub_.reset(
8284
new realtime_tools::RealtimePublisher<visualization_msgs::Marker>(controller_nh, "model_real", 10));
85+
is_shoot_after_delay_pub_.reset(new realtime_tools::RealtimePublisher<rm_msgs::GimbalDesError>(controller_nh, "allow_shoot", 10));
8386
}
8487

8588
double BulletSolver::getResistanceCoefficient(double bullet_speed) const
@@ -116,10 +119,17 @@ bool BulletSolver::solve(geometry_msgs::Point pos, geometry_msgs::Vector3 vel, d
116119
double r = r1;
117120
double z = pos.z;
118121
track_target_ = std::abs(v_yaw) < max_track_target_vel_;
119-
double switch_armor_angle = track_target_ ?
120-
acos(r / target_rho) - M_PI / 12 +
121-
(-acos(r / target_rho) + M_PI / 6) * std::abs(v_yaw) / max_track_target_vel_ :
122-
M_PI / 12;
122+
config_.angle1 = config_.angle1 * M_PI / 180;
123+
config_.angle2 = config_.angle2 * M_PI / 180;
124+
double switch_armor_angle = track_target_ ? acos(r / target_rho) - config_.angle1 +
125+
(-acos(r / target_rho) + config_.angle1 + config_.angle2) *
126+
std::abs(v_yaw) / max_track_target_vel_ :
127+
config_.angle2;
128+
is_shoot_after_delay_ = 1.;
129+
if (((((yaw + v_yaw * (rough_fly_time + config_.delay)) > output_yaw_ + switch_armor_angle) && v_yaw > 0.) ||
130+
(((yaw + v_yaw * (rough_fly_time + config_.delay)) < output_yaw_ - switch_armor_angle) && v_yaw < 0.)) &&
131+
track_target_)
132+
is_shoot_after_delay_ = 0.;
123133
if ((((yaw + v_yaw * rough_fly_time) > output_yaw_ + switch_armor_angle) && v_yaw > 0.) ||
124134
(((yaw + v_yaw * rough_fly_time) < output_yaw_ - switch_armor_angle) && v_yaw < 0.))
125135
{
@@ -210,6 +220,16 @@ void BulletSolver::getSelectedArmorPosAndVel(geometry_msgs::Point& armor_pos, ge
210220
}
211221
}
212222

223+
void BulletSolver::isShootAfterDelay(const ros::Time& time)
224+
{
225+
if (is_shoot_after_delay_pub_->trylock())
226+
{
227+
is_shoot_after_delay_pub_->msg_.stamp = time;
228+
is_shoot_after_delay_pub_->msg_.error = is_shoot_after_delay_;
229+
is_shoot_after_delay_pub_->unlockAndPublish();
230+
}
231+
}
232+
213233
void BulletSolver::bulletModelPub(const geometry_msgs::TransformStamped& odom2pitch, const ros::Time& time)
214234
{
215235
marker_desire_.points.clear();
@@ -316,6 +336,8 @@ void BulletSolver::reconfigCB(rm_gimbal_controllers::BulletSolverConfig& config,
316336
config.delay = init_config.delay;
317337
config.dt = init_config.dt;
318338
config.timeout = init_config.timeout;
339+
config.angle1 = init_config.angle1;
340+
config.angle2 = init_config.angle2;
319341
dynamic_reconfig_initialized_ = true;
320342
}
321343
Config config_non_rt{ .resistance_coff_qd_10 = config.resistance_coff_qd_10,
@@ -326,7 +348,9 @@ void BulletSolver::reconfigCB(rm_gimbal_controllers::BulletSolverConfig& config,
326348
.g = config.g,
327349
.delay = config.delay,
328350
.dt = config.dt,
329-
.timeout = config.timeout };
351+
.timeout = config.timeout,
352+
.angle1 = config.angle1,
353+
.angle2 = config.angle2 };
330354
config_rt_buffer_.writeFromNonRT(config_non_rt);
331355
}
332356
} // namespace rm_gimbal_controllers

0 commit comments

Comments
 (0)