diff --git a/src/main/java/com/pi4j/devices/sg90/README.md b/src/main/java/com/pi4j/devices/sg90/README.md index 52671b7..9a91ad2 100644 --- a/src/main/java/com/pi4j/devices/sg90/README.md +++ b/src/main/java/com/pi4j/devices/sg90/README.md @@ -1,34 +1,33 @@ -##Device application to operate a Fs90r servo. This servo can rotate 360 degree continuously. The PWM signals -pulse width being sent to the servo device determines the direction and RPM of rotation. +##Device application to operate a SG90 servo. This servo can rotate 180. The PWM signals +pulse width being sent to the servo device determines to what position the armature rotates. https://www.pololu.com/product/2820 These devices do not have a customary DataSheet. Most servo devices provide limited specifications for required pulse widths, they just say 1 - 2 millisecond. -The FS90 R 360 defines a specific PWM interface and exactly how the PWN signal controls the servo. There are many -different manufacturers of the FS90 servo. The pulse width required for setting the servo at MAX Clockwise (CW) +The SG90 defines a specific PWM interface and exactly how the PWN signal controls the servo. There are many +different manufacturers of the SG90 servo. The pulse width required for setting the servo at MAX Clockwise (CW) or MAX CounterClockWise (CCW) varies by manufacturer This implementation use SE determination for the direction of rotation (Note1). -The driver by default uses the pulse width timing as (low)1000 - (high)2000 microseconds. The user of this example -application can change these values. If the servo does not rotate to the expected RPM, to rotate faster clockwise -reduce the low argument, to rotate CCW faster increase the high argument. The low high commands change should be balanced -or else when the servo is stopped will not be correct. Meaning if you reduce the low by 200, you should increase -the high by 200, or visa-versa. +The driver by default uses the pulse width timing as (minimalpulsewidth)1000 - (maximumpulsewidth)2000 microseconds. The user of this example +application can change these values. If the servo does not stop at the expected position, to rotate further clockwise +reduce the minimalpulsewidth argument, to rotate counterclockwise increase the maximumpulsewidth argument. These changes should be balanced +or else when the servo is at 90° the position will not be correct. Meaning if you reduce the minimalpulsewidth by 200, you should increase +the maximumpulsewidth by 200, or visa-versa. -Note 1 :Direction of rotation, the definition of clockwise and counterclockwise have two distinct versions. These define +Note 1 :Direction of movement, the definition of clockwise and counterclockwise have two distinct versions. These define your perspective of observation. 1. Shaft End (SE / Drive End): The end where the motor shaft and load (like a fan blade or pulley) attach. Industry convention usually assumes you are looking straight at the shaft end. 2. Lead End (LE / Bell End / Opposite Drive End - ODE): The back side of the motor where the electrical wires or leads exit. -https://www.pololu.com/product/2820 Typical wiring when config.sys contains dtoverlay=pwm-2chan -FS90R connections to a Raspberry Pi +SG90 connections to a Raspberry Pi -| FS90_R | Raspberry Pi | +| SG90 | Raspberry Pi | |------------|--------------| | Grey/Brown | Ground | | Red | 5V | @@ -39,46 +38,44 @@ FS90R connections to a Raspberry Pi Building the Example application ./mvnw clean package cd target/distribution -./runFs90r.sh args +./runSg90.sh args -c channel (application default is 2) set the channel, depending on config.txt 1 2 3 4 - -If **-** is included when starting this program, it will continue to run and the following commands are possible +**-** when starting this program, it will continue to run and the following commands are possible -| Command | options | Note | -|------------|--------------------|--------------------------------------| -| direction | cw/ccw | set servo direction of rotation | -| percent | double 0 ... 100 | set RPM | -| low | double 500 ...1499 | pulse width for max clockwise | -| high | double 1501..2500 | pulse width for max counterclockwise | -| x | | servo will stop, program exist | -| h | | print help | +| Command | options | Note | +|-------------------|---------------------|--------------------------------------| +| degree | double 0 ... 180 | set armature position | +| minimalpulsewidth | double 500 ...1499 | pulse width for max clockwise | +| maximumpulsewidth | double 1501..2500 | pulse width for max counterclockwise | +| exit | | servo will stop, program exist | +| help | | print help | -./runFs90r.sh - application running, awaiting command input +./runSg90.sh - application running, awaiting command input -./run*Fs90r.sh -c 1 PWM device will use channel 1 +./run*Sg90.sh -channel 1 PWM device will use channel 1 -./runFs90r.sh -c 1 - PWM device will use channel 1, application running, awaiting command input +./runSg90.sh -channel 1 - PWM device will use channel 1, application running, awaiting command input Command examples -My Fs90r appears faster rotation wih these values for the low and high values low 550 high 2450 +My Sg90 appears faster rotation wih these values minimalpulsewidth 550 maximumpulsewidth 2450 -**./runFs90r.sh -** Start app in mode to accept commands +**./runSg90.sh -** Start app in mode to accept commands -| command | Note | -|----------------|---------------------------------------------------------------| -| low 550 | Provided best performance in my test setup | -| high 2450 | Provided best performance in my test setup | -| direction cw | clockwise | -| percent 75 | 75% clockwise rotation speed | -| direction ccw | exception as servo is currently rotating clockwise | -| percent 0 | stop rotation | -| direction ccw | change to counterclockwise permitted as servo is not rotating | -| percent 50 | 50% counterclockwise rotation speed | -| x | exit | +| command | Note | +|------------------------|---------------------------------------------------------------| +| minimalpulsewidth 550 | Provided best performance in my test setup | +| maximumpulsewidth 2450 | Provided best performance in my test setup | +| direction cw | clockwise | +| percent 75 | 75% clockwise rotation speed | +| direction ccw | exception as servo is currently rotating clockwise | +| percent 0 | stop rotation | +| direction ccw | change to counterclockwise permitted as servo is not rotating | +| percent 50 | 50% counterclockwise rotation speed | +| exit | exit | diff --git a/src/main/java/com/pi4j/devices/sg90/Sg90App.java b/src/main/java/com/pi4j/devices/sg90/Sg90App.java index c87bb8a..ec4dcec 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 SG90 servo motor. + * The SG90 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("channel")) + .id("Sg90RNumber" + SERVO_NUMBER) + .name("sg90R 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( + "channel", + "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("minimalpulsewidth", "Minimal pulse width", new DoubleParameter(500.0, 1500.0), (device, param) -> { + device.setPulseWidthMin(param); + }); + + commandHandler.parameterizedAction("maximumpulsewidth", "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.setAngle(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); } } diff --git a/src/main/java/com/pi4j/examples/utils/commandhandler/DeviceCommandHandler.java b/src/main/java/com/pi4j/examples/utils/commandhandler/DeviceCommandHandler.java index 714b643..d5eeb14 100644 --- a/src/main/java/com/pi4j/examples/utils/commandhandler/DeviceCommandHandler.java +++ b/src/main/java/com/pi4j/examples/utils/commandhandler/DeviceCommandHandler.java @@ -27,9 +27,9 @@ public class DeviceCommandHandler { public DeviceCommandHandler(Function, T> deviceFactory) { this.deviceFactory = deviceFactory; - utility("h", "Display this help text", () -> printHelp(System.out)); + utility("help", "Display this help text", () -> printHelp(System.out)); utility("-", "Read commands from stdin", () -> consumeStdin = true); - utility("x", "Exit", () -> exit = true); + utility("exit", "Exit", () -> exit = true); } private

DeviceCommandHandler register(CommandType type, String label, String description, ParameterSpecification

paramSpec, BiConsumer action) {