-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcar.py
More file actions
107 lines (90 loc) · 2.29 KB
/
car.py
File metadata and controls
107 lines (90 loc) · 2.29 KB
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
#! /usr/bin/python
# -*- coding:UTF-8 -*-
import sys
import RPi.GPIO as GPIO
import time
import motor as motor
import Getch
'''
L298N驱动马达状态表:
--------------------------------------------
EN | IN1 | IN2 | 电机状态
--------------------------------------------
低 | 任意 | 任意 | 停止
高 | 低 | 低 | 制动
高 | 高 | 高 | 制动
高 | 高 | 低 | 正传
高 | 低 | 高 | 反转
--------------------------------------------
'''
'''
四轮定义: 马达 = [IN1, IN2, EN]
'''
Motor_Right = [26, 19, 13] #右
Motor_Left = [21, 20, 16] #左
#小车点火
def Car_power_on():
GPIO.setmode(GPIO.BCM) #设置GPIO模式
GPIO.setwarnings(False)
motor.Motor_power_on(Motor_Right)
motor.Motor_power_on(Motor_Left)
#小车运行:倒车
def Car_run_back():
motor.Motor_positive(Motor_Right)
motor.Motor_positive(Motor_Left)
#小车运行:前进
def Car_run_forward():
motor.Motor_negative(Motor_Right)
motor.Motor_negative(Motor_Left)
#小车运行:左转
def Car_run_left():
motor.Motor_positive(Motor_Right)
motor.Motor_negative(Motor_Left)
#小车运行:右转
def Car_run_right():
motor.Motor_negative(Motor_Right)
motor.Motor_positive(Motor_Left)
#小车运行:停止
def Car_run_pause():
motor.Motor_pause(Motor_Right)
motor.Motor_pause(Motor_Left)
#小车运行:制动
def Car_run_brake():
motor.Motor_brake(Motor_Right)
motor.Motor_brake(Motor_Left)
#小车熄火
def Car_Power_Off():
GPIO.cleanup()
print("The Car is start...")
Car_power_on()
time.sleep(0.5)
#操作手册
print('----------------------------------')
print('w: Car forward.')
print('a: Car left.')
print('d: Car right.')
print('s: Car pause.')
print('x: Car back.')
print('f: Car brake.')
print('q: quit the program.')
print('----------------------------------')
while True:
opt = Getch.Getch()
if opt == 'w':
Car_run_forward()
elif opt == 'a':
Car_run_left()
elif opt == 'd':
Car_run_right()
elif opt == 's':
Car_run_pause()
elif opt == 'x':
Car_run_back()
elif opt == 'f':
Car_run_brake()
elif opt == 'q':
break
else:
print('Invaild input.')
Car_run_pause()
Car_Power_Off()