龙空技术网

通过pwm方式控制电机-L298N-Java版本

物联网树莓派 155

前言:

如今朋友们对“l298n引脚图和说明”大约比较讲究,看官们都想要知道一些“l298n引脚图和说明”的相关内容。那么小编同时在网络上汇集了一些关于“l298n引脚图和说明””的相关知识,希望你们能喜欢,咱们一起来了解一下吧!

我们知道树莓派提供一个硬件pwm引脚,可以通过脉冲宽度调制进行控制电机的速度。实际上在精度要求不是非常高的情况下,普通引脚也可以通过软件模拟来控制电机,实现pwm一样的功能。

一.准备

树莓派4b

电机驱动L298N

直流电机

电源盒(4节1.5v电池)

电源线若干

二.电路连接示意图

使用方式如下:

L298N示意图

详细的针脚

其中,IN1,IN2控制电机A,IN3,IN4控制电机B

L298N12符输入,接电源盒正极;L298N的GND接电源盒的负极,同时接树莓派GND,以实现共地。

电路连接设计思路:使用gpio 1控制IN1;GUIO 02控制IN2;GPIO29控制ENA(A通道使能;通过使能接口接成pwm模式,控制速度)。

关于跳线帽:

关于ENA和ENB键帽,拆下后ENA和ENB分别有两根线,与IN1-4平行的是ENA和ENB使能端,剩下的是5V电源针脚。由于必须在ENA或ENB处于高电平时,才能使相应的电机运转,所以通过键帽把它们默认接到5V电源上,使之默认为高电平。

故若只需控制电机的正反转,可以不用键帽,只关心IN1-4即可。若要对电机进行调速,则需拆下键帽,对ENA和ENB使能端输入PWM脉冲,而剩下的5V电源针脚空闲即可。

不知道你买的是什么型号的L298N。有些型号标记了EA、EB和5V,就算没标记也没关系,自己实际试一下就知道了。

实际上,可以用万用表测试一下两个针脚的电压,如果红色的(万用表的正极)连接的一端显示出来的数字是正数,那么这端就是高电压(实际测试下来是2v,不到5v).所以在将低电压的那一段外接到树莓派即可。

测试下来,我买的红板,是靠近字符 ENA这边更近的那根针是需要外接的,是低电平。

三.程序

原理:GPIO使用GPIO模式进行工作,通过GPIO接口,设置脉冲幅度数值(默认值是100,通过调整数值来控制电流的强弱,从而控制电机的运动速度)。

官网的例子

