Chapter 1 – Introduction to Python with Zumis
1.6 – Intermediate Driving Commands
Overview
Now that we’ve explored some basic drive commands such as go forward, reverse, left, and right, let’s take a look at other ways that we can program Zumi to move around. The Zumi library has many types of commands for you so that Zumi can operate just like a real self-driving car! Let’s take a look!
First, let’s import the required libaries and create a Zumi object.
from zumi.zumi import Zumi
import time
zumi = Zumi().
Drive Functions
One of the basic driving functions that we initally learned was forward(int speed)
, which took speed as parameter to be used to set Zumi’s speed.
Along with the basic driving functions that allows Zumi to move forward, backwards, left, and right, there are many other advanced driving functions that we can also use. Let’s take a look!
- right_u_turn(speed=30, step=4, delay=0.02)
- left_u_turn(speed=30, step=4, delay=0.02)
- circle(speed=30, step=2, direction=1, delay=0.02)
- right_circle(speed=30, step=2)
- left_circle(speed=30, step=2)
- square(speed=40, seconds=1, direction=1)
- square_right(speed=40, seconds=1.0)
- square_left(speed=40, seconds=1.0)
- j_turn(speed=100, step=4, delay=0.005)
- figure_8(speed=30, step=3, delay=0.02)
- parallel_park(speed=15, step=1, delay=0.01)
- rectangle(speed=40, seconds=1.0, direction=1, ratio=2)
- triangle(speed=40, seconds=1.5, direction=1)
In the commands provided above, various parameters can be added in and modified to control various aspects of each of these commands(i.e. speed, direction). Take a look below at what each of these parameters do!
- speed: the forward speed you want Zumi to drive at
- step: the angle step size as it goes from (0 – 180)
- delay: the delay between each angle step
- direction: -1 for clockwise , +1 for counterclockwise
- seconds: the duration Zumi will drive for each side
- ratio: the ratio of the longer side to the shorter side
Note: Diagrams have been provided in the images folder that illustrate the motion of each of these commands. Take a look!
Now that we’ve taken a look at some of the advanced driving functions available, let’s try using them in an actual example.
Try running the code below. What does it do?
zumi.figure_8() # Runs figure 8 at default values
time.sleep(1)
zumi.figure_8(speed = 50) # Changing one value
time.sleep(1)
zumi.figure_8(step=4, delay=0.03) # Changing two values
time.sleep(1)
zumi.figure_8(speed=40, step=4, delay=0.03) # Changing all three values
Activity
Using the knowledge that we’ve learned in this lesson, use the new driving functions to write 2 different programs, one that draws a square using basic commands, and another one that uses the advanced driving commands. What differences do you notice in the programs? Is there a particular way you prefer?
Starter code for the 2 programs have been provided below to get you started.
Drawing a square using basic driving commands
from zumi.zumi import Zumi
import time
zumi = Zumi()
fill in the rest of the code here...
Drawing a square using advanced driving commands
from zumi.zumi import Zumi
import time
zumi = Zumi()
fill in the rest of the code here...
Demo Video
Questions