[MicroPython]TPYBoard智能小車「飛奔的TPYBoard裝甲一號」

    智能小車做爲現代的新發明,是之後的發展方向,他能夠按照預先設定的模式在一個環境裏自動的運做,不須要人爲的管理,可應用於科學勘探等等的用途。智能小車可以實時顯示時間、速度、里程,具備自動尋跡、尋光、避障功能,可程控行駛速度、準肯定位停車,遠程傳輸圖像等功能。下面帶你們作一個智能藍牙小車,用手機APP來控制小車前進、後退、向左、向右、中止,本次藍牙小車的設計在於探索藍牙智能小車的設計理念及設計方法,學習一下PWM控制電機差速來控制小車的方向,下面就動手搞起來吧!!!!!html

    1.效果展現python

    給你們上視頻鏈接,能夠藍牙控制,能夠手柄控制哦
   http://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.htmlui

    3.藍牙模塊設計

    藍牙( Bluetooth):是一種無線技術標準,可實現固定設備、移動設備和樓宇我的域網之間的短距離數據交換(使用2.4-2.485GHz的ISM波段的UHF無線電波)。
    咱們在此使用的藍牙模塊(HC-06)已經在內部實現了藍牙協議,不用咱們再去本身開發調試協議。這類模塊通常都是藉助於串口協議通訊,所以咱們只需藉助串口將咱們須要發送的數據發送給藍牙模塊,藍牙模塊會自動將數據經過藍牙協議發送給配對好的藍牙設備。調試

    4.單片機-TPYBoard v102code

    TPYBoard v102 是遵循MIT協議,由TurnipSmart公司製做的一款MicroPython開發板,它基於STM32F405單片機,經過USB接口進行數據傳輸。該開發板內置4個LED燈、一個加速度傳感器,可在3V-10V之間的電壓正常工做。讓你會Python就能作極客, 用Python控制硬件,支持Python語言的開發板。比樹莓派更小巧,更簡單,更便宜,比Arduino更強大,更加容易編程。視頻

    小車擴展板
    以TPYBoard v102開發板爲主控板,小車擴展板具備四路PWM調速電機、8個可控LED、1個蜂鳴器、5路舵機接口、1個藍牙接口、1個PS2無線接口、引出TPYBoard v102開發板所有針腳,可裝載循跡模塊、超聲波模塊、機械手臂、紅外接收頭,兼容入門級電機和專業級電機,兩節18650單獨供電。htm

    源代碼
    咱們只須要把TPYBoard v102 插小車擴展板上,把藍牙模塊插上,把程序寫入就行,下面是main.py源程序
 

# main.py -- put your code here!
from pyb import Pin
from pyb import UART

N1 = Pin('Y1', Pin.OUT_PP)
N2 = Pin('Y2', Pin.OUT_PP)
N3 = Pin('Y3', Pin.OUT_PP)
N4 = Pin('Y4', Pin.OUT_PP)
N5 = Pin('Y6', Pin.OUT_PP)
N6 = Pin('Y7', Pin.OUT_PP)
N7 = Pin('Y8', Pin.OUT_PP)
N8 = Pin('Y9', Pin.OUT_PP)

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

led_red.value(1)
led_right.value(0)
led_left.value(0)


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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



while True:
    if blue.any()>0:
        data=blue.read().decode()
        print(data)
        if data.find('0')>-1:
            #stop
            stop()
            print('stop')
        if data.find('1')>-1:
            pyb.LED(2).on()
            pyb.LED(3).off()
            pyb.LED(4).off()
            #-------------
            go(5)
            print('go')
        if data.find('2')>-1:
            pyb.LED(2).off()
            pyb.LED(3).on()
            pyb.LED(4).off()
            #-------------
            back(5)

        if data.find('3')>-1:
            pyb.LED(2).off()
            pyb.LED(3).off()
            pyb.LED(4).on()
            left(5)
        if data.find('4')>-1:
            pyb.LED(2).off()
            pyb.LED(3).off()
            pyb.LED(4).on()
            right(5)
相關文章
相關標籤/搜索