forked from UTFPR-Robotics/tutorial_controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerServer.cpp
More file actions
222 lines (168 loc) · 4.7 KB
/
Copy pathControllerServer.cpp
File metadata and controls
222 lines (168 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <ros/ros.h>
#include <tutorial_controller/TutorialAction.h>
#include <actionlib/server/simple_action_server.h>
#include "std_msgs/Float64.h"
#include "geometry_msgs/Vector3.h"
#include "sensor_msgs/JointState.h"
#define PI 3.14159265
//Class for containing the server
class ControllerServer{
public:
ControllerServer(std::string name):
as(n, "pid_control", boost::bind(&ControllerServer::executeCB, this, _1), false),
action_name(name)
{
as.registerPreemptCallback(boost::bind(&ControllerServer::preemptCB, this));
//Start the server
as.start();
//Subscriber current positon of servo
positionservosub = n2.subscribe("/sensor/encoder/servo", 1, &ControllerServer::SensorCallBack, this);
//Publisher setpoint, current position and error of control
error_controlpub = n2.advertise<geometry_msgs::Vector3>("/control/error", 1);
//Publisher PID output in servo
positionservopub = n2.advertise<std_msgs::Float64>("/motor/servo", 1);
//Max e Min Output PID Controller
float max = PI;
float min = -PI;
//Initializing PID Controller
Initialize(min,max);
}
//Callback for handling preemption. Reset your helpers here.
//Note that you still have to check for preemption in your work method to break it off
void preemptCB()
{
ROS_INFO("%s got preempted!", action_name.c_str());
result.ok = 0;
as.setPreempted(result, "I got Preempted!");
}
//Callback for processing a goal
void executeCB(const tutorial_controller::TutorialGoalConstPtr& goal)
{
prevTime = ros::Time::now();
//If the server has been killed, don't process
if(!as.isActive()||as.isPreemptRequested()) return;
//Run the processing at 100Hz
ros::Rate rate(100);
//Setup some local variables
bool success = true;
//Loop control
while(1)
{
std_msgs::Float64 msg_pos;
//PID Controller
msg_pos.data = PIDController(goal->position, position_encoder);
//Publishing PID output in servo
positionservopub.publish(msg_pos);
//Auxiliary Message
geometry_msgs::Vector3 msg_error;
msg_error.x = goal->position;
msg_error.y = position_encoder;
msg_error.z = goal->position - position_encoder;
//Publishing setpoint, feedback and error control
error_controlpub.publish(msg_error);
feedback.position = position_encoder;
//Publish feedback to action client
as.publishFeedback(feedback);
//Check for ROS kill
if(!ros::ok())
{
success = false;
ROS_INFO("%s Shutting Down", action_name.c_str());
break;
}
//If the server has been killed/preempted, stop processing
if(!as.isActive()||as.isPreemptRequested()) return;
//Sleep for rate time
rate.sleep();
}
//Publish the result if the goal wasn't preempted
if(success)
{
result.ok = 1;
as.setSucceeded(result);
}
else
{
result.ok = 0;
as.setAborted(result,"I Failed!");
}
}
void Initialize( float min, float max)
{
setOutputLimits(min, max);
lastError = 0;
errSum = 0;
kp = 1.5;
ki = 0.1;
kd = 0;
// kp = 1;
// ki = 2.3;
// kd = 0;
}
void setOutputLimits(float min, float max)
{
if (min > max) return;
minLimit = min;
maxLimit = max;
}
float PIDController(float setpoint, float PV)
{
ros::Time now = ros::Time::now();
ros::Duration change = now - prevTime;
float error = setpoint - PV;
float dErr = error - lastError;
errSum += error*change.toSec();
errSum = std::min(errSum, maxLimit);
errSum = std::max(errSum, minLimit);
dErr = (error - lastError)/change.toSec();
//Do the full calculation
float output = (kp*error) + (ki*errSum) + (kd*dErr);
//Clamp output to bounds
output = std::min(output, maxLimit);
output = std::max(output, minLimit);
//Required values for next round
lastError = error;
return output;
}
void SensorCallBack(const sensor_msgs::JointState& msg)
{
position_encoder = msg.position[0];
}
protected:
ros::NodeHandle n;
ros::NodeHandle n2;
//Subscriber
ros::Subscriber positionservosub;
//Publishers
ros::Publisher positionservopub;
ros::Publisher error_controlpub;
//Actionlib variables
actionlib::SimpleActionServer<tutorial_controller::TutorialAction> as;
tutorial_controller::TutorialFeedback feedback;
tutorial_controller::TutorialResult result;
std::string action_name;
//Control variables
float position_encoder;
float errSum;
float lastError;
float minLimit, maxLimit;
ros::Time prevTime;
float kp;
float ki;
float kd;
};
//Used by ROS to actually create the node. Could theoretically spawn more than one server
int main(int argc, char** argv)
{
ros::init(argc, argv, "pid_server");
//Just a check to make sure the usage was correct
if(argc != 1)
{
ROS_INFO("Usage: pid_server");
return 1;
}
//Spawn the server
ControllerServer server(ros::this_node::getName());
ros::spin();
return 0;
}