Skip to content

Commit e7d00f3

Browse files
committed
add erros topics
1 parent dd688ae commit e7d00f3

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

include/diff_drive_controller/pid_control.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define DEBUG
44

55
#include <ros/ros.h>
6+
#include <std_msgs/Float64.h>
67
#include <nav_msgs/Odometry.h>
78
#include <geometry_msgs/Twist.h>
89
#include <tf/transform_datatypes.h>
@@ -17,6 +18,7 @@ typedef struct
1718
double p_x, i_x, d_x;
1819
double p_yaw, i_yaw, d_yaw;
1920
double x_max_vel, yaw_max_vel;
21+
double dist_error, angle_error;
2022
} PID;
2123

2224
typedef struct
@@ -27,7 +29,6 @@ typedef struct
2729

2830

2931
class PID_Control{
30-
3132
public:
3233

3334
PID_Control(ros::NodeHandle node, ros::NodeHandle private_nh);
@@ -42,7 +43,7 @@ class PID_Control{
4243
void initGoal();
4344

4445
/*---ROS Topics-------------------*/
45-
ros::Publisher _cmd_pub;
46+
ros::Publisher _cmd_pub, _edist_pub, _eangle_pub;
4647
ros::Subscriber _odom_sub;
4748
ros::NodeHandle _node;
4849
ros::ServiceClient _spawn, _dspawn;

src/controller_node.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ int main(int argc, char** argv)
1212
// reset simulation
1313
pid.resetGazebo();
1414

15-
ros::Rate loop_rate(10); //10Hz
15+
ros::Rate loop_rate(5); //5Hz
1616

1717
do
1818
{

src/pid_control.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ PID_Control::PID_Control(ros::NodeHandle node, ros::NodeHandle private_nh)
2929
/*---ROS Topics Init--------------*/
3030
_odom_sub = node.subscribe(_odometry_subscriber, 1, &PID_Control::odomCallback, this);
3131
_cmd_pub = node.advertise<geometry_msgs::Twist>(_cmd_vel_publisher, 10);
32+
_edist_pub = node.advertise<std_msgs::Float64>("/dist_error", 10);
33+
_eangle_pub = node.advertise<std_msgs::Float64>("/angle_error", 10);
34+
35+
/*---ROS Services-----------------*/
3236
ros::service::waitForService("gazebo/spawn_sdf_model");
3337
_spawn = node.serviceClient<gazebo_msgs::SpawnModel>("gazebo/spawn_sdf_model");
3438
_dspawn = node.serviceClient<gazebo_msgs::DeleteModel>("gazebo/delete_model");
@@ -132,9 +136,9 @@ double PID_Control::calculateTargetDistance(const Orientation& pose, const Orien
132136

133137
double PID_Control::linearControl(const Orientation& pose, const Orientation& target)
134138
{
135-
double distance, out_control;
136-
distance = calculateTargetDistance(_pose, _target);
137-
out_control = _pid.p_x * distance;
139+
double out_control;
140+
_pid.dist_error = calculateTargetDistance(_pose, _target);
141+
out_control = _pid.p_x * _pid.dist_error;
138142
if (out_control > _pid.x_max_vel)
139143
{
140144
out_control = _pid.x_max_vel;
@@ -149,7 +153,8 @@ double PID_Control::angularControl(const Orientation& pose, const Orientation& t
149153

150154
goal_angle = atan2(target.y - pose.y, target.x - pose.x);
151155

152-
out_control = _pid.p_yaw * (goal_angle - _pose.yaw);
156+
_pid.angle_error = (goal_angle - _pose.yaw);
157+
out_control = _pid.p_yaw * _pid.angle_error;
153158
if (abs(out_control) > _pid.yaw_max_vel)
154159
{
155160
if (out_control < 0)
@@ -166,6 +171,7 @@ double PID_Control::angularControl(const Orientation& pose, const Orientation& t
166171
bool PID_Control::moveTarget()
167172
{
168173
geometry_msgs::Twist cmd_vel;
174+
std_msgs::Float64 error;
169175
auto target_tol = calculateTargetDistance(_pose, _target);
170176

171177
if ((target_tol > 0.01))
@@ -174,6 +180,10 @@ bool PID_Control::moveTarget()
174180
cmd_vel.angular.z = angularControl(_pose, _target);
175181

176182
_cmd_pub.publish(cmd_vel);
183+
error.data = _pid.dist_error;
184+
_edist_pub.publish(error);
185+
error.data = _pid.angle_error;
186+
_eangle_pub.publish(error);
177187
}
178188
else
179189
{

0 commit comments

Comments
 (0)