|
6 | 6 | from geometry_msgs.msg import Pose, Point, Twist, Vector3, Quaternion, TransformStamped |
7 | 7 | from tf.transformations import euler_from_quaternion |
8 | 8 |
|
9 | | - |
| 9 | +#Clase control que ejecuta las acciones de contol y contiene las variables involucradas |
10 | 10 | class Control: |
| 11 | + #Init pose object |
11 | 12 | pose = Pose() |
| 13 | + #Topic we are going to publish |
12 | 14 | posPub = rospy.Publisher('cmd_vel', Twist, queue_size = 10) |
| 15 | + #The message is going to be a Twist class objects |
13 | 16 | msg = Twist() |
| 17 | + #Sintonization variables |
14 | 18 | kpl = 0.8 |
15 | 19 | kpt = 0.5 |
| 20 | + |
16 | 21 | def __init__(self): |
17 | 22 | self.tf_buffer = Buffer() |
| 23 | + #Start tf listener |
18 | 24 | self.listener = TransformListener(self.tf_buffer) |
19 | 25 |
|
| 26 | + #Method to set the position |
20 | 27 | def setDesiredPosition(self, data): |
| 28 | + #Send the position using the broadcast method |
21 | 29 | self.broadcastTransform(data) |
22 | 30 | x=True |
23 | 31 | while(x): |
24 | 32 | try: |
| 33 | + #Keep broadcasting the data |
25 | 34 | self.broadcastTransform(data) |
| 35 | + #Update x with the return value of control method wich is |
| 36 | + # True if we finished the control or false if not |
26 | 37 | x = (not self.startControl()) |
27 | 38 | except: |
28 | 39 | None |
29 | | - |
| 40 | + #Set velocities to 0 |
30 | 41 | self.setVelocities(Twist()) |
31 | 42 |
|
32 | | - |
| 43 | + #Set the pose |
33 | 44 | def setPosition(self, data): |
34 | 45 | self.pose = data |
35 | 46 |
|
36 | | - |
| 47 | + #Method which execute control |
37 | 48 | def startControl(self): |
38 | | - |
| 49 | + #Look for the transform between robot and points |
39 | 50 | transformObject = self.tf_buffer.lookup_transform("robot", "point", rospy.Time()) |
40 | 51 | rospy.loginfo(transformObject) |
41 | | - |
| 52 | + #Extract translation |
42 | 53 | trans = transformObject.transform.translation |
| 54 | + #Compute PID |
43 | 55 | self.pidCalculation(trans) |
| 56 | + #Return true if robot reached the desired position |
44 | 57 | return(abs(trans.x)<0.01 and abs(trans.y)<0.01) |
45 | 58 |
|
46 | | - |
47 | | - |
| 59 | + #Compute PID method |
48 | 60 | def pidCalculation(self, error): |
| 61 | + #Compute and Set linear and angular speed on msg |
49 | 62 | self.msg.linear.x = self.kpl*error.x |
50 | | - |
51 | 63 | yaw = atan2(error.y,error.x) |
52 | 64 | self.msg.angular.z = self.kpt*yaw |
| 65 | + #Publish velocities |
53 | 66 | self.setVelocities(self.msg) |
54 | 67 |
|
55 | 68 |
|
56 | | - |
| 69 | + #Publish the message for control propourses |
57 | 70 | def setVelocities(self, speed): |
58 | 71 | self.posPub.publish(speed) |
59 | | - |
| 72 | + |
60 | 73 | def broadcastTransform(self, data): |
| 74 | + #Init transform broadcaster |
61 | 75 | br = TransformBroadcaster() |
62 | 76 | t = TransformStamped() |
| 77 | + #Fill the transform with time, frames, translation and orientation data |
63 | 78 | t.header.stamp = rospy.Time.now() |
64 | 79 | t.header.frame_id = "world" |
65 | 80 | t.child_frame_id = "point" |
66 | | - t.transform.translation = Vector3(data.x, data.y, data.z) |
67 | | - t.transform.rotation = Quaternion(0,0,0,1) |
| 81 | + t.transform.translation = Vector3(data.x, data.y, data.z) #Translation |
| 82 | + t.transform.rotation = Quaternion(0,0,0,1) #Orientation |
68 | 83 | br.sendTransform(t) |
69 | 84 |
|
70 | 85 |
|
|
0 commit comments