Uncategorized

pico 4wd

*** pwm_int_55.py – pwm and interrupt testing, tlfong01, 2021oct08hkt2044 ***

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’: {‘StdByPinNum’ : 8, #GP 8
‘AinPinNum1’ : 10, #GP 10
‘AinPinNum2’ : 11, #GP 11
‘PwmPinNum1’ : 0, #GP 0
‘BinPinNum1’ : 12, #GP 12
‘BinPinNum2’ : 13, #GP 13
‘PwmPinNum2’ : 1, #GP 1
},
‘1’: {‘StdByPinNum’ : 9, #GP 9
‘AinPinNum1’ : 14, #GP 14
‘AinPinNum2’ : 15, #GP 15
‘PwmPinNum1’ : 2, #GP 2
‘BinPinNum1’ : 16, #GP 16
‘BinPinNum2’ : 17, #GP 17
‘PwmPinNum2’ : 3, #GP 3
},
}

========= ========= ========= ========= ========= ========= ========= =========

========= ========= ========= ========= ========= ========= ========= =========

*** 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 ***

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.