-
Notifications
You must be signed in to change notification settings - Fork 0
/
aligntowall.py
executable file
·150 lines (131 loc) · 3.81 KB
/
aligntowall.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
#!/usr/bin/env python
import time
import math
import rospy
from geometry_msgs.msg import Twist, Vector3
from sensor_msgs.msg import LaserScan
from std_msgs.msg import *
import Adafruit_TCS34725
import smbus
turn = None
drive = None
angle_increment = None
vel = None
tolerance = .30
tSize = 5
distances = []
detectOpening=[];
forwardSpeed=0.1 #m/s
pGain=-0.000
justTurned=False
soundStart=True
tcs=Adafruit_TCS34725.TCS34725()
inRoom=False
'''
For the beginning, it might be better look to at the back half of the robot to alignToWall, but this
will only work for the beginning.
-Stephane
'''
def alignToWall(n):
global distances, angle_increment, detectOpening
minDist = distances[n]
minDistAngle = n
for i in range(n-45, n+45):
if distances[i] < minDist:
minDist = distances[i]
minDistAngle = i
print("Moving to " + str(minDistAngle) + "degrees")
if minDistAngle < 0:
turnLeftDegrees(-math.fabs(minDistAngle))
else:
turnRightDegrees(-minDistAngle)
rospy.sleep(0.25)
def colorHandler(data):
global inRoom
inRoom = data.data
print("in color handler: "+str(inRoom))
def setup():
global distances, angle_increment, turn, vel, drive
rospy.init_node('wallfollower', anonymous=False)
rospy.Subscriber("/scan", LaserScan, scanHandler, queue_size=1, buff_size=1)
rospy.Subscriber("/color", Bool, colorHandler)
vel = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
turn = rospy.Publisher('turn', Float64, queue_size=10)
drive = rospy.Publisher('drive', Float64, queue_size=10)
time.sleep(1)
rospy.spin()
def scanHandler(scan):
global distances, detectOpening, angle_increment, justTurned, soundStart, tcs, inRoom
distances = scan.ranges
d = []
for dist in range(len(distances)):
if (not math.isinf(distances[dist])):
d.append(distances[dist])
else:
d.append(1000)
distances = d
angle_increment = scan.angle_increment
if rospy.get_time()-scan.header.stamp.secs<1 and soundStart:
alignToWall(0)
rospy.sleep(.05)
# Moves forward m meters at .20
def moveForwardDistance(distance):
global drive
drive.publish(Float64(distance))
# Turns left at 1 radians (? degrees) per second
def turnLeftDegrees(degrees):
global turn
radians = - degrees * (math.pi / (180))
turn.publish(Float64(radians))
# Turns right at 1 radians (? degrees) per second
def turnRightDegrees(degrees):
global turn
radians = degrees * (math.pi/180)
turn.publish(Float64(radians))
#
#SPEED IS IN M/S
def moveForward(speed):
global vel
msg = Twist()
msg.linear = Vector3(speed, 0, 0)
msg.angular = Vector3(0, 0, 0)
vel.publish(msg)
def moveBackward(speed):
global vel
msg = Twist()
msg.linear = Vector3(-speed, 0, 0)
msg.angular = Vector3(0, 0, 0)
vel.publish(msg)
def turnLeft(speed):
global vel
msg = Twist()
msg.linear = Vector3(0, 0, 0)
msg.angular = Vector3(0, 0, -speed)
vel.publish(msg)
def turnRight(speed):
global vel
msg = Twist()
msg.linear = Vector3(0, 0, 0)
msg.angular = Vector3(0, 0, speed)
vel.publish(msg)
def turnAndMove(speedForward, speedClockwise):
global vel
msg = Twist()
msg.linear = Vector3(speedForward, 0, 0)
msg.angular = Vector3(0, 0, speedClockwise)
vel.publish(msg)
def stopeverything():
print('exiting')
vel = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
msg = Twist()
msg.linear = Vector3(0, 0, 0)
msg.angular = Vector3(0, 0, 0)
vel.publish(msg)
print('done exiting')
if __name__ == '__main__':
print(rospy.get_published_topics())
while not (['/motor_listener_listening', 'std_msgs/String'] in rospy.get_published_topics()):
pass
print('starting')
rospy.on_shutdown(stopeverything)
setup()