透過pwm方式控制電機-L298N-Java版本

我們知道樹莓派提供一個硬體pwm引腳,可以透過脈衝寬度調製進行控制電機的速度。實際上在精度要求不是非常高的情況下,普通引腳也可以透過軟體模擬來控制電機,實現pwm一樣的功能。

一。準備

樹莓派4b

電機驅動L298N

直流電機

電源盒(4節1。5v電池)

電源線若干

二。電路連線示意圖

使用方式如下:

透過pwm方式控制電機-L298N-Java版本

https://shumeipai。nxez。com/raspberry-pi-pins-version-40

L298N示意圖

透過pwm方式控制電機-L298N-Java版本

詳細的針腳

透過pwm方式控制電機-L298N-Java版本

透過pwm方式控制電機-L298N-Java版本

其中,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這邊更近的那根針是需要外接的,是低電平。

https://www。zhihu。com/question/50074435/answer/140589620

三。程式

原理: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: http://www。pi4j。com/ * ********************************************************************** * %% * 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; /** *

* This example code demonstrates how to setup a software emulated PWM pin using the RaspberryPi GPIO pins。 *

* * @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: http://wiringpi。com/reference/software-pwm-library/ // 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(“”); } } }

https://blog。csdn。net/qq_38605382/article/details/85111726

TAG: PINPWMGPIO