Choosing an optimum duty cycle for calibration/benchmarking
I found low duty cycles like 20% and 25% not satisfactory, because one of the four motors sometimes gets stuck. A 50% duty cycle is more smooth and repeatable. A 50% measurement is shown below:
>>> %Run -c $EDITOR_CONTENT
setupPwmPinNumList(), ...
pwmPinNumList = [0, 1, 2, 3]
pwmFreqList = [1000, 1000, 1000, 1000]
pwmDutyCycleList = [50, 50, 50, 50]
testCountIntPinNumListIntOneTenthSecond()
ppsList = [32, 24, 28, 25] , min 24 , max 32 , dif 8 , avg 27
ppsList = [39, 26, 29, 23] , min 23 , max 39 , dif 16 , avg 29
ppsList = [31, 28, 28, 21] , min 21 , max 31 , dif 10 , avg 27
ppsList = [31, 28, 26, 16] , min 16 , max 31 , dif 15 , avg 25
ppsList = [31, 26, 34, 22] , min 22 , max 34 , dif 12 , avg 28
ppsList = [29, 26, 27, 19] , min 19 , max 29 , dif 10 , avg 25
ppsList = [28, 28, 24, 20] , min 20 , max 28 , dif 8 , avg 25
ppsList = [30, 24, 24, 28] , min 24 , max 30 , dif 6 , avg 26
ppsList = [33, 27, 18, 22] , min 18 , max 33 , dif 15 , avg 25
ppsList = [31, 26, 24, 19] , min 19 , max 31 , dif 12 , avg 25
ppsList = [28, 22, 25, 22] , min 22 , max 28 , dif 6 , avg 24
ppsList = [32, 21, 27, 32] , min 21 , max 32 , dif 11 , avg 28
ppsList = [34, 23, 27, 22] , min 22 , max 34 , dif 12 , avg 26
ppsList = [28, 24, 25, 20] , min 20 , max 28 , dif 8 , avg 24
ppsList = [36, 23, 31, 21] , min 21 , max 36 , dif 15 , avg 27
ppsList = [32, 26, 26, 23] , min 23 , max 32 , dif 9 , avg 26
ppsList = [32, 27, 23, 23] , min 23 , max 32 , dif 9 , avg 26
ppsList = [32, 24, 26, 23] , min 23 , max 32 , dif 9 , avg 26
ppsList = [30, 23, 30, 27] , min 23 , max 30 , dif 7 , avg 27
ppsList = [33, 26, 28, 23] , min 23 , max 33 , dif 10 , avg 27
>>>
'''
This is a full listing of the code, with sample results.
# *** pwm_int_48.py - pwm and interrupt testing, tlfong01, 2021oct05hkt2031 ***
from machine import Pin, PWM
import utime
# ========= ========= ========= ========= ========= ========= ========= =========
# ========= ========= ========= ========= ========= ========= ========= =========
# Conents
#
# Part 1 - Interrupt Functions
# 1.1 Using GP4, 5, 6, 7 input pins to detect and count mtor encoder A signals and calculate motor speed
# Part 2 - PWM Functions
# 2.1 Using PWM pin to fade in and fade out the system LED
# 2.2 Using GP 0, 1, 2, 3 pins' PWM signals to control the speed of TB6612FNG driving TM310 DC motor
# ========= ========= ========= ========= ========= ========= ========= =========
# ========= ========= ========= ========= ========= ========= ========= =========
# Interrupt functions for multiple (4) interrupt pins GP 4, 5, 6, 7 ***
intPinNum0 = 4 #GP4
intPinNum1 = 5 #GP5
intPinNum2 = 6 #GP6
intPinNum3 = 7 #GP7
intPinNumDict = {'0': 4,
'1': 5,
'2': 6,
'3': 7,
}
intPinNumList = [intPinNum0, intPinNum1, intPinNum2, intPinNum3]
intPin0 = Pin(intPinNum0, Pin.IN, Pin.PULL_DOWN)
intPin1 = Pin(intPinNum1, Pin.IN, Pin.PULL_DOWN)
intPin2 = Pin(intPinNum2, Pin.IN, Pin.PULL_DOWN)
intPin3 = Pin(intPinNum3, Pin.IN, Pin.PULL_DOWN)
intPinDict = {
'0': intPin0,
'1': intPin1,
'2': intPin2,
'3': intPin3,
}
global intCount0
global intCount1
global intCount2
global intCount3
intCount0 = 0
intCount1 = 0
intCount2 = 0
intCount3 = 0
intCountDict = {
'0': intCount0,
'1': intCount1,
'2': intCount2,
'3': intCount3,
}
def intCallBack0(pin):
global intCount0
intCount0 = intCount0 + 1
return
def intCallBack1(pin):
global intCount1
intCount1 = intCount1 + 1
return
def intCallBack2(pin):
global intCount2
intCount2 = intCount2 + 1
return
def intCallBack3(pin):
global intCount3
intCount3 = intCount3 + 1
return
intCallBackDict = {
'0': intCallBack0,
'1': intCallBack1,
'2': intCallBack2,
'3': intCallBack3,
}
intPin0.irq(intCallBack0, Pin.IRQ_FALLING)
intPin1.irq(intCallBack1, Pin.IRQ_FALLING)
intPin2.irq(intCallBack2, Pin.IRQ_FALLING)
intPin3.irq(intCallBack3, Pin.IRQ_FALLING)
def countIntPinIntCountTime(intPinNum, countTime):
global intCount0
global intCount1
global intCount2
global intCount3
intCount0 = 0
intCount1 = 0
intCount2 = 0
intCount3 = 0
utime.sleep(countTime)
if intPinNum == 0:
intCount = intCount0
elif intPinNum == 1:
intCount = intCount1
elif intPinNum == 2:
intCount = intCount2
else:
intCount = intCount3
return intCount
def countIntPinIntOneTenthSecond(intPinNum):
intCount = countIntPinIntCountTime(intPinNum = intPinNum, countTime = 0.01)
return intCount
def countIntPinNumListIntOneTenthSecond(intPinNumList):
intCountList = [0] * len(intPinNumList)
for index in range(len(intPinNumList)):
intCountList[index] = countIntPinIntOneTenthSecond(intPinNumList[index])
return intCountList
# *** Test functions ***
def repeatCountIntPinNumListIntOneTenthSecond(intPinNumList, repeatTimes, pauseTime):
print('\n testCountIntPinNumListIntOneTenthSecond()')
for count in range(repeatTimes):
ppsList = countIntPinNumListIntOneTenthSecond(intPinNumList)
print(' ppsList =', ppsList, end = '')
print(' , min ', min(ppsList), end = '')
print(' , max ', max(ppsList), end = '')
print(' , dif ', max(ppsList) - min(ppsList), end = '')
print(' , avg ', int(sum(ppsList) / len(ppsList)))
'''
rpmList = ppsList.copy()
for index in range(len(rpmList)):
rpmList[index] = int(((rpmList[index] / 12) / 90) * 10 * 60)
print(' rpmList =', rpmList, end = '')
print(' , min ', min(rpmList), end = '')
print(' , max ', max(rpmList), end = '')
print(' , avg ', int(sum(rpmList) / len(rpmList)))
'''
utime.sleep(pauseTime)
return
# *** Sample Test ***
#repeatCountIntPinNumListIntOneTenthSecond(intPinNumList = [0, 1, 2, 3], repeatTimes = 4, pauseTime = 0.5)
# ========= ========= ========= ========= ========= ========= ========= =========
# ========= ========= ========= ========= ========= ========= ========= =========
# *** Part 2 - PWM Functions ***
def pwmSystemLed():
systemLedPinNum = 25
pwmPin = PWM(Pin(systemLedPinNum))
pwmPin.freq(1000)
for count in range(4):
for dutyCycle in range(65025):
pwmPin.duty_u16(dutyCycle)
utime.sleep(0.0001)
for dutyCycle in range(65025, 0, -1):
pwmPin.duty_u16(dutyCycle)
utime.sleep(0.0001)
return
def testPwmSystemLed():
print('testPwmSystemLed(), ...')
print(' System LED now fades in and out a couple of times')
pwmSystemLed()
print(' End of test.')
return
# *** Sample Test ***
#testPwmSystemLed()
# ========= ========= ========= ========= ========= ========= ========= =========
# ========= ========= ========= ========= ========= ========= ========= =========
# *** Setup 4 PWM Pins ***
# Notes:
# 1. Setting up GP 0, 1, 2, 3 as pwm pins at 1 kHz, 50% duty cycle
# 2. Connecting the pwm pins to the pwm inputs of TB6612FNG move the DC motor TM310.
# *** Pwm Pin Numbers and List ***
pwmPinNum0 = 0 #GP0
pwmPinNum1 = 1 #GP1
pwmPinNum2 = 2 #GP2
pwmPinNum3 = 3 #GP3
pwmPinNumList = [pwmPinNum0, pwmPinNum1, pwmPinNum2, pwmPinNum3]
pwmPinNumDict = {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
}
# *** Pwm Pin Objects and List ***
pwmPin0 = PWM(Pin(pwmPinNum0))
pwmPin1 = PWM(Pin(pwmPinNum1))
pwmPin2 = PWM(Pin(pwmPinNum2))
pwmPin3 = PWM(Pin(pwmPinNum3))
pwmPinList01 = [pwmPin0, pwmPin1, pwmPin2, pwmPin3]
pwmPinDict = {'0': pwmPin0,
'1': pwmPin1,
'2': pwmPin2,
'3': pwmPin3,
}
# *** Defualt Frequency and Duty Cycle ***
defaultPwmFreq = 1000
defaultPwmDutyCycle = 50
# *** Initializing Pwm Pin Objects and List ***
def setPwmFreq(pwmPin, pwmFreq):
pwmPin.freq(pwmFreq)
return
def setPwmDutyCycle(pwmPin, dutyCycle):
u16DutyCycle = int((dutyCycle / 100) * 65536)
pwmPin.duty_u16(u16DutyCycle)
return
def setupPwmPinNumList(pwmPinNumList, pwmFreqList, pwmDutyCycleList):
print(' setupPwmPinNumList(), ...')
print(' pwmPinNumList =', pwmPinNumList)
print(' pwmFreqList =', pwmFreqList)
print(' pwmDutyCycleList =', pwmDutyCycleList)
for index in range(len(pwmPinNumList)):
pwmPin = pwmPinDict[str(index)]
setPwmFreq(pwmPin, pwmFreqList[index])
setPwmDutyCycle(pwmPin, pwmDutyCycleList[index])
return
def testSetupPwmPinNumList():
setupPwmPinNumList(pwmPinNumList = [0, 1, 2, 3], pwmFreqList = [1000, 1000, 1000, 1000],
pwmDutyCycleList = [90, 90, 90, 90])
return
# Sample test ***
#testSetupPwmPinNumList()
# *** Main Tests ***
setupPwmPinNumList(pwmPinNumList = [0, 1, 2, 3],
pwmFreqList = [1000, 1000, 1000, 1000],
pwmDutyCycleList = [50, 50, 50, 50])
repeatCountIntPinNumListIntOneTenthSecond(intPinNumList = [0, 1, 2, 3], repeatTimes = 20, pauseTime = 0.2)
# *** End ***
# *** Sample Output tlfong01 2021oct04hkt1657 ***
'''
>>> %Run -c $EDITOR_CONTENT
setupPwmPinNumList(), ...
pwmPinNumList = [0, 1, 2, 3]
pwmFreqList = [1000, 1000, 1000, 1000]
pwmDutyCycleList = [50, 50, 50, 50]
testCountIntPinNumListIntOneTenthSecond()
ppsList = [32, 24, 28, 25] , min 24 , max 32 , dif 8 , avg 27
ppsList = [39, 26, 29, 23] , min 23 , max 39 , dif 16 , avg 29
ppsList = [31, 28, 28, 21] , min 21 , max 31 , dif 10 , avg 27
ppsList = [31, 28, 26, 16] , min 16 , max 31 , dif 15 , avg 25
ppsList = [31, 26, 34, 22] , min 22 , max 34 , dif 12 , avg 28
ppsList = [29, 26, 27, 19] , min 19 , max 29 , dif 10 , avg 25
ppsList = [28, 28, 24, 20] , min 20 , max 28 , dif 8 , avg 25
ppsList = [30, 24, 24, 28] , min 24 , max 30 , dif 6 , avg 26
ppsList = [33, 27, 18, 22] , min 18 , max 33 , dif 15 , avg 25
ppsList = [31, 26, 24, 19] , min 19 , max 31 , dif 12 , avg 25
ppsList = [28, 22, 25, 22] , min 22 , max 28 , dif 6 , avg 24
ppsList = [32, 21, 27, 32] , min 21 , max 32 , dif 11 , avg 28
ppsList = [34, 23, 27, 22] , min 22 , max 34 , dif 12 , avg 26
ppsList = [28, 24, 25, 20] , min 20 , max 28 , dif 8 , avg 24
ppsList = [36, 23, 31, 21] , min 21 , max 36 , dif 15 , avg 27
ppsList = [32, 26, 26, 23] , min 23 , max 32 , dif 9 , avg 26
ppsList = [32, 27, 23, 23] , min 23 , max 32 , dif 9 , avg 26
ppsList = [32, 24, 26, 23] , min 23 , max 32 , dif 9 , avg 26
ppsList = [30, 23, 30, 27] , min 23 , max 30 , dif 7 , avg 27
ppsList = [33, 26, 28, 23] , min 23 , max 33 , dif 10 , avg 27
>>>
'''
# *** End of Sample Output ***
Pico Running Out of Pins
Pico is now running out of pins. I need to consider using GPIO Extenders, such as MCP23017, MCP23s17 etc.
pwmPinNum0 = 0 #GP0
pwmPinNum1 = 1 #GP1
pwmPinNum2 = 2 #GP2
pwmPinNum3 = 3 #GP3
intPinNum0 = 4 #GP4
intPinNum1 = 5 #GP5
intPinNum2 = 6 #GP6
intPinNum3 = 7 #GP7
picoMotorDriverGpPinNumDict = { \
'0': {'StdByPinNum1': 8, #GP 8
'StdByPinNum2': 9, #GP 9
'AinPinNum1' : 10, #GP 10
'AinPinNum2' : 11, #GP 11
'PwmPinNum1' : 12, #GP 12
'BinPinNum1' : 13, #GP 13
'BinPinNum2' : 14, #GP 14
'PwmPinNum2' : 15, #GP 15
}
'1': {'StdByPinNum1': 16, #GP 16
'StdByPinNum2': 17, #GP 17
'AinPinNum1' : 18, #GP 18
'AinPinNum2' : 19, #GP 19
'PwmPinNum1' : 20, #GP 20
'BinPinNum1' : 21, #GP 21
'BinPinNum2' : 22, #GP 22
'PwmPinNum2' : 26, #GP 23
},
}
Complete Program Listing
# *** pwm_int_49.py - pwm and interrupt testing, tlfong01, 2021oct07hkt1542 ***
from machine import Pin, PWM
import utime
# ========= ========= ========= ========= ========= ========= ========= =========
# ========= ========= ========= ========= ========= ========= ========= =========
# Conents
#
# Part 1 - Interrupt Functions
# 1.1 Using GP4, 5, 6, 7 input pins to detect and count mtor encoder A signals and calculate motor speed
# Part 2 - PWM Functions
# 2.1 Using PWM pin to fade in and fade out the system LED
# 2.2 Using GP 0, 1, 2, 3 pins' PWM signals to control the speed of TB6612FNG driving TM310 DC motor
# Part 3 - TB6612FNG MotorDriver Functions
# 3.1 Setting up TB6612FNG motor drivers
# ========= ========= ========= ========= ========= ========= ========= =========
# ========= ========= ========= ========= ========= ========= ========= =========
# *** Part 1 - Interrupt Functions ***
# Interrupt functions for multiple (4) interrupt pins GP 4, 5, 6, 7 ***
intPinNum0 = 4 #GP4
intPinNum1 = 5 #GP5
intPinNum2 = 6 #GP6
intPinNum3 = 7 #GP7
picoIntGpPinNumDict = {'0': 4, # GP4
'1': 5, # GP5
'2': 6, # GP6
'3': 7, # GP7
}
intPinNumList = [intPinNum0, intPinNum1, intPinNum2, intPinNum3]
intPin0 = Pin(intPinNum0, Pin.IN, Pin.PULL_DOWN)
intPin1 = Pin(intPinNum1, Pin.IN, Pin.PULL_DOWN)
intPin2 = Pin(intPinNum2, Pin.IN, Pin.PULL_DOWN)
intPin3 = Pin(intPinNum3, Pin.IN, Pin.PULL_DOWN)
intPinDict = {
'0': intPin0,
'1': intPin1,
'2': intPin2,
'3': intPin3,
}
global intCount0
global intCount1
global intCount2
global intCount3
intCount0 = 0
intCount1 = 0
intCount2 = 0
intCount3 = 0
intCountDict = {
'0': intCount0,
'1': intCount1,
'2': intCount2,
'3': intCount3,
}
def intCallBack0(pin):
global intCount0
intCount0 = intCount0 + 1
return
def intCallBack1(pin):
global intCount1
intCount1 = intCount1 + 1
return
def intCallBack2(pin):
global intCount2
intCount2 = intCount2 + 1
return
def intCallBack3(pin):
global intCount3
intCount3 = intCount3 + 1
return
intCallBackDict = {
'0': intCallBack0,
'1': intCallBack1,
'2': intCallBack2,
'3': intCallBack3,
}
intPin0.irq(intCallBack0, Pin.IRQ_FALLING)
intPin1.irq(intCallBack1, Pin.IRQ_FALLING)
intPin2.irq(intCallBack2, Pin.IRQ_FALLING)
intPin3.irq(intCallBack3, Pin.IRQ_FALLING)
def countIntPinIntPeriod(intPinNum, countPeriod):
global intCount0
global intCount1
global intCount2
global intCount3
intCount0 = 0
intCount1 = 0
intCount2 = 0
intCount3 = 0
utime.sleep(countPeriod)
if intPinNum == 0:
intCount = intCount0
elif intPinNum == 1:
intCount = intCount1
elif intPinNum == 2:
intCount = intCount2
else:
intCount = intCount3
return intCount
def countIntPinNumListIntPeriod(intPinNumList, countPeriod):
intCountList = [0] * len(intPinNumList)
for index in range(len(intPinNumList)):
intCountList[index] = countIntPinIntPeriod(intPinNumList[index], countPeriod)
return intCountList
# *** Test functions ***
def repeatCountIntPinNumListIntPeriod(intPinNumList, countPeriod, pauseTime, repeatTimes):
print('\n countIntPinNumListIntPeriod()')
picoIntGpPinNumList = [0] * len(intPinNumList)
for index in range(len(picoIntGpPinNumList)):
picoIntGpPinNumList[index] = picoIntGpPinNumDict[str(index)]
print(' intPinNumList =', intPinNumList)
print(' picoIntGpPinNumList =', picoIntGpPinNumList)
print(' countPeriod (seconds) =', countPeriod)
print(' pauseTime (seconds) =', pauseTime)
print(' repeat count times =', repeatTimes)
print('')
for count in range(repeatTimes):
ppsList = countIntPinNumListIntPeriod(intPinNumList, countPeriod)
print(' ppsList =', ppsList, end = '')
print(' , min ', min(ppsList), end = '')
print(' , max ', max(ppsList), end = '')
print(' , dif ', max(ppsList) - min(ppsList), end = '')
print(' , avg ', int(sum(ppsList) / len(ppsList)))
'''
rpmList = ppsList.copy()
for index in range(len(rpmList)):
rpmList[index] = int(((rpmList[index] / 12) / 90) * 10 * 60)
print(' rpmList =', rpmList, end = '')
print(' , min ', min(rpmList), end = '')
print(' , max ', max(rpmList), end = '')
print(' , avg ', int(sum(rpmList) / len(rpmList)))
'''
utime.sleep(pauseTime)
return
# *** Sample Test ***
# ...
# ========= ========= ========= ========= ========= ========= ========= =========
# ========= ========= ========= ========= ========= ========= ========= =========
# *** Part 2 - PWM Functions ***
def pwmSystemLed():
systemLedPinNum = 25
pwmPin = PWM(Pin(systemLedPinNum))
pwmPin.freq(1000)
for count in range(4):
for dutyCycle in range(65025):
pwmPin.duty_u16(dutyCycle)
utime.sleep(0.0001)
for dutyCycle in range(65025, 0, -1):
pwmPin.duty_u16(dutyCycle)
utime.sleep(0.0001)
return
def testPwmSystemLed():
print('testPwmSystemLed(), ...')
print(' System LED now fades in and out a couple of times')
pwmSystemLed()
print(' End of test.')
return
# *** Sample Test ***
#testPwmSystemLed()
# ========= ========= ========= ========= ========= ========= ========= =========
# ========= ========= ========= ========= ========= ========= ========= =========
# *** Setup 4 PWM Pins ***
# Notes:
# 1. Setting up GP 0, 1, 2, 3 as pwm pins at 1 kHz, 50% duty cycle
# 2. Connecting the pwm pins to the pwm inputs of TB6612FNG move the DC motor TM310.
# *** Pwm Pin Numbers and List ***
pwmPinNum0 = 0 #GP0
pwmPinNum1 = 1 #GP1
pwmPinNum2 = 2 #GP2
pwmPinNum3 = 3 #GP3
pwmPinNumList = [pwmPinNum0, pwmPinNum1, pwmPinNum2, pwmPinNum3]
picoPwmGpPinNumDict = {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
}
# *** Pwm Pin Objects and List ***
pwmPin0 = PWM(Pin(pwmPinNum0))
pwmPin1 = PWM(Pin(pwmPinNum1))
pwmPin2 = PWM(Pin(pwmPinNum2))
pwmPin3 = PWM(Pin(pwmPinNum3))
pwmPinList01 = [pwmPin0, pwmPin1, pwmPin2, pwmPin3]
pwmPinDict = {'0': pwmPin0,
'1': pwmPin1,
'2': pwmPin2,
'3': pwmPin3,
}
# *** Defualt Frequency and Duty Cycle ***
defaultPwmFreq = 1000
defaultPwmDutyCycle = 50
# *** Initializing Pwm Pin Objects and List ***
def setPwmFreq(pwmPin, pwmFreq):
pwmPin.freq(pwmFreq)
return
def setPwmDutyCycle(pwmPin, dutyCycle):
u16DutyCycle = int((dutyCycle / 100) * 65536)
pwmPin.duty_u16(u16DutyCycle)
return
def setupPwmPinNumList(pwmPinNumList, pwmFreqList, pwmDutyCycleList):
picoPwmGpPinNumList = [0] * len(pwmPinNumList)
for index in range(len(picoPwmGpPinNumList)):
picoPwmGpPinNumList[index] = picoPwmGpPinNumDict[str(index)]
print(' setupPwmPinNumList(), ...')
print(' pwmPinNumList =', pwmPinNumList)
print(' Pico GP pin num list =', picoPwmGpPinNumList)
print(' pwmFreqList =', pwmFreqList)
print(' pwmDutyCycleList =', pwmDutyCycleList)
for index in range(len(pwmPinNumList)):
pwmPin = pwmPinDict[str(index)]
setPwmFreq(pwmPin, pwmFreqList[index])
setPwmDutyCycle(pwmPin, pwmDutyCycleList[index])
return
def testSetupPwmPinNumList():
setupPwmPinNumList(pwmPinNumList = [0, 1, 2, 3], pwmFreqList = [1000, 1000, 1000, 1000],
pwmDutyCycleList = [90, 90, 90, 90])
return
# Sample test ***
#testSetupPwmPinNumList()
# ========= ========= ========= ========= ========= ========= ========= =========
# ========= ========= ========= ========= ========= ========= ========= =========
# *** Part 3 - TB6612FNG Motor Driver Functions ***
picoMotorDriverGpPinNumDict = { \
'0': {'StdByPinNum1': 8, #GP 8
'StdByPinNum2': 9, #GP 9
'AinPinNum1' : 10, #GP 10
'AinPinNum2' : 11, #GP 11
'PwmPinNum1' : 12, #GP 12
'BinPinNum1' : 13, #GP 13
'BinPinNum2' : 14, #GP 14
'PwmPinNum2' : 15, #GP 15
}
'1': {'StdByPinNum1': 16, #GP 16
'StdByPinNum2': 17, #GP 17
'AinPinNum1' : 18, #GP 18
'AinPinNum2' : 19, #GP 19
'PwmPinNum1' : 20, #GP 20
'BinPinNum1' : 21, #GP 21
'BinPinNum2' : 22, #GP 22
'PwmPinNum2' : 26, #GP 23
},
}
# ========= ========= ========= ========= ========= ========= ========= =========
# ========= ========= ========= ========= ========= ========= ========= =========
# *** Old Main Tests ***
# *** Old Tests V1 2021oct07hkt1545 ***
def moveFourMotorsV1():
setupPwmPinNumList(pwmPinNumList = [0, 1, 2, 3],
pwmFreqList = [1000, 1000, 1000, 1000],
pwmDutyCycleList = [50, 50, 50, 50])
return
def checkFourMotorsV1():
repeatCountIntPinNumListIntPeriod(intPinNumList = [0, 1, 2, 3],
countPeriod = 0.1,
pauseTime = 0.2,
repeatTimes = 4,)
return
# ***Old tests ***
moveFourMotorsV1()
checkFourMotorsV1()
# *** Main Tests ***
#testSetupMotorDriverList(motorDriverNumList)
# *** End ***
# *** Sample Output tlfong01 2021oct04hkt1657 ***
# *** End of Sample Output ***
Moving and Stopping 4 Motors
Now I have written functions to move and start 4 motors.
def testStartAndStopMovingFourMotors():
print('Start moving 4 motors, ...')
setupMotorDriverChannel(motorDriverNum = 0, channelNum = 0)
setupMotorDriverChannel(motorDriverNum = 0, channelNum = 1)
setupMotorDriverChannel(motorDriverNum = 1, channelNum = 0)
setupMotorDriverChannel(motorDriverNum = 1, channelNum = 1)
utime.sleep(4)
print('Stop moving 4 motors.')
stopMotorDriverChannel(motorDriverNum = 0, channelNum = 0)
stopMotorDriverChannel(motorDriverNum = 0, channelNum = 1)
stopMotorDriverChannel(motorDriverNum = 1, channelNum = 0)
stopMotorDriverChannel(motorDriverNum = 1, channelNum = 1)
return
And this is the partial listing of the code.
# *** pico_4wd_v61.py - tlfong01, 2021oct09hkt1534 ***
from machine import Pin, PWM
import utime
# ========= ========= ========= ========= ========= ========= ========= =========
# ========= ========= ========= ========= ========= ========= ========= =========
# Conents
#
# Part 1 - Measure 4WD Motor Speeds
# 1.1 Using GP4, 5, 6, 7 input pins to detect and count mtor encoder A signals and calculate motor speed
# Part 2 - Change 4WD Speed and Direction
# 2.1 Using PWM pin to fade in and fade out the system LED
# 2.2 Using GP 0, 1, 2, 3 pins' PWM signals to control the speed of TB6612FNG driving TM310 DC motor
# Part 3 - Setup 4WD Motor Drivers
# 3.1 Setting up TB6612FNG motor drivers
# ========= ========= ========= ========= ========= ========= ========= =========
# ========= ========= ========= ========= ========= ========= ========= =========
# *** Part 3 - TB6612FNG Motor Driver Functions ***
stdByPinNum0 = 8
stdByPinNum1 = 9
aIn1PinNum0 = 10
aIn2PinNum0 = 11
aPwmPinNum0 = 0
bIn1PinNum0 = 12
bIn2PinNum0 = 13
bPwmPinNum0 = 1
aIn1PinNum1 = 14
aIn2PinNum1 = 15
aPwmPinNum1 = 2
bIn1PinNum1 = 16
bIn2PinNum1 = 17
bPwmPinNum1 = 3
motorDriverGpPinNumDict = { \
'0': {'StdByPinNum' : stdByPinNum0,
'0' : {'In1PinNum' : aIn1PinNum0,
'In2PinNum' : aIn2PinNum0,
'PwmPinNum' : aPwmPinNum0,
},
'1' : {'In1PinNum' : bIn1PinNum0,
'In2PinNum' : bIn2PinNum0,
'PwmPinNum' : bPwmPinNum0,
},
},
'1': {'StdByPinNum' : stdByPinNum1,
'0' : {'In1PinNum' : aIn1PinNum1,
'In2PinNum' : aIn2PinNum1,
'PwmPinNum' : aPwmPinNum1,
},
'1' : {'In1PinNum' : bIn1PinNum1,
'In2PinNum' : bIn2PinNum1,
'PwmPinNum' : bPwmPinNum1,
},
},
}
def setupMotorDriverChannel(motorDriverNum, channelNum):
stdByPinNum = motorDriverGpPinNumDict[str(motorDriverNum)]['StdByPinNum']
in1PinNum = motorDriverGpPinNumDict[str(motorDriverNum)][str(channelNum)]['In1PinNum']
in2PinNum = motorDriverGpPinNumDict[str(motorDriverNum)][str(channelNum)]['In2PinNum']
pwmPinNum = motorDriverGpPinNumDict[str(motorDriverNum)][str(channelNum)]['PwmPinNum']
stdByPin = Pin(stdByPinNum, Pin.OUT)
in1Pin = Pin(in1PinNum, Pin.OUT)
in2Pin = Pin(in2PinNum, Pin.OUT)
pwmPin = Pin(pwmPinNum, Pin.OUT)
stdByPin.high()
in1Pin.low()
in2Pin.high()
pwmPin.high()
return
def stopMotorDriverChannel(motorDriverNum, channelNum):
stdByPinNum = motorDriverGpPinNumDict[str(motorDriverNum)]['StdByPinNum']
in1PinNum = motorDriverGpPinNumDict[str(motorDriverNum)][str(channelNum)]['In1PinNum']
in2PinNum = motorDriverGpPinNumDict[str(motorDriverNum)][str(channelNum)]['In2PinNum']
pwmPinNum = motorDriverGpPinNumDict[str(motorDriverNum)][str(channelNum)]['PwmPinNum']
stdByPin = Pin(stdByPinNum, Pin.OUT)
in1Pin = Pin(in1PinNum, Pin.OUT)
in2Pin = Pin(in2PinNum, Pin.OUT)
pwmPin = Pin(pwmPinNum, Pin.OUT)
stdByPin.high()
in1Pin.high()
in2Pin.high()
pwmPin.high()
return
def testStartAndStopMovingFourMotors():
print('Start moving 4 motors, ...')
setupMotorDriverChannel(motorDriverNum = 0, channelNum = 0)
setupMotorDriverChannel(motorDriverNum = 0, channelNum = 1)
setupMotorDriverChannel(motorDriverNum = 1, channelNum = 0)
setupMotorDriverChannel(motorDriverNum = 1, channelNum = 1)
utime.sleep(4)
print('Stop moving 4 motors.')
stopMotorDriverChannel(motorDriverNum = 0, channelNum = 0)
stopMotorDriverChannel(motorDriverNum = 0, channelNum = 1)
stopMotorDriverChannel(motorDriverNum = 1, channelNum = 0)
stopMotorDriverChannel(motorDriverNum = 1, channelNum = 1)
return
# ***
testStartAndStopMovingFourMotors()
# ========= ========= ========= ========= ========= ========= ========= =========
# ========= ========= ========= ========= ========= ========= ========= =========
# *** Old Main Tests ***
# *** Old Tests V1 2021oct07hkt1545 ***
def pwmChange4MotorSpeeds():
setupPwmPinNumList(pwmPinNumList = [0, 1, 2, 3],
pwmFreqList = [1000, 1000, 1000, 1000],
pwmDutyCycleList = [50, 50, 50, 50])
return
def intMeasure4MotorSpeeds():
repeatCountIntPinNumListIntPeriod(intPinNumList = [0, 1, 2, 3],
countPeriod = 0.1,
pauseTime = 0.2,
repeatTimes = 4,)
return
# ***Sample Tests ***
#pwmChange4MotorSpeeds()
#intMeasure4MotorSpeeds()
# *** Main Tests ***
testStartAndStopMovingFourMotors()
#utime.sleep(2)
# *** End ***
# *** Sample Output tlfong01 2021oct04hkt1657 ***
# *** End of Sample Output ***
/ to continue, …