Class MotorHelper
java.lang.Object
com.opensourcewithslu.outputdevices.MotorHelper
Helper class to control a DC motor using PWM (Pulse Width Modulation).
This class provides methods to enable, disable, and set the speed of a motor.
-
Constructor Summary
ConstructorsConstructorDescriptionMotorHelper(com.pi4j.io.pwm.Pwm motor, com.pi4j.io.gpio.digital.DigitalOutput pin1, com.pi4j.io.gpio.digital.DigitalOutput pin2) Constructs a new MotorHelper. -
Method Summary
Modifier and TypeMethodDescriptionvoiddisable()Disables the motor, effectively stopping any ongoing PWM signal.voidenable()Enables the DC motor by setting an initial duty cycle and frequency.voidsetClockwise(boolean clockwise) Sets the direction of the DC motor.voidsetSpeed(double speed) Sets the speed of the DC motor.voidSwitches the direction of the motor.
-
Constructor Details
-
MotorHelper
public MotorHelper(com.pi4j.io.pwm.Pwm motor, com.pi4j.io.gpio.digital.DigitalOutput pin1, com.pi4j.io.gpio.digital.DigitalOutput pin2) Constructs a new MotorHelper.- Parameters:
motor- A PWM interface to control the motor.pin1- A DigitalOutput interface for the first GPIO pin.pin2- A DigitalOutput interface for the second GPIO pin.
-
-
Method Details
-
enable
public void enable()Enables the DC motor by setting an initial duty cycle and frequency. The motor remains disabled until this method is called. -
disable
public void disable()Disables the motor, effectively stopping any ongoing PWM signal. -
setSpeed
public void setSpeed(double speed) Sets the speed of the DC motor. This method calculates the necessary pulse width and duty cycle to achieve the specified speed.- Parameters:
speed- the target speed for the motor, as a percentage between 0 and 100.
-
setClockwise
public void setClockwise(boolean clockwise) Sets the direction of the DC motor.- Parameters:
clockwise- whether the motor should rotate clockwise.
-
switchDirection
public void switchDirection()Switches the direction of the motor.
-