2. Conditionals and Logic
Do different things for different states. This is how a subsystem decides what to do each frame.
Summary
if voltage < 11:
print('CRITICAL')
elif voltage < 12:
print('LOW')
else:
print('GOOD')
Rules and tools:
- A condition ends with a colon
:. The lines under it are indented with four spaces. Indented lines belong to theif. A wrong indent gives a syntax error. elifmeans “else if”. The conditions run top to bottom. The first true one runs, and the rest are skipped. Order sets priority.elseruns when no condition above it was true. It is optional.- Comparisons:
==equal,!=not equal,<,<=,>,>=. - Chain comparisons like math:
2.5 < time_left < 3means both parts are true. Our hub-shift rumble code uses this. - Combine conditions with
and,or,not:
is_intaking = True
fuel_in_hopper = 10
is_intaking and fuel_in_hopper < 24 # True: both parts are true
is_intaking or fuel_in_hopper == 0 # True: one part is true
not is_intaking # False
Nonemeans: no value yet. Example:self.auto_win = Noneuntil the FMS sends the auto winner. Test it withis Noneoris not None, not with==.- Name bool variables like questions:
is_intaking,at_default,has_fuel. The name should tell you what True means.
Common mistake:
if is_intaking = True:assigns a value. It does not compare. Python shows a SyntaxError. Use==to compare, or just writeif is_intaking:.
In our robot code
robot/subsystems/intake.py, periodic(). This runs every frame. The order sets the priority:
if self.robot.intake_at_default:
self.stop_intake()
elif self.robot.clear_jam:
self.set_intake_speed(-0.3 * 100)
elif self.robot.is_intaking:
self.set_intake_speed(0.85 * 100)
robot/robot.py, the Auto Win logging:
if self.auto_win is None:
SmartDashboard.putString("Auto Win", "UNKNOWN")
Try it
Run the code. Then change match_time to 5 and to 130. Run again each time.
The code operates in your browser. The page sends nothing to a server and saves your work on this computer. Press Ctrl+Enter to run it.
Exercises
2.1 Battery warnings and shift windows
Use if/elif/else chains to select practice labels. These thresholds are exercise values, not FRC rules or battery operating limits.
PART A: for voltage = 11.8, set level to ‘CRITICAL’ below 11, ‘LOW’ below 12, or ‘GOOD’ otherwise.
PART B: for match_time = 75, set shift to ‘TRANSITION’ at 10 or less, ‘SHIFT’ at 110 or less, or ‘ENDGAME’ otherwise.
PART C: use the same battery rules with critical_voltage = 10.9. Write a separate chain that sets critical_level.
Select Run checks. Then change an input and run your code again. Discuss the result with a mentor. Battery boundaries: 11.0 gives LOW; 12.0 gives GOOD. Time boundaries: 10 gives TRANSITION; 11 and 110 give SHIFT; 111 gives ENDGAME. The automated checks use the supplied inputs; they do not verify all these boundaries.
The code operates in your browser. The page sends nothing to a server and saves your work on this computer. Press Ctrl+Enter to run it.
2.2 The intake state machine
periodic() in robot/subsystems/intake.py is a chain of flags. The order sets the priority.
The four states are: 'stowed', 'reversing', 'intaking', 'stopped'.
SCENARIO A: at_default False, clear_jam True, is_intaking False. Fill in state_a with an if/elif/else chain.
SCENARIO B: all three flags are True. Write the same chain for state_b.
The code operates in your browser. The page sends nothing to a server and saves your work on this computer. Press Ctrl+Enter to run it.
2.3 Label the auto winner
robot.py logs who won autonomous, but until the FMS sends the result there is no answer. That is Python’s None.
auto_win: None means unknown, True means RED, False means BLUE.
PART A: auto_win_a = None. Set label_a with if/elif/else.
PART B: auto_win_b = True. Set label_b the same way.
The code operates in your browser. The page sends nothing to a server and saves your work on this computer. Press Ctrl+Enter to run it.
2.4 Extension: test conflicting robot flags
Optional extension. Use the same branch order as exercise 2.2: default, jam clearing, intake, then stopped.
For each supplied scenario, first fill in prediction_a, prediction_b, or prediction_c. Then replace the matching state assignment with an if/elif/else chain. Keep the supplied inputs.
Use these state labels: stowed, reversing, intaking, stopped. Scenario A requests both jam clearing and intake. Scenario B enables all flags. Scenario C enables no flag.
Select Run checks. Explain why the first true branch wins. These labels describe the teaching model, not a complete subsystem simulation.
The code operates in your browser. The page sends nothing to a server and saves your work on this computer. Press Ctrl+Enter to run it.
Next lesson: 3. Lists and Loops.