python 树莓派 基础移动demo(记得要把中文注释删除掉)
2021年5月14日
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ * @par Copyright (C): 2010-2020, hunan CLB Tech * @file Basic_movement * @version V2.0 * @details * @par History @author: zhangfang """ import RPi.GPIO as GPIO import time PWMA = 18 AIN1 = 22 AIN2 = 27 PWMB = 23 BIN1 = 25 BIN2 = 24 GPIO.setwarnings(False) #警告提示关闭 GPIO.setmode(GPIO.BCM) #按物理位置对GPIO编号 GPIO.setup(AIN1, GPIO.OUT) GPIO.setup(AIN2, GPIO.OUT) GPIO.setup(PWMA, GPIO.OUT) GPIO.setup(BIN1, GPIO.OUT) GPIO.setup(BIN2, GPIO.OUT) GPIO.setup(PWMB, GPIO.OUT) L_Motor = GPIO.PWM(PWMA, 100) L_Motor.start(0) R_Motor = GPIO.PWM(PWMB, 100) R_Motor.start(0) ## #定义前进的方法 # def t_up(speed, t_time): L_Motor.ChangeDutyCycle(speed) #左电机速率设置 GPIO.output(AIN2, False) #4轮 GPIO.output(AIN1, True) #4轮 R_Motor.ChangeDutyCycle(speed) #右电机速率设置 GPIO.output(BIN2, False) #4轮 GPIO.output(BIN1, True) #4轮 time.sleep(t_time) ## #定义停止的方法 # def t_stop(t_time): L_Motor.ChangeDutyCycle(0) GPIO.output(AIN2, False) #AIN2 GPIO.output(AIN1, False) #AIN1 R_Motor.ChangeDutyCycle(0) GPIO.output(BIN2, False) #BIN2 GPIO.output(BIN1, False) #BIN1 time.sleep(t_time) ## #叮后后退的方法 # def t_down(speed, t_time): L_Motor.ChangeDutyCycle(speed) GPIO.output(AIN2, True) #AIN2 GPIO.output(AIN1, False) #AIN1 R_Motor.ChangeDutyCycle(speed) GPIO.output(BIN2, True) #BIN2 GPIO.output(BIN1, False) #BIN1 time.sleep(t_time) ## #定义左拐的方法 # def t_left(speed, t_time): L_Motor.ChangeDutyCycle(speed) GPIO.output(AIN2, True) #AIN2 GPIO.output(AIN1, False) #AIN1 R_Motor.ChangeDutyCycle(speed) GPIO.output(BIN2, False) #BIN2 GPIO.output(BIN1, True) #BIN1 time.sleep(t_time) ## #定义右拐的方法 # def t_right(speed, t_time): L_Motor.ChangeDutyCycle(speed) GPIO.output(AIN2, False) # AIN2 GPIO.output(AIN1, True) # AIN1 R_Motor.ChangeDutyCycle(speed) GPIO.output(BIN2, True) # BIN2 GPIO.output(BIN1, False) # BIN1 time.sleep(t_time) try: while True: t_up(50, 3) t_down(50, 3) t_left(50, 3) t_right(50, 3) t_stop(3) except KeyboardInterrupt: GPIO.cleanup() |