-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotors.py
147 lines (102 loc) · 2.53 KB
/
motors.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
from time import sleep
sleep(10)
f = open("/boot/tempreadings.txt", "w")
f.write("Starting rover program\n")
import RPi.GPIO as GPIO
motor1f = 21
motor1r = 22
motor2f = 23
motor2r = 24
led = 25
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(motor1f, GPIO.OUT)
GPIO.setup(motor1r, GPIO.OUT)
GPIO.setup(motor2f, GPIO.OUT)
GPIO.setup(motor2r, GPIO.OUT)
GPIO.setup(led, GPIO.OUT)
GPIO.output(motor1f, GPIO.LOW)
GPIO.output(motor1r, GPIO.LOW)
GPIO.output(motor2f, GPIO.LOW)
GPIO.output(motor2r, GPIO.LOW)
GPIO.output(led, GPIO.LOW)
sleep(5)
import os
import glob
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
def temperature():
f.write("Getting temperature\n")
c = read_temp()
a = 22
if c > a:
GPIO.output(led, GPIO.HIGH)
else:
GPIO.output(led, GPIO.LOW)
f.write("Temperature is: " + str(c) + "\n")
def forwards():
f.write("Moving forwards\n")
GPIO.output(motor1f, GPIO.HIGH)
GPIO.output(motor2f, GPIO.HIGH)
sleep(3)
GPIO.output(motor1f, GPIO.LOW)
GPIO.output(motor2f, GPIO.LOW)
temperature()
sleep(0.5)
def turnLeft():
f.write("Turning Left\n")
GPIO.output(motor1f, GPIO.HIGH)
GPIO.output(motor2r, GPIO.HIGH)
sleep(3)
GPIO.output(motor2r, GPIO.LOW)
GPIO.output(motor1f, GPIO.LOW)
sleep(0.5)
def turnRight():
f.write("Turning Right\n")
GPIO.output(motor1r, GPIO.HIGH)
GPIO.output(motor2f, GPIO.HIGH)
sleep(3)
GPIO.output(motor2f, GPIO.LOW)
GPIO.output(motor1r, GPIO.LOW)
sleep(0.5)
f.write("Motor control\n")
for i in range(10):
for x in range(10):
forwards()
turnLeft()
forwards()
turnLeft()
for z in range(10):
forwards()
turnRight()
forwards()
turnRight()
turnRight()
turnRight()
for v in range(20):
forwards()
f.write("Finished\n")
GPIO.output(motor1f, GPIO.LOW)
GPIO.output(motor1r, GPIO.LOW)
GPIO.output(motor2f, GPIO.LOW)
GPIO.output(motor2r, GPIO.LOW)
GPIO.output(led, GPIO.LOW)
f.close()