Up Python ソースプログラム例 作成: 2021-01-21
更新: 2021-01-21


AI coordinator から引用:
#!/usr/bin/python from Adafruit_MotorHAT import Adafruit_MotorHAT import time import atexit import Adafruit_PCA9685 # create a default object, no changes to I2C address or frequency mh = Adafruit_MotorHAT(addr=0x60) myMotor1 = mh.getMotor(1) myMotor2 = mh.getMotor(2) # servo pwm = Adafruit_PCA9685.PCA9685(address=0x60) pwm.set_pwm_freq(60) def move(degree_1,degree_2,degree_3,degree_8): degree_1 = int(degree_1 * 5.27) degree_2 = int(degree_2 * 5.27) degree_3 = int(degree_3 * 5.27) degree_8 = int(degree_8 * 5.27) pwm.set_pwm(0, 0, degree_1) pwm.set_pwm(1, 0, degree_2) pwm.set_pwm(2, 0, degree_3) pwm.set_pwm(7, 0, degree_8) while (True): command = raw_input() if command == 'f': print "Forward! " myMotor1.run(Adafruit_MotorHAT.FORWARD) myMotor2.run(Adafruit_MotorHAT.FORWARD) myMotor1.setSpeed(255) myMotor2.setSpeed(255) time.sleep(1.5) print "Release" myMotor1.run(Adafruit_MotorHAT.RELEASE) myMotor2.run(Adafruit_MotorHAT.RELEASE) time.sleep(1.0) elif command == 'b': print "Backward! " myMotor1.run(Adafruit_MotorHAT.BACKWARD) myMotor2.run(Adafruit_MotorHAT.BACKWARD) myMotor1.setSpeed(255) myMotor2.setSpeed(255) time.sleep(1.5) print "Release" myMotor1.run(Adafruit_MotorHAT.RELEASE) myMotor2.run(Adafruit_MotorHAT.RELEASE) time.sleep(1.0) elif command == 'r': print "Backward! " myMotor1.run(Adafruit_MotorHAT.FORWARD) myMotor2.run(Adafruit_MotorHAT.BACKWARD) myMotor1.setSpeed(255) myMotor2.setSpeed(255) time.sleep(1.5) print "Release" myMotor1.run(Adafruit_MotorHAT.RELEASE) myMotor2.run(Adafruit_MotorHAT.RELEASE) time.sleep(1.0) elif command == 'l': print "Backward! " myMotor1.run(Adafruit_MotorHAT.BACKWARD) myMotor2.run(Adafruit_MotorHAT.FORWARD) myMotor1.setSpeed(255) myMotor2.setSpeed(255) time.sleep(1.5) print "Release" myMotor1.run(Adafruit_MotorHAT.RELEASE) myMotor2.run(Adafruit_MotorHAT.RELEASE) time.sleep(1.0) elif command == '1': move(65,0,0,0) time.sleep(1) move(95,0,0,0) time.sleep(1.0) elif command == '2': move(0,90,0,0) time.sleep(1) move(0,65,0,0) time.sleep(1.0) elif command == '3': move(0,0,90,0) time.sleep(1) move(0,0,55,0) time.sleep(1.0) elif command == '8': move(0,0,0,35) time.sleep(1) move(0,0,0,90) time.sleep(1.0) move(0,0,0,60) time.sleep(1.0) elif command == '0': move(65,90,55,0) time.sleep(1) elif command == 'all': print "Forward! " myMotor1.run(Adafruit_MotorHAT.FORWARD) myMotor2.run(Adafruit_MotorHAT.FORWARD) myMotor1.setSpeed(255) myMotor2.setSpeed(255) time.sleep(1) print "Release" myMotor1.run(Adafruit_MotorHAT.RELEASE) myMotor2.run(Adafruit_MotorHAT.RELEASE) time.sleep(1) print "Backward! " myMotor1.run(Adafruit_MotorHAT.BACKWARD) myMotor2.run(Adafruit_MotorHAT.BACKWARD) myMotor1.setSpeed(255) myMotor2.setSpeed(255) time.sleep(1) print "Release" myMotor1.run(Adafruit_MotorHAT.RELEASE) myMotor2.run(Adafruit_MotorHAT.RELEASE) time.sleep(1) print "Backward! " myMotor1.run(Adafruit_MotorHAT.FORWARD) myMotor2.run(Adafruit_MotorHAT.BACKWARD) myMotor1.setSpeed(255) myMotor2.setSpeed(255) time.sleep(1) print "Release" myMotor1.run(Adafruit_MotorHAT.RELEASE) myMotor2.run(Adafruit_MotorHAT.RELEASE) time.sleep(1) print "Backward! " myMotor1.run(Adafruit_MotorHAT.BACKWARD) myMotor2.run(Adafruit_MotorHAT.FORWARD) myMotor1.setSpeed(255) myMotor2.setSpeed(255) time.sleep(1) print "Release" myMotor1.run(Adafruit_MotorHAT.RELEASE) myMotor2.run(Adafruit_MotorHAT.RELEASE) time.sleep(1) move(95,0,0,0) time.sleep(0.5) move(0,90,0,0) time.sleep(0.5) move(0,65,0,0) time.sleep(0.5) move(0,0,90,0) time.sleep(0.5) move(0,0,55,0) time.sleep(0.5) move(0,0,0,35) time.sleep(0.5) move(0,0,0,90) time.sleep(0.5) move(0,0,0,60) time.sleep(0.5) move(65,90,55,0) time.sleep(0.5)