/* * #%L * ********************************************************************** * ORGANIZATION  :  Pi4J * PROJECT       :  Pi4J :: Java Examples * FILENAME      :  SoftPwmExample.java * * This file is part of the Pi4J project. More information about * this project can be found here:   * ********************************************************************** * %% * Copyright (C) 2012 - 2019 Pi4J * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Lesser Public License for more details. * * You should have received a copy of the GNU General Lesser Public * License along with this program.  If not, see * <;. * #L% */ import com.pi4j.io.gpio.*;import com.pi4j.platform.PlatformAlreadyAssignedException;import com.pi4j.util.CommandArgumentParser;import com.pi4j.util.Console; /** * <p> * This example code demonstrates how to setup a software emulated PWM pin using the RaspberryPi GPIO pins. * </p> * * @author Robert Savage */public class SoftPwmExample {     /**     * [ARGUMENT/OPTION "--pin (#)" | "-p (#)" ]     * This example program accepts an optional argument for specifying the GPIO pin (by number)     * to use with this GPIO listener example. If no argument is provided, then GPIO #1 will be used.     * -- EXAMPLE: "--pin 4" or "-p 0".     *     * @param args     * @throws InterruptedException     * @throws PlatformAlreadyAssignedException     */    public static void main(String[] args) throws InterruptedException, PlatformAlreadyAssignedException {         // create Pi4J console wrapper/helper        // (This is a utility class to abstract some of the boilerplate code)        final Console console = new Console();         // print program title/header        console.title("<-- The Pi4J Project -->", "SoftPWM Example (Software-driven PWM Emulation)");         // allow for user to exit program using CTRL-C        console.promptForExit();         // create gpio controller        final GpioController gpio = GpioFactory.getInstance();         // by default we will use gpio pin #01; however, if an argument        // has been provided, then lookup the pin by address        Pin pin = CommandArgumentParser.getPin(                RaspiPin.class,    // pin provider class to obtain pin instance from                RaspiPin.GPIO_01,  // default pin if no pin argument found                args);             // argument array to search in         // we will provision the pin as a software emulated PWM output        // pins that support hardware PWM should be provisioned as normal PWM outputs        // each software emulated PWM pin does consume additional overhead in        // terms of CPU usage.        //        // Software emulated PWM pins support a range between 0 (off) and 100 (max) by default.        //        // Please see:         // for more details on software emulated PWM        GpioPinPwmOutput pwm = gpio.provisionSoftPwmOutputPin(pin);         // optionally set the PWM range (100 is default range)        pwm.setPwmRange(100);         // prompt user that we are ready        console.println(" ... Successfully provisioned PWM pin: " + pwm.toString());        console.emptyLine();         // set the PWM rate to 100 (FULLY ON)        pwm.setPwm(100);        console.println("Software emulated PWM rate is: " + pwm.getPwm());         console.println("Press ENTER to set the PWM to a rate of 50");        System.console().readLine();         // set the PWM rate to 50 (1/2 DUTY CYCLE)        pwm.setPwm(50);        console.println("Software emulated PWM rate is: " + pwm.getPwm());         console.println("Press ENTER to set the PWM to a rate to 0 (stop PWM)");        System.console().readLine();         // set the PWM rate to 0 (FULLY OFF)        pwm.setPwm(0);        console.println("Software emulated PWM rate is: " + pwm.getPwm());         // stop all GPIO activity/threads by shutting down the GPIO controller        // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)        gpio.shutdown();    }}

其中console是java控制台,打日志用的,方便调试。

// by default we will use gpio pin #01; however, if an argument        // has been provided, then lookup the pin by address        Pin pin = CommandArgumentParser.getPin(                RaspiPin.class,    // pin provider class to obtain pin instance from                RaspiPin.GPIO_01,  // default pin if no pin argument found                args);             // argument array to search in

这里的含义是,如果输入控制引脚,则使用输入的引脚,如果不输入,就使用默认的GPIO_01引脚

具体程序,可以根据例子,进行设置针脚的高低电平实现

package top.fairy.global.pi.led.utils; import com.pi4j.io.gpio.*;import com.pi4j.util.CommandArgumentParser; /** * 设置轮子的速度 * */public class PwmUtil {//    public boolean setSpeed(Pin enableAWheel, int speed){//        boolean result = true;//        try{//            GpioController gpio = GpioUtil.getGpioController();////            GpioPinPwmOutput pwm = gpio.provisionSoftPwmOutputPin(enableAWheel);//            pwm.setPwmRange(100);//默认值//            pwm.setPwm(speed);//        }catch (Exception e){//            result = false;//        }//        return result;//    }     public static void main(String[] args){        try{            GpioController gpio = GpioFactory.getInstance();             //启动            GpioPinDigitalOutput wheelAOut1 = gpio.provisionDigitalOutputPin(                    RaspiPin.GPIO_00, "wheelAOut1", PinState.HIGH);            GpioPinDigitalOutput wheelAOut2 = gpio.provisionDigitalOutputPin(                    RaspiPin.GPIO_01, "wheelAOut2", PinState.LOW);             Pin enableAWheel = CommandArgumentParser.getPin(//A的使能控制                    RaspiPin.class,    // pin provider class to obtain pin instance from                    RaspiPin.GPIO_29,  // default pin if no pin argument found                    args);             GpioPinPwmOutput pwm = gpio.provisionSoftPwmOutputPin(enableAWheel);            pwm.setPwmRange(100);            pwm.setPwm(100);             try {                Thread.sleep(2000);//睡眠2秒            } catch (InterruptedException e) {                e.printStackTrace();            }            pwm.setPwmRange(50);            try {                Thread.sleep(2000);//睡眠2秒            } catch (InterruptedException e) {                e.printStackTrace();            }             pwm.setPwmRange(0);            try {                Thread.sleep(2000);//睡眠2秒            } catch (InterruptedException e) {                e.printStackTrace();            }             pwm.setPwmRange(30);            try {                Thread.sleep(5000);//睡眠2秒            } catch (InterruptedException e) {                e.printStackTrace();            }            pwm.setPwmRange(50);            try {                Thread.sleep(2000000);//持续时间久一点            } catch (InterruptedException e) {                e.printStackTrace();            }        }catch(Exception e){            System.out.println("");        }     } }

标签: #l298n引脚图和说明 #l298n的ena两个引脚怎么接 #l298n的ena和enb #l298n引脚图 #java控制电路