Uncategorized

spv 2WD program

Program Name

move_dc_motor_v11.py – tlfong01 2021jul10hkt1658

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

Thonny 3.3.3, Windows 10 (64-bit), Python 3.7.9 (32-bit), Tk 8.6.9, USB COM Port #4

Intepreter

Micropython (Rapsberry Pi Pico)

DC Motor

(a) TT130 DC3~6V DC Gear Motor – AliEXpress US$1#

(b) N20 Gear Motor x 2

DC Motor Driver

(a) MX1508 2~10V, 1.5A, Dual H-Bridge DC Motor Driver – AliExpress US$1

(b) TB6612FNG Dual DC Motor Driver

Program Functions

(a) Move two N20 DC motors forward, backward, and stop.

(b) Move left motor forward, right motor backward, so to turn vehicle right.

(c) Similarly, left motor backward, right motor forward, so to turn vehicle left

User Guide

(a) Configuration

GP10 – Motor1 IN1

GP11 – Motor1 IN2

GP12 – Motor1 PWMA

GP13 – Motor2 IN1

GP14 – Motor2 IN2

GP15 – Motor2 PWMB

GP6 – Motor1 Encoder Output 1

GP7 – Motor1 Encoder Output 2

GP8 – Motor2 Encoder Output 1

GP9 – Motor2 Encoder Output 2

GP5 – Motor Drivr Standby

(b) Run program to move in sequence, two motors forward, backward, and stop

(c) Turn vehicle right and left

(d) Input N20 encoder signals into Pico

import utime
from machine import Pin

Configuration

multiMotorDriverDict01 = {
‘Title’ : ‘Motor Driver Dict For 4WD’,
‘STBY’ : 9,
‘1’ : {‘IN1’ : 10,
‘IN2’ : 11,
‘PWM’ : 12,
‘ENA’ : 6,
‘ENB’ : 7,
},
‘2’ : {‘IN1’ : 13,
‘IN2’ : 14,
‘PWM’ : 15,
‘ENA’ : 8,
‘ENB’ : 9,
},
‘3’ : {‘IN1’ : 0,
‘IN2’ : 0,
‘PWM’ : 0,
‘ENA’ : 0,
‘ENB’ : 0,
},
‘4’ : {‘IN1’ : 0,
‘IN2’ : 0,
‘PWM’ : 0,
‘ENA’ : 0,
‘ENB’ : 0,
},
}

multiMotorDriverDictDict = {
‘1’: multiMotorDriverDict01,
‘1’: multiMotorDriverDict01,
}

***

def setupMotor(multiMotorDriverDictDictNum, motorDriverNum):
motorDriverDict = multiMotorDriverDictDict[str(multiMotorDriverDictDictNum)]

in1PinNum = motorDriverDict[str(motorDriverNum)]['IN1']
in2PinNum = motorDriverDict[str(motorDriverNum)]['IN2']
pwmPinNum = motorDriverDict[str(motorDriverNum)]['PWM']
enaPinNum = motorDriverDict[str(motorDriverNum)]['ENA']
enbPinNum = motorDriverDict[str(motorDriverNum)]['ENB']

print('In1PinNum =', in1PinNum)
print('In2PinNum =', in2PinNum)
print('PwmPinNum =', pwmPinNum)
print('EnaPinNum =', enaPinNum)
print('EnbPinNum =', enbPinNum)    

in1Pin = Pin(in1PinNum, Pin.OUT)
in2Pin = Pin(in2PinNum, Pin.OUT)
pwmPin = Pin(pwmPinNum, Pin.OUT)
enaPin = Pin(enaPinNum, Pin.IN)
enbPin = Pin(enbPinNum, Pin.IN)       

picoMotorDriverControlPinDict = {'IN1': in1Pin, 'IN2': in2Pin, 'PWM': pwmPin, 'ENA': enaPin, 'ENB': enbPin}

return picoMotorDriverControlPinDict

def moveMotorForward(motorDriverDict):
in1Pin = motorDriverDict[‘IN1’]
in2Pin = motorDriverDict[‘IN2’]
pwmPin = motorDriverDict[‘PWM’]

in1Pin.low()
in2Pin.high()
pwmPin.high()    
return

def moveMotorBackward(motorDriverDict):
in1Pin = motorDriverDict[‘IN1’]
in2Pin = motorDriverDict[‘IN2’]
pwmPin = motorDriverDict[‘PWM’]

in1Pin.high()
in2Pin.low()
pwmPin.high()    
return

def stopMotor(motorDriverDict):
in1Pin = motorDriverDict[‘IN1’]
in2Pin = motorDriverDict[‘IN2’]
pwmPin = motorDriverDict[‘PWM’]

in1Pin.low()
in2Pin.low()
pwmPin.high()    
return

def turnVehicleRight(motorDriverDictLeft, motorDriverDictRight):
moveMotorForward(motorDriverDictLeft)
moveMotorBackward(motorDriverDictRight)
return

def turnVehicleLeft(motorDriverDictLeft, motorDriverDictRight):
moveMotorForward(motorDriverDictRight)
moveMotorBackward(motorDriverDictLeft)
return

Main test functions

def testSequentiallyMoveTwoMotorsForwardBackwardAndStop():
motorDictDictNum = 1
motorNumList = [1, 2]

for motorNum in motorNumList:
    motorDriverDict = setupMotor(motorDictDictNum, motorNum)
    moveMotorForward(motorDriverDict)
    utime.sleep(1)
    moveMotorBackward(motorDriverDict)
    utime.sleep(1)
    stopMotor(motorDriverDict)    
return

def testTurnVehicleRightOneSecondThenLeftOneSecond():
motorDictDictNum = 1
leftMotorDriverDict = setupMotor(motorDictDictNum, 1)
rightMotorDriverDict = setupMotor(motorDictDictNum, 2)

turnVehicleRight(leftMotorDriverDict, rightMotorDriverDict)    
utime.sleep(2)
turnVehicleLeft(leftMotorDriverDict, rightMotorDriverDict)    
utime.sleep(2)
stopMotor(leftMotorDriverDict)
stopMotor(rightMotorDriverDict)

Main

testSequentiallyMoveTwoMotorsForwardBackwardAndStop()

testTurnVehicleRightOneSecondThenLeftOneSecond()

End of program

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 )

Google photo

You are commenting using your Google 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.