Can an AI chatbot assist with cyber:bot script development? Let’s try. The image above is an example of a script that makes the cyber:bot roll forward and then pivot to the right. In this tutorial, we will follow Bing Chat developing a similar script.
You will need:
Complete these tutorials first:
You will be able to:
Let’s try a script that’s a little different from the overview. It’s a little easier to manage on a table. Here, we will make the cyber:bot roll forward for 1.25 seconds, then turn in place for 0.85 seconds. At the time of this writing, ChatGPT has trouble with this, so we are using Bing Chat.
User
Write a Python script to make the cyber:bot robot roll forward for 1250 ms, then rotate right in place for 850 ms.
Bing Chat
Searching for: cyber:bot robot roll forward for 1250 ms, then rotate right in place for 850 ms
Searching for: Python script cyber:bot robot roll forward for 1250 ms, then rotate right in place for 850 ms
Generating answers for you…
Here is a Python script that makes the cyber:bot robot roll forward for 1250 ms, then rotate right in place for 850 ms:
Python
AI-generated code. Review and use carefully. More info on FAQ.
from cyberbot import * import time # Roll forward for 1250 ms bot(18).servo_speed(75) # Left wheel counterclockwise bot(19).servo_speed(-75) # Right wheel clockwise time.sleep(1.25) # Rotate right in place for 850 ms bot(18).servo_speed(75) # Left wheel counterclockwise bot(19).servo_speed(75) # Right wheel counterclockwise time.sleep(0.85) # Stop the servos bot(18).servo_speed(None) bot(19).servo_speed(None)
This script uses the cyberbot library to control the robot’s servos. The bot(18) and bot(19) calls are used to control the left and right wheels, respectively. The servo_speed function is used to set the speed and direction of each servo. The time.sleep function is used to pause the script execution for the specified duration.
I hope this helps!
Learn more:
1. learn.parallax.com
cyberbot-template-with-blink.hex [5]
Did the cyber:bot roll forward and turn right as expected?
Which would be quicker for tuning the right turn to 90 degrees, asking the AI to do it, or just testing and updating time.sleep values?
Now that we know Bing Chat can make a script for the cyber:bot to roll forward and turn, let's see if it can expand upon that.
User
Assuming this results in forward followed by a 90 degree turn, modify the script to make the cyber:bot travel in a square.
Searching for: cyber:bot travel in a square
AI Chatbot
Searching for: Python script cyber:bot travel in a square
Generating answers for you…
To make the cyber:bot travel in a square, you can modify the previous script to include four movements: forward, right turn, forward, and right turn. Here is the modified Python script:
Python
AI-generated code. Review and use carefully. More info on FAQ.
from cyberbot import * import time # Roll forward for 1250 ms bot(18).servo_speed(75) # Left wheel counterclockwise bot(19).servo_speed(-75) # Right wheel clockwise time.sleep(1.25) # Rotate right in place for 850 ms bot(18).servo_speed(75) # Left wheel counterclockwise bot(19).servo_speed(75) # Right wheel counterclockwise time.sleep(0.85) # Roll forward for 1250 ms bot(18).servo_speed(75) # Left wheel counterclockwise bot(19).servo_speed(-75) # Right wheel clockwise time.sleep(1.25) # Rotate right in place for 850 ms bot(18).servo_speed(75) # Left wheel counterclockwise bot(19).servo_speed(75) # Right wheel counterclockwise time.sleep(0.85) # Roll forward for 1250 ms bot(18).servo_speed(75) # Left wheel counterclockwise bot(19).servo_speed(-75) # Right wheel clockwise time.sleep(1.25) # Rotate right in place for 850 ms bot(18).servo_speed(75) # Left wheel counterclockwise bot(19).servo_speed(75) # Right wheel counterclockwise time.sleep(0.85) # Roll forward for 1250 ms bot(18).servo_speed(75) # Left wheel counterclockwise bot(19).servo_speed(-75) # Right wheel clockwise time.sleep(1.25) # Stop the servos bot(18).servo_speed(None) bot(19).servo_speed(None)
This script uses the cyberbot library to control the robot’s servos. The bot(18) and bot(19) calls are used to control the left and right wheels, respectively. The servo_speed function is used to set the speed and direction of each servo. The time.sleep function is used to pause the script execution for the specified duration.
I hope this helps!
Learn more:
1. learn.parallax.com
For more information on how to write scripts to make the cyber:bot navigate, see Navigation with the cyber:bot [2].
After the AI learned what it needed to from Navigation with the cyber:bot [2], you might have expected it to use a for… loop to repeat the same maneuver four times. But instead, it repeated the forward-left maneuver four times! It would be better to write it as shown here:
Do you think maybe Bing Chat can be prompted to do a better scripting job?
User
That's a pretty long script. Couldn't you use a loop to get that done in fewer lines?
Bing Chat
Searching for: Python script cyber:bot travel in a square
Generating answers for you…
Yes, you can use a loop to make the script more concise. Here is a modified version of the script that uses a for loop to repeat the forward and right turn movements four times:
Python
AI-generated code. Review and use carefully. More info on FAQ.
from cyberbot import * import time # Define the duration of the forward and right turn movements forward_duration = 1.25 # seconds turn_duration = 0.85 # seconds # Repeat the forward and right turn movements four times for i in range(4): # Roll forward for 1250 ms bot(18).servo_speed(75) # Left wheel counterclockwise bot(19).servo_speed(-75) # Right wheel clockwise time.sleep(forward_duration) # Rotate right in place for 850 ms bot(18).servo_speed(75) # Left wheel counterclockwise bot(19).servo_speed(75) # Right wheel counterclockwise time.sleep(turn_duration) # Stop the servos bot(18).servo_speed(None) bot(19).servo_speed(None)
This script defines the duration of the forward and right turn movements using variables, then uses a for loop to repeat the movements four times. The range(4) function generates a sequence of numbers from 0 to 3, which are used as the loop variable i. The loop repeats the forward and right turn movements four times, then stops the servos.
I hope this helps!
Learn more:
1. learn.parallax.com
Links
[1] https://www.parallax.com/product/cyberbot-robot-kit-with-microbit/
[2] https://learn.parallax.com/tutorials/robot/cyberbot/navigation-cyberbot
[3] https://learn.parallax.com/tutorials/robot/cyberbot/ai-simple-python-scripts
[4] https://learn.parallax.com/tutorials/robot/cyberbot/add-modules-your-microbit
[5] https://learn.parallax.com/sites/default/files/content/cyberbot/Software/cyberbot-template-with-blink.hex