-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_touch_rotation.py
72 lines (57 loc) · 2.23 KB
/
test_touch_rotation.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
import brickpi
import time
touch_port_right = 1
touch_port_left = 4
interface=brickpi.Interface()
interface.initialize()
interface.sensorEnable(touch_port_right, brickpi.SensorType.SENSOR_TOUCH)
interface.sensorEnable(touch_port_left, brickpi.SensorType.SENSOR_TOUCH)
motors = [2,1]
interface.motorEnable(motors[0])
interface.motorEnable(motors[1])
leftParam = interface.MotorAngleControllerParameters()
rightParam = interface.MotorAngleControllerParameters()
leftParam.maxRotationAcceleration = 6.0
leftParam.maxRotationSpeed = 12.0
leftParam.feedForwardGain = 255/20.0
leftParam.minPWM = 18.0
leftParam.pidParameters.minOutput = -255
leftParam.pidParameters.maxOutput = 255
leftParam.pidParameters.k_p = 100.0
leftParam.pidParameters.k_i = 0.0
leftParam.pidParameters.k_d = 0.0
rightParam.maxRotationAcceleration = 6.0
rightParam.maxRotationSpeed = 12.0
rightParam.feedForwardGain = 255/20.0
rightParam.minPWM = 18.0
rightParam.pidParameters.minOutput = -255
rightParam.pidParameters.maxOutput = 255
rightParam.pidParameters.k_p = 100.0
rightParam.pidParameters.k_i = 0.0
rightParam.pidParameters.k_d = 0.0
interface.setMotorAngleControllerParameters(motors[0],leftParam)
interface.setMotorAngleControllerParameters(motors[1],rightParam)
logfile = raw_input("Give a logfile name: ")
interface.startLogging("./logfiles/{}.txt".format(logfile))
while True:
left_result = interface.getSensorValue(touch_port_left)
right_result = interface.getSensorValue(touch_port_right)
left_touched, right_touched = 0, 0
if left_result:
left_touched = left_result[0]
if right_result:
right_touched = right_result[0]
if left_touched and not right_touched:
# obstacle on the left, turn right
interface.increaseMotorAngleReferences(motors,[-20,-20])
interface.increaseMotorAngleReferences(motors,[-20,20])
elif not left_touched and right_touched:
# obstacle on the right, turn left
interface.increaseMotorAngleReferences(motors,[-20,-20])
interface.increaseMotorAngleReferences(motors,[20,-20])
elif left_touched and right_touched:
# obstacle on the way, ???
interface.increaseMotorAngleReferences(motors,[-20,-20])
print "Destination reached!"
interface.stopLogging()
interface.terminate()