forked from sangwansangwan/ROS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPID.py
285 lines (187 loc) · 10.2 KB
/
PID.py
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python
'''
This python file runs a ROS-node of name drone_control which holds the position of Drone on the given dummy.
This node publishes and subsribes the following topics:
PUBLICATIONS SUBSCRIPTIONS
/drone_command /whycon/poses
/alt_error /pid_tuning_altitude
/pitch_error /pid_tuning_pitch
/roll_error /pid_tuning_roll
Rather than using different variables, use list. eg : self.setpoint = [1,2,3], where index corresponds to x,y,z ...rather than defining self.x_setpoint = 1, self.y_setpoint = 2
CODE MODULARITY AND TECHNIQUES MENTIONED LIKE THIS WILL HELP YOU GAINING MORE MARKS WHILE CODE EVALUATION.
'''
# Importing the required libraries
from edrone_client.msg import *
from geometry_msgs.msg import PoseArray
from std_msgs.msg import Int16
from std_msgs.msg import Int64
from std_msgs.msg import Float64
from pid_tune.msg import PidTune
import rospy
import time
class Edrone():
def __init__(self):
rospy.init_node('drone_control') # initializing ros node with name drone_control
# This corresponds to your current position of drone. This value must be updated each time in your whycon callback
# [x,y,z]
self.drone_position = [0.0,0.0,0.0]
# [x_setpoint, y_setpoint, z_setpoint]
self.setpoint = [2,2,20] # whycon marker at the position of the dummy given in the scene. Make the whycon marker associated with position_to_hold dummy renderable and make changes accordingly
#Declaring a cmd of message type PlutoMsg and initializing values
self.cmd = edrone_msgs()
self.cmd.rcRoll = 1500
self.cmd.rcPitch = 1500
self.cmd.rcYaw = 1500
self.cmd.rcThrottle = 1000
self.cmd.rcAUX1 = 1500
self.cmd.rcAUX2 = 1500
self.cmd.rcAUX3 = 1500
self.cmd.rcAUX4 = 1500
#initial setting of Kp, Kd and ki for [roll, pitch, throttle]. eg: self.Kp[2] corresponds to Kp value in throttle axis
#after tuning and computing corresponding PID parameters, change the parameters
self.Kp = [30,30,40]
self.Ki = [0,0,0.00018]
self.Kd = [150000,150000,150000]
#-----------------------Add other required variables for pid here ----------------------------------------------
self.sample_time = 60
self.prev_values = [0,0,0]
self.max_values = 2000
self.min_values = 1000
self.error = [0,0,0]
self.now=0.0000
self.timechange=0.000
self.errsum=[0,0,0]
self.derr=[0,0,0]
self.last_time=0.0000
self.out_roll=0.000
self.out_pitch=0.000
self.out_throttle=0.000
# Hint : Add variables for storing previous errors in each axis, like self.prev_values = [0,0,0] where corresponds to [pitch, roll, throttle] # Add variables for limiting the values like self.max_values = [2000,2000,2000] corresponding to [roll, pitch, throttle]
# self.min_values = [1000,1000,1000] corresponding to [pitch, roll, throttle]
# You can change the upper limit and lower limit accordingly.
#----------------------------------------------------------------------------------------------------------
# # This is the sample time in which you need to run pid. Choose any time which you seem fit. Remember the stimulation step time is 50 ms
# self.sample_time = 0.060 # in seconds
# Publishing /drone_command, /alt_error, /pitch_error, /roll_error
self.command_pub = rospy.Publisher('/drone_command', edrone_msgs, queue_size=1)
#------------------------Add other ROS Publishers here-----------------------------------------------------
self.altError = rospy.Publisher('/alt_error',Float64, queue_size=1)
self.pitchError = rospy.Publisher('/pitch_error', Float64, queue_size=1)
self.rollError = rospy.Publisher('/roll_error', Float64, queue_size=1)
#-----------------------------------------------------------------------------------------------------------
# Subscribing to /whycon/poses, /pid_tuning_altitude, /pid_tuning_pitch, pid_tuning_roll
rospy.Subscriber('whycon/poses', PoseArray, self.whycon_callback)
rospy.Subscriber('/pid_tuning_altitude',PidTune,self.altitude_set_pid)
#-------------------------Add other ROS Subscribers here----------------------------------------------------
#rospy.Subscriber('/pid_tuning_pitch',PidTune,self.pitch_set_pid)
#rospy.Subscriber('/pid_tuning_roll',PidTune,self.roll_set_pid)
#------------------------------------------------------------------------------------------------------------
self.arm() # ARMING THE DRONE
# Disarming condition of the drone
def disarm(self):
self.cmd.rcAUX4 = 1100
self.command_pub.publish(self.cmd)
rospy.sleep(1)
# Arming condition of the drone : Best practise is to disarm and then arm the drone.
def arm(self):
self.disarm()
self.cmd.rcRoll = 1500
self.cmd.rcYaw = 1500
self.cmd.rcPitch = 1500
self.cmd.rcThrottle = 1000
self.cmd.rcAUX4 = 1500
self.command_pub.publish(self.cmd) # Publishing /drone_command
rospy.sleep(1)
# Whycon callback function
# The function gets executed each time when /whycon node publishes /whycon/poses
def whycon_callback(self,msg):
self.drone_position[0] = msg.poses[0].position.x
#--------------------Set the remaining co-ordinates of the drone from msg----------------------------------------------
self.drone_position[1] = msg.poses[0].position.y
self.drone_position[2] = msg.poses[0].position.z
#---------------------------------------------------------------------------------------------------------------
# Callback function for /pid_tuning_altitude
# This function gets executed each time when /tune_pid publishes /pid_tuning_altitude
def altitude_set_pid(self,alt):
self.Kp[2] = alt.Kp * 0.06# This is just for an example. You can change the ratio/fraction value accordingly
self.Ki[2] = alt.Ki * 0.008
self.Kd[2] = alt.Kd * 0.3
#----------------------------Define callback function like altitide_set_pid to tune pitch, roll--------------
def pitch_set_pid(self,pitch):
self.Kp[0] = pitch.Kp * 0.06 # This is just for an example. You can change the ratio/fraction value accordingly
self.Ki[0] = pitch.Ki * 0.008
self.Kd[0] = pitch.Kd * 0.3
def roll_set_pid(self,roll):
self.Kp[1] = roll.Kp * 0.06 # This is just for an example. You can change the ratio/fraction value accordingly
self.Ki[1] = roll.Ki * 0.008
self.Kd[1] = roll.Kd * 0.3
#----------------------------------------------------------------------------------------------------------------------
def pid(self):
#-----------------------------Write the PID algorithm here--------------------------------------------------------------
# Steps:
# 1. Compute error in each axis. eg: error[0] = self.drone_position[0] - self.setpoint[0] ,where error[0] corresponds to error in x...
# 2. Compute the error (for proportional), change in error (for derivative) and sum of errors (for integral) in each axis. Refer "Understanding PID.pdf" to understand PID equation.
# 3. Calculate the pid output required for each axis. For eg: calcuate self.out_roll, self.out_pitch, etc.
# 4. Reduce or add this computed output value on the avg value ie 1500. For eg: self.cmd.rcRoll = 1500 + self.out_roll. LOOK OUT FOR SIGN (+ or -). EXPERIMENT AND FIND THE CORRECT SIGN
# 5. Don't run the pid continously. Run the pid only at the a sample time. self.sampletime defined above is for this purpose. THIS IS VERY IMPORTANT.
# 6. Limit the output value and the final command value between the maximum(1800) and minimum(1200)range before publishing. For eg : if self.cmd.rcPitch > self.max_values[1]:
# self.cmd.rcPitch = self.max_values[1]
# 7. Update previous errors.eg: self.prev_error[1] = error[1] where index 1 corresponds to that of pitch (eg)
# 8. Add error_sum
#time functions
self.now = int(round(time.time() * 1000))
self.timechange=self.now-self.last_time
#delta time must be more than step time of Gazebo, otherwise same values will be repeated
if (self.timechange>self.sample_time):
if (self.last_time!=0):
#Getting error of all coordinates
self.error[0]=self.drone_position[0] - self.setpoint[0]
self.error[1]=self.drone_position[1] - self.setpoint[1]
self.error[2]=self.drone_position[2] - self.setpoint[2]
#Integration for Ki
#self.errsum[0]=self.errsum[0]+(self.error[0]*self.timechange)
#self.errsum[1]=self.errsum[1]+(self.error[1]*self.timechange)
self.errsum[2]=self.errsum[2]+(self.error[2]*self.timechange)
#Derivation for Kd
self.derr[0]=(self.error[0]-self.prev_values[0])/self.timechange
self.derr[1]=(self.error[1]-self.prev_values[1])/self.timechange
self.derr[2]=(self.error[2]-self.prev_values[2])/self.timechange
#Calculating output in 1500
self.cmd.rcRoll=1500-(self.Kp[0]*self.error[0])-(self.Kd[0]*self.derr[0])
self.cmd.rcPitch=1500+(self.Kp[1]*self.error[1])+(self.Kd[1]*self.derr[1])
self.cmd.rcThrottle=1500+(self.Kp[2]*self.error[2])+(self.Kd[2]*self.derr[2])-(self.errsum[2]*self.Ki[2])
#Checking min and max threshold and updating on true
#Throttle Conditions
if self.cmd.rcThrottle>2000:
self.cmd.rcThrottle=self.max_values
if self.cmd.rcThrottle<1000:
self.cmd.rcThrottle=self.min_values
#Pitch Conditions
if self.cmd.rcPitch>2000:
self.cmd.rcPitch=self.max_values
if self.cmd.rcPitch<1000:
self.cmd.rcPitch=self.min_values
#Roll Conditions
if self.cmd.rcRoll>2000:
self.cmd.rcRoll=self.max_values
if self.cmd.rcRoll<1000:
self.cmd.rcRoll=self.min_values
#Publishing values on topic 'drone command'
self.command_pub.publish(self.cmd)
#Updating prev values for all axis
self.prev_values[0]=self.error[0]
self.prev_values[1]=self.error[1]
self.prev_values[2]=self.error[2]
#Updating last time value
self.last_time=self.now
#Getting values for Plotjuggler
self.rollError.publish(self.error[0])
self.pitchError.publish(self.error[1])
self.altError.publish(self.error[2])
#------------------------------------------------------------------------------------------------------------------------
if __name__ == '__main__':
e_drone = Edrone()
r = rospy.Rate(29) #specify rate in Hz based upon your desired PID sampling time, i.e. if desired sample time is 33ms specify rate as 30Hz
while not rospy.is_shutdown():
e_drone.pid()
r.sleep()