プログラムを実行すると下記のようなエラーがでてしまいます,修正するにはどのようにしたらよろしいのでしょうか.
Traceback (most recent call last):
File "/home/pi/Desktop/0901.py", line 362, in <module>
sys.stdout.write("Timer running: %i seconds\r" % (runtime + (motor.start() - start)))
TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'
Python
1 import smbus 2import RPi.GPIO as GPIO 3from time import sleep 4import math 5import atexit 6import sys 7import datetime 8import time 9import touchphat 10@touchphat.on_touch("Enter") 11def start_stop(): 12 global start 13 global stopped 14 global runtime 15 if stopped: 16 start = motor.start() 17 stopped = False 18 else: 19 stopped = True 20 runtime += (time.time() - start) 21 22@touchphat.on_touch("Back") 23def reset(): 24 global runtime 25 runtime = 0 26 27blanks = " " * 25 + "\r" 28 29class motorDrive: 30 in1 = 8 31 in2 = 11 32 en = 24 33 temp1 = 1 34 lastState = 'LOW' 35 GPIO.setmode(GPIO.BCM) 36 GPIO.setup(in1, GPIO.OUT) 37 GPIO.setup(in2, GPIO.OUT) 38 GPIO.setup(en, GPIO.OUT) 39 GPIO.output(in1, GPIO.LOW) 40 GPIO.output(in2, GPIO.LOW) 41 p = GPIO.PWM(en, 1000) 42 p.start(55) 43 44 directionState = 'forward' 45 # #print("\n") 46 # #print("The default speed & direction of motor is LOW & Forward.....") 47 # #print("r-run s-stop f-forward b-backward l-low m-medium h-high e-exit") 48 # #print("\n") 49 50 def start(self): 51 52 GPIO.output(self.in1, GPIO.HIGH) 53 GPIO.output(self.in2, GPIO.LOW) 54 self.directionState = 'forward' 55 #print("MOTOR STARTING") 56 57 def setPower(self, power): 58 if self.lastState == power: 59 return 60 if power == 'LOW': 61 self.p.ChangeDutyCycle(30) 62 elif power == 'MEDIUM': 63 self.p.ChangeDutyCycle(50) 64 elif power == 'HIGH': 65 self.p.ChangeDutyCycle(90) 66 67 #print("PWR CHANGED ", self.lastState, " TO ", power) 68 self.lastState = power 69 70 def setDirection(self, direction): 71 if self.directionState == direction: 72 return 73 if direction == "forward": 74 GPIO.output(self.in1, GPIO.HIGH) 75 GPIO.output(self.in2, GPIO.LOW) 76 self.setPower('LOW') 77 else: 78 GPIO.output(self.in1, GPIO.LOW) 79 GPIO.output(self.in2, GPIO.LOW) 80 #print("DIRECTION CHANGED ", self.directionState, " TO ", direction) 81 self.directionState = direction 82 83 def stop(self): 84 GPIO.output(self.in1,GPIO.LOW) 85 GPIO.output(self.in2,GPIO.LOW) 86 #print("MOTOR stopING") 87 88 89 def clean(self): 90 self.stop() 91 GPIO.cleanup() 92 #print('GPIO cleanup') 93 94 95try: 96 while True: 97 if stopped and runtime > 0: 98 sys.stdout.write(blanks) 99 sys.stdout.write("motorstart: %i seconds\r" % runtime) 100 sys.stdout.flush() 101 102 elif stopped: 103 sys.stdout.write(blanks) 104 sys.stdout.write("Touch enter to start!\r") 105 sys.stdout.flush() 106 107 else: 108 sys.stdout.write(blanks) 109 sys.stdout.write("Timer running: %i seconds\r" % (runtime + (motor.start() - start))) 110 sys.stdout.flush() 111 112 accel_data = mpu.get_accel_data() 113 114 y_angle = util.get_y_rotation(accel_data['x'], accel_data['y'], accel_data['z']) 115 116 if y_angle < normalMin: 117 motor.setDirection("stop") 118 else: 119 motor.setDirection("forward") 120 121 if y_angle >= mediumMin and highMin < y_angle: 122 motor.setPower("HIGH") 123 elif y_angle >= highMin: 124 motor.setPower("MEDIUM") 125 time.sleep(5) 126finally: 127 sys.stdout.close() 128 motor.clean() 129 time.sleep(0.5) 130 131atexit.register(exitHandler) 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
回答1件
あなたの回答
tips
プレビュー
2020/10/16 06:49
2020/10/16 06:57
2020/10/16 06:57