Ligne 266 : |
Ligne 266 : |
| | | |
| == Montage PyBoard == | | == Montage PyBoard == |
− |
| |
− | {{traduction}}
| |
| | | |
| === Une seule source d'alimentation === | | === Une seule source d'alimentation === |
Ligne 292 : |
Ligne 290 : |
| [[Fichier:Hacl-L293D_montage-closeup.jpg|800px]] | | [[Fichier:Hacl-L293D_montage-closeup.jpg|800px]] |
| | | |
− | === Code Arduino === | + | == Classe HBridge == |
− | <nowiki> | + | Voici le contenu de la classe {{fname|HBridge}} qui permet de commander l'un des deux pont-H du L293D avec... en prime... le contrôle de la vitesse en PWM. |
− | int motor1Pin1 = 3; // pin 2 (Input 1) du L293D
| + | |
− | int motor1Pin2 = 4; // pin 7 (Input 2) du L293D
| + | Relativement simple, vous pourrez efficacement commander vos deux moteurs (voyez les différent exemples proposés) |
− | int enablePin = 9; // pin 1 (Enable 1) du L293D
| + | |
| + | <nowiki># Commande d'un pont-H L293D à l'aide de MicroPython PyBoard |
| + | # http://shop.mchobby.be/product.php?id_product=155 |
| + | # Voir Tutoriel |
| + | # http://wiki.mchobby.be/index.php?title=Hack-micropython-L293D |
| + | # |
| + | from pyb import delay, Timer |
| + | |
| + | class HBridge: |
| + | """ Control à H-Bridge. Also support PWM to control the speed (between 0 to 100%) """ |
| + | PWM_FREQ = 100 # Frequency 100 Hz |
| + | (UNDEFINED,HALT,FORWARD,BACKWARD) = (-1, 0,1,2) |
| + | |
| + | def __init__( self, input_pins, pwm = None ): |
| + | """:param input_pins: tuple with input1 and input 2 pins |
| + | :param pwm: dic with pin, timer and channel """ |
| + | self.speed = 0 |
| + | self.state = HBridge.UNDEFINED |
| + | |
| + | # Init HBridge control pins |
| + | self.p_input1 = pyb.Pin( input_pins[0], pyb.Pin.OUT_PP ) |
| + | self.p_input2 = pyb.Pin( input_pins[1], pyb.Pin.OUT_PP ) |
| + | |
| + | # Init PWM Control for speed control (without PWM, the L293D's |
| + | # Enable pin must be place to HIGH level) |
| + | self.has_pwm = (pwm != None) |
| + | if self.has_pwm: |
| + | self._timer = pyb.Timer( pwm['timer'], freq=self.PWM_FREQ ) |
| + | self._channel = self._timer.channel( pwm['channel'], Timer.PWM, pin=pwm['pin'], pulse_width_percent=100 ) |
| + | |
| + | self.halt() |
| + | |
| + | def set_speed( self, speed ): |
| + | if not(0 <= speed <= 100): |
| + | raise ValueError( 'invalid speed' ) |
| + | # Use PWM speed ? |
| + | if self.has_pwm: |
| + | self._channel.pulse_width_percent( speed ) |
| + | self.speed = speed |
| + | else: |
| + | # Non PWM |
| + | self.speed = 0 if speed == 0 else 100 |
| + | if self.speed == 0 and self.state != HBridge.HALT: |
| + | self.halt() # force motor to stop by manipulating input1 & input2 |
| + | |
| + | |
| + | def halt( self ): |
| + | self.p_input1.low() |
| + | self.p_input2.low() |
| + | self.state = HBridge.HALT # Do not invert ... |
| + | self.set_speed( 0 ) # thoses 2 lines |
| + | |
| + | def forward(self, speed = 100 ): |
| + | # reconfigure HBridge |
| + | if self.state != HBridge.FORWARD : |
| + | self.halt() |
| + | self.p_input1.low() |
| + | self.p_input2.high() |
| + | self.state = HBridge.FORWARD |
| + | # Set speed |
| + | self.set_speed( speed ) |
| + | |
| + | def backward(self, speed = 100 ): |
| + | # reconfigure HBridge |
| + | if self.state != HBridge.BACKWARD: |
| + | self.halt() |
| + | self.p_input1.high() |
| + | self.p_input2.low() |
| + | self.state = HBridge.BACKWARD |
| + | # Set speed |
| + | self.set_speed( speed ) |
| + | |
| + | |
| + | # Pont-H broches de commande Input 1 et Input 2 |
| + | MOT1_PINS = ( pyb.Pin.board.X6, pyb.Pin.board.X5 ) |
| + | # Commande PWM pont-H |
| + | MOT1_PWM = {'pin' : pyb.Pin.board.X3, 'timer' : 2, 'channel' : 3 } |
| | | |
− | void setup() {
| + | # Pont-H broches de commande Input 3 et Input 4 |
− | // set all the other pins you're using as outputs:
| + | MOT2_PINS = ( pyb.Pin.board.X7, pyb.Pin.board.X8 ) |
− | pinMode(motor1Pin1, OUTPUT);
| + | # Commande PWM pont-H |
− | pinMode(motor1Pin2, OUTPUT);
| + | MOT2_PWM = {'pin' : pyb.Pin.board.X4, 'timer' : 5, 'channel' : 4 } |
− | pinMode(enablePin, OUTPUT);
| |
| | | |
− | // Mettre la broche Enable a high comme ca le moteur tourne
| + | def test_bridge1_simple(): |
− | digitalWrite(enablePin, HIGH);
| + | """ Simply test the HBridge without speed control """ |
− | }
| + | h1 = HBridge( MOT1_PINS ) |
| + | print( "Forward" ) |
| + | h1.forward() |
| + | delay( 3000 ) |
| + | print( "Halt" ) |
| + | h1.halt() |
| + | delay( 3000 ) |
| + | print( "Backward" ) |
| + | h1.backward() |
| + | delay( 3000 ) |
| + | print( "Halt" ) |
| + | h1.halt() |
| + | print( "end." ) |
| | | |
− | void loop() {
| + | def test_bridge1_speed(): |
− | // Le moteur tourne dans un sens
| + | """ Test the HBridge without speed control """ |
− | digitalWrite(motor1Pin1, LOW); // mettre pin 2 a 293D low
| + | h1 = HBridge( MOT1_PINS, MOT1_PWM ) |
− | digitalWrite(motor1Pin2, HIGH); // mettre pin 7 a L293D high
| + | print( "Forward full speed" ) |
− |
| + | h1.forward() |
− | delay( 3000 ); // Attendre 3 secondes
| + | delay( 3000 ) |
| + | print( "Forward half speed 50%" ) |
| + | h1.forward( 50 ) |
| + | delay( 3000 ) |
| + | print( "Forward 30% speed" ) |
| + | h1.forward( 30 ) |
| + | delay( 3000 ) |
| + | for the_speed in range( 10, 70, 10 ): |
| + | print( "Backward at speed %i" % (100-the_speed) ) |
| + | h1.backward( 100-the_speed ) |
| + | delay( 1500 ) |
| + | h1.halt() |
| | | |
− | // Le moteur tourne dans l'autre sens
| + | def test_two_bridges(): |
− | digitalWrite(motor1Pin1, HIGH); // Mettre pin 2 a L293D high
| + | """ Test the two H-Bridges of the L293D """ |
− | digitalWrite(motor1Pin2, LOW); // Mettre pin 7 a L293D low
| + | h1 = HBridge( MOT1_PINS, MOT1_PWM ) |
− |
| + | h2 = HBridge( MOT2_PINS, MOT2_PWM ) |
− | delay( 3000 ); // Attendre 3 secondes
| + | # move the 2 motors forwards |
− | }
| + | h1.forward( 70 ) |
− | </nowiki>
| + | h2.forward( 70 ) |
| + | delay( 3000 ) |
| + | # Stop the 2 motors |
| + | h1.halt() |
| + | h2.halt() |
| | | |
− | === Deux sources d'alimentation === | + | def test_bridge2_simple(): |
− | Il est possible d'alimenter le moteur avec sa propre source de tension.
| + | """ Test the two H-Bridges of the L293D """ |
| + | h2 = HBridge( MOT2_PINS )#, MOT2_PWM ) |
| + | print( "Forward full speed" ) |
| + | h2.forward() |
| + | delay( 3000 ) |
| + | print( "Halt" ) |
| + | h2.halt() |
| | | |
− | C'est le cas, par exemple, des moteurs en 9 ou 12 Volts.
| |
| | | |
− | Comme l'alimentation de la logique de commande reste en 5 Volts, nous sommes face à un cas de double source d'alimentation.
| + | test_bridge1_simple() |
| + | # test_bridge2_simple() |
| + | # test_bridge1_speed() |
| + | # test_two_bridges()</nowiki> |
| | | |
− | Règles de raccordement:
| + | === Deux sources d'alimentation === |
− | * Les masses (GND) des sources d'alimentation doivent être raccordées ensembles.
| + | Il est possible d'alimenter le moteur avec sa propre source de tension séparée de la source d'alimentation de la logique de contrôle (PyBoard et logique de contrôle du L293D). |
− | * La tension moteur (9v) est raccordée sur la broche '''VS''' (broche 8).
| |
− | * La tension de la logique (5v) est raccordée sur la broche '''VSS''' (broche 16).
| |
| | | |
− | Notes:
| + | <small>...''nous augmenterons cette partie du tutoriel dès que possible'''...</small> |
− | * Faire bien attention de ne pas intervertir les broches VS et VSS par erreur.
| |
− | * Le programme Arduino présenté ci-dessus ne change pas.
| |
− |
| |
− | [[Fichier:L293D Montage_12v(LowRes,closer).jpg]]
| |
| | | |
| == Ou Acheter == | | == Ou Acheter == |