Skip to content

Commit 5bfc744

Browse files
authored
Merge pull request #2 from Juan-Carlos-TV/comentarios_control
Comentarios control 03-05-2022
2 parents ed3aeba + a51eedb commit 5bfc744

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
lines changed

src/control.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,80 @@
66
from geometry_msgs.msg import Pose, Point, Twist, Vector3, Quaternion, TransformStamped
77
from tf.transformations import euler_from_quaternion
88

9-
9+
#Clase control que ejecuta las acciones de contol y contiene las variables involucradas
1010
class Control:
11+
#Init pose object
1112
pose = Pose()
13+
#Topic we are going to publish
1214
posPub = rospy.Publisher('cmd_vel', Twist, queue_size = 10)
15+
#The message is going to be a Twist class objects
1316
msg = Twist()
17+
#Sintonization variables
1418
kpl = 0.8
1519
kpt = 0.5
20+
1621
def __init__(self):
1722
self.tf_buffer = Buffer()
23+
#Start tf listener
1824
self.listener = TransformListener(self.tf_buffer)
1925

26+
#Method to set the position
2027
def setDesiredPosition(self, data):
28+
#Send the position using the broadcast method
2129
self.broadcastTransform(data)
2230
x=True
2331
while(x):
2432
try:
33+
#Keep broadcasting the data
2534
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
2637
x = (not self.startControl())
2738
except:
2839
None
29-
40+
#Set velocities to 0
3041
self.setVelocities(Twist())
3142

32-
43+
#Set the pose
3344
def setPosition(self, data):
3445
self.pose = data
3546

36-
47+
#Method which execute control
3748
def startControl(self):
38-
49+
#Look for the transform between robot and points
3950
transformObject = self.tf_buffer.lookup_transform("robot", "point", rospy.Time())
4051
rospy.loginfo(transformObject)
41-
52+
#Extract translation
4253
trans = transformObject.transform.translation
54+
#Compute PID
4355
self.pidCalculation(trans)
56+
#Return true if robot reached the desired position
4457
return(abs(trans.x)<0.01 and abs(trans.y)<0.01)
4558

46-
47-
59+
#Compute PID method
4860
def pidCalculation(self, error):
61+
#Compute and Set linear and angular speed on msg
4962
self.msg.linear.x = self.kpl*error.x
50-
5163
yaw = atan2(error.y,error.x)
5264
self.msg.angular.z = self.kpt*yaw
65+
#Publish velocities
5366
self.setVelocities(self.msg)
5467

5568

56-
69+
#Publish the message for control propourses
5770
def setVelocities(self, speed):
5871
self.posPub.publish(speed)
59-
72+
6073
def broadcastTransform(self, data):
74+
#Init transform broadcaster
6175
br = TransformBroadcaster()
6276
t = TransformStamped()
77+
#Fill the transform with time, frames, translation and orientation data
6378
t.header.stamp = rospy.Time.now()
6479
t.header.frame_id = "world"
6580
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
6883
br.sendTransform(t)
6984

7085

src/odometry.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,65 +9,88 @@
99
from geometry_msgs.msg import Pose, TransformStamped, Vector3
1010
import numpy as np
1111

12+
#Class which has some characteristics of the robot and
13+
# has the odometry variables
1214
class Odom:
15+
#Initialize pose class
1316
p = Pose()
17+
#Speed and delta time
1418
wl = 0
1519
wr = 0
1620
dt = 0.01
1721
#posPub = rospy.Publisher('Position', Pose, queue_size = 10)
1822
r = 0.05 # Wheel radius, 0.05m
1923
l = 0.19 # Distance between robot wheels 0.19m
2024

25+
#Get the speed of left wheel
2126
def lSpeed(self, data):
2227
self.wl = data.data
23-
28+
#Get the speed of rigth wheel
2429
def rSpeed(self, data):
2530
self.wr = data.data
2631

32+
#Calculate position based on the integration of velocities
2733
def calculate(self):
2834
euler = euler_from_quaternion((self.p.orientation.x,self.p.orientation.y,self.p.orientation.z,self.p.orientation.w))
2935
yaw = euler[2]
3036

37+
#Computing and updating the position on the pose object
3138
self.p.position.x += (self.r*(self.wr+self.wl)/2*self.dt*cos(yaw))
3239
self.p.position.y += (self.r*(self.wr+self.wl)/2*self.dt*sin(yaw))
3340

3441
if(yaw>pi or yaw<-pi):
3542
yaw*=-1
36-
43+
44+
#Update yaw
3745
yaw+=self.r*(self.wr-self.wl)/self.l*self.dt
46+
#Send yaw value to ros log
3847
rospy.loginfo(yaw)
48+
#Compute the new quaternion with new yaw value
3949
quat = quaternion_from_euler(0, 0, yaw)
50+
#Update orientation
4051
self.p.orientation.x = quat[0]
4152
self.p.orientation.y = quat[1]
4253
self.p.orientation.z = quat[2]
4354
self.p.orientation.w = quat[3]
4455
#self.posPub.publish(self.p)
4556

57+
#Send transformation using broadcaster
4658
def broadcastTransform(self):
59+
#Initialize broadcaster
4760
br = TransformBroadcaster()
4861
t = TransformStamped()
62+
#Fill the transform with the position and orientations
4963
t.header.stamp = rospy.Time.now()
64+
#Frame names
5065
t.header.frame_id = "world"
5166
t.child_frame_id = "robot"
5267
t.transform.translation = Vector3(self.p.position.x, self.p.position.y,self.p.position.z)
5368
t.transform.rotation = self.p.orientation
69+
#Send transform
5470
br.sendTransform(t)
55-
71+
72+
#Restart pose
5673
def restart(self,_):
5774
self.p = Pose()
5875

76+
#Get the pose directly
5977
def getPose(self):
6078
return self.p
6179

6280

6381
def main():
82+
#Init the of odometry
6483
rospy.init_node('Odometry', anonymous=True)
84+
#Set rospy rate
6585
r = rospy.Rate(100)
86+
#Iniatilize class
6687
odometria = Odom()
88+
#Get speed from topic using the Odometry class methods
6789
rospy.Subscriber("/wl", Float32, odometria.lSpeed)
6890
rospy.Subscriber("/wr", Float32, odometria.rSpeed)
6991
rospy.Subscriber("position/restart",Empty, odometria.restart)
7092

93+
#Keep calculating the position and sending the transform
7194
while not rospy.is_shutdown():
7295
odometria.calculate()
7396
odometria.broadcastTransform()

0 commit comments

Comments
 (0)