[實驗目的]

  利用馬達驅動板模組,來控制車子的前後左右。


[指令說明]

使用馬達驅動模組 要匯入 wemotor 模組

import wemotor

取 motor 名稱物件

motor= wemotor.Motor()

馬達模組角度

motor.move(左馬達,右馬達)

步驟 1:開啟「Thonny」軟體。

投影片1.JPG


步驟 2:選擇板子「工具」>「選項」。

投影片2.JPG


步驟 3:選擇「MicroPython(ESP8266)」>「序列阜」。

投影片3.JPG


步驟 4:在右下方 點「安裝或是更新MicroPython」。

投影片4.JPG


步驟 5:選擇「序列阜」> 點「Browse...」。

投影片5.JPG


步驟 6:在 「模組」>「esp8266-20191220-v1.12.bin」。

投影片6.JPG

官網連結 = [ https://micropython.org/ ]

下載連結 = [ https://micropython.org/resources/firmware/esp8266-20191220-v1.12.bin ]


步驟 7:點「安裝」>「等待中」> 顯示「Done!」表示完成 >「關閉」

投影片7.JPG


步驟 8:點「確認」> 查看訊息是否換成ESP8266

投影片8.JPG


步驟 10:開啟「myWemotor」

投影片9.JPG


步驟 13:再「模組」>  「Wemotor.py」>「右鍵」>「上傳到/」 。

投影片10.JPG

投影片11.JPG


步驟 14:在下方就可以看到「Wemotor.py」。

投影片12.JPG


步驟 15:點「停止」>「執行」。

投影片13.JPG

注意 = 直接執行是沒有儲存在D1 mini 開發板內,而是由電腦端控制,拔除電源後,程式碼將消失。


步驟 16:點「檔案」>「儲存副本」。

投影片14.JPG

注意 = 此步驟是將程式碼儲存在 D1 mini 開發板內。


步驟 17:點「MicroPython設備」> 輸入「main.py」>「確定」。

投影片15.JPG

注意 = 儲存檔名為 main.py 之後,板子插上電源會一直執行,

如未取 main.py 只是將程式碼儲存至板子中,下次連接依然存在並不執行。


步驟 19:下方就會顯示 main.py

投影片16.JPG


步驟 20:輕按 板子上重啟鈕。

投影片17.JPG


import wemotor          #匯入wemotor(馬達)模組
import time             #匯入time(時間)模組

motor= wemotor.Motor()  #motor為馬達物件

motor.move(40,40)       # 前
time.sleep(1)           # 暫停1秒
motor.move(-40,-40)     # 後
time.sleep(1)
motor.move(20,80)       # 左
time.sleep(1)
motor.move(80,20)       # 右
time.sleep(1)
motor.move(0,0)         # 停止

wemotor

import ustruct
from machine import I2C, Pin
import time

i2c = I2C(scl=Pin(5),sda=Pin(4))    # freq=100000

A = const(0)   # Motor A
B = const(1)   # Motor B

BRAKE = const(0)
CCW = const(1)
CW = const(2)
STOP = const(3)
STANDBY = const(4)

leftSensor = Pin(12,Pin.IN)      # D6 左輪感測器
rightSensor = Pin(16,Pin.IN)     # D0 右輪感測器

flagL = 0   # 左輪狀態
flagR = 0   # 右輪狀態

valL = 0    # 左輪脈衝值
valR = 0    # 右輪脈衝值

speedL = 0  # 左輪速度
speedR = 0  # 右輪速度

class Motor:
    def __init__(self, address=0x30, freq=1000, standbyPin=None):
            
        self.i2c = i2c
        self.address = address
        self.standbyPin = standbyPin
        
        self.lNowSpeed = 0
        self.rNowSpeed = 0
        
        if standbyPin is not None:
            standbyPin.init(standbyPin.OUT, 0)

        self.setFreq(freq)

    def setMotor(self, dir, speed):         # setmotor(模式,轉速) 模式:1是後退、2是前進、3是停止 。轉速 0~100
        if self.standbyPin is not None:
            if dir == STANDBY:
                self.standbyPin.value(0)
                return
            else:
                self.standbyPin.value(1)

        _speed = int(speed * 100)

        if _speed > 10000:
            _speed = 10000

        if dir not in range(0,5):
           dir = 3

        s0 = _speed >> 8 & 0xff
        s1 = _speed & 0xff

        self.i2c.writeto(self.address, ustruct.pack(">4B", self.motor | 0x10, dir, s0, s1))
        
    def setFreq(self, freq):
        n0 = freq >> 16 & 0x0f
        n1 = freq >> 16 & 0xff
        n2 = freq & 0xffff

        self.i2c.writeto(self.address, ustruct.pack(">2BH", n0, n1, n2))
    
    def move(self, lSpeed, rSpeed):
        self.lNowSpeed = lSpeed
        self.rNowSpeed = rSpeed
        
        self.motor=A
        if(lSpeed>=0):
            self.setMotor(2,lSpeed)
        elif(lSpeed<0):
            self.setMotor(1,abs(lSpeed))
            
        self.motor=B    
        if(rSpeed>=0):
            self.setMotor(2,rSpeed)
        elif(rSpeed<0):
            self.setMotor(1,abs(rSpeed))
    
    # 等速前進
    def constantSpeed(self,mode, lRotating, rRotating):  #  lRotating、rRotating 0.02 有不錯的效果
        global flagL,flagR,valL,valR,speedL,speedR
        
        d_time = 20    # 單位時間(毫秒)
        
        last_time = now_time = time.ticks_ms()
        
        while (now_time-last_timeif(flagL == 0 and leftSensor.value() == 1):  # 左輪
                valL += 1
                flagL = 1
            if(flagL == 1 and leftSensor.value() == 0):
                valL += 1
                flagL = 0            
            if(flagR == 0 and rightSensor.value() == 1): # 右輪
                valR += 1
                flagR = 1
            if(flagR == 1 and rightSensor.value() == 0):
                valR += 1
                flagR = 0
                
            now_time = time.ticks_ms()  # 更新現在時間(毫秒)
        
        l = valL/d_time                 # 計算左輪單位速度
        r = valR/d_time                 # 計算右輪單位速度
      
        if(l<=lRotating):      
            speedL += 1
        if(l>=lRotating + 0.02):      
            speedL -= 1      
        if(r<=rRotating):      
            speedR += 1
        if(r>=rRotating + 0.02):      
            speedR -= 1
        
        if(speedL>100):
            speedL = 100
        if(speedL<0):
            speedL = 0      
        if(speedR>100):
            speedR = 100
        if(speedR<0):
            speedR = 0
            
        lDirection = 1   # -1為後退、1為前進、0為停止
        rDirection = 1   # -1為後退、1為前進、0為停止
        
        if(mode == 'forward'):
            lDirection = 1
            rDirection = 1
        elif(mode == 'backward'):
            lDirection = -1
            rDirection = -1                   
        elif(mode == 'stop'):
            lDirection = 0
            rDirection = 0
            speedL = 0
            speedR = 0
        elif(mode == 'left'):
            lDirection = -1
            rDirection = 1
        elif(mode == 'right'):
            lDirection = 1
            rDirection = -1
            
        self.move(lDirection*(speedL),rDirection*(speedR))    
        
        valL = 0
        valR = 0
        
    def avoidTimeout(self):
        self.move(self.lNowSpeed,self.rNowSpeed)

[成果影片]

創作者介紹
創作者 Code Create++ 的頭像
程式創造++

Code Create++

程式創造++ 發表在 痞客邦 留言(0) 人氣( 5 )