-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_bot.py
More file actions
executable file
·204 lines (164 loc) · 5.91 KB
/
server_bot.py
File metadata and controls
executable file
·204 lines (164 loc) · 5.91 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python2.7
#Server imports
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import json
import threading
import thread
import time
PORT = 8080
#turtlebot imports
import rospy
from nav_msgs.msg import Odometry
from tf.transformations import euler_from_quaternion
from geometry_msgs.msg import Point, Twist
from math import atan2, sqrt, pi
from sensor_msgs.msg import LaserScan
from std_msgs.msg import Empty
x = 0.0
y = 0.0
theta = 0.0
last_coords = [0,0]
turtle_thread = None
stop = False
# Callback function
def newOdom(msg):
global x
global y
global theta
x = msg.pose.pose.position.x
y = msg.pose.pose.position.y
rot_q = msg.pose.pose.orientation
(roll, pitch, theta) = euler_from_quaternion([rot_q.x, rot_q.y, rot_q.z, rot_q.w])
def obstacle_detection_callback(dt):
global stop
global pub
print '-------------------------------------------'
print 'Range data at 0 deg: {}'.format(dt.ranges[0])
print 'Range data at 15 deg: {}'.format(dt.ranges[15])
print 'Range data at 345 deg: {}'.format(dt.ranges[345])
print '-------------------------------------------'
# Thresholds
thr1 = 0.25
thr2 = 0.3
# Checks if obstacles in front and 15 deg left and right
if not (dt.ranges[0] > thr1 and dt.ranges[15] > thr2 and dt.ranges[345] > thr2):
if (dt.ranges[0] > 0 and dt.ranges[15] > 0 and dt.ranges[345] > 0):
speed.linear.x = 0.0
speed.angular.z = 0.0
stop = True
print("OBSTACLE!!!")
pub.publish(speed)
# Initialize mode
rospy.init_node("speed_controller")
# rospy.init_node('obstacle_avoidance_node')
# Publish linear and angular velocities to cmd_vel topic
pub = rospy.Publisher("/cmd_vel", Twist, queue_size = 10)
# Subscribe to the odom topic to get information about the current position and velocity
# of the robot
sub = rospy.Subscriber("/odom", Odometry, newOdom)
sub1 = rospy.Subscriber("/scan", LaserScan, obstacle_detection_callback)
speed = Twist()
# Initialize mode
rospy.init_node("speed_controller")
# Subscribe to the odom topic to get information about the current position and velocity
# of the robot
sub = rospy.Subscriber("/odom", Odometry, newOdom)
# Publish linear and angular velocities to cmd_vel topic
pub = rospy.Publisher("/cmd_vel", Twist, queue_size = 1)
reset_odom = rospy.Publisher('/mobile_base/commands/reset_odometry', Empty, queue_size=1)
speed = Twist()
rate = rospy.Rate(10)
def turtle(waypoints):
global stop
# Establish target coordinates
goal = Point()
current_goal=0
goal.x=waypoints[current_goal][0]
goal.y=waypoints[current_goal][1]
# Strategy is to first turn to face the target coordinates
# and then move towards them
while not rospy.is_shutdown() and current_goal < len(waypoints) and not stop:
# Compute difference between current position and target position
inc_x = goal.x -x
inc_y = goal.y -y
angle_to_goal = atan2(inc_y, inc_x)
difference_angle = abs(angle_to_goal - theta)
msg2 = ("x: {}, y: {}, velocity: {}, angle: {}".format(x,y,speed.linear.x,theta))
rospy.loginfo(msg2)
dist = sqrt(inc_x**2 + inc_y**2)
# Check whether to turn or move.
angle_change=angle_to_goal-theta
if dist<0.1:
speed.angular.z=0
speed.linear.x=0
current_goal+=1
if current_goal < len(waypoints):
goal.x=waypoints[current_goal][0]
goal.y=waypoints[current_goal][1]
elif dist>1:
speed.angular.z = 0
speed.linear.x = 0
goal.x = (x + goal.x)/2
goal.y = (x + goal.y)/2
elif abs(angle_change) > 0.15:
if angle_change > 0:
speed.angular.z = 0.60
else:
speed.angular.z = -0.60
else:
speed.angular.z = 0.0
speed.linear.x = 0.30
rospy.loginfo("Ready to move")
#print("curr: {} length_waypoint: {}".format(current_goal, len(waypoints)))
pub.publish(speed)
rate.sleep()
speed.angular.z = 0.0
speed.linear.x = 0.0
pub.publish(speed)
# Server Class
class Handler(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_POST(self):
global last_coords
global stop
global turtle_thread
global thread_arr
content_length = int(self.headers['Content-Length']) # Size of Data
data = self.rfile.read(content_length).decode('utf-8') # Data
if data == "STOP":
stop = True
else:
if (turtle_thread != None) and not turtle_thread.is_alive():
turtle_thread = None
if turtle_thread == None:
end_time = time.time() + 2 #Publish reset for 5 seconds
while time.time() < end_time:
reset_odom.publish(Empty())
stop = False
coords = json.loads(data)
for c in coords:
c[0] += last_coords[0]
c[1] += last_coords[1]
turtle_thread = threading.Thread(target = turtle, args = (coords,))
turtle_thread.start()
last_coords = coords[-1]
self._set_response()
self.wfile.write("Recieved".encode('utf-8'))
def run(server_class=HTTPServer, handler_class=Handler, port=PORT):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print('Starting server...\n')
try:
print('PORT: ' + str(PORT))
print('URL: localhost:' + str(PORT))
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print('Stopping server...\n')
stop=True
if __name__ == '__main__':
run()