void loop()
The main loop starts with the ps4.getPS4() command. This command gets the
status of all the PS4 controller’s buttons, triggers, joysticks, and sensors and returns
the information to PRIZM. This command needs to be frequently repeated so
that as the user uses the controller, the status of what buttons are pressed gets
communicated to PRIZM.
The first if statement determines if there is an obstacle within the designated
detection range of the Ultrasonic Sensor. If the sensor detects an obstacle, the PS4’s
rumble motors are turned on. Otherwise, they are stopped.
The second if statement determines if there is a difference between what is stored
in the touchx variable and the current
x
value of the PS4’s touch pad. The
ps4.Touchpad(TOUCHX) command is what returns the
x
value of the touch pad.
If there is a difference between these two values, then the touchPadControl()
function is called. Otherwise, this function is skipped over. The touchPadControl()
function is where the speed of the motors are set using the touch pad.
Next, there are three while loops. These while loops determine if certain button
combinations are pressed. The ps4.buttons_1 command returns a unique byte
value depending on which buttons are pressed from Group 1. Group 1 buttons
include L1, L2, R1, R2, Triangle, Square, Circle, and Cross. Like Group 1, the
ps4.buttons_2 command also returns a unique byte value based on which buttons
from Group 2 are pressed. Group 2 includes the Up, Down, Left, Right, L3, R3, Share,
and Options buttons.
The first while loop will continuously call the lineFollow() function if the value
returned from ps4.buttons_1 equals the lineBtnCombo variable, which is 5. Five
is the unique value for when both the L2 and R2 buttons are pressed at the same
time. To simplify, the sketch will continuously run the lineFollow() function if the L2
and R2 buttons are both pressed.
Similar to the first while loop, the second while loop continuously calls the
pitchRollControl() function when the L1 and R1 buttons are pressed. When
these two buttons are pressed, the ps4.buttons_1 command will return 10,
which is the same value that is set for the pitchRollBtnCombo variable, and the
pitchRollControl() function is executed.
The third while loop is for controlling the robot with the direction buttons. The
direction buttons are part of Group 2, which is why ps4.buttons_2 is used. The only
time ps4.buttons_2 returns 0 is when none of the buttons in Group 2 are pressed.
This while loop uses a not (!=) comparison to say, “while not none of the buttons in
Group 2 are pressed, call the directionButtonControl() function.”
Not none
doesn’t
make a lot of sense in English, but in coding, a double negative makes a positive
just like it does in math. So in other words, this while loop says, “while at least one
of the Group 2 buttons is pressed, call the directionButtonControl() function.”
6
TETRIX Tele-Op Control Module Activity 2