-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_velocity.py
More file actions
executable file
·202 lines (172 loc) · 6.52 KB
/
command_velocity.py
File metadata and controls
executable file
·202 lines (172 loc) · 6.52 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
import rospy
from geometry_msgs.msg import Twist # This is the message type the robot uses for velocities
class CommandVelocity():
"""Driving my robot
"""
def __init__(self):
rospy.loginfo("Starting node")
self.pub = rospy.Publisher("...", Twist) # Creating a publisher with whatever name...
# A function to send velocities until the node is killed
def send_velocities(self):
r = rospy.Rate(10) # Setting a rate (hz) at which to publish
while not rospy.is_shutdown(): # Runnin until killed
rospy.loginfo("Sending commands")
twist_msg = Twist() # Creating a new message to send to the robot
# ... put something relevant into your message
self.pub.publish(twist_msg) # Sending the message via our publisher
r.sleep() # Calling sleep to ensure the rate we set above
if __name__ == '__main__':
rospy.init_node("command_velocity")
cv = CommandVelocity()
cv.send_velocities() # Calling the function
rospy.spin()
#!/usr/bin/env python
#roslaunch uol_turtlebot_simulator maze1.launch
import numpy
import cv2
import cv_bridge
import rospy
#green until not seen
from sensor_msgs.msg import Image
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
#follow yellow.
#if yellow not found, avoid banging into walls
#turn when faced with red DONE
#if at green, stop DONE
#if yellow found, follow, else object detect
class Follower:
def __init__(self):
self.bridge = cv_bridge.CvBridge()
self.image_sub = rospy.Subscriber('/camera/rgb/image_raw', Image,
self.image_callback)
self.cmd_vel_pub = rospy.Publisher('/mobile_base/commands/velocity', Twist)
self.laser_sub = rospy.Subscriber('/scan',LaserScan,self.laser_callback) #scan
self.twist = Twist()
#a,b,c
self.turning = False
def laser_callback(self,msg):
#get laser messages
#remove 'NaNs'
clean = [i for i in msg.ranges if str(i) != 'nan']
#divide field of perception by 3 (left,front,side)
division = (len(clean)/3)
a = clean[0:division]
a = sum(clean[0:division])/division
b = sum(clean[division:(division*2)])/division
c = sum(clean[division*2:])/division
#print scan values
print(a)
print(b)
print(c)
print('--------')
#if corner
#turn the direction of larger gap
if self.turning == True:
#keep turning until a = 0.8
if (a > 0.8) & (b > 0.8) & (c > 0.8):
self.turning = False
self.twist.linear.x = 0.0
self.twist.angular.z = 0.0
else:
if (a < 0.4) & (c < 0.4):
if (a < c):
self.twist.linear.x = 0.0
self.twist.angular.z = 1
print('turning away left- cornered')
#rospy.sleep(1.)
else:
self.twist.linear.x = 0.0
self.twist.angular.z = -1
print('turning away right- cornered')
#rospy.sleep(1.)
self.turning = True
#if close to wall
#turn the direction of larger gap
elif (b < 0.45):
self.twist.linear.x = 0.0
if (a < c):
self.twist.angular.z = 1
print('turning away left- facing wall')
#rospy.sleep(1.)
else:
self.twist.angular.z = -1
print('turning away right- facing wall')
#rospy.sleep(1.)
self.turning = True
#if one side too close, turn the other way
elif (a < 0.4) & (c > 0.4):
self.twist.linear.x = 0.0
self.twist.angular.z = 1
print('turning away left')
self.turning = True
#rospy.sleep(1.)
elif (a > 0.4) & (c < 0.4):
self.twist.linear.x = 0.0
self.twist.angular.z = -1
print('turning away right')
#rospy.sleep(1.)
self.turning = True
#else move forward
else:
print('moving forward')
self.twist.linear.x = 0.2
self.twist.angular.z = 0.0
#rospy.sleep(1.)
def image_callback(self, msg):
cv2.namedWindow("window", 1)
image = self.bridge.imgmsg_to_cv2(msg, desired_encoding='bgr8')
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
#if green stop
lower_green = numpy.array([0, 250, 0])
upper_green = numpy.array([250, 255, 255])
mask = cv2.inRange(image, lower_green, upper_green)
h, w, d = image.shape
search_top = 3*h/4
# print("search_top =", search_top)
search_bot = 3*h/4 + 10
# print("search_bot=", search_bot)
#circle
mask[0:search_top, 0:w] = 0
mask[search_bot:h, 0:w] = 0
M = cv2.moments(mask)
if M['m00'] > 0:
cx = int(M['m10']/M['m00'])#circle x
cy = int(M['m01']/M['m00'])#circley
cv2.circle(image, (cx, cy), 20, (75, 150, 75), -1)
err = cx - w/2
#print(cx)
self.twist.angular.z = 0
self.twist.linear.x = 0
print("Goal Reached!")
#if red, turn
lower_red = numpy.array([0, 0, 250])
upper_red = numpy.array([250, 255, 255])
#if red turn around
mask = cv2.inRange(image, lower_red, upper_red)
h, w, d = image.shape
search_top = 3*h/4
# print("search_top =", search_top)
search_bot = 3*h/4 + 10
# print("search_bot=", search_bot)
#circle
mask[0:search_top, 0:w] = 0
mask[search_bot:h, 0:w] = 0
M = cv2.moments(mask)
if M['m00'] > 0:
cx = int(M['m10']/M['m00'])#circle x
cy = int(M['m01']/M['m00'])#circley
cv2.circle(image, (cx, cy), 20, (75, 150, 75), -1)
err = cx - w/2
#print(cx)
self.twist.angular.z = float(err) / 100 # turns here
print("Danger spotted!")
# print self.twist.angular.z
self.cmd_vel_pub.publish(self.twist)
cv2.imshow("window", image)
cv2.waitKey(3)
# cv2.startWindowThread()
rospy.init_node('follower')
follower = Follower()
pub = rospy.Publisher('/cmd_vel',Twist)
rospy.spin()