Note: it is assumed that the reader has already read and understands Play Station Control Redux. An article written by Jon Williams
First I will explain the basics of how the PS2 controller works
For each button pressed there is a specific number stored in either the psxThumbR or psxThumbL variable depending on which side of the controller the button is on. The button values stored in the psxThumbR or psxThumbL variables can be displayed in either binomial and decimal format. When only one button is being pressed at a time the decimal format will work fine. But if you want to be able to press multiple buttons at a time then the binomial format may be easer to use. Although the numbering scheme for all play station remotes should be similar double check!
Equipment Used
You should have the basic Boe-Bot setup (frame, wheels, board, etc.) all hooked up. See page 101 in "Robotics with the Boe-Bot" by Andy Lindsay.
NOTE: I have the Boe-Bot faced in the opposite direction (the spherical ball is in the "front" of my bot). So if you have the traditional set-up then you may need to interchange pin 12 and pin 13 in the "Variables/Constants/Pins for boe-bot movements" section.
In addition I also set up a lego grab claw and a rotating ping sensor. If you do not have these components hooked up just comment out that part of the code and the rest of it should work fine.
If you just have the basic setup you should be able to move the boe-bot around using the four movement arrows on the left thumb pad (buttons E, F, G, and H. See Diagram 1 below) while in digital mode. While in analog mode you should be able to use the right joystick to move the boe-bot around.
Diagram 1 below maps out the buttons on the PS2 controller (code also contains diagram).
' Diagram 1.
'
' L (left) R (right)
' _____ _____
' | | | |
' | [ ] | A | [ ] | B
' | [ ] | C | [ ] | D
' |_____| |_____|
'
' _____ _____
' | | | |
' | | | |
' / H \_______________/ L \
' | O M N O |
' | EO OG [ ] [ ] IO OK |
' | O ___ [ ] ___ O |
' \___F___/ \____O___/ \__J___/
' \___/ \___/
P Q
Introductory Comments on Digital Values
One of the First sections of notes we run into is as follows:
' Button Numbers Chart:
'
' psxThumbR:
' A: (L 2), BIN8 = 11111110, DEC = 254
' B: (R 2), BIN8 = 11111101, DEC = 253
' C: (L 1), BIN8 = 11111011, DEC = 251
' D: (R 1), BIN8 = 11110111, DEC = 247
'
' psxThumbL
' E: (L arrow), BIN8 = 01111111, DEC = 127
...
What this tells us is that the variable psxThumbR will be used to store the information directly underneath it. Namely, if the A button is pressed thenpsxThumbR will hold the value 254 (in decimal format). And like wise with the buttons B, C, and D when pressed individually.
Looking at the first line, A represents the arbitrary designation of the button in question (See Diagram 1).
(L 2) is a more descriptive name that I refer to when typing the code
BIN8 = 11111110 tells us the that 1111110 is the binomial number stored in the variable psxThumbR
DEC = 254 tells us the that 254 is the decimal number that is stored in the variable psxThumbR
Introductory Comments on Analog Values
Unlike digital devices, analog devices can take on a wide range of values. Below is an excerpt of code that describes the extreme values that the analog joysticks on the PS2 controller can take on.
' (127, 000)
' ^
' |
' Q: (psxJoyR), |
' P: (psxJoyL), (000, 127) <-- (127, 127) --> (255, 127)
' |
' |
' (127, 255)
What the above tells us is that psxJoyR (joystick Q) and psxJoyL (joystick P, see Diagram 1) take on a range of values between 0 and 255. When both joysticks are pressed fully forward, both psxJoyR and psxJoyL will store the values (127, 000). When both joysticks are pressed fully to the right, bothpsxJoyR and psxJoyL will store the value (255, 127). Process is similar for the left and back. I only use the right joystick in this application. If I push the right joystick forward then psxJoyR will store the values (127, 000). Furthermore psxJoyR is broken up into its x-components, represented by the variablepsxJoyRX (the first three numbers, the ones before the comma) and its y-components, represented by the variable psxJoyRY (the last three numbers, the ones after the comma).
Variables/Constants/Pins
I have used many variables, constants, and pins due to all the sensors and servos I am using on the boe-bot. If you are not extensively using sensors then you may remove the variables, constants, pins, and code relating to them.
Main Loop
I have broken the main loop in to two sections. The first section is used when the PS2 controller is in analog mode. The second section is used when the controller is in digital mode. This may seem odd because when in analog mode none of the other sensors can be used. The user will have to go to digital mode to use any of the sensors. This is because when in analog mode we must use our time wisely. In analog mode we must get rid of any procedures that use up too much of the Boe-Bots processing time. First I will describe how the digital code works, because it is the easiest to understand, then I will explain how the analog code works.
Digital Code
When a button is pressed its value will either be stored in psxThumbR or psxThumbL depending on which side of the controller the button lies on. The value of each button click is shown in decimal and binary format in Diagram 1. So if we want a button press, say button E, to correspond to a specific action, all we have to do is check to see if the psxThumbL variable stores the value that corresponds to that particular button click i.e. the decimal number 127. That is exactly what the code below does for us.
IF psxThumbL = 127 THEN ' (L arrow)
GOSUB TurnLeft
If the E button is pressed then the Basic Stamp will execute the TurnLeft subroutine. NOTE, this is assuming only one button at a time is being pressed.
Analog Code
There are two main mathematical operations taking place which are described below.
JoyToPulseForBac = psxJoyRY + 623 ' < 750 means go forward, > 750 means go backward. Range: 623 to 877.
JoyToPulseLefRig = psxJoyRX ' < 127 means turn left, > 127 means turn right. Range: 0 to 255.
One might ask why we add the 623 to psxJoyRY. We add 623 to psxJoyRY because the middle value for psxJoyRY is 127, and 127 + 623 = 750. The range of JoyToPulseForBac then becomes 623 to 877 as the value of psxJoyRY goes from 0 to 255
PulseLeft = ((1500 - (JoyToPulseForBac)) + (JoyToPulseLefRig)) - 127
PulseRight = ((JoyToPulseForBac) + (JoyToPulseLefRig)) - 127
The pulse functions are a bit more complicated.
We will start by explaining the PulseLeft function. JoyToPulseForBac values go from 623 (joystick fully forward) to 877 (joystick fully back). But larger values of PulseLeft correspond to forward motion while smaller values correspond to backward motion. So we take 1500 - JoyToPulseForBac because 1500 - 750 = 750, thereby inversing the JoyToPulseForBac values, making large JoyToPulseForBac small and vice versa. We then add JoyToPulseLefRig to the the previous quantity. We add the JoyToPulseLefRig value because a larger value corresponds to turning more right, thus dictating that the left wheel move forward faster than the right wheel. We subtract 127 lastly because JoyToPulseLefRig goes from 0 to 255, with 127 being the middle value. By subtracting 127 we are making JoyToPulseLefRig go from -127 to +127 thereby centering the JoyToPulseLefRig value. One might ask why we don't simply subtract 127 directly from psxJoyRX and make JoyToPulseLefRig have a range of -127 to +127 to make this step more intuitive. it would look like this:JoyToPulseLefRig = psxJoyRX - 127. The reason we don't do this is because the BS2 can't handle variables with negative values. By making the subtraction of 127 our last step we make all the variables have positive values.