Class MotorHelper

java.lang.Object
com.opensourcewithslu.outputdevices.MotorHelper

public class MotorHelper extends Object
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

    Constructors
    Constructor
    Description
    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.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Disables the motor, effectively stopping any ongoing PWM signal.
    void
    Enables the DC motor by setting an initial duty cycle and frequency.
    void
    setClockwise(boolean clockwise)
    Sets the direction of the DC motor.
    void
    setSpeed(double speed)
    Sets the speed of the DC motor.
    void
    Switches the direction of the motor.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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.