Once a script has the x and y tilt values, it can calculate rotation angles.
For example, if you hold the micro:bit (or cyber:bot) vertically, and then rotate it 30°, the script can tell you so.
A script can also calculate the overall direction you are tilting the micro:bit, even if you’re not holding it vertically.
For example, if you rotate it 45° and then tilt it away from you by another 45°, the 45° angle measurement tells you which corner you are tilting it toward. If that angle was 135°, it would mean you are tilting it toward a different corner.
# test_accel_xy_angle from microbit import * import math sleep(1000) while True: x = accelerometer.get_x() y = accelerometer.get_y() angle = round( math.degrees( math.atan2(y, x) ) ) print("x =", x, ", y =", y, ", angle =", angle) sleep(750)
Are both results close to 1?
See how the x and y values are different for rotated vs. rotated+tilted but the y/x ratios work out to the same?
In addition to the microbit module, this script has to import the math module, which contains common trigonometry methods. Then, sleep(1000) gives the terminal a second to set up communication with the micro:bit before it has to display any messages.
# test_accel_xy_angle from microbit import * import math sleep(1000)
In the main loop, the x and y axis accelerometer measurements are stored in variables named x and y.
while True: x = accelerometer.get_x() y = accelerometer.get_y()
The next line performs a calculation called arctangent. If you have never heard of it, the next page explains it.
The method call math.atan2(x, y) is what returns the angle result, but that result is in radians—a different kind of angle measurement where a half circle is approximately 3.14, or Pi (π), radians instead of 180°. To convert radians to degrees in Python, use the math.degrees() method.*
angle = round( math.degrees( math.atan2(y, x) ) )
Another way to think about the line above is to break it into steps, each on an individual line. For example:
radians = math.atan2(y, x)
degrees_fp = math.degrees( radians )
angle = round( degrees_fp )
Now that the angle is calculated, the script displays labels for each variable, followed by its value.
print("x =", x, ", y =", y, ", angle =", angle) sleep(750)
*Alternately, the script could multiply by 180 and divide by math.pi. At this point, there’s a floating point result, and the round function will round to the nearest integer. For example, if the result was 179.5 or higher, it would round to 180. If the value was instead between 179 and 179.499…., it would round to 179 instead.
Trigonometry is used to calculate the rotation angle. Even if you haven’t studied it yet, try to follow along. It's pretty neat!
A triangle with a 90° angle (called a right triangle) can have one of its corners marked θ (Greek letter theta) to represent the angle there. Of the right triangle's three sides, the one is opposite of θ, one adjacent to θ forming the right angle, and the hypotenuse is the side that connects the other two. The diagrams will use the shorthand opp, adj, and hyp for these sides.
In trigonometry, the ratios of each pair of sides have names: sine (sin), cosine (cos), and tangent (tan). For each angle of θ, the ratio for sin, cos, or tan will be the same, regardless of the size of the triangle. Scientific calculators have sin, cos, and tan keys to complete the division calculation for you, and all you have to do is give it the angle. Python’s math library has methods for calculating these as well.
If you only know the result of a division, like opp / adj, you can get the angle θ with the arctan calculation. That’s also called the inverse tangent. Calculators often have tan-1 key for calculating this, and Python’s math module has the atan and atan2 methods.
The arctan calculation is particularly useful for calculating the micro:bit’s rotation angle. All your script needs is the amount of gravity acting on the x and y accelerometer axis. Treat the y-axis measurement as the opposite side and the x-axis measurement as the adjacent side of a right triangle, and the arctan calculation will give you the angle θ.
For any angle you rotate to, the micro:bit accelerometer’s x sensing axis will align with gravity by a certain amount (xg), and the y sensing axis will also align by some amount (yg). Let’s say you are holding the micro:bit vertically and have it rotated by 30°. In that case, yg measures about 512, or 0.5 g. (1 g is 1024). xg will be around 888, which is about 0.87 g.
If you look closely, the x and y sensing axis triangles are identical. So, you could take the xg leg from the x-axis triangle and fit it on the adjacent side of the y-axis triangle.
Like the script, if all you know is the x and y axis measurements (xg and yg), you could now use a calculator to figure out the angle θ by taking the arctan of 512 / 888. The result is 29.96, or rounding up, it’s 30.
Here is an example where the rotation is 45°. Both x and y report 728, and the arctan(728/728) = arctan(1) = 45.
If you were wondering how the script could calculate the angle even when the micro:bit was tilted, it’s because the portions of the effects on each axis were reduced by the same fraction. When holding vertical, the arctan(728/728) resulted in arctan(1) = 45. Likewise, with the tilt away from you, arctan(512/512) = arctan(1) = 45. In other words, the ratio of the x and y measurements are the same, so the result of the arctan calculation is also the same.