質問編集履歴
1
プログラムを変更しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,6 +4,100 @@
|
|
4
4
|
sys.stdout.write("Timer running: %i seconds\r" % (runtime + (motor.start() - start)))
|
5
5
|
TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'
|
6
6
|
```Python
|
7
|
+
import smbus
|
8
|
+
import RPi.GPIO as GPIO
|
9
|
+
from time import sleep
|
10
|
+
import math
|
11
|
+
import atexit
|
12
|
+
import sys
|
13
|
+
import datetime
|
14
|
+
import time
|
15
|
+
import touchphat
|
16
|
+
@touchphat.on_touch("Enter")
|
17
|
+
def start_stop():
|
18
|
+
global start
|
19
|
+
global stopped
|
20
|
+
global runtime
|
21
|
+
if stopped:
|
22
|
+
start = motor.start()
|
23
|
+
stopped = False
|
24
|
+
else:
|
25
|
+
stopped = True
|
26
|
+
runtime += (time.time() - start)
|
27
|
+
|
28
|
+
@touchphat.on_touch("Back")
|
29
|
+
def reset():
|
30
|
+
global runtime
|
31
|
+
runtime = 0
|
32
|
+
|
33
|
+
blanks = " " * 25 + "\r"
|
34
|
+
|
35
|
+
class motorDrive:
|
36
|
+
in1 = 8
|
37
|
+
in2 = 11
|
38
|
+
en = 24
|
39
|
+
temp1 = 1
|
40
|
+
lastState = 'LOW'
|
41
|
+
GPIO.setmode(GPIO.BCM)
|
42
|
+
GPIO.setup(in1, GPIO.OUT)
|
43
|
+
GPIO.setup(in2, GPIO.OUT)
|
44
|
+
GPIO.setup(en, GPIO.OUT)
|
45
|
+
GPIO.output(in1, GPIO.LOW)
|
46
|
+
GPIO.output(in2, GPIO.LOW)
|
47
|
+
p = GPIO.PWM(en, 1000)
|
48
|
+
p.start(55)
|
49
|
+
|
50
|
+
directionState = 'forward'
|
51
|
+
# #print("\n")
|
52
|
+
# #print("The default speed & direction of motor is LOW & Forward.....")
|
53
|
+
# #print("r-run s-stop f-forward b-backward l-low m-medium h-high e-exit")
|
54
|
+
# #print("\n")
|
55
|
+
|
56
|
+
def start(self):
|
57
|
+
|
58
|
+
GPIO.output(self.in1, GPIO.HIGH)
|
59
|
+
GPIO.output(self.in2, GPIO.LOW)
|
60
|
+
self.directionState = 'forward'
|
61
|
+
#print("MOTOR STARTING")
|
62
|
+
|
63
|
+
def setPower(self, power):
|
64
|
+
if self.lastState == power:
|
65
|
+
return
|
66
|
+
if power == 'LOW':
|
67
|
+
self.p.ChangeDutyCycle(30)
|
68
|
+
elif power == 'MEDIUM':
|
69
|
+
self.p.ChangeDutyCycle(50)
|
70
|
+
elif power == 'HIGH':
|
71
|
+
self.p.ChangeDutyCycle(90)
|
72
|
+
|
73
|
+
#print("PWR CHANGED ", self.lastState, " TO ", power)
|
74
|
+
self.lastState = power
|
75
|
+
|
76
|
+
def setDirection(self, direction):
|
77
|
+
if self.directionState == direction:
|
78
|
+
return
|
79
|
+
if direction == "forward":
|
80
|
+
GPIO.output(self.in1, GPIO.HIGH)
|
81
|
+
GPIO.output(self.in2, GPIO.LOW)
|
82
|
+
self.setPower('LOW')
|
83
|
+
else:
|
84
|
+
GPIO.output(self.in1, GPIO.LOW)
|
85
|
+
GPIO.output(self.in2, GPIO.LOW)
|
86
|
+
#print("DIRECTION CHANGED ", self.directionState, " TO ", direction)
|
87
|
+
self.directionState = direction
|
88
|
+
|
89
|
+
def stop(self):
|
90
|
+
GPIO.output(self.in1,GPIO.LOW)
|
91
|
+
GPIO.output(self.in2,GPIO.LOW)
|
92
|
+
#print("MOTOR stopING")
|
93
|
+
|
94
|
+
|
95
|
+
def clean(self):
|
96
|
+
self.stop()
|
97
|
+
GPIO.cleanup()
|
98
|
+
#print('GPIO cleanup')
|
99
|
+
|
100
|
+
|
7
101
|
try:
|
8
102
|
while True:
|
9
103
|
if stopped and runtime > 0:
|
@@ -22,7 +116,7 @@
|
|
22
116
|
sys.stdout.flush()
|
23
117
|
|
24
118
|
accel_data = mpu.get_accel_data()
|
25
|
-
|
119
|
+
|
26
120
|
y_angle = util.get_y_rotation(accel_data['x'], accel_data['y'], accel_data['z'])
|
27
121
|
|
28
122
|
if y_angle < normalMin:
|
@@ -34,7 +128,29 @@
|
|
34
128
|
motor.setPower("HIGH")
|
35
129
|
elif y_angle >= highMin:
|
36
130
|
motor.setPower("MEDIUM")
|
37
|
-
|
131
|
+
time.sleep(5)
|
132
|
+
finally:
|
133
|
+
sys.stdout.close()
|
134
|
+
motor.clean()
|
135
|
+
time.sleep(0.5)
|
136
|
+
|
137
|
+
atexit.register(exitHandler)
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
|
38
154
|
```
|
39
155
|
|
40
156
|
|