質問編集履歴
1
文章を書きなおしました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,359 +1 @@
|
|
1
|
-
python言語を用いて角度に応じてDCモータの回転数を変化させるプログラムを作成したいのですがどのようなにプログラムを組めばいいのでしょうか。ご教授お願いします。
|
1
|
+
python言語を用いて角度に応じてDCモータの回転数を変化させるプログラムを作成したいのですがどのようなにプログラムを組めばいいのでしょうか。ご教授お願いします。
|
2
|
-
|
3
|
-
```python
|
4
|
-
|
5
|
-
import smbus
|
6
|
-
|
7
|
-
import RPi.GPIO as GPIO
|
8
|
-
|
9
|
-
from time import sleep
|
10
|
-
|
11
|
-
import math
|
12
|
-
|
13
|
-
import atexit
|
14
|
-
|
15
|
-
import sys
|
16
|
-
|
17
|
-
import datetime
|
18
|
-
|
19
|
-
import time
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
millis = int(round(time.time() * 1000))
|
26
|
-
|
27
|
-
stopped = True
|
28
|
-
|
29
|
-
runtime = 0
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
'''@touchphat.on_touch("Enter")
|
34
|
-
|
35
|
-
def start_stop():
|
36
|
-
|
37
|
-
global start
|
38
|
-
|
39
|
-
global stopped
|
40
|
-
|
41
|
-
global runtime
|
42
|
-
|
43
|
-
if stopped:
|
44
|
-
|
45
|
-
start = time.time()
|
46
|
-
|
47
|
-
stopped = False
|
48
|
-
|
49
|
-
else:
|
50
|
-
|
51
|
-
stopped = True
|
52
|
-
|
53
|
-
runtime += (time.time() - start)
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
@touchphat.on_touch("Back")
|
58
|
-
|
59
|
-
def reset():
|
60
|
-
|
61
|
-
global runtime
|
62
|
-
|
63
|
-
runtime = 0
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
blanks = " " * 25 + "\r"'''
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
class motorDrive:
|
72
|
-
|
73
|
-
in1 = 8
|
74
|
-
|
75
|
-
in2 = 11
|
76
|
-
|
77
|
-
en = 24
|
78
|
-
|
79
|
-
temp1 = 1
|
80
|
-
|
81
|
-
lastState = 'LOW'
|
82
|
-
|
83
|
-
GPIO.setmode(GPIO.BCM)
|
84
|
-
|
85
|
-
GPIO.setup(in1, GPIO.OUT)
|
86
|
-
|
87
|
-
GPIO.setup(in2, GPIO.OUT)
|
88
|
-
|
89
|
-
GPIO.setup(en, GPIO.OUT)
|
90
|
-
|
91
|
-
GPIO.output(in1, GPIO.LOW)
|
92
|
-
|
93
|
-
GPIO.output(in2, GPIO.LOW)
|
94
|
-
|
95
|
-
p = GPIO.PWM(en, 1000)
|
96
|
-
|
97
|
-
p.start(55)
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
directionState = 'forward'
|
102
|
-
|
103
|
-
# #print("\n")
|
104
|
-
|
105
|
-
# #print("The default speed & direction of motor is LOW & Forward.....")
|
106
|
-
|
107
|
-
# #print("r-run s-stop f-forward b-backward l-low m-medium h-high e-exit")
|
108
|
-
|
109
|
-
# #print("\n")
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
def start(self):
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
GPIO.output(self.in1, GPIO.HIGH)
|
118
|
-
|
119
|
-
GPIO.output(self.in2, GPIO.LOW)
|
120
|
-
|
121
|
-
self.directionState = 'forward'
|
122
|
-
|
123
|
-
#print("MOTOR STARTING")
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
def setPower(self, power):
|
128
|
-
|
129
|
-
if self.lastState == power:
|
130
|
-
|
131
|
-
return
|
132
|
-
|
133
|
-
if power == 'LOW':
|
134
|
-
|
135
|
-
self.p.ChangeDutyCycle(55)
|
136
|
-
|
137
|
-
elif power == 'MEDIUM':
|
138
|
-
|
139
|
-
self.p.ChangeDutyCycle(70)
|
140
|
-
|
141
|
-
elif power == 'HIGH':
|
142
|
-
|
143
|
-
self.p.ChangeDutyCycle(90)
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
#print("PWR CHANGED ", self.lastState, " TO ", power)
|
148
|
-
|
149
|
-
self.lastState = power
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
def setDirection(self, direction):
|
154
|
-
|
155
|
-
if self.directionState == direction:
|
156
|
-
|
157
|
-
return
|
158
|
-
|
159
|
-
if direction == "forward":
|
160
|
-
|
161
|
-
GPIO.output(self.in1, GPIO.HIGH)
|
162
|
-
|
163
|
-
GPIO.output(self.in2, GPIO.LOW)
|
164
|
-
|
165
|
-
self.setPower('LOW')
|
166
|
-
|
167
|
-
else:
|
168
|
-
|
169
|
-
GPIO.output(self.in1, GPIO.LOW)
|
170
|
-
|
171
|
-
GPIO.output(self.in2, GPIO.LOW)
|
172
|
-
|
173
|
-
#print("DIRECTION CHANGED ", self.directionState, " TO ", direction)
|
174
|
-
|
175
|
-
self.directionState = direction
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
def stop(self):
|
180
|
-
|
181
|
-
GPIO.output(self.in1,GPIO.LOW)
|
182
|
-
|
183
|
-
GPIO.output(self.in2,GPIO.LOW)
|
184
|
-
|
185
|
-
#print("MOTOR stopING")
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
def clean(self):
|
192
|
-
|
193
|
-
self.stop()
|
194
|
-
|
195
|
-
GPIO.cleanup()
|
196
|
-
|
197
|
-
#print('GPIO cleanup')
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
class mpu6050:
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
# Global Variables
|
206
|
-
|
207
|
-
GRAVITIY_MS2 = 9.80665
|
208
|
-
|
209
|
-
address = None
|
210
|
-
|
211
|
-
bus = None
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
# Scale Modifiers
|
216
|
-
|
217
|
-
ACCEL_SCALE_MODIFIER_2G = 16384.0
|
218
|
-
|
219
|
-
ACCEL_SCALE_MODIFIER_4G = 16384.0
|
220
|
-
|
221
|
-
ACCEL_SCALE_MODIFIER_8G = 16384.0
|
222
|
-
|
223
|
-
ACCEL_SCALE_MODIFIER_16G = 2048.0
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
GYRO_SCALE_MODIFIER_250DEG = 131.0
|
228
|
-
|
229
|
-
GYRO_SCALE_MODIFIER_500DEG = 131.0
|
230
|
-
|
231
|
-
GYRO_SCALE_MODIFIER_1000DEG = 131.0
|
232
|
-
|
233
|
-
GYRO_SCALE_MODIFIER_2000DEG = 16.4
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
# Pre-defined ranges
|
238
|
-
|
239
|
-
ACCEL_RANGE_2G = 0x00
|
240
|
-
|
241
|
-
ACCEL_RANGE_4G = 0x08
|
242
|
-
|
243
|
-
ACCEL_RANGE_8G = 0x10
|
244
|
-
|
245
|
-
ACCEL_RANGE_16G = 0x18
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
GYRO_RANGE_250DEG = 0x00
|
250
|
-
|
251
|
-
GYRO_RANGE_500DEG = 0x08
|
252
|
-
|
253
|
-
GYRO_RANGE_1000DEG = 0x10
|
254
|
-
|
255
|
-
GYRO_RANGE_2000DEG = 0x18
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
# MPU-6050 Registers
|
260
|
-
|
261
|
-
PWR_MGMT_1 = 0x6B
|
262
|
-
|
263
|
-
PWR_MGMT_2 = 0x6C
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
ACCEL_XOUT0 = 0x3B
|
268
|
-
|
269
|
-
ACCEL_YOUT0 = 0x3D
|
270
|
-
|
271
|
-
ACCEL_ZOUT0 = 0x3F
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
TEMP_OUT0 = 0x41
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
GYRO_XOUT0 = 0x43
|
280
|
-
|
281
|
-
GYRO_YOUT0 = 0x45
|
282
|
-
|
283
|
-
GYRO_ZOUT0 = 0x47
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
ACCEL_CONFIG = 0x1C
|
288
|
-
|
289
|
-
GYRO_CONFIG = 0x1B
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
def __init__(self, address, bus=1):
|
294
|
-
|
295
|
-
self.address = address
|
296
|
-
|
297
|
-
self.bus = smbus.SMBus(bus)
|
298
|
-
|
299
|
-
# Wake up the MPU-6050 since it starts in sleep mode
|
300
|
-
|
301
|
-
self.bus.write_byte_data(self.address, self.PWR_MGMT_1, 0x00)
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
# I2C communication methods
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
def read_i2c_word(self, register):
|
310
|
-
|
311
|
-
# Read the data from the registers
|
312
|
-
|
313
|
-
high = self.bus.read_byte_data(self.address, register)
|
314
|
-
|
315
|
-
low = self.bus.read_byte_data(self.address, register + 1)
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
value = (high << 8) + low
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
```
|