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)

PlantUML Syntax:<br /><noscript><img src=http://www.plantuml.com/plantuml/img/HO_1JiD034Jl-nK-9h-G72h80Gc1L0b-OBMTT0sfNTbUKF-ULTIAgySpyoPDpwf1BA7qbfkK2flJj1XoW1OrTCqZIy6S3DT-Jh-LDz2mg8Jfji7edTrPpeSnJH_8LYgjQlJ_C2V8QRUbwGL1hthpuF9DHtKELl4vyKh3Kmd-wRKrTvUSSHWfAeG6qJqwwAAXvjHf1IqiRntsQ5hWJXT8wqsRTxWRV7V8YSeZZKepWmHzFUhxiC0En-jz7TtNVLh3cFu0 alt= :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.

PlantUML Syntax:<br /><noscript><img src=http://www.plantuml.com/plantuml/img/PSmz2e0m30RGlQV8s5wX3WuUmHC4-ccB_GbfmEiBWfFh6vwBM-PQqK-GPGKdciPgI8jJ2DXS5EWnj97nl3wbEvSAyhiD4QGrK9da-mZk7ngqbSxl-G00 alt= :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. 

PlantUML Syntax:<br /><noscript><img src=http://www.plantuml.com/plantuml/img/JO_12i9034Jl-nK1GTgZ7bIw1m_UX7fels1nKxhORfOaelNhtHOLBs48CuyPKpKdq9xHUH1rhEYjOKper8GEYfgKM214kE1TsGrbTcE48b9fgcQEdMaI3TSRjEeKhQdgy8Jb8-1Ync8-uym1IOXUG0bEnDcAWo0K4qfcjRDjlJfiDplxGgP5xkrVPc58JaKVkaz9UFHXo8nMAINqnnA2VClzXYdo6A9Rjhq1 alt= :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">