Uncategorized

Program Name

measure_motor_speed_v01.py – tlfong01 2021aug17hkt2151

Reference

Pi-Top Forum – making-a-rpi-pico-based-smart-vehicle/924

https://forum.pi-top.com/t/making-a-rpi-pico-based-smart-vehicle/924

#

Configuration

Acer Intel COIRE i5 PC Chinese Windows 10 (64-bit), Thonny 3.3.3, Python 3.7.9 (32-bit), Tk 8.6.9, USB COM Port #4

#

Thonny MicroPython Intepreter

Micropython (Rapsberry Pi Pico)

DC Motor

N20 1:48 6V DC Gear Motor with quadrature encoder signals A, B

DC Motor Driver

TB6612FNG Dual DC Motor Driver

Program Function

Move Two DC motors forward, backward, and stop

User Guide

import utime
from machine import Pin

driverDict01 = {
‘Title’ : ‘TB6612FNG Dual DC Motor Driver’,
‘STANDBY’ : 5,
‘1’: {‘IN1’: 10, ‘IN2’: 11, ‘PWM’ : 3, ‘ENCODE’: 14},
‘2’: {‘IN1’: 12, ‘IN2’: 13, ‘PWM’ : 4, ‘ENCODE’: 15},
‘3’: {‘IN1’: 0, ‘IN2’: 0, ‘PWM’ : 0},
‘4’: {‘IN1’: 0, ‘IN2’: 0, ‘PWM’ : 0},
}

driverDictDict = {
‘1’: driverDict01,
‘2’: driverDict01,
}

def setupDriver(driverDictNum):
driverDict = driverDictDict[str(driverDictNum)]
standBy = Pin(driverDict[‘STANDBY’], Pin.OUT)
standBy.high()
return

def setupMotor(driverDictNum, motorNum):
driverDict = driverDictDict[str(driverDictNum)]
motorIn1 = Pin(driverDict[str(motorNum)][‘IN1’], Pin.OUT)
motorIn2 = Pin(driverDict[str(motorNum)][‘IN2’], Pin.OUT)
motorPwm = Pin(driverDict[str(motorNum)][‘PWM’], Pin.OUT)
motorEncode = Pin(driverDict[str(motorNum)][‘ENCODE’], Pin.IN, Pin.PULL_DOWN)
motorControlPinList = [motorIn1, motorIn2, motorPwm, motorEncode]
return motorControlPinList

def moveMotorForward(driverDictNum, motorNum):
motorControlPinList = setupMotor(driverDictNum, motorNum)
motorControlPinList[0].low()
motorControlPinList[1].high()
motorControlPinList[2].high()
return

def moveMotorBackward(driverDictNum, motorNum):
motorControlPinList = setupMotor(driverDictNum, motorNum)
motorControlPinList[0].high()
motorControlPinList[1].low()
motorControlPinList[2].high()
return

def moveMotorStop(driverDictNum, motorNum):
motorControlPinList = setupMotor(driverDictNum, motorNum)
motorControlPinList[0].low()
motorControlPinList[1].low()
motorControlPinList[2].high()
return

def readMotorEncodeValue(driverDictNum, motorNum):
motorControlPinList = setupMotor(driverDictNum, motorNum)
encodeValue = motorControlPinList[3].value()
return encodeValue

*** Motor Test Functions ***

def testMoveMotor01(driverDictNum, motorNum):
# *** Setup Driver ***
setupDriver(driverDictNum)
# *** Move motor forward ***
moveMotorForward(driverDictNum, motorNum)
utime.sleep(1)
# *** Move motor backward ***
moveMotorBackward(driverDictNum, motorNum)
utime.sleep(1)
# *** Move motor stop ***
moveMotorStop(driverDictNum, motorNum)
return

def testReadMotorEncodeValue01(driverDictNum, motorNum):
# *** Setup Driver ***
setupDriver(driverDictNum)
# *** Read motor encode value ***
print(‘Move motor by hand and read encode value every second, …’)
for secondCount in range(100):
encodeValue = readMotorEncodeValue(driverDictNum, motorNum)
print(‘ motor encode value =’, encodeValue)
utime.sleep(1)
return

def testReadMotorEncodeValue02(driverDictNum, motorNum):
# *** Setup Driver ***
setupDriver(driverDictNum)

# *** Move motor forward ***
moveMotorForward(driverDictNum, motorNum)       

# *** Read motor encode value ***
print('Move motor by hand and read encode value every second, ...')
for secondCount in range(100):
    encodeValue = readMotorEncodeValue(driverDictNum, motorNum)
    print('  motor encode value =', encodeValue)
    utime.sleep(1)
return  

def testMeasureMotorSpeed01(driverDictNum, motorNum):
# *** Setup Driver ***
setupDriver(driverDictNum)

# *** Move motor forward ***
moveMotorForward(driverDictNum, motorNum)       

# *** Measure Motor Speed ***

# *** Start counting 100 revolutions ***
msTicks1 = utime.ticks_ms()

# *** Find lapse time of 100 revolutions ***
revCount = 0    
while revCount < 100:
    if readMotorEncodeValue(driverDictNum, motorNum) == 0:
        revCount = revCount + 1
    utime.sleep(0.01)            
msTicks2 = utime.ticks_ms()    
lapseMs = utime.ticks_diff(msTicks2, msTicks1)    
moveMotorStop(driverDictNum, motorNum)

print('lapse_ms of 100 revolutions =', lapseMs)
print('rpm                         =', int(60 / ((lapseMs / 100) / 1000))) 

return

*** Main Tests ***

*** Test move motor forward, backward, stop ***

testMoveMotor01(driverDictNum = 1, motorNum = 1)

testMoveMotor01(driverDictNum = 1, motorNum = 2)

*** Test read motor encoder signal value ***

testReadMotorEncodeValue01(driverDictNum = 1, motorNum = 1)

testReadMotorEncodeValue02(driverDictNum = 1, motorNum = 1)

moveMotorStop(driverDictNum = 1, motorNum = 1)

testMeasureMotorSpeed01(driverDictNum = 1, motorNum = 1)

*** End of program ***

*** Sample Output – tlfong01 2021jul01hkt1707

”’

%Run -c $EDITOR_CONTENT
”’

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.