Chapter 1 – Introduction to Python with Zumis
1.2 – Basic Driving Commands
Welcome to your first lesson with Zumi! Before you start exploring artificial intelligence and having Zumi drive on her own, you need to learn some basic drive commands. In this lesson, you will program Zumi to go forward, drive in reverse, turn right, and turn left.
Import Libraries
Before writing code with Zumi, we must first import the libraries needed and run them.
from zumi.zumi import Zumi
import time
zumi = Zumi()
Degrees
This code will turn Zumi to the right by 45 degrees.
zumi.turn_right(45)
If the same function is run without a paramater, it will default to 90 degrees.
zumi.turn_right()
Recalibration
Zumi can be recalibrated which means that all her sensors are reset in case of any bugs or errors that may have occurred.
zumi.mpu.calibrate_MPU()
What is a Function?
Zumi uses functions for its movement. Functions are like packages of code that can help make programs more efficient. Below is a list of basic functions Zumi uses.
* forward() #Drive forward in the direction Zumi is facing at speed 40 for 1 second
* reverse() #Reverse in the direction Zumi is facing at speed 40 for 1 second
* turn_left() #Pivot 90 degrees to the left
* turn_right() #Pivot 90 degrees to the right
To call these functions, we need to attach it to the Zumi object we created:
* zumi.forward()
* zumi.reverse()
* zumi.turn_left()
* zumi.turn_right()
Parameters
Some of these functions contain parameters, which are like inputs you can pass in to change the output of the function.
The forward() function can be called with 0 or 2 parameters. If called with 2 parameters, then the speed and duration must be specified.
zumi.forward(speed = 30, duration = 2)
Also, the turn functions also have parameters. When you use turn_right() or turn_left(), you can leave the parameters empty or specify 2 parameters.
The 2 parameters contain the desired angle, and the duration that you turn for.
zumi.turn_left(desired_angle=45, duration=1)
“` python
“`
Conclusion
Congratulations, you can start using Basic Driving Commands to control Zumi in Python!
Demo Video
Questions