Discover our
resources
Materials required:
- 1 computer/robot
- 1 robot minimum or software simulation
- Optional activity arena (robot race)
Software configuration :
- example configuration: depends on the code situation
Duration :
1 to 2 hours
Age :
+ 14 years
The advantages of this activity :
- Can be performed with the simulator
- A very good introduction to programming
In this Python programming activity, students learn how to send commands to the robot based on the keys pressed on the keyboard, and then organize a robot race.
[Video content coming soon]
Handling the robot
The alphai.py module sends instructions written in Python to the robot.
For the module to work, the "AlphAI" software must be running. You can connect to an AlphAI robot (or its simulated version).
In your favorite Python editor, create a new project in the location of your choice and configure the Python environment to contain the alphai module by typing in the IPython console or a terminal :
pip install alphai-api
This command downloads and installs the module and its dependencies.
Open a new python document or restart the console, copy and execute the following code:
from alphai import *
motor(30, -30, 5)
If the first line of the program causes an error, the alphai library has not been installed correctly. If the line motor(30, -30, 5) causes an error, AlphAI has probably not been started.
If there is no error, you watch the robot (or the robot simulated in the software) turn on itself for 5 seconds.
The motor(left, right, duration) command sends left and right values to the robot's left and right motors for duration, after which it stops. The left and right values must be between -50 and +50. If the duration is not specified, the robot continues to move forward until the next motor(0,0) call.
⚠️ It is possible and sometimes useful to make the connection in your python script rather than by clicking "connect" in the software, using the functions connect_bluetooth(n) (replacing n with the number of your robot) or connect_wifi(), as well as disconnect() to disconnect.
The code in this case looks like this:
from alphai import *
connect_bluetooth(n)
motor(30, -30, 5)
disconnect()
On the right-hand side of this page, in the downloadable appendices, you'll find the alphai-api library documentation, which contains all the functions included in the alphailibrary . Check it out now.
First script
Now you can create a Python script in your favorite programming environment.
Create a new Python file and copy the following code:
from alphai import *
if _ _name_ _ == '_ _main_ _':
connect_bluetooth(n)
motor(30,30,1)
disconnect()
Then execute it: the robot moves forward for one second, then stops.
The motor(left, right, duration)command sends left and right values to the left and right motors. These values must be between -40 and +40. Naturally, motor(0, 0) is used to stop the robot.
On the right-hand side of this page, in the downloadable appendices, you'll find the documentation for the alpha-api library, which contains all the functions included in the alphai library . Check it out now.
Programming a sequence
To make coding easier, define the following five functions. Make them very simple: each function should only make one call to the motor function.
forward()
Moves the robot straight ahead.
backward()
Moves the robot backwards.
stop()
Stop the robot.
left()
Rotates the robot to the left.
right()
Rotates the robot to the right.
Finally, create a walk_around() function that causes the robot to perform a sequence of actions using these five functions. Test it by calling it in the main block.
Remote control programming
Reading pressed keys
Using the API function wait_for_key (see explanations on last page) and a while loop in the main function, print the keys pressed on the keyboard in the console. To avoid writing an infinite loop, use a special key to exit the loop, such as the "esc" key:
keys = []
while "esc" not in keys:
# don't forget indentation
Remote control programming
Still using the wait_for_key function and a while loop, create a remote_control()function that reacts to directional keys in such a way that the robot moves according to the instructions given on the keyboard. To retrieve the last key pressed, select the last item in the list using the -1 index. Warning: the list may sometimes be empty!
Remember to stop the robot when no key is pressed. Test your code by remote-controlling the robot!
Further information
We'll be able to organize races between your remote-controlled robots, but you'll also want to improve your program by adding new commands, as suggested below... These improvements are only suggestions, so don't hesitate to create your own!
Different curves
Create two new functions that you can use when the wait_for_key function returns a list of several keys:
forward_left()
Moves the robot to the left when the top and left arrows are pressed simultaneously. In this way, you can make the robot make both tight turns with left() (left arrow only) and wide turns with forward_left() (top and left arrows).
forward_right()
Pressing the up and right arrows simultaneously also moves the robot to the right.
Braking and boost
Add input variables to all your functions, so you can modulate the robot's speed, or the strength of turns, depending on which other keys are pressed.
For example, add a speed input variable to all your functions (e.g. forward(speed)), and call the functions with a lower speed when the space bar is pressed at the same time (braking), and a higher speed when the "x" key is pressed at the same time ("boost").
Pilot your robot and adjust the speed parameters to make it as maneuverable and fast as possible. Don't forget to practice overtaking competitors too!
Programming a sequence
On the key of your choice, have the robot perform a little choreography, for example to celebrate victory!
Review and feedback
In programming, the list of accessible program functions is called an API, for Application Programming Interface. It is used by programmers to find out how to interact with the program. You'll find the API for the alphai python module here: https: //drive.google.com/file/d/1C4ovPW_eH5KFz5Y9JvSrzLhtmdOpcp6-/view