Uncategorized

spv2 notes

Making a Rpi Pico Based Smart Vehicle

ProjectsJun 3013 / 13Aug 1719h ago

tlfong01Jul 10 tlfong01:

Next step is writing functions to turn vehicle right and left.

The turn right/left functions are easy to write and test. The ‘#’ symbol crashes with the forum editor. So I replaced the symbol to ‘$’

$ Program Name
$   move_dc_motor_v08.py - tlfong01 2021jul10hkt1625
$ 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
$         https://www.aliexpress.com/item/32688083107.html
$   (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
$       GP9  - Motor1, 2, Standby
$   (b) Run program to move in sequence, two motors forward, backward, and stop

import utime
from machine import Pin

$ Configuration

multiMotorDriverDict01 = {
    'Title'   : 'Motor Driver Dict For 4WD',
    'STBY'    : 9,
    '1' : {'IN1'    : 10,
           'IN2'    : 11,
           'PWM'    : 12,
          },
    '2' : {'IN1'    : 13,
           'IN2'    : 14,
           'PWM'    : 15,
          },
    '3' : {'IN1'    : 0,
           'IN2'    : 0,
           'PWM'    : 0,
          },
    '4' : {'IN1'    : 0,
           'IN2'    : 0,
           'PWM'    : 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']
    
    print('In1PinNum =', in1PinNum)
    print('In2PinNum =', in2PinNum)
    print('pwmPinNum =', pwmPinNum)
    
    in1Pin = Pin(in1PinNum, Pin.OUT)
    in2Pin = Pin(in2PinNum, Pin.OUT)
    pwmPin = Pin(pwmPinNum, Pin.OUT)
    
    picoMotorDriverControlPinDict = {'IN1': in1Pin, 'IN2': in2Pin, 'PWM': pwmPin}
    
    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


Now that I have completed the point to point wiring of Pico GB6~9 pins to the two N20 encoder outputs. Next step is to write a Pico microPython to do all motor control in software.


spv_2wd_wire_2021jul10021024×396 212 KBReply

tlfong01Jul 11 tlfong01:

Now that I have completed the point to point wiring of Pico GB6~9 pins to the two N20 encoder outputs. Next step is to write a Pico microPython to do all motor control in software.

I forgot I need first to do some calibration of the N20 motor encoder signal vs speed (rpm) off line (ie no Pico software). So I disconnect the Pico GP signal and use manual jumper wires to give the control signals, and use my 50MHz scope to check the motor speed. as shown below.n20_encoder_analysis_2021jul11011024×408 183 KB


Now I am analysing the N20 Motor 1 Encoder signal Output 1, and see if we can calculate the motor speed from this signal. Avery rough formula is 600uS ~= 30 rpm. Next step is is see how to do the time stamping use micro python.


n20_motor_speed_2021jul12011024×408 160 KB


MicroPython utime – time related functions
https://docs.micropython.org/en/latest/library/utime.html

Description – The utime module provides functions for getting the current time and date, measuring time intervals, and for delays.


I read the docs for utime and found it similar to Thonny python, except a little bet smaller. I found user friendly examples on how to use the utime (now I know the “u” of utime means “micro”! :grinning:). One good such example has limit of time calculation less than 500us, but N20 motor’s timing is of the order of 600uS, in other words, just NOT make. I am too lazy to modify my demo programs to it my N20 applications. So lazy me go to Tom’s Hardware for help. I remember Tom;s Hardware has developed a big number of tutorials for newbies, as listed below.


Tom’s Hardware Tutorials on Rpi Pico

In the relatively short time that the Pico has been on the market, the Raspberry Pi community has already developed a ton of resources. At Tom’s Hardware, we’ve been publishing our fair share of Pico how-tos, which you can find below.

  1. How to Set Up and Program Raspberry Pi Pico

Tutorial #7 on DC motors is useful for my N20 motor 2WD project here. There is also a useful demo program on the use of buttons. The critical statements are highlighted in pink.


pico_button_demo_2021jul1304685×676 110 KBReply1 MONTH LATER

tlfong0119h

Resuming Long Stalled SPV (Smart Pico Vehicle) Project

Part 1

This project has be stalled for over a month, and I have forgotten what I did last time. So I need now to refresh my memory. The first thing is to make sure the basic Pico hardware still running OK. My first test procedure is to run the “toggle system led program”. Luckily all looks well. Below is the updated toggle led program, with sample output.

# Program Name
#   toggle_pico_system_led_v04.py - tlfong01 2021aug16hkt1606
# Configuration
#   Thonny 3.3.3, Acer Intel CORE i5 Chinese Windows 10 (64-bit), Python 3.7.9 (32-bit), Tk 8.6.9, USB COM Port #4
# Intepreter
#   Micropython (Rapsberry Pi Pico)
# Program Function
#   Toggle Pico system LED at GPIO pin 15 (run program to switch on LED, run again to switch off)
# User Guide
#   Run code to toggle LED from On to Off or Off to On

from machine import Pin

systemLed = Pin(25, Pin.OUT)

print('Testing Toggling Pcio System LED, v0.4  tlfong01  2021aug16hkt1613')

systemLed.toggle()

# END

# Sample Output - tlfong01 2021aug16hkt1614
'''
>>> %Run -c $EDITOR_CONTENT
Testing Toggling Pcio System LED, v0.4  tlfong01  2021aug16hkt1613
>>> %Run -c $EDITOR_CONTENT
Testing Toggling Pcio System LED, v0.4  tlfong01  2021aug16hkt1613
>>> 
'''


Part 2 – Checking the 2WD Wiring

I vaguely remember that I first tested one TT130 toy motor with the very simple MX1508 driver, as described by the Tom’s Hardware tutorial. Then I moved on to used TB6616 motr driver, with two N20 motors with speed encoders. I vaguely remember that I used Pico GPIO to read the one of the two encoder output signals, and got a rough idea of the motor speed vs encoder signal periods. So I need to resume from this point. But before that, I need to make sure the motor driver and motor can still move. First step is check out the hardware setup and wiring, and run the basic test program.

Actually I forgot the details of the hardware. So I read my old post to refresh my memory: Making a Rpi Pico Based Smart Vehicle.

The following image is a good memory refresher:

image

Part 3 – Manual Jumper Wire Motor Moving Testing

Now I am using the following cheat sheet to refresh my memory on how to use jumper wires to off line testing moving motorsimage850×840 192 KB

To move Motor 1 CW, Motor 2 CCW, I am using the following conifg/wiring

  1. IN1, IN2 (Yellow, Blue) to Low High or High Low
  2. PWM (Purple) = High
  3. StdBy (Brown) = High

Part 4 – Using MicroPython to test moving motors

Now I am using the following Pico MicroPython program is move one motor forward, backward, and stop, and found everything OK.

# Program Name
#   move_one_motor_v01.py - tlfong01 2021aug16hkt2218
# 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
#       https://www.aliexpress.com/item/32855311589.html
#   (b) N20 Gear Motor
#   
# DC Motor Driver
#   (a) MX1508 2~10V, 1.5A, Dual H-Bridge DC Motor Driver - AliExpress US$1
#         https://www.aliexpress.com/item/32688083107.html
#   (b) TB6612FNG Dual DC Motor Driver

# Program Function
#   Move DC motor forward, backward, and stop

# User Guide
#   (a) Connect PWMA (purple), PWMB (purple) to High (Vcc)
#   (b) Connect Standby (brown) to High
#   (c) Run program to move motor forward, backward, and stop

import utime
from machine import Pin

motor1a = Pin(10, Pin.OUT)
motor1b = Pin(11, Pin.OUT)

def moveMotorForward():
    motor1a.low()
    motor1b.high()

def moveMotorBackward():
    motor1a.high()
    motor1b.low()

def stopMotor():
    motor1a.low()
    motor1b.low()

def test():
    print('  Begin move motor test()')
    moveMotorForward()
    utime.sleep(1)
    moveMotorBackward()
    utime.sleep(1)
    stopMotor()
    print('  End   test()')

for i in range(2):
    print('Test ', i)
    test()

# *** End of program ***

# *** Sample Output - tlfong01  2021jul01hkt1707
'''
>>> %Run -c $EDITOR_CONTENT
Test  0
  Begin move motor test()
  End   test()
Test  1
  Begin move motor test()
  End   test()
>>> 
'''


Part 5 – Moving two motors

Now that my little demo microPython program moving one N20 motor goes well, I am now upgrading it to move two motors. Now I have a problem of mixing up the getting complicated jumper wires, and therefore find it difficult to do troubleshooting. So I need to do some proper documentation, starting with the photo below.spv2_wiring_2021aug17061024×480 185 KB

Now the time has come to use Pico MicroPython to move one and two motors

/ to continue, …

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.