HomeTurtlegraficsGPanelRobotics WebTigerPython |
Python - Online |
Deutsch English |
YOU LEARN HERE... |
how you can use the acceleration sensor to detect changes in position and movements of the Calliope. |
SENSOR VALUES |
The position sensor (acceleration sensor) measures both the constant gravitational acceleration of around 10 m/s2, which points vertically downwards, and the accelerations caused by movements.
It is similar to the attitude display of aeroplanes, which are displayed with an artificial horizon. If you tilt the board forwards or backwards, the pitch angle changes; if you tilt it sideways, the roll angle changes. In the following programme you use the functions accelerometer.get_x(), accelerometer.get_y() or accelerometer.get_z(), which return values in the range from approximately -2000 to 2000 (corresponds to the accelerations -20 m/s2 tos 20 m/s2). With acceloremeter.get_values() you get back all three values in one tuple. If the z-axis is pointing vertically downwards, the value of the acceleration due to gravity, i.e. 9.81 m/s2must be measured. The sensor returns approximately 981. Program: from calliope_mini import * while True: ax = accelerometer.get_x() ay = accelerometer.get_y() az = accelerometer.get_z() print(ax, ay, az) sleep(1000) |
EXAMPLES |
Program: from calliope_mini import * while True: acc = accelerometer.get_x() print(acc) if acc > 0: display.show(Image.ARROW_E) else: display.show(Image.ARROW_W) sleep(100)
Only one pixel is displayed, which shifts to the right, left, up or down depending on the tilt. The aim is to align the level so that the centre LED lights up. In the programme, you use four conditions to determine the x and y values of the pixel and then delete the pixel before lighting the new one. Program: from calliope_mini import * x = 1 y = 1 while True: accX = accelerometer.get_x() accY = accelerometer.get_y() if accX > 100 and y < 4: y += 1 elif accX < -100 and y > 0: y -= 1 elif accY > 100 and x < 4: x += 1 elif accY < -100 and x > 0: x -= 1 display.clear() display.set_pixel(x, y, 9) sleep(100)
|
REMEMBER ... |
You can use the acceleration sensor to record the movements and position of the Calliope. The sensor values are regularly queried every 100 milliseconds in an ‘endless’ while true loop. This is also known as polling the sensor values. |
TO SOLVE BY YOURSELF |
|