質問編集履歴

1

プログラムを変更しました。

2020/10/16 07:26

投稿

1617028s
1617028s

スコア5

test CHANGED
File without changes
test CHANGED
@@ -10,6 +10,194 @@
10
10
 
11
11
  ```Python
12
12
 
13
+ import smbus
14
+
15
+ import RPi.GPIO as GPIO
16
+
17
+ from time import sleep
18
+
19
+ import math
20
+
21
+ import atexit
22
+
23
+ import sys
24
+
25
+ import datetime
26
+
27
+ import time
28
+
29
+ import touchphat
30
+
31
+ @touchphat.on_touch("Enter")
32
+
33
+ def start_stop():
34
+
35
+ global start
36
+
37
+ global stopped
38
+
39
+ global runtime
40
+
41
+ if stopped:
42
+
43
+ start = motor.start()
44
+
45
+ stopped = False
46
+
47
+ else:
48
+
49
+ stopped = True
50
+
51
+ runtime += (time.time() - start)
52
+
53
+
54
+
55
+ @touchphat.on_touch("Back")
56
+
57
+ def reset():
58
+
59
+ global runtime
60
+
61
+ runtime = 0
62
+
63
+
64
+
65
+ blanks = " " * 25 + "\r"
66
+
67
+
68
+
69
+ class motorDrive:
70
+
71
+ in1 = 8
72
+
73
+ in2 = 11
74
+
75
+ en = 24
76
+
77
+ temp1 = 1
78
+
79
+ lastState = 'LOW'
80
+
81
+ GPIO.setmode(GPIO.BCM)
82
+
83
+ GPIO.setup(in1, GPIO.OUT)
84
+
85
+ GPIO.setup(in2, GPIO.OUT)
86
+
87
+ GPIO.setup(en, GPIO.OUT)
88
+
89
+ GPIO.output(in1, GPIO.LOW)
90
+
91
+ GPIO.output(in2, GPIO.LOW)
92
+
93
+ p = GPIO.PWM(en, 1000)
94
+
95
+ p.start(55)
96
+
97
+
98
+
99
+ directionState = 'forward'
100
+
101
+ # #print("\n")
102
+
103
+ # #print("The default speed & direction of motor is LOW & Forward.....")
104
+
105
+ # #print("r-run s-stop f-forward b-backward l-low m-medium h-high e-exit")
106
+
107
+ # #print("\n")
108
+
109
+
110
+
111
+ def start(self):
112
+
113
+
114
+
115
+ GPIO.output(self.in1, GPIO.HIGH)
116
+
117
+ GPIO.output(self.in2, GPIO.LOW)
118
+
119
+ self.directionState = 'forward'
120
+
121
+ #print("MOTOR STARTING")
122
+
123
+
124
+
125
+ def setPower(self, power):
126
+
127
+ if self.lastState == power:
128
+
129
+ return
130
+
131
+ if power == 'LOW':
132
+
133
+ self.p.ChangeDutyCycle(30)
134
+
135
+ elif power == 'MEDIUM':
136
+
137
+ self.p.ChangeDutyCycle(50)
138
+
139
+ elif power == 'HIGH':
140
+
141
+ self.p.ChangeDutyCycle(90)
142
+
143
+
144
+
145
+ #print("PWR CHANGED ", self.lastState, " TO ", power)
146
+
147
+ self.lastState = power
148
+
149
+
150
+
151
+ def setDirection(self, direction):
152
+
153
+ if self.directionState == direction:
154
+
155
+ return
156
+
157
+ if direction == "forward":
158
+
159
+ GPIO.output(self.in1, GPIO.HIGH)
160
+
161
+ GPIO.output(self.in2, GPIO.LOW)
162
+
163
+ self.setPower('LOW')
164
+
165
+ else:
166
+
167
+ GPIO.output(self.in1, GPIO.LOW)
168
+
169
+ GPIO.output(self.in2, GPIO.LOW)
170
+
171
+ #print("DIRECTION CHANGED ", self.directionState, " TO ", direction)
172
+
173
+ self.directionState = direction
174
+
175
+
176
+
177
+ def stop(self):
178
+
179
+ GPIO.output(self.in1,GPIO.LOW)
180
+
181
+ GPIO.output(self.in2,GPIO.LOW)
182
+
183
+ #print("MOTOR stopING")
184
+
185
+
186
+
187
+
188
+
189
+ def clean(self):
190
+
191
+ self.stop()
192
+
193
+ GPIO.cleanup()
194
+
195
+ #print('GPIO cleanup')
196
+
197
+
198
+
199
+
200
+
13
201
  try:
14
202
 
15
203
  while True:
@@ -46,7 +234,7 @@
46
234
 
47
235
  accel_data = mpu.get_accel_data()
48
236
 
49
- #x_angle = util.get_x_rotation(accel_data['x'], accel_data['y'], accel_data['z'])
237
+
50
238
 
51
239
  y_angle = util.get_y_rotation(accel_data['x'], accel_data['y'], accel_data['z'])
52
240
 
@@ -70,7 +258,51 @@
70
258
 
71
259
  motor.setPower("MEDIUM")
72
260
 
73
-
261
+ time.sleep(5)
262
+
263
+ finally:
264
+
265
+ sys.stdout.close()
266
+
267
+ motor.clean()
268
+
269
+ time.sleep(0.5)
270
+
271
+
272
+
273
+ atexit.register(exitHandler)
274
+
275
+
276
+
277
+
278
+
279
+
280
+
281
+
282
+
283
+
284
+
285
+
286
+
287
+
288
+
289
+
290
+
291
+
292
+
293
+
294
+
295
+
296
+
297
+
298
+
299
+
300
+
301
+
302
+
303
+
304
+
305
+
74
306
 
75
307
  ```
76
308