树莓派控制步进电机-TB6600-Java版本( 三 )


setPwm(int value) 硬pwm , setPwm从0-1024 , 软针脚默认为从0-100;
我们使用 软pwm , 两个值都为100.
三.程序package top.fairy.global.globalfairytoppi4j.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 fromRaspiPin.GPIO_29,// default pin if no pin argument foundargs);GpioPinPwmOutput pwm = gpio.provisionSoftPwmOutputPin(enableAWheel);pwm.setPwmRange(3000);pwm.setPwm(2000);try {Thread.sleep(50000);//睡眠2秒} catch (InterruptedException e) {e.printStackTrace();}pwm.setPwmRange(1000);try {Thread.sleep(5000);//睡眠2秒} catch (InterruptedException e) {e.printStackTrace();}pwm.setPwmRange(0);try {Thread.sleep(5000);//睡眠2秒} catch (InterruptedException e) {e.printStackTrace();}pwm.setPwmRange(3000);try {Thread.sleep(5000);//睡眠2秒} catch (InterruptedException e) {e.printStackTrace();}pwm.setPwmRange(3000);try {Thread.sleep(2000000);//持续时间久一点} catch (InterruptedException e) {e.printStackTrace();}}catch(Exception e){e.printStackTrace();}}}
树莓派控制步进电机-TB6600-Java版本文章插图
注意事项:
TB6600供电电压在DC.9~42VDC 。 我直接使用4块干电池 , 电压不足9v , 才5v , 其他连线的电压都正确 , 这种情况下 , 需要更换个大电压的供电器才行 。
另外:千万别把连接线的导线头露在外面 , 一不小心短接 , 树莓派就报废了 。