赞
踩
短学期要我们做一个项目,我看网上红外防坠落小车还是有点资料的,所以就选了,想着也许比较简单,而且实验室也有学长流传下来的小车,电机、电池、轮子、esp32都连好了,其实只要给板子写一些逻辑就行了。学长很贴心,把它们造小车的技术文档都发给我了,但是是用arduino写的,而我们是要用micropython写,对于他的那个电机驱动模块的频率什么的我都看不懂,我尝试了312500Hz,100000Hz,1000Hz好像都能动,而且速度也一样,不知道频率代表了什么,我同学说,如果esp32是40M,而我这个电机模块是20M的话,我的duty就只能填0,512,1023,也就是20M/40M=1/2,整个1 的数字信号被分成了两部分,我的duty可以写相对于1023的0(0)、 一半(512)、全部(1023).而如果电机填了2M,那么整个1被分成了40M/2M=20份,每一份是1024/20=51.2,也就是我的duty可以填0, 51.2, 51.2×2,51.2×3 … 51.2×20,也就是速度被分成了20档。
但是我没管那么多,为了简单,我直接把duty设置为1023,就是全速。
小车是用flutter写的控制小车的app控制的,功能只有前后左右和刹车,还有往前开或者往后开的时候,如果发现前面是悬崖,就直接刹车。
至于硬件方面,我好像没做什么,因为小车是现成的,我只是连了两个红外传感器,VCC连3V3,GND连GND,D0连G26。另外一个VCC的杜邦线把皮扒了,露出了里面的铜丝,和之前的VCC的线连起来,一起连了3V3,也可以直接连5V的。GND连GND,D0连G25.
import machine from machine import PWM ,Pin import utime import time from umqtt.simple import MQTTClient import ubinascii import network import dht from machine import Timer from machine import UART # right wheel p1 =Pin(27 ,Pin.OUT) p2 =Pin(14 ,Pin.OUT) # left wheel p3 =Pin(21 ,Pin.OUT) p4 =Pin(33 ,Pin.OUT) v_left =Pin(16) v_right =Pin(17) v_right_pwm =PWM(Pin(17) ,freq=312500 ,duty=512 ) # pwm 速度 right v_left_pwm =PWM(Pin(16) ,freq=312500,duty=512 ) # pwm 速度 left infra_red_receive =Pin(5,Pin.IN) infra_red_receive_pwm =PWM(infra_red_receive) def Forward_speed_regulation(which,speed):#正转调速 if which=='left': p3.value(1) p4.value(0) v_left_pwm.duty(speed) else: p1.value(1) p2.value(0) v_right_pwm.duty(speed) def Back_speed_regulation(which,speed): if which=='left': p3.value(0) p4.value(1) v_left_pwm.duty(speed) else: p1.value(0) p2.value(1) v_right_pwm.duty(speed) def turn_left(): Back_speed_regulation('left',1023) Forward_speed_regulation('right',1023) def turn_right(): Forward_speed_regulation('left',1023) Back_speed_regulation('right',1023) def back(): Back_speed_regulation('left',1023) Back_speed_regulation('right',1023) def forward(): Forward_speed_regulation('left',1023) Forward_speed_regulation('right',1023) def full_speed_forward_tyre(which): if which=='left': p3.value(1) p4.value(0) v_left.on() else: p1.value(1) p2.value(0) v_right.on() def full_speed_back_tyre(which): if which=='left': p3.value(0) p4.value(1) v_left.on() else: p1.value(0) p2.value(1) v_right.on() def full_speed_forward(): full_speed_forward_tyre('left') full_speed_forward_tyre('right') def full_speed_back(): full_speed_back_tyre('left') full_speed_back_tyre('right') def full_speed_left(): full_speed_back_tyre('left') full_speed_forward_tyre('right') def full_speed_right(): full_speed_forward_tyre('left') full_speed_back_tyre('right') def brake(): p1.off() p2.off() p3.off() p4.off() SERVER = '你的服务器' # MQTT服务器 CLIENT_ID = ubinascii.hexlify(machine.unique_id()) # 获取ESP32的ID PHONE_TOPIC = b'你的接收的topic' # TOPIC的ID username = '你的账号' # 账号,随便写 password = '你的密码' # 密码,随便写 PUBLISH_TOPIC =b'你的发出去的topic' def send_msg(msg): client.publish(PUBLISH_TOPIC ,msg) def set_client(): global client,client_infra_red client = MQTTClient(CLIENT_ID, SERVER, 0, username, password, 60) client.set_callback(mqtt_callback) client.connect() client.subscribe(PHONE_TOPIC) state=b'0' car_state=b'brake' def mqtt_callback(topic, msg): global state,car_state if msg==b'0' or msg==b'1' or msg==b'2' or msg==b'3': state=msg if msg==b'2' or msg==b'3': if car_state==b'forward': brake() print('save') if msg==b'1' or msg==b'3': if car_state==b'back': brake() print('save back') #state是红外线传过来的状态,有以下表示 #0 forward can , back can ,可以前进也可以后退 #1 forward can , back not #2 forward not , back can #3 forward not , back not if msg==b'brake': print('brake') send_msg('brake') brake() car_state=b'brake' elif msg==b'left': print('turn left') send_msg('turn left') full_speed_left() elif msg==b'right': print('turn right') send_msg('turn right') full_speed_right() elif msg==b'back': #0 forward can , back can #1 forward can , back not #2 forward not , back can #3 forward not , back not if state==b'1' or state==b'3': send_msg('Cliff there, you want me to die?') print('Cliff there, you want me to die?') brake() car_state=b'brake' else : print('back') send_msg('back') back() car_state=b'back' elif msg==b'forward': if state==b'3' or state==b'2': send_msg('Cliff over there, you want me to die?') print('Cliff over there, you want me to die?') brake() car_state=b'brake' else: # 红外没发现悬崖,才能前进 print('forward') send_msg('forward') forward() car_state=b'forward' def check(): global time_ok,ok print('check') while True: # 查看是否有数据传入 # 有的话就执行 mqtt_callback #client_infra_red.check_msg() client.check_msg() utime.sleep_ms(100 ) def connect_to_wifi(): wifi = network.WLAN(network.STA_IF) # 创建一个网络接口 if not wifi.isconnected(): wifi.active(True ) # 激活网络接口 wifi.disconnect( ) # 中断之前连接的wifi wifi.connect('你的网络名字' ,'你的网络密码') # 输入账号密码连接网络 while(wifi.ifconfig()[0 ]=='0.0.0.0'): time.sleep(1) print(wifi.ifconfig()) def test(): full_speed_back() utime.sleep(1) brake() utime.sleep(1) full_speed_forward() utime.sleep(1) brake() #infra_red_test() def init_net(): print('connecting to the car') connect_to_wifi() set_client() print('connecting completed') send_msg('are you my master?') check() def test_velocity_move(): forward() utime.sleep(1) brake() utime.sleep(1) back() utime.sleep(1) brake() utime.sleep(1) turn_left() utime.sleep(1) turn_right() time.sleep(1) brake() #test_velocity_move() init_net()
红外线的main,因为小车上的esp32已经插满了,所以用单独一块板作为红外线的
import machine from machine import PWM ,Pin import utime import time from umqtt.simple import MQTTClient import ubinascii import network import ubinascii import dht from machine import Timer infra_red_receive = Pin(26, Pin.IN) infra_red_receive2=Pin(25,Pin.IN) SERVER = '你的服务器' # MQTT服务器 CLIENT_ID = ubinascii.hexlify(machine.unique_id()) # 获取ESP32的ID SUBSCRIBE_TOPIC = b'你的订阅topic' # TOPIC的ID username = '你的账号' # 账号,随便写 password = '你的密码' # 密码,随便写 PUBLISH_TOPIC = b'你的发送主题' PHONE_TOPIC=b'' #手机主题,相当于红外线代替手机发了一个信息 def send_msg(msg): client.publish(PUBLISH_TOPIC, msg) def set_client(): global client client = MQTTClient(CLIENT_ID, SERVER, 0, username, password, 60) client.connect() def connect_to_wifi(): wifi = network.WLAN(network.STA_IF) # 创建一个网络接口 if not wifi.isconnected(): wifi.active(True) # 激活网络接口 wifi.disconnect() # 中断之前连接的wifi wifi.connect('你的网络账号', '你的网络密码') # 输入账号密码连接网络 while (wifi.ifconfig()[0] == '0.0.0.0'): time.sleep(1) print(wifi.ifconfig()) # infra_red_test() from machine import UART,Pin import utime def init(): print('connecting to the infra_red') connect_to_wifi() set_client() print('connecting completed') client.publish('要发送到的主题','infra_red connected') #check() def send_to_net_infra_red_info(): while True: if infra_red_receive.value()==0 and infra_red_receive2.value()==0: send_msg('0') print('forward and back all can') elif infra_red_receive.value()==0 and infra_red_receive2.value()==1: send_msg('1') print('forward can , back not') elif infra_red_receive.value()==1 and infra_red_receive2.value()==0: send_msg('2') print('forward not , back can') #print('okBack') elif infra_red_receive.value()==1 and infra_red_receive2.value()==1: send_msg('3') print('forward not, back not') utime.sleep_ms(300) init() send_to_net_infra_red_info() #infra_red_test()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。