标签:stop none utf-8 nan argv put 退出 usr elf
1、树莓派
2、充电宝
3、小车套件(底盘,车轮,电机,电池座)
6、L298N
4、导线,杜邦线
第一步接线:
ENA两条线接在树莓派的5、6端口
ENB两条线接在树莓派的13、19端口
IN4—IN4分别接在21、22、23、24
Out1 Out2接在左边的电机
Out3 Out4接在右边的电机
第二步写程序:
1、创建文件夹
sudo nano car.py
2、写代码
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author :hstking 4 #E-mail :hstking@hotmail.com 5 #Ctime :2015/09/21 6 #Mtime : 7 #Version : 8 9 10 import RPi.GPIO as GPIO 11 import time 12 import sys 13 14 15 #### 定义Car类 16 class Car(object): 17 def __init__(self): 18 self.enab_pin = [5,6,13,19] 19 #### self.enab_pin是使能端的pin 20 self.inx_pin = [21,22,23,24] 21 #### self.inx_pin是控制端in的pin 22 self.RightAhead_pin = self.inx_pin[0] 23 self.RightBack_pin = self.inx_pin[1] 24 self.LeftAhead_pin = self.inx_pin[2] 25 self.LeftBack_pin = self.inx_pin[3] 26 #### 分别是右轮前进,右轮退后,左轮前进,左轮退后的pin 27 self.setup() 28 29 #### setup函数初始化端口 30 def setup(self): 31 print "begin setup ena enb pin" 32 GPIO.setmode(GPIO.BCM) 33 GPIO.setwarnings(False) 34 for pin in self.enab_pin: 35 GPIO.setup(pin,GPIO.OUT) 36 GPIO.output(pin,GPIO.HIGH) 37 #### 初始化使能端pin,设置成高电平 38 pin = None 39 for pin in self.inx_pin: 40 GPIO.setup(pin,GPIO.OUT) 41 GPIO.output(pin,GPIO.LOW) 42 #### 初始化控制端pin,设置成低电平 43 print "setup ena enb pin over" 44 45 #### fornt函数,小车前进 46 def front(self): 47 self.setup() 48 GPIO.output(self.RightAhead_pin,GPIO.HIGH) 49 GPIO.output(self.LeftAhead_pin,GPIO.HIGH) 50 51 #### leftFront函数,小车左拐弯 52 def leftFront(self): 53 self.setup() 54 GPIO.output(self.RightAhead_pin,GPIO.HIGH) 55 56 #### rightFront函数,小车右拐弯 57 def rightFront(self): 58 self.setup() 59 GPIO.output(self.LeftAhead_pin,GPIO.HIGH) 60 61 #### rear函数,小车后退 62 def rear(self): 63 self.setup() 64 GPIO.output(self.RightBack_pin,GPIO.HIGH) 65 GPIO.output(self.LeftBack_pin,GPIO.HIGH) 66 67 #### leftRear函数,小车左退 68 def leftRear(self): 69 self.setup() 70 GPIO.output(self.RightBack_pin,GPIO.HIGH) 71 72 #### rightRear函数,小车右退 73 def rightRear(self): 74 self.setup() 75 GPIO.output(self.LeftBack_pin,GPIO.HIGH) 76 77 #### 定义main主函数 78 def main(status): 79 car = Car() 80 if status == "front": 81 car.front() 82 elif status == "leftFront": 83 car.leftFront() 84 elif status == "rightFront": 85 car.rightFront() 86 elif status == "rear": 87 car.rear() 88 elif status == "leftRear": 89 car.leftRear() 90 elif status == "rightRear": 91 car.rightRear() 92 elif status == "stop": 93 car.setup() 94 95 96 if __name__ == ‘__main__‘: 97 main(sys.argv[1])
3、保存并退出
大功告成我们可以开始玩小车了
Sudo python ./car.py front 前进
Sudo python ./car.py rear 后退
Sudo python ./car.py leftFront 左拐
Sudo python ./car.py rightFront 右拐
标签:stop none utf-8 nan argv put 退出 usr elf
原文地址:https://www.cnblogs.com/RM-Anton/p/9406805.html