6. Mission 1 Drive the Car

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp

Use nano to edit the main code, as mentioned previously in Step 2.

pi@raspberrypi:~/my_pismart $ nano my_pismart.py

It’s pretty easy to drive the car to move forward – just replace pass

in the loop() with the following code.

def loop():
    my_pismart.MotorA = 60  # Set MotorA speed 60
    my_pismart.MotorB = 60  # Set MotorB speed 60


Pay attention to the indent of the two lines: four spaces compared to def loop(). loop() is a function and the two lines underneath is the content. The indent indicates the affiliation between the two lines and loop().

When completed, the code will be like below:

GNU nano 2.2.6                                                                  File: my_pismart.py
from pismart.amateur import PiSmart
from time import sleep
 
def setup():
    global my_pismart
    my_pismart= PiSmart()
    # If one of the motor is reversed, change its False to True
    my_pismart.MotorA_reversed = False
    my_pismart.MotorA_reversed = False
 
def end():
    my_pismart.end()

def loop():
    my_pismart.MotorA = 60  # Set MotorA speed 60
    my_pismart.MotorB = 60  # Set MotorB speed 60
 
 
if __name__ == "__main__":
    try:
        setup()
        while True:
            loop()
    except KeyboardInterrupt:
        end()
 
[ Read 25 lines ]
^G Get Help   ^O WriteOut    ^R Read File   ^Y Prev Page    ^K Cut Text       ^C Cur Pos
^X Exit       ^J Justify     ^W Where Is    ^V Next Page    ^U UnCut Text     ^T To Spell


After the code modification is done, press Ctrl + O to save and Ctrl + X to exit.

Use python my_pismart.py to run this code like Step 3.  

pi@raspberrypi:~/my_pismart $ python my_pismart.py

If you see the following error when running the code,

File "my_pismart.py", line 17
    my_pismart.MotorB = 60
^
IndentationError: unexpected indent


Find the line with error: line 17. Check whether the space quantity is right or not.

Similarly, if other error messages appear, you can check according to the prompted line number and compare it with the code above.

After the command, you will see both wheels of the car start spinning. Put the car on the ground, and then the car will move forward.

If the car does not move forward as designed, it should be the motor installation – the movement of the car is decided by both the original rotation direction of the motor and the direction in installation. You can change False to True behind MotorA_reversed andMotorB_reversed in setup(). (Only change the value of the reversely-rotating motor).

my_pismart.MotorA_reversed = True   # If MotorA is reversed, change the value False to True
my_pismart.MotorB_reversed = True   # If MotorB is reversed, change the value False to True


The added my_pismart.MotorA = 60 and my_pismart.MotorB = 60 in loop() is to set the speed of both motors as 60, thus the car will move forward (positive value means forward).

You can try modifying the rotation speed of the motors in the code. The movement will be different due to different speed settings.

When the speed is set as 0, the motor stops rotating.

# PiSmart Car Stop

my_pismart.MotorA = 0   # set MotorA speed as 0
my_pismart.MotorB = 0   # set MotorB speed as 0


When it is negative, the car will move backward and the speed range should be -100-100. 

# PiSmart Car backward
my_pismart.MotorA = -60
my_pismart.MotorB = -60


When the speed of two motors is different, the car will change its direction. When MotorB rotates slower than MotorA, the car will turn to the side of MotorB.

# PiSmart Car turn left
my_pismart.MotorA = 60
my_pismart.MotorB = 20

On the other hand, if MotorA rotates slower than MotorB, the car will turn toward the direction of MotorA.

# PiSmart Car turn right
my_pismart.MotorA = 20
my_pismart.MotorB = 60


We can also put all movements together to form a series of coherent action. Use nano to open the code:

pi@raspberrypi:~/my_pismart $ nano my_pismart.py

Then write the actions above in def loop():

def loop():
    # PiSmart Car move forward
    my_pismart.MotorA = 60  # Set MotorA speed 60
    my_pismart.MotorB = 60  # Set MotorB speed 60
    sleep(3)    # sleep 3 seconds
 
    # PiSmart Car stop
    my_pismart.MotorA = 0 
    my_pismart.MotorB = 0 
    sleep(3)  
 
    # PiSmart Car backward
    my_pismart.MotorA = -60 
    my_pismart.MotorB = -60 
    sleep(3)   
 
    # PiSmart Car turn left
    my_pismart.MotorA = 60 
    my_pismart.MotorB = 20 
    sleep(3)   
 
    # PiSmart Car turn right
    my_pismart.MotorA = 20 
    my_pismart.MotorB = 60 
    sleep(3)  


For better understanding, we’ve added comments (contents after #) for you to read more conveniently, which would not affect the running of the code.

sleep(3) Let the program wait for 3 seconds. Here is to keep the action for 3 seconds.

After modifying the code, press Ctrl + O to save and Ctrl + X to exit. And type in y to confirm to save, and press Enter for the file name. Now, you can use python my_pismart.py to run the code.

pi@raspberrypi:~/my_pismart $ python my_pismart.py