Piwars Coding
The basic principal behind all 4 versions of Tito (including stretch) is they use a similar code structure. There is one main thread (separate program) that controls the motors and constantly loops reading controller state. This means no matter what is happening, nothing can interrupt or block the motors from being disabled by the ps4 controller #SafetyFirst. This thread also deals with the main menu and can start and stop a completely separate “Challenge” thread. Got it?
Main Thread (Launcher)
:Boot into Launcher;
:Create Core instance;
repeat
while (Controller Missing?)
:Search for Controller;
endwhile
:Get Joystick Positions;
if (Active Challenge?) then (no)
:Set motors to neutral;
:Process menu;
else (yes)
:Send challenge current joystick state;
endif
repeat while (True)
" usemap="#plantuml_map">
Basic Challenge Thread
Each challenge thread (rc mode, over the rainbow etc) MUST expose a set of common methods that will be called by the Launcher. E.g.
Run() – Called each loop through the launcher code to pass controller state, a bit like arduino Loop().
Stop() – Called when user wants to exit current challenge.
:Challenge thread started;
repeat
:perform task;
repeat while (Stopped?) is (no)
:Challenge thread terminated;
" usemap="#plantuml_map">
RC Challenge Thread
The RC thread is unique in that it never ends by itself. This allows the user to stay in remote control mode until they press the HOME button on the controller. This causes the Launcher thread to issue a “Stop()” call to the current challenge (the RC thread). All other autonomous challenges will stop after a set time, or when it senses it can’t go further.
:RC thread started;
repeat
if (Motors in Neutral) then (no)
:Read Joystick State;
:Mix sticks and send motor speeds to Core;
else (yes)
:Send ‘zero’ speeds to motors;
endif
repeat while (Stopped?) is (no)
:RC thread terminated;
" usemap="#plantuml_map">