Did you remember that the micro:bit module’s accelerometer also has a z-axis?
Like the hyp calculation from the previous activity, the accelerometer’s z sensing axis can give your script information about the overall level of tilt.
The main advantage of using the z-axis is that it can also tell your script whether the accelerometer is facing up or down. In fact, the hyp calculation is normally only used with two-axis (x-y) accelerometers. Since the micro:bit has a three-axis (x-y-z) accelerometer, scripts can use a combination of atan2(y, x) and z-axis measurements and calculations to get both rotation and tilt level.
In other words, the script can know exactly how the micro:bit is oriented in space.
# test-z-axis-tilt from microbit import * sleep(1000) while True: z = accelerometer.get_z() print("z =", z) sleep(750)
The accelerometer’s z sensing axis aligns with gravity when the micro:bit is held level with its LED display facing down. In that position, accelerometer.get_z() returns a measurement in the -1024 neighborhood.
Held vertically, it returns zero since the z-axis is perpendicular to gravit y— it doesn’t line up at all. If held vertically, rotation angle won’t matter to the z-axis, it still returns zero. Held with the LED display facing up, the z-axis is aligned with gravity again but pointing the opposite direction. Because of that, the accelerometer.get_z() function returns a value near -1024.
As for how the script test-z-axis-tilt works, it is basically the z-axis version of test_tilt_intro [1]. The main difference is that:
x = accelerometer.get_x() y = accelerometer.get_y() print("x =", x, ", y =", y)
…has been replaced with
z = accelerometer.get_z() print("z =", z)
Without the z-axis, the x and y tilt with the atan2 calculation from Measure Rotation Angles [2]can give your script information about how the micro:bit has been rotated. By adding the hyp calculation from the previous activity your script can also have information about the degree of tilt, but that information is incomplete. The reason being, it cannot tell if you are facing the LEDs up or down.
The hyp calculation is unable to tell up from down because sqrt(x**2 + y**2) always gives a positive result. That’s because any time you multiply a number by itself, the result is always positive regardless of the number’s sign. For example:
32 = 3 x 3 = 9 -32 = -3 x -3 = 9
For tilt sensing, the accelerometer’s z-axis can complete the picture because the sign is negative for half the range with the LEDs facing up (-1024 to -1) and positive for the other half with the LEDs facing down (-1 to -1024). And of course, it’s zero if it’s neither facing up nor down.
Your script can even use the accelerometer’s z sensing axis to detect how far from level you are holding the micro:bit as you rotate it. For example, if you hold the micro:bit vertical while you rotate it, the z-axis will report zero the whole time. That’s because the z-axis will remain perpendicular to gravity. Some combination of the x- and y-axes will align with gravity and report values, but the z value will stay at zero.
Let’s say that you have tilted the micro:bit as you’ve rotated it. As you saw, a script can use atan2(y, x) to determine the rotation angle, but how far have you tilted it from level? Think of the z-axis measurement as an adjacent leg that aligns with gravity in a triangle. The angle θ between the adjacent leg and the 1 g (1024) hypotenuse can be determined by taking the arccos( zg / 1024).
Keep in mind that the z-axis measurement only tells you how far from level the micro:bit is tilted. For example, these two tilts all result in the same z-axis measurement. So, it doesn’t mean that much without more info from the x and y measurements.
The previous page Did You Know? This Way Up [4] explained how to use arccos to calculate the angle between the micro:bit’s z sensing axis and gravity.
When the LED display is facing straight down, so is the z sensing axis. So the angle between z and gravity is zero.
As the angle between the z-axis and gravity increases, so does the measurement. When the LED display (and z-axis) is facing straight up, it’s angle is 180°. Again, rotating past 180°, the angle decreases, like the last example where it’s back down to 135° again.
# z_axis_degree_of_tilt_try_this from microbit import * import math sleep(1000) while True: z = accelerometer.get_z() if z > 1024: z = 1024 elif z < -1024: z = -1024 z_angle = round( math.degrees( math.acos( z/1024) ) ) print("z =", z, ", z_angle =", z_angle) print() sleep(750)
z | z_angle | Direction LEDs are facing |
---|---|---|
1024 | 0 | Straight down |
728 | 45 | Half way between straight down and sideways |
Sideways | ||
-728 | 135 | Half way between sideways and straight up |
-1024 | 180 | Straight up |
Half way past straight up on way to sideways | ||
Tilted toward you by 45° | ||
Tilted away from you by 45° | ||
GND corner tilted down by 45° | ||
0 corner tilted down by 45° |
You can combine the xy calculated angle from Measuring Rotation Angles [5] with the z angle introduced here to get a complete picture of how the micro:bit is oriented in space.
Example script: z_axis_and_xy_axis_angles_your_turn
# z_axis_and_xy_axis_angles_your_turn from microbit import * import math sleep(1000) while True: x = accelerometer.get_x() y = accelerometer.get_y() z = accelerometer.get_z() xy_angle = round( math.degrees( math.atan2(y, x) ) ) if z > 1024: z = 1024 elif z < -1024: z = -1024 z_angle = round( math.degrees( math.acos( z/1024) ) ) print("z_angle =", z_angle) print("xy_angle =", xy_angle) print() sleep(750)
Links
[1] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-tilt-control/measure-accelerometer-tilt/test-tilts
[2] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-tilt-control/measure-tilt-angles/measure-rotation
[3] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-tilt-control/measure-tilt-angles/how-measuring-rotation
[4] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-tilt-control/z-axis-which-way/did-you-know-way
[5] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-tilt-control/measure-rotation-angles/measuring-rotation