Lets take an inventory of all of the functions that we've used so far to control various aspects of the Linkbot.
| Function Name | Function Description |
|---|---|
setLEDColor(r, g, b) | Changes the LED color based on red, green, and blue intensities. |
setBuzzerFrequency(Hz) | Makes the Linkbot buzzer play a frequency in Hertz. |
moveJoint(jointNum, degrees) | Makes a single motor move. |
move(degrees, degrees, degrees) | Makes multiple motors move simultaneously |
Using these functions, we can move our robot around, change the robot's LED color, and make the robot beep or play simple melodies. However, using only these functions, it is not possible to make a robot play a tune and change its LED colors while the robot is moving. For example, lets say we want to make our robot beep 2 times _while_ our robot is moving. We can try something like this:Template:Message box
myLinkbot.setBuzzerFrequency(440)# 1time.sleep(0.25)# 2myLinkbot.setBuzzerFrequency(0)# 3time.sleep(0.25)# 4myLinkbot.setBuzzerFrequency(440)time.sleep(0.25)myLinkbot.setBuzzerFrequency(0)time.sleep(0.25)myLinkbot.move(180,0,-180)# 5
Lets reason our way through each major point of this program.
move() function makes the Linkbot roll forward.This isalmost what we want to do, except we want the robot to beep twicewhile the robot is moving; not before it starts moving. Lets try again:
myLinkbot.move(180,0,-180)# 1myLinkbot.setBuzzerFrequency(440)# 2time.sleep(0.25)myLinkbot.setBuzzerFrequency(0)time.sleep(0.25)myLinkbot.setBuzzerFrequency(440)time.sleep(0.25)myLinkbot.setBuzzerFrequency(0)time.sleep(0.25)
This program is almost exactly the same as the program before except we relocated themove() function to the beginning of the program. Will this program make the robot beep twice as the robot is moving?
move() function which moves the robot's motors. The robot begins moving. However, the program stays at item 1 until themove() function is completed. After the robot finishes moving, the program continues.setBuzzerFrequency() andtime.sleep(), it makes the buzzer beep twice.moveNB() FunctionAs we can see, we have failed yet again to make the robot beep twice while moving. In order to make the robot do these things simultaneously, we must use a similar but new type of function: Non-blocking functions. Lets take a look at an example:
myLinkbot.moveNB(180,0,-180)# 1myLinkbot.setBuzzerFrequency(440)# 2time.sleep(0.25)myLinkbot.setBuzzerFrequency(0)time.sleep(0.25)myLinkbot.setBuzzerFrequency(440)time.sleep(0.25)myLinkbot.setBuzzerFrequency(0)time.sleep(0.25)
move() function, we use themoveNB() function.The "NB" in the function name stands for "non-blocking". Both functions move the motors on the Linkbot, but there is one important difference: The NB version of the function "returns" immediately. What this means is Python continues on to the next line of code immediately without waiting for the movement to finish first. In other words, themoveNB() function does not "block" Python from continuing on immediately after the function is called.
moveWait() FunctionNow lets try one more example. Lets say we want to make the robot move its wheels 180 degrees to roll forward while beeping twice, and then change the LED color to greenafter the movement is done. To accomplish this, we introduce themoveWait() function. Lets take a look and see how it works:
myLinkbot.moveNB(180,0,-180)# 1myLinkbot.setBuzzerFrequency(440)# 2time.sleep(0.25)myLinkbot.setBuzzerFrequency(0)time.sleep(0.25)myLinkbot.setBuzzerFrequency(440)time.sleep(0.25)myLinkbot.setBuzzerFrequency(0)time.sleep(0.25)myLinkbot.moveWait()# 3myLinkbot.setLEDColor(0,255,0)# 4
moveWait(). This function blocks until all motor movements on the robot are finished. In effect, Python will wait at # 3 until the motors are done moving.If we had omittedmoveWait() at # 3, the LED color would've been set whether the motors were still moving or not. By using themoveWait() function, we force Python to wait for the motors to stop moving before setting the LED color.
Almost all Linkbot movement commands have a non-blocking version with the "NB" suffix. The ones that do not have a non-blocking version are the ones that move a Linkbot's motor continuously forever. We haven't explored any of them yet, but they are out there. These functions do not have non-blocking versions because they would block forever.
All of the functions that begin with the prefix "set", such assetBuzzerFrequency() andsetLEDColor(), can be considered non-blocking because of how fast the buzzer and LED colors are set. For instance, if you run the following snippet of code:
myLinkbot.setBuzzerFrequency(440)myLinkbot.setLEDColor(0,255,0)
The Linkbot would appear to start buzzing and change the LED color to green simultaneously. Technically, the robot is actually doing one before the other, but the two things happen so fast (typically within 5 milliseconds of each other) that it is practically simultaneous.
| Learning Python 3 with the Linkbot | ||
| ← Decisions | Linkbot Multitasking | Debugging → |