From 691000923b039d5389c8e84222a2d2cccca3a24d Mon Sep 17 00:00:00 2001 From: Tom Aarts Date: Mon, 2 Nov 2026 18:17:10 -0600 Subject: [PATCH] sg90 servo, migrate to new cmd structure --- .../java/com/pi4j/devices/sg90/Sg90App.java | 128 ++++++------------ 1 file changed, 40 insertions(+), 88 deletions(-) diff --git a/src/main/java/com/pi4j/devices/sg90/Sg90App.java b/src/main/java/com/pi4j/devices/sg90/Sg90App.java index c87bb8a..d731ae4 100644 --- a/src/main/java/com/pi4j/devices/sg90/Sg90App.java +++ b/src/main/java/com/pi4j/devices/sg90/Sg90App.java @@ -10,44 +10,37 @@ import com.pi4j.boardinfo.util.PwmChipUtil; import com.pi4j.context.Context; import com.pi4j.drivers.motor.sg90.Sg90Driver; +import com.pi4j.examples.utils.commandhandler.DeviceCommandHandler; +import com.pi4j.examples.utils.commandhandler.DoubleParameter; +import com.pi4j.examples.utils.commandhandler.IntegerParameter; import com.pi4j.io.pwm.Pwm; -import com.pi4j.io.pwm.PwmConfig; -import com.pi4j.io.pwm.PwmConfigBuilder; import com.pi4j.io.pwm.PwmType; import com.pi4j.util.Console; -import com.pi4j.util.Delay; /** - * SG90 servo motor control. The driver will use the PWM device created by + * SG90 servo motor control. + * Example implementation for a FS90R servo motor. + * The Fs90R defines a specific PWM interface and exactly how the PWN signal + * controls the servo. + * https://www.friendlywire.com/projects/ne555-servo-safe/SG90-datasheet.pdf + *

*

* See README.md for more information and detail. + *

*/ class Sg90App { - private static final int DEFAULT_PWM_FREQUENCY = 50; - private static final int DEFAULT_CHANNEL_NUMBER = 2; + private static final int PWM_CHANNEL_NUMBER = 2; private static final int SERVO_NUMBER = 1; - - private static final double LOW_PULSSE = 1000.0; - private static final double HIGH_PULSE = 2000.0; - - private static Integer channel = DEFAULT_CHANNEL_NUMBER; private static Console console; - private static Sg90Driver sg90; - private static final Delay delay = new Delay(); - private static java.util.Scanner scanner; - private static double lowPulse = LOW_PULSSE; - private static double highPulse = HIGH_PULSE; - public Sg90App() { super(); - } - /** *

main.

* @@ -57,87 +50,46 @@ public Sg90App() { static void main(String[] args) throws Exception { System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "INFO"); - scanner = new java.util.Scanner(System.in); - console = new Console(); Context pi4j = Pi4J.newAutoContext(); console.title("<-- The Pi4J V5 Project Extension -->", "Sg90App"); String helpString = " Parms: -c channel -q quit -low microseconds -high microseconds -h HELP"; + DeviceCommandHandler commandHandler = new DeviceCommandHandler<>(settings -> + new Sg90Driver(pi4j.create(Pwm.newConfigBuilder() + .channel((Integer) settings.get("c")) + .id("fs90RNumber" + SERVO_NUMBER) + .name("fs90R number " + SERVO_NUMBER) + .pwmType(PwmType.HARDWARE) + .chip(PwmChipUtil.getPWMChip()) + .frequency(Sg90Driver.PWM_FREQUENCY)))); - String o; - for (int i = 0; i < args.length; i++) { - switch (o = args[i]) { - case "-low" -> { - lowPulse = Integer.parseInt(args[i + 1]); - i++; - } - case "-high" -> { - highPulse = Integer.parseInt(args[i + 1]); - i++; - } - case "-c" -> { - channel = Integer.parseInt(args[i + 1]); - i++; - } - case "-q" -> { - console.println("Exit"); - System.exit(40); - } - case "-h" -> { - console.println(helpString); - System.exit(41); - } - default -> { - console.println(" !!! Invalid Parm " + o); - console.println(helpString); - System.exit(42); - } - } - } - - - Pwm pwm = createPwm(SERVO_NUMBER, channel, pi4j); - - sg90 = new Sg90Driver(pwm, lowPulse, highPulse); - - waitChange(10L); - - pwm.close(); + commandHandler.configuration( + "c", + "Set the PWM channel number", + new IntegerParameter(1, 4), + PWM_CHANNEL_NUMBER); - } + // Following actions can be used as arguments when first invoking this program (-low value) , or + // after instantiation used on the command line (low value). - static void waitChange(long c) { - while (true) { - delay.setMillis(c).materialize(); - console.println("Enter degree value or enter any key to quit"); - if (scanner.hasNextInt()) { - double nextDegree = scanner.nextDouble(); - if ((nextDegree < 0.0) || (nextDegree > 180.0)) { - console.println(" degree must be in the range 0..180"); - } else { - sg90.setServoAngle(nextDegree); - } - } else { - break; - } - } - } + commandHandler.parameterizedAction("low", "Minimal pulse width", new DoubleParameter(500.0, 1500.0), (device, param) -> { + device.setPulseWidthMin(param); + }); + + commandHandler.parameterizedAction("high", "Maximum pulse width", new DoubleParameter(1500.0, 2500.0), (device, param) -> { + device.setPulseWidthMax(param); + }); + commandHandler.parameterizedAction("degree", "rotation degree", new DoubleParameter(0.0, 180.0), (device, param) -> { + device.setServoAngle(param); + }); - static Pwm createPwm(int servoNumber, Integer channel, Context pi4j) { + commandHandler.process(args); - final PwmConfig config = PwmConfigBuilder.newInstance(pi4j) - .id("sg90Number" + servoNumber) - .name("SG90 number " + servoNumber) - .channel(channel) - .pwmType(PwmType.HARDWARE) - .channel(channel) - .chip(PwmChipUtil.getPWMChip()) - .frequency(DEFAULT_PWM_FREQUENCY) - .build(); + // Shutdown Pi4J + pi4j.shutdown(); - return pi4j.create(config); } }