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
Two small if/elif/else chains, the same pattern as the shift logic in robot.py.
voltage = 11.8 and match_time = 75.
PART A: set level to 'CRITICAL' if voltage is under 11, 'LOW' if under 12, else 'GOOD'.
PART B: set shift to 'TRANSITION' if match time is 10 or less, 'SHIFT' if 110 or less, else 'ENDGAME'.
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.
Next lesson: 3. Lists and Loops.