-
Notifications
You must be signed in to change notification settings - Fork 0
/
angles_tunning.txt
151 lines (123 loc) · 5.55 KB
/
angles_tunning.txt
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
#!/usr/bin/env python3
import rospy
from geometry_msgs.msg import Twist, Pose2D
from sensor_msgs.msg import Range
from nav_msgs.msg import Odometry
from tf.transformations import euler_from_quaternion
from evry_project_plugins.srv import DistanceToFlag
class Robot:
def __init__(self, robot_name):
"""Constructor of the class Robot
The required publishers / subscribers are created.
The attributes of the class are initialized
Args:
robot_name (str): Name of the robot, like robot_1, robot_2, etc. To be used for your subscriber and publisher with the robot itself
"""
self.speed = 0.0
self.angle = 0.0
self.sonar = 0.0 # Sonar distance
self.x, self.y = 0.0, 0.0 # coordinates of the robot
self.yaw = 0.0 # yaw angle of the robot
self.robot_name = robot_name
# Listener and publisher
rospy.Subscriber(self.robot_name + "/sensor/sonar_front",
Range, self.callbackSonar)
rospy.Subscriber(self.robot_name + "/odom",
Odometry, self.callbackPose)
self.cmd_vel_pub = rospy.Publisher(
self.robot_name + "/cmd_vel", Twist, queue_size=1)
def callbackSonar(self, msg):
"""Callback function that gets the data coming from the ultrasonic sensor
Args:
msg (Range): Message that contains the distance separating the US sensor from a potential obstacle
"""
self.sonar = msg.range
def get_sonar(self):
"""Method that returns the distance separating the ultrasonic sensor from a potential obstacle
"""
return self.sonar
def callbackPose(self, msg):
"""Callback function that gets the data coming from the ultrasonic sensor
Args:
msg (Odometry): Message that contains the coordinates of the agent
"""
self.x = msg.pose.pose.position.x
self.y = msg.pose.pose.position.y
quaternion = msg.pose.pose.orientation
quaternion_list = [quaternion.x,
quaternion.y, quaternion.z, quaternion.w]
roll, pitch, yaw = euler_from_quaternion(quaternion_list)
self.yaw = yaw
def get_robot_pose(self):
"""Method that returns the position and orientation of the robot"""
return self.x, self.y, self.yaw
def constraint(self, val, min=-2.0, max=2.0):
"""Method that limits the linear and angular velocities sent to the robot
Args:
val (float): Desired velocity to send
min (float, optional): Minimum velocity accepted. Defaults to -2.0.
max (float, optional): Maximum velocity accepted. Defaults to 2.0.
Returns:
float: Limited velocity whose value is within the range [min; max]
"""
# DO NOT TOUCH
if val < min:
return min
if val > max:
return max
return val
def set_speed_angle(self, linear, angular):
"""Method that publishes the proper linear and angular velocities commands on the related topic to move the robot
Args:
linear (float): Desired linear velocity
angular (float): Desired angular velocity
"""
cmd_vel = Twist()
cmd_vel.linear.x = self.constraint(linear)
cmd_vel.angular.z = self.constraint(angular, min=-1, max=1)
self.cmd_vel_pub.publish(cmd_vel)
def getDistanceToFlag(self):
"""Get the distance separating the agent from a flag. The service 'distanceToFlag' is called for this purpose.
The current position of the robot and its id should be specified. The id of the robot corresponds to the id of the flag it should reach
Returns:
float: The distance separating the robot from the flag
"""
rospy.wait_for_service('/distanceToFlag')
try:
service = rospy.ServiceProxy('/distanceToFlag', DistanceToFlag)
pose = Pose2D()
pose.x = self.x
pose.y = self.y
# int(robot_name[-1]) corresponds to the id of the robot. It is also the id of the related flag
result = service(pose, int(self.robot_name[-1]))
return result.distance
except rospy.ServiceException as e:
print("Service call failed: %s" % e)
def run_demo():
"""Main loop"""
robot_name = rospy.get_param("~robot_name")
robot = Robot(robot_name)
print(f"Robot : {robot_name} is starting..")
while not rospy.is_shutdown():
velocity = 2
angle = 0
distance = float(robot.getDistanceToFlag())
print(f"{robot_name} distance to flag = {distance}, velocity = {velocity}")
# Strategy 01
if distance > 30: # 30 is the distance where the robots colliding
angle = 0.02
elif distance < 30:
angle = -0.06
# Set the robot's speed and angle
robot.set_speed_angle(velocity, angle)
# Check if the robot reached the flag and stop if needed
if distance <= 1.5:
print(f"{robot_name} reached the flag. Stopping...")
robot.set_speed_angle(0.0, 0.0) # Set velocity and angle to zero
rospy.sleep(2.0) # Wait for the robot to come to a complete stop
break
rospy.sleep(0.5)
if __name__ == "__main__":
print("Running ROS..")
rospy.init_node("Controller", anonymous=True)
run_demo()