-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwalk.py
110 lines (80 loc) · 2.35 KB
/
walk.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
from time import sleep
import sys
import random
print "Initializing motors"
import
import motors.sn75SoftwarePwm as dcMotorControl
from motors.servoWirPWM import *
from ultrasonic_sensor.ussContinuous import *
from motors.pigpio_encoder_functions import *
# generate and send the correct commands to the robot
def _send_robot_commands( self ):
...
v_l, v_r = self._uni_to_diff( v, omega )
self.robot.set_wheel_drive_rates( v_l, v_r )
def _uni_to_diff( self, v, omega ):
# v = translational velocity (m/s)
# omega = angular velocity (rad/s)
R = self.robot_wheel_radius
L = self.robot_wheel_base_length
v_l = ( (2.0 * v) - (omega*L) ) / (2.0 * R)
v_r = ( (2.0 * v) + (omega*L) ) / (2.0 * R)
return v_l, v_r
def loop():
try:
prevTurn = False
direction = 0
while True:
distance = getDistance()
# if an object is closer than 15 cm turn to a random side for 1 second and check again
if distance < 40 and distance != -1:
# find a new direction to turn to if we are meeting a new wall
if not prevTurn:
direction = random.choice([-1,1])
prevTurn = True
print " %s less than 15cm, turning %s" % (distance, direction)
else:
print "Still turning"
# turn for 1 second and restart the loop
dcMotorControl.turn(direction, 100)
sleep(0.6)
dcMotorControl.stop()
continue
# go straight
else:
print "Going straight"
prevTurn = False
dcMotorControl.move(100)
#sleep(0.5)
#dcMotorControl.stop()
except KeyboardInterrupt:
print "User cancelled"
except:
print "Unexpected error:", sys.exc_info()[0]
raise
finally:
print "Cleaning up.."
dcMotorControl.cleanup()
servoCleanup()
def test():
try:
#dcMotorControl.move(100)
#sleep(0.5)
#dcMotorControl.turn(1,100)
#sleep(0.3)
dcMotorControl.move(60)
sleep(5)
except KeyboardInterrupt:
print "User cancelled"
except:
print "Unexpected error:", sys.exc_info()[0]
raise
finally:
print "Cleaning up.."
dcMotorControl.cleanup()
servoCleanup()
def main():
loop()
# test()
if __name__ == "__main__":
main()