Modifications

Sauter à la navigation Sauter à la recherche
4 845 octets ajoutés ,  28 août 2015 à 11:10
Ligne 440 : Ligne 440 :  
* Un accéléromètre (pour détecter le mouvement)
 
* Un accéléromètre (pour détecter le mouvement)
 
* Un magnétomètre (pour détecter les champs magnétiques)
 
* Un magnétomètre (pour détecter les champs magnétiques)
  −
{{traduction}}
      
Avant de commencer vos expériences avec les senseurs de mouvement, il est important de comprendre 3 termes clés couverts par se guide et dans cette [https://www.youtube.com/watch?v=pQ24NtnaLl8 vidéo] (''en anglais'').
 
Avant de commencer vos expériences avec les senseurs de mouvement, il est important de comprendre 3 termes clés couverts par se guide et dans cette [https://www.youtube.com/watch?v=pQ24NtnaLl8 vidéo] (''en anglais'').
Ligne 448 : Ligne 446 :  
* Elévation, dit "Pitch" en anglais (comme quand l'avion décolle)
 
* Elévation, dit "Pitch" en anglais (comme quand l'avion décolle)
 
* Roulis/Roulement, dit "Roll" en anglais (comme un avion qui faire la rouleau de la victoire, ou une voiture qui fait un tonneau)
 
* Roulis/Roulement, dit "Roll" en anglais (comme un avion qui faire la rouleau de la victoire, ou une voiture qui fait un tonneau)
* Embardée, dit "Yaw" en anglais  (imaginez un avion qui dévie de sa route comme le ferait une voiture)
+
* Lacet/Embardée, dit "Yaw" en anglais  (imaginez un avion qui dévie de sa route comme le ferait une voiture, où une voiture qui prend un virage "en lacet")
    
Etant donné que l'interface de programmation du Sense Hat utilise ces mêmes mots clés en anglais pour vous permettre d'accéder aux valeurs, nous allons préserver les termes "Pitch"n "Roll", "Yaw".
 
Etant donné que l'interface de programmation du Sense Hat utilise ces mêmes mots clés en anglais pour vous permettre d'accéder aux valeurs, nous allons préserver les termes "Pitch"n "Roll", "Yaw".
Ligne 457 : Ligne 455 :     
{{traduction}}
 
{{traduction}}
 +
 +
You can find out the orientation of the Sense HAT using the sense.get_orientation() method:
 +
 +
<nowiki>pitch, roll, yaw = sense.get_orientation().values()</nowiki>
 +
 +
This would get the three orientation values (measured in degrees) and store them as the three variables {{fname|pitch}}, {{fname|roll}} et {{fname|yaw}}. The {{fname|.values()}} obtains the three values so that they can be stored separately.
 +
 +
'''1.''' You can explore these values with a simple program:
 +
 +
<nowiki>from sense_hat import SenseHat
 +
 +
sense = SenseHat()
 +
 +
while True:
 +
    pitch, roll, yaw = sense.get_orientation().values()
 +
    print("pitch=%s, roll=%s, yaw=%s" % (pitch,yaw,roll))</nowiki>
 +
 +
'''2.''' Click File -- Save As, give your program a name e.g. {{fname|orientation.py}}, then press F5 to run.
 +
 +
{{tmbox|Note: When using the movement sensors it is important to poll them often in a tight loop. If you poll them too slowly, for example with time.sleep(0.5) in your loop, you will see strange results. This is because the code behind needs lots of measurements in order to successfully combine the data coming from the gyroscope, accelerometer and magnetometer.}}
 +
 +
'''3.''' Another way to detect orientation is to use the {{fname|sense.get_accelerometer_raw()}} method which tells you the amount of g-force acting on each axis. If any axis has ±1g then you know that axis is pointing downwards.
 +
 +
In this example, the amount of gravitational acceleration for each axis (x, y, and z) is extracted and is then rounded to the nearest whole number:
 +
 +
<nowiki>from sense_hat import SenseHat
 +
 +
sense = SenseHat()
 +
 +
while True:
 +
    x, y, z = sense.get_accelerometer_raw().values()
 +
 +
    x=round(x, 0)
 +
    y=round(y, 0)
 +
    z=round(z, 0)
 +
 +
    print("x=%s, y=%s, z=%s" % (x, y, z))</nowiki>
 +
 +
'''4.''' Click File -- Save As, give your program a name e.g. {{fname|acceleration.py}}, then press F5 to run.
 +
 +
As you turn the screen you should see the values for x and y change between -1 and 1. If you place the Pi flat or turn it upside down, the z axis will be 1 and then -1.
 +
 +
'''5.''' If we know which way round the Raspberry Pi is, then we can use that information to set the orientation of the LED matrix. First you would display something on the matrix, then continually check which way round the board is, and use that to update the orientation of the display.
 +
 +
<nowiki>from sense_hat import SenseHat
 +
 +
sense = SenseHat()
 +
 +
sense.show_letter("J")
 +
 +
while True:
 +
    x, y, z = sense.get_accelerometer_raw().values()
 +
 +
    x = round(x, 0)
 +
    y = round(y, 0)
 +
 +
    if x == -1:
 +
        sense.set_rotation(180)
 +
    elif y == 1:
 +
        sense.set_rotation(90)
 +
    elif y == -1:
 +
        sense.set_rotation(270)
 +
    else:
 +
        sense.set_rotation(0)</nowiki>
 +
 +
 +
 +
'''6.''' Click File -- Save As, give your program a name e.g. {{fname|rotating_letter.py}}, then press F5 to run.
 +
 +
In this program you are using an {{fname|if, elif, else}} structure to check which way round the Raspberry Pi is. The {{fname|if}} and {{fname|elif}} test three of the orientations, and if the orientation doesn't match any of these then the program assumes it is the "right" way round. By using the {{fname|else}} statement we also catch all those other situations, like when the board is at 45 degrees or sitting level.
 +
 +
'''7.''' If the board is only rotated, it will only experience 1g of acceleration in any direction; if we were to shake it, the sensor would experience more than 1g. We could then detect that rapid motion and respond. For this program we will introduce the {{fname|abs()}} function which is not specific to the Sense HAT library and is part of standard Python. {{fname|abs()}}} gives us the size of a value and ignores whether the value is positive or negative. This is helpful as we don't care which direction the sensor is being shaken, just that it is shaken.
 +
 +
<nowiki>from sense_hat import SenseHat
 +
 +
sense = SenseHat()
 +
 +
while True:
 +
    x, y, z = sense.get_accelerometer_raw().values()
 +
 +
    x = abs(x)
 +
    y = abs(y)
 +
    z = abs(z)
 +
 +
    if x > 1 or y > 1 or z > 1:
 +
        sense.show_letter("!", text_colour=[255, 0, 0])
 +
    else:
 +
        sense.clear()</nowiki>
 +
 +
 +
 +
'''8.''' Click File -- Save As, give your program a name e.g. {{fname|shake.py}}, then press F5 to run.
 +
 +
You might find this is quite sensitive, but you could change the value from 1 to a higher number.
 +
 +
=== Idées ===
 +
* You could write a program that displays an arrow (or other symbol) on screen; this symbol could be used to point to which way is down. This way the astronauts (in low gravity) always know where the Earth is.
 +
* You could improve the dice program from earlier in this activity, so that shaking the Pi triggers the dice roll.
 +
* You could use the accelerometer to sense small movements; this could form part of a game, alarm system or even an earthquake sensor.
    
== Tout mettre ensemble ==
 
== Tout mettre ensemble ==
29 917

modifications

Menu de navigation