Modifications

Sauter à la navigation Sauter à la recherche
4 071 octets ajoutés ,  19 février 2022 à 21:27
Ligne 95 : Ligne 95 :  
It can be controled with few lines of codes, just key-in them directly in the REPL session
 
It can be controled with few lines of codes, just key-in them directly in the REPL session
   −
<nowiki>from machine import Pin
+
<syntaxhighlight lang="python">
 +
from machine import Pin
 
led = Pin(25, Pin.OUT)
 
led = Pin(25, Pin.OUT)
 
led.value(1) # allume la LED
 
led.value(1) # allume la LED
led.value(0) # éteint la LED</nowiki>
+
led.value(0) # éteint la LED
 +
</syntaxhighlight>
    
The following screen capture just show the results of entering the command lines.  
 
The following screen capture just show the results of entering the command lines.  
Ligne 106 : Ligne 108 :  
by entering {{fname|led.value()}} without parameter, you can query the current pin state (1 for High level, 0 for Low level).
 
by entering {{fname|led.value()}} without parameter, you can query the current pin state (1 for High level, 0 for Low level).
   −
=== Run a file ===
+
=== Run & Stop a file ===
{{traduction}}
+
With Thonny you can also write Python code inside a file and execute it at once on the target MicroPython board.
   −
Thonny permet également d'écrire du code Python dans un fichier et de l'exécuter en une seule fois sur la plateforme MicroPython cible.
+
In the following example, the status LED (GP25) is pulsed by controling the output pin with PWM signal.
   −
