-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathspeeds_assignment.py
executable file
·152 lines (110 loc) · 4.97 KB
/
speeds_assignment.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
#!/usr/bin/env python
import rospy
from sensor_msgs.msg import Range
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
from sonar_data_aggregator import SonarDataAggregator
from laser_data_aggregator import LaserDataAggregator
from navigation import Navigation
# Class for assigning the robot speeds
class RobotController:
# Constructor
def __init__(self):
# Debugging purposes
self.print_velocities = rospy.get_param('print_velocities')
# Where and when should you use this?
self.stop_robot = False
# Create the needed objects
self.sonar_aggregation = SonarDataAggregator()
self.laser_aggregation = LaserDataAggregator()
self.navigation = Navigation()
self.linear_velocity = 0
self.angular_velocity = 0
# Check if the robot moves with target or just wanders
self.move_with_target = rospy.get_param("calculate_target")
# The timer produces events for sending the speeds every 110 ms
rospy.Timer(rospy.Duration(0.11), self.publishSpeeds)
self.velocity_publisher = rospy.Publisher(\
rospy.get_param('speeds_pub_topic'), Twist,\
queue_size = 10)
# Read the velocities architecture
self.velocity_arch = rospy.get_param("velocities_architecture")
print "The selected velocities architecture is " + self.velocity_arch
# This function publishes the speeds and moves the robot
def publishSpeeds(self, event):
# Choose architecture
if self.velocity_arch == "subsumption":
self.produceSpeedsSubsumption()
else:
self.produceSpeedsMotorSchema()
# Create the commands message
twist = Twist()
twist.linear.x = self.linear_velocity
twist.linear.y = 0
twist.linear.z = 0
twist.angular.x = 0
twist.angular.y = 0
twist.angular.z = self.angular_velocity
# Send the command
self.velocity_publisher.publish(twist)
# Print the speeds for debuggind purposes
if self.print_velocities == True:
print "[L,R] = [" + str(twist.linear.x) + " , " + \
str(twist.angular.z) + "]"
# Produce speeds from sonars
def produceSpeedsSonars(self):
# Get the sonars' measurements
front = self.sonar_aggregation.sonar_front_range
left = self.sonar_aggregation.sonar_left_range
right = self.sonar_aggregation.sonar_right_range
r_left = self.sonar_aggregation.sonar_rear_left_range
r_right = self.sonar_aggregation.sonar_rear_right_range
linear = 0
angular = 0
# YOUR CODE HERE ------------------------------------------------------
# Adjust the linear and angular velocities using the five sonars values
# ---------------------------------------------------------------------
return [linear, angular]
# Produces speeds from the laser
def produceSpeedsLaser(self):
# Get the laser scan
scan = self.laser_aggregation.laser_scan
linear = 0
angular = 0
# YOUR CODE HERE ------------------------------------------------------
# Adjust the linear and angular velocities using the laser scan
# ---------------------------------------------------------------------
return [linear, angular]
# Combines the speeds into one output using a subsumption approach
def produceSpeedsSubsumption(self):
# Produce target if not existent
if self.move_with_target == True and self.navigation.target_exists == False:
self.navigation.selectTarget()
# Get the submodules' speeds
[l_sonar, a_sonar] = self.produceSpeedsSonars()
[l_laser, a_laser] = self.produceSpeedsLaser()
[l_goal, a_goal] = self.navigation.velocitiesToNextSubtarget()
self.linear_velocity = 0
self.angular_velocity = 0
# Combine the speeds following the subsumption architecture
# YOUR CODE HERE ------------------------------------------------------
# ---------------------------------------------------------------------
# Combines the speeds into one output using a motor schema approach
def produceSpeedsMotorSchema(self):
# Produce target if not existent
if self.move_with_target == True and self.navigation.target_exists == False:
self.navigation.selectTarget()
# Get the submodule's speeds
[l_sonar, a_sonar] = self.produceSpeedsSonars()
[l_laser, a_laser] = self.produceSpeedsLaser()
[l_goal, a_goal] = self.navigation.velocitiesToNextSubtarget()
self.linear_velocity = 0
self.angular_velocity = 0
# Get the speeds using the motor schema approach
# YOUR CODE HERE ------------------------------------------------------
# ---------------------------------------------------------------------
# Assistive functions - Do you have to call them somewhere?
def stopRobot(self):
self.stop_robot = True
def resumeRobot(self):
self.stop_robot = False