查看: 1893|回复: 0

[MicroPython]TPYBoard智能小车“飞奔的TPYBoard装甲一号”

[复制链接]
  • TA的每日心情
    慵懒
    2018-1-6 09:01
  • 签到天数: 7 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2018-7-19 15:39:50 | 显示全部楼层 |阅读模式
    分享到:
        智能小车作为现代的新发明,是以后的发展方向,他可以按照预先设定的模式在一个环境里自动的运作,不需要人为的管理,可应用于科学勘探等等的用途。智能小车能够实时显示时间、速度、里程,具有自动寻迹、寻光、避障功能,可程控行驶速度、准确定位停车,远程传输图像等功能。下面带大家做一个智能蓝牙小车,用手机APP来控制小车前进、后退、向左、向右、停止,本次蓝牙小车的设计在于探索蓝牙智能小车的设计理念及设计方法,学习一下PWM控制电机差速来控制小车的方向,下面就动手搞起来吧!!!!!

        1.效果展示

        给大家上视频连接,可以蓝牙控制,可以手柄控制哦
        https://v.qq.com/x/page/k0721or47dw.html

        2.材料准备

        TPYBoard  v102   1块
        蓝牙串口模块  1个
        TPYBoard v102小车扩展板(包含4个车轮,4个电机)
        18650电池 2节
        数据线    1条
        杜邦线  若干
        蓝牙APP (http://old.tpyboard.com/download/tool/190.html)

        3.蓝牙模块

        蓝牙( Bluetooth):是一种无线技术标准,可实现固定设备、移动设备和楼宇个人域网之间的短距离数据交换(使用2.4-2.485GHz的ISM波段的UHF无线电波)。
        我们在此使用的蓝牙模块(HC-06)已经在内部实现了蓝牙协议,不用我们再去自己开发调试协议。这类模块一般都是借助于串口协议通信,因此我们只需借助串口将我们需要发送的数据发送给蓝牙模块,蓝牙模块会自动将数据通过蓝牙协议发送给配对好的蓝牙设备。

    1.png

        4.单片机-TPYBoard v102

        TPYBoard v102 是遵循MIT协议,由TurnipSmart公司制作的一款MicroPython开发板,它基于STM32F405单片机,通过USB接口进行数据传输。该开发板内置4个LED灯、一个加速度传感器,可在3V-10V之间的电压正常工作。让你会Python就能做极客, 用Python控制硬件,支持Python语言的开发板。比树莓派更小巧,更简单,更便宜,比Arduino更强大,更加容易编程。

    QQ图片20180719153905.png

        小车扩展板
        以TPYBoard v102开发板为主控板,小车扩展板具有四路PWM调速电机、8个可控LED、1个蜂鸣器、5路舵机接口、1个蓝牙接口、1个PS2无线接口、引出TPYBoard v102开发板全部针脚,可装载循迹模块、超声波模块、机械手臂、红外接收头,兼容入门级电机和专业级电机,两节18650单独供电。

    3.png

        源代码
        我们只需要把TPYBoard v102 插小车扩展板上,把蓝牙模块插上,把程序写入就行,下面是main.py源程序
    1. # main.py -- put your code here!
    2. from pyb import Pin
    3. from pyb import UART

    4. N1 = Pin('Y1', Pin.OUT_PP)
    5. N2 = Pin('Y2', Pin.OUT_PP)
    6. N3 = Pin('Y3', Pin.OUT_PP)
    7. N4 = Pin('Y4', Pin.OUT_PP)
    8. N5 = Pin('Y6', Pin.OUT_PP)
    9. N6 = Pin('Y7', Pin.OUT_PP)
    10. N7 = Pin('Y8', Pin.OUT_PP)
    11. N8 = Pin('Y9', Pin.OUT_PP)

    12. led_red=Pin('Y5', Pin.OUT_PP)
    13. led_right=Pin('Y12', Pin.OUT_PP)
    14. led_left=Pin('Y11', Pin.OUT_PP)

    15. led_red.value(1)
    16. led_right.value(0)
    17. led_left.value(0)


    18. blue=UART(1,9600,timeout=100)

    19. def        go(speed):
    20.         M1_0=pyb.Timer(8, freq=10000).channel(1, pyb.Timer.PWM, pin=pyb.Pin.board.Y1, pulse_width=(speed*200)+10000)
    21.         M1_1=pyb.Timer(8, freq=10000).channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.Y2, pulse_width=0)

    22.         M2_0=pyb.Timer(4, freq=10000).channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.Y3, pulse_width=(speed*100)+5000)
    23.         M2_1=pyb.Timer(4, freq=10000).channel(4, pyb.Timer.PWM, pin=pyb.Pin.board.Y4, pulse_width=0)

    24.         M3_0=pyb.Timer(1, freq=10000).channel(1, pyb.Timer.PWM, pin=pyb.Pin.board.Y6, pulse_width=(speed*220)+10000)
    25.         M3_1=pyb.Timer(1, freq=10000).channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.Y7, pulse_width=0)

    26.         M4_0=pyb.Timer(2, freq=10000).channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.Y9, pulse_width=(speed*50)+5000)
    27.         M4_1=pyb.Timer(12, freq=10000).channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.Y8, pulse_width=0)
    28.        
    29.         led_red.value(0)
    30.        
    31. def        back(speed):
    32.         M1_0=pyb.Timer(8, freq=10000).channel(1, pyb.Timer.PWM, pin=pyb.Pin.board.Y1, pulse_width=0)
    33.         M1_1=pyb.Timer(8, freq=10000).channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.Y2, pulse_width=(speed*200)+10000)

    34.         M2_0=pyb.Timer(4, freq=10000).channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.Y3, pulse_width=0)
    35.         M2_1=pyb.Timer(4, freq=10000).channel(4, pyb.Timer.PWM, pin=pyb.Pin.board.Y4, pulse_width=(speed*100)+10000)

    36.         M3_0=pyb.Timer(1, freq=10000).channel(1, pyb.Timer.PWM, pin=pyb.Pin.board.Y6, pulse_width=0)
    37.         M3_1=pyb.Timer(1, freq=10000).channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.Y7, pulse_width=(speed*200)+10000)

    38.         M4_0=pyb.Timer(2, freq=10000).channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.Y9, pulse_width=0)
    39.         M4_1=pyb.Timer(12, freq=10000).channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.Y8, pulse_width=(speed*100)+10000)
    40.        
    41.         led_red.value(1)

    42. def        stop():
    43.         M1_0=pyb.Timer(8, freq=10000).channel(1, pyb.Timer.PWM, pin=pyb.Pin.board.Y1, pulse_width=0)
    44.         M1_1=pyb.Timer(8, freq=10000).channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.Y2, pulse_width=0)

    45.         M2_0=pyb.Timer(4, freq=10000).channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.Y3, pulse_width=0)
    46.         M2_1=pyb.Timer(4, freq=10000).channel(4, pyb.Timer.PWM, pin=pyb.Pin.board.Y4, pulse_width=0)

    47.         M3_0=pyb.Timer(1, freq=10000).channel(1, pyb.Timer.PWM, pin=pyb.Pin.board.Y6, pulse_width=0)
    48.         M3_1=pyb.Timer(1, freq=10000).channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.Y7, pulse_width=0)

    49.         M4_0=pyb.Timer(12, freq=10000).channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.Y8, pulse_width=0)
    50.         M4_1=pyb.Timer(2,  freq=10000).channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.Y9, pulse_width=0)
    51.         led_right.value(0)
    52.         led_left.value(0)
    53.         led_red.value(1)
    54.        
    55. def        left(speed):
    56.         M1_0=pyb.Timer(8, freq=10000).channel(1, pyb.Timer.PWM, pin=pyb.Pin.board.Y1, pulse_width=(speed*30)+10000)
    57.         M1_1=pyb.Timer(8, freq=10000).channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.Y2, pulse_width=0)

    58.         M2_0=pyb.Timer(4, freq=10000).channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.Y3, pulse_width=(speed*100)+10000)
    59.         M2_1=pyb.Timer(4, freq=10000).channel(4, pyb.Timer.PWM, pin=pyb.Pin.board.Y4, pulse_width=0)

    60.         M3_0=pyb.Timer(1, freq=10000).channel(1, pyb.Timer.PWM, pin=pyb.Pin.board.Y6, pulse_width=(speed*30)+10000)
    61.         M3_1=pyb.Timer(1, freq=10000).channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.Y7, pulse_width=0)

    62.         M4_0=pyb.Timer(2, freq=10000).channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.Y9, pulse_width=(speed*100)+10000)
    63.         M4_1=pyb.Timer(12, freq=10000).channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.Y8, pulse_width=0)
    64.         led_right.value(1)
    65.         led_left.value(0)
    66.        
    67. def        right(speed):
    68.         M1_0=pyb.Timer(8, freq=10000).channel(1, pyb.Timer.PWM, pin=pyb.Pin.board.Y1, pulse_width=(speed*200)+20000)
    69.         M1_1=pyb.Timer(8, freq=10000).channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.Y2, pulse_width=0)

    70.         M2_0=pyb.Timer(4, freq=10000).channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.Y3, pulse_width=(speed*200)+3000)
    71.         M2_1=pyb.Timer(4, freq=10000).channel(4, pyb.Timer.PWM, pin=pyb.Pin.board.Y4, pulse_width=0)

    72.         M3_0=pyb.Timer(1, freq=10000).channel(1, pyb.Timer.PWM, pin=pyb.Pin.board.Y6, pulse_width=(speed*100)+20000)
    73.         M3_1=pyb.Timer(1, freq=10000).channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.Y7, pulse_width=0)

    74.         M4_0=pyb.Timer(2, freq=10000).channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.Y9, pulse_width=(speed*100)+3000)
    75.         M4_1=pyb.Timer(12, freq=10000).channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.Y8, pulse_width=0)
    76.         led_right.value(0)
    77.         led_left.value(1)



    78. while True:
    79.     if blue.any()>0:
    80.         data=blue.read().decode()
    81.         print(data)
    82.         if data.find('0')>-1:
    83.             #stop
    84.             stop()
    85.             print('stop')
    86.         if data.find('1')>-1:
    87.             pyb.LED(2).on()
    88.             pyb.LED(3).off()
    89.             pyb.LED(4).off()
    90.             #-------------
    91.             go(5)
    92.             print('go')
    93.         if data.find('2')>-1:
    94.             pyb.LED(2).off()
    95.             pyb.LED(3).on()
    96.             pyb.LED(4).off()
    97.             #-------------
    98.             back(5)

    99.         if data.find('3')>-1:
    100.             pyb.LED(2).off()
    101.             pyb.LED(3).off()
    102.             pyb.LED(4).on()
    103.             left(5)
    104.         if data.find('4')>-1:
    105.             pyb.LED(2).off()
    106.             pyb.LED(3).off()
    107.             pyb.LED(4).on()
    108.             right(5)
    复制代码



    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-6-2 22:25 , Processed in 0.117525 second(s), 16 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.