Step 2
Numbers and maths
Use Python as a calculator, import the math module and convert degrees to radians.
Python as a calculator
Python does arithmetic with these operators:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ - | add, subtract | 7 - 2 | 5 |
* | multiply | 3 * 4 | 12 |
/ | divide | 7 / 2 | 3.5 |
// | divide, rounding down | 7 // 2 | 3 |
% | remainder | 7 % 2 | 1 |
** | power | 2 ** 3 | 8 |
Whole numbers like 4 are ints. Numbers with a decimal point like 0.25 are floats. The usual order of operations applies, and parentheses group: (1 + 2) * 3 is 9.
Floats are stored in binary, so some decimals come out a hair off: 0.1 + 0.2 prints 0.30000000000000004. That's normal, and you'll learn to round for display in the next step.
The math module
More maths lives in the math module. Write import math at the top of the program, then use math.pi, math.sqrt(x) (square root), math.sin(x) and friends.
Robots measure angles in radians, not degrees. A full turn is 360°, or radians, so:
Your task
The arm's upper arm is 0.30 m long and its forearm 0.25 m. The starter stores them in upper_arm and forearm.
- Set
reachto the two lengths added together. Use the variables, not the numbers. - Set
angle_radtoangle_degconverted to radians. - Set
diagonalto the length of the diagonal across a square table 0.8 m on each side: .