If you’re familiar with the CEENBoT, then you’ve probably used its remote controller. The PS2-like controller comes with a detachable receiver and allows users to drive the robot, change speeds, and change modes.
Early on in the development phase of the STEMBoT, it was decided that the radio system needed to be developed to give users more control over its buttons. This resulted in the new controller system, which uses an Xbox controller case and custom hardware that allows users to program (in Python) nearly every button and the joysticks. In this post, I’ll explain the basics of programming the remote, as well as how to get joystick values. The following post will demonstrate how to get button values and use them to control the SB2 LED. The final post will go through the process of writing a complete program for the remote controller that lets you get the most from the new radio system.
To begin, make sure your remote has charged batteries, and that your STEMBoT is plugged in to your computer and the REPL is running. To use the remote system, we’ll import the remote module.
>>>import remote
There are two major objects that we’ll need to create to program using the remote: the Remote() object which allows us to access various functions, and a RemoteData() object which will let us store and organize the remote controller signals. We’ll instantiate (or create an instance of) each object by assigning them to variables. For simplicity’s sake, we’ll call these new objects ‘r’ and ‘rdata’.
>>>r=remote.Remote()
>>>rdata=remote.RemoteData()
The next step is to make sure the remote and STEMBoT are talking to each other. To do this, we must use the pair() method within the Remote() class. In the case of the STEMBoT and its remote, pairing is a five-step “handshaking” process that allows the STEMBoT and the remote to agree to a set of rules. So long as the rules are observed (which is done automatically) the STEMBoT knows exactly which controller is trying to talk to it. As soon as the code to begin pairing is sent, press the pairing button next to the left button (LB) at the top of the controller. If the pairing was successful, the REPL will return 2. If pairing is unsuccessful, you may need to reset the STEMBoT and try again. However, once a STEMBoT and remote is paired, both devices will remember one another, so this only needs to be done once.
>>>r.pair()
2
>>>
Now that the devices are paired, we can send (transmit) data from the remote and capture (receive) it on the robot. The remote sends data constantly and automatically as long as it’s on, so we need to capture and read what was sent. To do this, use the read() method of the Remote() class. This method accepts two parameters: the variable where the data will be stored (rdata, in our case), and how long it should try to read the data, called the timeout. The timeout is set to 2 seconds by default, and probably doesn’t need to be changed. We want to store the data in the rdata variable that we created previously, so the function will look like this:
>>>r.read(rdata)
As soon as you send that line of code, the STEMBoT will capture the most recent data sent by the remote. Now we have to analyze the data to put it to use. The RemoteData() class contains 5 variables, 4 are reserved for joystick directions, and one is for the sixteen programmable buttons. Let’s take a look at the joysticks first. Type in the following line (if the remote shuts off while programming, just turn it back on and it should still be paired and ready to go):
>>>rdata.ljoy_up_down
0
>>>
This line reads the up/down value from the left joystick. The other values are ljoy_left_right, rjoy_up_down, and rjoy_left_right. The code will return 0 unless the joystick was being pressed or the remote needs calibration. Try holding the left joystick up all the way, then capture the remote data again with r.read(rdata) and read the joystick value with rdata.ljoy_up_down. It should return 128, which is the maximum value for upward deflection on the this joystick. The maximum value in the opposite direction is -127. The complete list of joystick values can be found in section 4.4.2.1 of the User’s Manual.
The next article in this series will explore the programmable buttons and will include a sample program to change the color of the LED using the controller.
Pingback: Programming the STEMBoT Remote, Part 3 – STEMBOT Incorporated | United States | Home | STEMBoT | Education
Pingback: Programming the STEMBoT Remote, Part 2 – STEMBOT Incorporated | United States | Home | STEMBoT | Education