forked from atulsriv/ROS_Winlab2017
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroomba_pid.py
76 lines (56 loc) · 1.49 KB
/
roomba_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
#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
import tf
from math import radians
rospy.init_node("roomba_pid_controller")
velocity_publisher = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
rate = rospy.Rate(10.0)
listener = tf.TransformListener()
def check_camera():
got_one = False
while not got_one:
try:
(t,rot) = listener.lookupTransform('/map', '/roomba', rospy.Time(0))
got_one = True
except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
continue
euler = tf.transformations.euler_from_quaternion(rot)
yaw = euler[2]
quat = tf.transformations.quaternion_from_euler(0.0,0.0,yaw)
trans = (t[0],t[1],0.0)
return (trans,quat)
def publish_cmd_vel(ang_vel):
vel_msg = Twist()
vel_msg.angular.z = ang_vel
velocity_publisher.publish(vel_msg)
kp = 0.955
ki = 0
kd = 0
integral = 0
last_error = 0
derivative = 0
while not rospy.is_shutdown():
i = 0
target_achieved = False
t = input("Target Angle [degrees]:")
target = radians(t)
while not target_achieved:
(trans,quat) = check_camera()
euler = tf.transformations.euler_from_quaternion(quat)
yaw = euler[2]
error = target - yaw
print "Error: "+ str(error)
integral += error
derivative = error - last_error
ang_vel = kp*error + ki*integral + kd*derivative
print "Ang_vel" + str(ang_vel)
if abs(error) < 0.006:
ang_vel = 0
i += 1
publish_cmd_vel(ang_vel)
last_error = error
print "---"
if i >= 5:
target_achieved = True
rate.sleep()