1. Variables and Types

A variable stores a value under a name. Robot code is full of variables: motor speeds, positions, and True/False flags.

Summary

fly_speed = 50          # int: whole number
hood_angle = 35.5       # float: number with a decimal point
is_intaking = False     # bool: only True or False
team_name = 'Ctrl-Z'    # str: text

Rules and tools:

  • = stores a value in a name. == compares two values (more in lesson 2).
  • Anything after # on a line is a comment. Python ignores it.
  • Use snake_case for variable names. Use UPPER_CASE for constants that never change, like the ones in const.py.
  • A name must exist before you read it. Reading a name that does not exist gives a NameError. Most NameErrors are typos.
  • Math operators: + - * work as usual. Division is special:
    • / always gives a float: 7 / 2 is 3.5
    • // divides and drops the remainder: 7 // 2 is 3
    • % gives only the remainder: 7 % 2 is 1
    • ** is a power: 2 ** 3 is 8
  • tick += 1 is short for tick = tick + 1. You will see this in every loop we write.
  • Useful built-in functions: abs(x), min(a, b), max(a, b).
  • An f-string puts values into text. Prefix the string with f and put the variable inside {}:
speed = 3.14159
print(f'moving at {speed} m/s')      # moving at 3.14159 m/s
print(f'moving at {speed:.1f} m/s')  # moving at 3.1 m/s

Use :.1f to keep one decimal place, :.2f for two. Most dashboard numbers in our code print this way.

In our robot code

Constants in robot/const.py:

SWERVE_DRIVE_GEAR_RATIO = 5.68
DRIVETRAIN_TRACKWIDTH_METERS = inchesToMeters(23.5)

Booleans in robot/robot.py store the state of the robot:

self.shoot_intent = False
self.is_intaking = False
self.fly_speed = 50

Try it

Try it
Try it: variables and f-strings
✓ solved

Run the code. Then change speed to 0 and run again. Add your own f-string that prints your name and your team number.

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

1.1 Inches to meters

Try it
Inches to meters
✓ solved

The robot’s geometry values are in meters. We build in inches. 1 inch is 0.0254 meters.

inches = 28.5 is our drive base width.

PART A: set meters to the inches value converted to meters.

PART B: set message to an f-string that says 28.5 inches = 0.72 meters. Print the meters value with two decimal places.

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.

1.2 Clamp joystick values

Try it
Clamp joystick values
✓ solved

A joystick can report a little more than 1.0 when it is pushed hard. Commands to the drivetrain must stay in [-1, 1].

PART A: clamp stick_high into clamped_high.

PART B: clamp stick_low into clamped_low.

PART C: clamp stick_mid into clamped_mid.

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.

1.3 Battery flags

Try it
Battery flags
✓ solved

The driver station warns about a weak battery with booleans.

voltage = 11.8.

PART A: set is_low to True when the voltage is under 12.0.

PART B: set is_critical to True when the voltage is under 11.0.

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: 2. Conditionals and Logic.