当前位置:   article > 正文

micropython-esp32-红外防坠落小车-双路直流电机驱动7A160W-红外TCRT5000-flutter-mqtt控制_esp32 tcrt5000

esp32 tcrt5000

短学期要我们做一个项目,我看网上红外防坠落小车还是有点资料的,所以就选了,想着也许比较简单,而且实验室也有学长流传下来的小车,电机、电池、轮子、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()




  • 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
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292

红外线的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()




  • 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
  • 108
  • 109
  • 110
  • 111
  • 112
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/599662
推荐阅读
相关标签
  

闽ICP备14008679号