#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ----极光编程----
# 文件名:motor.py
# 版本:V2.0
# author: Jessy
# 说明:智能小车实验
#####################################################
from machine import Pin, PWM
from utime import sleep
from myIRremote import IR
# 设置管脚PIN
left1_pin = 13
left2_pin = 12
right1_pin = 14 #5
right2_pin = 27
avoid_left_pin = 26 #寻迹 22
avoid_right_pin = 25 #23
follow_left_pin = 33 #避障
follow_right_pin = 32
mode_pin = 34
IRremote_pin = 35
redLED_pin = 15
blueLED_pin = 2
yellowLED_pin = 4
#buzzer_pin = 33
#motor初始化
def motor_setup():
global motro_left1
global motro_left2
global motro_right1
global motro_right2
motro_left1 = PWM(Pin(left1_pin), freq=20000, duty=0) # 创建motor pwm对象,设置为输出模式
motro_left2 = PWM(Pin(left2_pin),freq=20000, duty=0)
motro_right1 = PWM(Pin(right1_pin),freq=20000, duty=0)
motro_right2 = PWM(Pin(right2_pin),freq=20000, duty=0)
# 初始化寻迹避障模块GPIO口
def gpio_setup():
global avoid_left
global avoid_right
global follow_left
global follow_right
global mode_button
global led_red
global led_blue
global led_yellow
avoid_left = Pin(avoid_left_pin,Pin.IN)
avoid_right = Pin(avoid_right_pin,Pin.IN)
follow_left = Pin(follow_left_pin,Pin.IN,Pin.PULL_UP)
follow_right = Pin(follow_right_pin,Pin.IN,Pin.PULL_UP)
mode_button = Pin(mode_pin,Pin.IN,Pin.PULL_UP)
led_red = Pin(redLED_pin,Pin.OUT)
led_blue = Pin(blueLED_pin,Pin.OUT)
led_yellow = Pin(yellowLED_pin,Pin.OUT)
#方向函数
def turn_left():
motro_left1.duty(380) # 输出高电平 400
motro_left2.duty(0)
motro_right1.duty(680) # 输出高电平
motro_right2.duty(0)
def turn_right():
motro_left1.duty(680) # 输出高电平
motro_left2.duty(0)
motro_right1.duty(380) # 输出高电平
motro_right2.duty(0)
def fast_forward():
motro_left1.duty(1000) # 输出高电平
motro_left2.duty(0)
motro_right1.duty(1000) # 输出高电平
motro_right2.duty(0)
def slow_forward():
motro_left1.duty(780) # 输出高电平
motro_left2.duty(0)
motro_right1.duty(780) # 输出高电平
motro_right2.duty(0)
def backward():
motro_left1.duty(0) # 输出高电平
motro_left2.duty(780)
motro_right1.duty(0) # 输出高电平
motro_right2.duty(780)
def stop():
motro_left1.duty(0) # 输出高电平
motro_left2.duty(0)
motro_right1.duty(0) # 输出高电平
motro_right2.duty(0)
def tracing():
slow_forward()
if follow_left.value()==1 and follow_right.value()==0:
turn_left()
print('Black line is detected on the left,turn left')
elif follow_left.value()==0 and follow_right.value()==1:
turn_right()
print('Black line is detected on the ritht,turn right')
else:
slow_forward()
print('forward')
def avoid_obstacle():
if avoid_left.value()==0 and avoid_right.value()==1 : #前方任何一个红外遇到黑色(障碍物)
backward()
print('backward')
sleep(1)
turn_right()
print('turn right')
sleep(0.5)
slow_forward()
print('forward')
elif avoid_left.value()==1 and avoid_right.value()==0 : #前方任何一个红外遇到黑色(障碍物)
backward()
print('backward')
sleep(1)
turn_left()
print('turn left')
sleep(0.5)
slow_forward()
print('forward')
elif avoid_left.value()==0 and avoid_right.value()==0 : #前方任何一个红外遇到黑色(障碍物)
backward()
print('backward')
sleep(1)
slow_forward()
print('forward')
else:
slow_forward()
print('forward')
def ir_control():
state = 0
speed = 512
ir = IR(IRremote_pin)
changed, s, repeat, ir_ok = ir.scan()
if changed == True:
if s == '1':
speed = 512
elif s == '2':
speed = 640
elif s == '3':
speed = 768
if repeat >= 2:
if s == 'up' and state != 1:
state = 1
motro_left1.duty(speed)
motro_left2.duty(0)
motro_right1.duty(speed)
motro_right2.duty(0)
print("state1"," up")
elif s == 'down' and state != 2:
state = 2
motro_left1.duty(0)
motro_left2.duty(speed)
motro_right1.duty(0)
motro_right2.duty(speed)
print("state2"," down")
elif s == 'left' and state != 3:
state = 3
motro_left1.duty(0)
motro_left2.duty(0)
motro_right1.duty(int(speed/2 + 128))
motro_right2.duty(0)
print("state3"," left")
elif s == 'right' and state != 4:
state = 4
motro_left1.duty(int(speed/2 + 128))
motro_left2.duty(0)
motro_right1.duty(0)
motro_right2.duty(0)
print("state4"," right")
else:
if state != 0:
state = 0
pwm1.duty(0)
pwm2.duty(0)
pwm3.duty(0)
pwm4.duty(0)
print("state0")
sleep(0.2)
def led_show():
if mode==1: #寻迹模式
led_red.on()
led_blue.off()
led_yellow.off()
elif mode==2: #避障模式
led_red.off()
led_blue.on()
led_yellow.off()
elif mode==3:
led_red.off()
led_blue.off()
led_yellow.on()
else:
led_red.off()
led_blue.off()
led_yellow.off()
#通过红外模块获取方向
#红外传感器,二极管不断发射红外线,遇到白色反射回来,红外接收管饱和,输出低电平,否则输出高电平
# (black line:1,white line:0)
def handler_interrupt(pin):
global mode
if(mode<3):
mode = mode + 1
else:
mode = 0
print("mode:",mode,end='\t')
def get_mode():
mode_button.irq(trigger=Pin.IRQ_FALLING,handler=handler_interrupt)
return mode
#主程序
mode = 0
motor_setup()
gpio_setup()
while True:
if get_mode():
break
while True:
mode = get_mode()
if mode==1:
tracing()
led_show()
print("tracing mode",end="\t")
elif mode==2:
avoid_obstacle()
led_show()
print("avoid obstacle mode",end="\t")
elif mode==3:
ir_control()
led_show()
print("IRremote control mode",end="\t")
else:
stop()
led_show()
break
print("break",end="\t")
没有合适的资源?快使用搜索试试~ 我知道了~
基于ESP32的智能遥控小车
共19个文件
py:16个
jpg:2个
md:1个
需积分: 50 14 下载量 68 浏览量
2022-02-22
19:09:21
上传
评论 10
收藏 1.39MB ZIP 举报
温馨提示
基于ESP32的智能遥控小车
资源详情
资源评论
资源推荐
收起资源包目录
智能小车.zip (19个子文件)
智能小车
new car(改了引脚而已)
2.jpg 552KB
智能小车新.py 5KB
1.jpg 865KB
智能小车
ir.py 2KB
样例.py 7KB
红外遥控小车.py 7KB
仅遥控小车.py 2KB
myIRremote.py 5KB
小车介绍.md 755B
遥控智能小车.py 2KB
car.py 1KB
main.py 193B
智能小车.py 4KB
智能小车2.py 5KB
红外遥控车
ir.py 2KB
样例.py 7KB
智能小车2.py 7KB
myIRremote.py 5KB
智能小车1.py 5KB
共 19 条
- 1
淬子
- 粉丝: 22
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0