Dans l'exemple suivant la luminosité de la LED de statut est contrôlée à l'aide d'un signal PWM (valeur entre 0 et 65534 pour un cycle utile de 0 à 100% .
+
PWM stand for ''Pulse Width Modulation'', see this [https://www.electronicwings.com/pic/pic18f4550-pwm animation on ElectronicWings clearly show a PWM signal with its duty cycle and results on a LED].
   −
C'est le cas de code suivant:
+
Under MicroPython, the duty cycle is set with {{fname|duty_16( value )}} where value is a number from 0 to 65534 (for duty cycle from 0 to 100% .
 +
   −
<nowiki>import time
+
Key-in the following code into the main's Thonny section (the code editor):
 +
 
 +
<syntaxhighlight lang="python">
 +
import time
 
led = PWM( Pin(25 ))
 
led = PWM( Pin(25 ))
 
counter = 0
 
counter = 0
Ligne 126 : Ligne 132 :  
     for i in range( 0, 100, 5 ):
 
     for i in range( 0, 100, 5 ):
 
         led.duty_u16( int(65534*(100-i)/100) )
 
         led.duty_u16( int(65534*(100-i)/100) )
         time.sleep_ms( 50 )</nowiki>
+
         time.sleep_ms( 50 )
 +
</syntaxhighlight>
   −
Que l'on sauve dans un fichier sous le nom "demo.py" soit sur l'ordinateur, soit sur la carte microcontrôleur.
+
Then save the file under the name "demo.py" either on the computer, either on the microcontroler board.
   −
Peu importe l'emplacement du fichier (ordinateur ou microcontrôleur), il sera exécuté sur le microcontrôleur dans une session REPL.
+
No matter where it is saved (computer or microcontroler), it can be executed against the microcontroler and shows the results into the REPL/shell session.
   −
Pour exécuter le script il faut presser:
+
To execute the script, you can either:
* le bouton "Start" [[Fichier:ENG-CANSAT-PICO-THONNY-05.png]]
+
* Press the "Start" button [[Fichier:ENG-CANSAT-PICO-THONNY-05.png]] of the toolbar
* le menu "Run | Run current script" ou
+
* Select the menu "'''Run | Run current script'''"
* presser la touche "F5"  
+
* Press the "F5" key
    
[[Fichier:ENG-CANSAT-PICO-THONNY-06.png|480px]]
 
[[Fichier:ENG-CANSAT-PICO-THONNY-06.png|480px]]
   −
L'exécution du script affiche également les messages print() dans la section Shell en plus de faire pulser la LED de statut sur la carte.
+
The script execution will displays the {{fname|print()}} messages into the Shell section of Thonny.
 +
 
 +
As you may guess, the onboard status LED is also pulsing.
   −
Le script peut être arrêté:
+
To stop the script (since it run infinitly),  you can either:
* En pressant le bouton "Stop"
+
* press the "Stop" button on the toolbar
* En pressant la combinaison de touche "Ctrl+C"  
+
* Press the "Ctrl+C" combination into the shell section
   −
=== Use the plotter ==
+
=== Use the plotter ===
Le plotter peut être activé par l'intermédiaire du menu "View | Plotter".
+
The plotter is a graphical tool displaying script outputs generated with {{fname|print()}} as graphics.
   −
Cette action affiche un outil graphique dans la section shell.
+
The graphics is updated each time a new information appears in the output.
 +
 
 +
The Plotter can be activated with the "'''View |  Plotter'''" menu.
 +
 
 +
That action displays a graphical tool aside the shell section.
    
[[Fichier:ENG-CANSAT-PICO-THONNY-10.png|480px]]
 
[[Fichier:ENG-CANSAT-PICO-THONNY-10.png|480px]]
   −
Le Plotter reprend les données numériques affichée dans session REPL. Lorsque la ligne contient exclusivement une valeur numérique, celle-ci est reprise dans le plotter.
+
The Plotter catch the numeric values sends/print in the REPL/Shell and displays it in the chart.  
   −
Dans l'exemple suivant, nommé demo2.py, utilise une fonction gamma pour faire pulser la LED de statut sur le GPIO. C'est qu'une progression proportionnel du signal PWM ne produit pas une évolution proportionnelle de la luminosité de la LED (voir livre [https://www.editions-eni.fr/livre/micropython-et-pyboard-python-sur-microcontroleur-de-la-prise-en-main-a-l-utilisation-avancee-9782409022906 MicroPython et Pyboard, Edition ENI], in French).
+
Only lines containing numeric values are used by the plotter.
   −
La correction gamma permet de corriger cet inconvénient de non proportionnalité.
+
On the following example, named {{fname|demo2.py}}, we do use the gamma function to make the LED pulsing in a more natural way. Indeed, a progressive PWM signal increase would not offers a proportional luminosity increase on the LED (voir livre [https://www.editions-eni.fr/livre/micropython-et-pyboard-python-sur-microcontroleur-de-la-prise-en-main-a-l-utilisation-avancee-9782409022906 MicroPython et Pyboard, Edition ENI], in French).
   −
<nowiki>from machine import Pin, PWM
+
We do use the {{fname|gamma()}} function to rectify the apparent proportionality between the PWM and emitted light.
 +
 
 +
<syntaxhighlight lang="python">
 +
from machine import Pin, PWM
 
import time
 
import time
 
led = PWM( Pin(25 ))
 
led = PWM( Pin(25 ))
Ligne 176 : Ligne 192 :  
         print( pwm_val )
 
         print( pwm_val )
 
         time.sleep_ms( 20 )</nowiki>
 
         time.sleep_ms( 20 )</nowiki>
 +
</syntaxhighlight>
 +
 +
Which produce the following results in Thonny and activated Plotter.
 +
 +
[[Fichier:ENG-CANSAT-PICO-THONNY-11.png|480px]]
 +
 +
'''{{underline|Drawing several curves}}'''
 +
 +
By modifying the script, we can return the PWM value and the gamma corrected value by using a [https://www.w3schools.com/python/python_tuples.asp Python tuple]. Then the plotter will draw several curves.
 +
 +
<syntaxhighlight lang="python">
 +
from machine import Pin, PWM
 +
import time
 +
led = PWM( Pin(25 ))
 +
 +
def gamma( pc ):
 +
    return pow(pc/100,2.2)*100
 +
 +
while True:
 +
    for i in range( 0, 100, 5 ):
 +
        pwm      = int( i*65534/100 )
 +
        pwm_gamma = int(gamma(i)*65534/100)
 +
        led.duty_u16( pwm_gamma )
 +
        print( (pwm, pwm_gamma) )
 +
        time.sleep_ms( 20 )
 +
    for i in range( 0, 100, 5 ):
 +
        pwm      = int((100-i)*65534/100)
 +
        pwm_gamma = int(gamma(100-i)*65534/100)
 +
        led.duty_u16( pwm_gamma )
 +
        print( (pwm, pwm_gamma) )
 +
        time.sleep_ms( 20 )
 +
</syntaxhighlight>
 +
 +
Which produce the following results in Thonny.
 +
 +
[[Fichier:ENG-CANSAT-PICO-THONNY-12.png|480px]]
 +
 +
As seen in the REPL/Shell output, the data are return under the format (pwm, pwm_gamma) . The first value is the proportional curve and the second value the gamma fixed value.
 +
 +
The plotter does show the curve correspondence between tuple value position and drawed curve (see to bottom right corner).
 +
 +
== System Shell - Pure REPL ==
 +
It is also possible to start a Pure REPL session inside an external terminal. By using a external terminal, the user escape from the Thonny shell panel and being directly connected to the microcontroler via an USB serial connection.
 +
 +
The "'''Tools | Open System Shell..'''" menu will open the terminal Window.
 +
 +
[[Fichier:ENG-CANSAT-PICO-THONNY-20.png|640px]]
 +
 +
{{underline|Notes:}}
 +
* Thonny must be connected to the target plateform to be able to open the External Terminal.
 +
* The file manager can helps to check, whether or not, when Thonny is connected to the MicroPython board.
 +
* It is not possible to open two terminal session (at the same time) to the MicroPython microcontroler.
 +
 +
== File manager ==
 +
 +
It is common to copy library/libraries on the board.
 +
 +
This will requires to '''transfer one (or several) files to the MicroPython board'''.
 +
 +
Such '''libraries''' are usually python scripts (eg: ce [https://raw.githubusercontent.com/mchobby/esp8266-upy/master/bme280-bmp280/bme280.py bme280.py file]) coded for target sensors (eg: [https://github.com/mchobby/esp8266-upy/tree/master/bme280-bmp280 BME280 atmospheric & temperature I2C sensor]).
 +
 +
Thank to Thonny you also have a dedicated "file manager" tool used to transfert files between the computer and the MicroPython board.
 +
 +
The file manager is available via the "'''View | Files'''" menu.
 +
 +
[[Fichier:ENG-CANSAT-PICO-THONNY-30.png|480px]]
 +
 +
Which would display the Thonny file manager.
 +
 +
[[Fichier:ENG-CANSAT-PICO-THONNY-31.png|480px]]
 +
 +
To transfer a file from the computer to the MicroPython board, you will need to :
 +
* Select the file into the "Computer" pane
 +
* Activate the contextual menu (mouse right click)
 +
* Select the option "Upload to" in the menu
 +
 +
{{ambox|text=The "/" drawed into the menu match the root folder on the target (the microcontroler in this case).}}
 +
 +
[[Fichier:ENG-CANSAT-PICO-THONNY-32.png|480px]]
   −
Ce qui produit le résultat suivant dans Thonny. La valeur numérique produite dans la session REPL est également reproduite dans le Plotter.
+
It would be easy to identify the features and behavior of the file manager by exploring and testing the various entries in the contextual menu. Please note that contectual menu are different in the board pane and computer pane.
    
{{ENG-CANSAT-PICO-TRAILER}}
 
{{ENG-CANSAT-PICO-TRAILER}}
29 917

modifications

Menu de navigation