ESP32(MicroPython) MeArm单超声波传感器交互+RGB灯(更新)

该程序展示了如何使用ESP32与MeArm机器人手臂配合,通过单个超声波传感器进行避障。原本计划实现双超声波传感器,但由于MicroPython固件问题改为摇杆控制转向。程序中包括摇杆控制版本和按键控制版本,涉及舵机控制、超声波测距、RGB灯变化以及按键输入的处理。
摘要由CSDN通过智能技术生成

ESP32 MeArm单超声波传感器交互

本程序用于使用单个超声波传感器与MeArm交互,并通过摇杆控制转向。刚开始想做双超声波传感器交互,但MicroPython固件有bug,不能正常运行,就改为通过摇杆控制转向。

更新:增加按键控制的版本

代码如下

摇杆控制

'''
  舵机1-->(17)
  舵机2-->(16)
  舵机3-->(22)
  (Trig)-->(5)
  (Echo)-->(18)
'''

#导入Pin模块
from machine import Pin
import time
from servo import Servo
from machine import Timer
from hcsr04 import HCSR04
from neopixel import NeoPixel
import random
from machine import ADC

#定义HCSR04控制对象
hcsr04=HCSR04(trigger_pin=5, echo_pin=18)
#定义SG90舵机控制对象
servo1 = Servo(Pin(17))
servo2 = Servo(Pin(16))
servo3 = Servo(Pin(22))
#定义RGB控制对象
#控制引脚为13,RGB灯串联5个
pin=13
rgb_num=5
rgb_led=NeoPixel(Pin(pin,Pin.OUT),rgb_num)
#定义ADC控制对象
adc3=ADC(Pin(39))
adc3.atten(ADC.ATTN_11DB)  #开启衰减,量程增大到3.3V

a=90
b=0
c=100
g=0
servo1.write_angle(a)
servo2.write_angle(160)
servo3.write_angle(c)
#定时器0中断函数
def time0_irq(time0):
    global a
    global b
    global c
    global g
    if adc3.read()>3000 and a<=179:
        a+=1
        servo1.write_angle(a)
    if adc3.read()<1000 and a>=1:
        a-=1
        servo1.write_angle(a)
    distance=hcsr04.distance_cm()//1
    if distance>13 and c<=150:  #机械臂跟随操作者
        c+=1
        servo3.write_angle(c)
    if distance<13 and c>=25:
        c-=1
        servo3.write_angle(c)
    g+=1
    if g==10 : #每10个周期RGB灯随机变色
        g=0
        for i in range(rgb_num):
            d=random.randint(0,255)
            e=random.randint(0,255)
            f=random.randint(0,255)
            rgb_led[i]=(d, e, f)
            rgb_led.write()     
        
#程序入口
if __name__=="__main__":
    time0=Timer(0)  #创建time0定时器对象
    time0.init(period=30,mode=Timer.PERIODIC,callback=time0_irq)
    while True:
        pass

按键控制

'''
  舵机1-->(17)
  舵机2-->(16)
  舵机3-->(22)
  (Trig)-->(5)
  (Echo)-->(18)
  SCL-->19
  SDA-->21
  DS-->27
   WS-->(13)
'''

#导入Pin模块
from machine import Pin
import time
from servo import Servo
from machine import Timer
from hcsr04 import HCSR04
from neopixel import NeoPixel
import random
from machine import ADC
from machine import Pin,I2C
import dht
from machine import RTC

#定义HCSR04控制对象
hcsr04=HCSR04(trigger_pin=5, echo_pin=18)
#定义SG90舵机控制对象
servo1 = Servo(Pin(17))
servo2 = Servo(Pin(16))
servo3 = Servo(Pin(22))
#定义RGB控制对象
#控制引脚为13,RGB灯串联5个
pin=13
rgb_num=5
rgb_led=NeoPixel(Pin(pin,Pin.OUT),rgb_num)
#定义按键控制对象
key1=Pin(26,Pin.IN,Pin.PULL_UP)
key2=Pin(25,Pin.IN,Pin.PULL_UP)
key3=Pin(33,Pin.IN,Pin.PULL_UP)
key4=Pin(32,Pin.IN,Pin.PULL_UP)
#定义按键键值
KEY1_PRESS,KEY2_PRESS,KEY3_PRESS,KEY4_PRESS=1,2,3,4
key_en=1

#按键扫描函数
def key_scan():
    global key_en
    if key_en==1 and (key1.value()==0 or key2.value()==0 or
                      key3.value()==0 or key4.value()==0 ):
        time.sleep_ms(10)
        key_en=1
        if key1.value()==0:
            return KEY1_PRESS
        elif key2.value()==0:
            return KEY2_PRESS
        elif key3.value()==0:
            return KEY3_PRESS
        elif key4.value()==0:
            return KEY4_PRESS
    elif key1.value()==1 and key2.value()==1 and key3.value()==1 and key4.value()==1:
        key_en=1
    return 0

a=100
b=0
c=130
g=0
h=0
j=150
temp=0
humi=0
k=0
l=0
servo1.write_angle(a)
servo2.write_angle(j)
servo3.write_angle(c)
#定时器0中断函数
def time0_irq(time0):
    global a
    global b
    global c
    global g
    global h
    global j
    global k
    global l
    global temp
    global humi
    key=key_scan()
    if key==KEY1_PRESS and a<=120:
        a+=1
        servo1.write_angle(a)
    if key==KEY2_PRESS and a>=60:
        a-=1
        servo1.write_angle(a)
    if key==KEY3_PRESS and j<=170:
        j+=1
        servo2.write_angle(j)
    if key==KEY4_PRESS and j>=130:
        j-=1
        servo2.write_angle(j)    
    distance=hcsr04.distance_cm()//1
    if distance>13 and c<=150:  #机械臂跟随操作者
        c+=2
        servo3.write_angle(c)
    if distance<13 and distance>=0 and c>=25:
        c-=2
        servo3.write_angle(c)
    g+=1
    h+=1
    if g==10 : #每10个周期RGB灯随机变色,屏幕刷新
        g=0
        for i in range(rgb_num):
            d=random.randint(0,255)
            e=random.randint(0,255)
            f=random.randint(0,255)
            while True :
                k=d-e
                if k<0 :
                    k=-k
                    l=e-f 
                    if l<0 :
                        l=-l
                if k+l>200 :
                    rgb_led[i]=(d, e, f)
                    break
                else :
                    d=random.randint(0,255)
                    e=random.randint(0,255)
                    f=random.randint(0,255)
            rgb_led.write()            
        
#程序入口
if __name__=="__main__":
    time0=Timer(0)  #创建time0定时器对象
    time0.init(period=40,mode=Timer.PERIODIC,callback=time0_irq)
    while True:
        pass

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路易斯720

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值