Up 障害物の手前で止まる 作成: 2021-04-03
更新: 2021-05-10


    Dexter distance sensor を使うと,エラーが発生する。
    エラーは,発生させるほどひどくなる。

    このエラーがサーボポート関連によるものでないことを,確かめておく。
    サーボポートは使わず,サーボのケーブルを GPIOピンに接続する。


    (venv) $ vi obstacle_stop.py
    #!/usr/bin/env python from time import sleep ###### motor ############################# from easygopigo3 import EasyGoPiGo3 egpg = EasyGoPiGo3() # Moving # go forward 1sec def go_fwd(): egpg.set_speed(100) egpg.forward() sleep(1) egpg.stop() ###### distance sensor ############################# from di_sensors.easy_distance_sensor import EasyDistanceSensor # Distance Sensor dist_sensor = EasyDistanceSensor() #Distance from obstacle where the GoPiGo should stop : 30cm distance_to_stop = 30 ###### servo ############################# import RPi.GPIO as GPIO # Set the GPIO pins by physical pin number GPIO.setmode(GPIO.BOARD) Servo_pin = 16 GPIO.setup(Servo_pin, GPIO.OUT) # PWM (Pulse Width Modulation) の設定 # SG90 の周波数は50[Hz] Servo = GPIO.PWM(Servo_pin, 50) # 指定角度 (単位 : 度) にサーボを定位 def servo_angle(angle): # 角度に対応するデューティ比 (%) を求める duty = 2.5 + (12.0 - 2.5) * (angle + 90) / 180 # デューティ比を変更 Servo.ChangeDutyCycle(duty) #0.3秒間待つ sleep(0.3) ###### exit process ############################# import sys # Exit def destroy(): #サーボモータをストップ Servo.ChangeDutyCycle(7.25) sleep(0.3) Servo.stop() #GPIOをクリーンアップ GPIO.cleanup() egpg.reset_all() sys.exit() #### The Program starts from here ############### print("Press ENTER to start") #Wait for input to start input() # デューティ比 7.25% (角度0) でサーボをスタート (固定) Servo.start(2.5) sleep(0.3) #Start moving go_fwd() try: while True: #Find the distance of the object in front dist = dist_sensor.read() sleep(1) print("Dist:",dist,'cm') #If the object is closer than distance_to_stop, stop the GoPiGo if dist < distance_to_stop: print("obstacle!") egpg.stop() destroy() sys.exit() else: go_fwd() # except the program gets interrupted by Ctrl+C on the keyboard. except KeyboardInterrupt: egpg.stop() destroy()

    (venv) $ chmod +x obstacle_stop.py
    (venv) $ ./obstacle_stop.py Press ENTER to start Dist: 300 cm [Errno 5] Input/output error [Errno 5] Input/output error [Errno 5] Input/output error Dist: 0 cm obstacle! (venv) $ ./obstacle_stop.py Traceback (most recent call last): File "./obstacle_stop.py", line 26, in <module> dist_sensor = EasyDistanceSensor() File "/usr/local/lib/python3.7/dist-packages/ DI_Sensors-1.0.0-py3.7.egg/di_sensors/easy_distance_sensor.py", line 56, in __init__ File "/usr/local/lib/python3.7/dist-packages/ DI_Sensors-1.0.0-py3.7.egg/di_sensors/distance_sensor.py", line 28, in __init__ File "/usr/local/lib/python3.7/dist-packages/ DI_Sensors-1.0.0-py3.7.egg/di_sensors/VL53L0X.py", line 126, in __init__ File "/usr/local/lib/python3.7/dist-packages/ Dexter_AutoDetection_and_I2C_Mutex-0.0.0-py3.7.egg/di_i2c.py", line 220, in write_reg_8 File "/usr/local/lib/python3.7/dist-packages/ Dexter_AutoDetection_and_I2C_Mutex-0.0.0-py3.7.egg/di_i2c.py", line 185, in transfer File "/usr/local/lib/python3.7/dist-packages/ Dexter_AutoDetection_and_I2C_Mutex-0.0.0-py3.7.egg/di_i2c.py", line 469, in transfer OSError: [Errno 5] Input/output error