From 7e91183810a3a20beedb20ae8f277fc81bad3332 Mon Sep 17 00:00:00 2001 From: Joe Folen Date: Wed, 10 Apr 2024 16:26:30 -0500 Subject: [PATCH 01/10] beginning servo motor implementation --- .../controllers/ServoMotorController.java | 16 ++++++++++++++++ components/src/main/resources/application.yml | 7 +++++++ .../outputdevices/ServoMotorHelper.java | 17 +++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 components/src/main/java/com/opensourcewithslu/components/controllers/ServoMotorController.java create mode 100644 pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java diff --git a/components/src/main/java/com/opensourcewithslu/components/controllers/ServoMotorController.java b/components/src/main/java/com/opensourcewithslu/components/controllers/ServoMotorController.java new file mode 100644 index 00000000..0be1d714 --- /dev/null +++ b/components/src/main/java/com/opensourcewithslu/components/controllers/ServoMotorController.java @@ -0,0 +1,16 @@ +package com.opensourcewithslu.components.controllers; + +import com.opensourcewithslu.outputdevices.ServoMotorHelper; +import com.pi4j.io.pwm.Pwm; +import io.micronaut.http.annotation.Controller; +import jakarta.inject.Named; + +@Controller("/servoMotor") +public class ServoMotorController { + + private final ServoMotorHelper servoMotorHelper; + + public ServoMotorController(@Named("servo-motor")Pwm servoMotor) { + this.servoMotorHelper = new ServoMotorHelper(servoMotor); + } +} diff --git a/components/src/main/resources/application.yml b/components/src/main/resources/application.yml index 86abad60..078263ac 100644 --- a/components/src/main/resources/application.yml +++ b/components/src/main/resources/application.yml @@ -28,6 +28,13 @@ pi4j: provider: pigpio-pwm initial: 0 shutdown: 0 + servo-motor: + name: Servo Motor + address: 18 + pwmType: SOFTWARE + provider: pigpio-pwm + initial: 0 + shutdown: 0 # end::pwm[] # tag::i2c[] diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java new file mode 100644 index 00000000..41afee4a --- /dev/null +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java @@ -0,0 +1,17 @@ +package com.opensourcewithslu.outputdevices; + +import com.pi4j.io.pwm.Pwm; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ServoMotorHelper { + + private static final Logger log = LoggerFactory.getLogger(ServoMotorHelper.class); + + private final Pwm servoMotor; + + public ServoMotorHelper(Pwm servoMotor) + { + this.servoMotor = servoMotor; + } +} From 044efbcd0ee0d697f557c6fac98591cafcfa1c55 Mon Sep 17 00:00:00 2001 From: Joe Folen Date: Wed, 10 Apr 2024 16:45:31 -0500 Subject: [PATCH 02/10] servo motor updates --- .../controllers/ServoMotorController.java | 17 +++++++++++++++++ .../outputdevices/ServoMotorHelper.java | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/components/src/main/java/com/opensourcewithslu/components/controllers/ServoMotorController.java b/components/src/main/java/com/opensourcewithslu/components/controllers/ServoMotorController.java index 0be1d714..fd8fda13 100644 --- a/components/src/main/java/com/opensourcewithslu/components/controllers/ServoMotorController.java +++ b/components/src/main/java/com/opensourcewithslu/components/controllers/ServoMotorController.java @@ -3,6 +3,8 @@ import com.opensourcewithslu.outputdevices.ServoMotorHelper; import com.pi4j.io.pwm.Pwm; import io.micronaut.http.annotation.Controller; +import io.micronaut.http.annotation.Get; +import io.micronaut.http.annotation.Post; import jakarta.inject.Named; @Controller("/servoMotor") @@ -13,4 +15,19 @@ public class ServoMotorController { public ServoMotorController(@Named("servo-motor")Pwm servoMotor) { this.servoMotorHelper = new ServoMotorHelper(servoMotor); } + + @Get("/enable") + public void enableServoMotor() { + servoMotorHelper.enable(); + } + + @Get("/disable") + public void disableServoMotor() { + servoMotorHelper.disable(); + } + + @Post("/setAngle/{angle}") + public void setAngle(int angle) { + servoMotorHelper.setAngle(angle); + } } diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java index 41afee4a..3a338c38 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java @@ -14,4 +14,23 @@ public ServoMotorHelper(Pwm servoMotor) { this.servoMotor = servoMotor; } + + public void enable() + { + log.info("enabling servo motor"); + + servoMotor.on(); + } + + public void disable() + { + log.info("disabling servo motor"); + + servoMotor.off(); + } + + public void setAngle(int angle) + { + servoMotor.setFrequency(angle); + } } From 8b829598a58a9d7b8c0ae7d51efec408e927850f Mon Sep 17 00:00:00 2001 From: Joe Folen Date: Fri, 12 Apr 2024 11:36:05 -0500 Subject: [PATCH 03/10] servo motor updates --- .../controllers/ServoMotorController.java | 3 +- .../outputdevices/ServoMotorHelper.java | 34 +++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/components/src/main/java/com/opensourcewithslu/components/controllers/ServoMotorController.java b/components/src/main/java/com/opensourcewithslu/components/controllers/ServoMotorController.java index fd8fda13..05aa2ad6 100644 --- a/components/src/main/java/com/opensourcewithslu/components/controllers/ServoMotorController.java +++ b/components/src/main/java/com/opensourcewithslu/components/controllers/ServoMotorController.java @@ -4,7 +4,6 @@ import com.pi4j.io.pwm.Pwm; import io.micronaut.http.annotation.Controller; import io.micronaut.http.annotation.Get; -import io.micronaut.http.annotation.Post; import jakarta.inject.Named; @Controller("/servoMotor") @@ -26,7 +25,7 @@ public void disableServoMotor() { servoMotorHelper.disable(); } - @Post("/setAngle/{angle}") + @Get("/setAngle/{angle}") public void setAngle(int angle) { servoMotorHelper.setAngle(angle); } diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java index 3a338c38..2ae751d0 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java @@ -4,24 +4,37 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * + */ public class ServoMotorHelper { private static final Logger log = LoggerFactory.getLogger(ServoMotorHelper.class); private final Pwm servoMotor; + /** + * + * @param servoMotor PWM + */ public ServoMotorHelper(Pwm servoMotor) { this.servoMotor = servoMotor; } + /** + * + */ public void enable() { log.info("enabling servo motor"); - servoMotor.on(); + servoMotor.on(0, 50); } + /** + * + */ public void disable() { log.info("disabling servo motor"); @@ -29,8 +42,25 @@ public void disable() servoMotor.off(); } + private float map(float value, int inMax, int outMin, int outMax) + { + return (outMax - outMin) * (value) / (inMax) + outMin; + } + + /** + * + * @param angle integer + */ public void setAngle(int angle) { - servoMotor.setFrequency(angle); + int MIN_ANGLE = 0; + int MAX_ANGLE = 180; + int SERVO_MIN_PULSE = 500; + int SERVO_MAX_PULSE = 2500; + + angle = Math.max(MIN_ANGLE, Math.min(MAX_ANGLE, angle)); + float pulse = map(angle, MAX_ANGLE, SERVO_MIN_PULSE, SERVO_MAX_PULSE); + float pwm = map(pulse, 20000, 0, 100); + servoMotor.setDutyCycle(pwm); } } From 4302fa471d705fc9220ae3a6eea03169dd87c62f Mon Sep 17 00:00:00 2001 From: Joe Folen Date: Sat, 13 Apr 2024 14:13:36 -0500 Subject: [PATCH 04/10] servo motor updates --- .../outputdevices/ServoMotorHelper.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java index 2ae751d0..30a975de 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java @@ -5,7 +5,7 @@ import org.slf4j.LoggerFactory; /** - * + * The ServoMotorHelper class contains methods which are used to implement and control a servo motor. */ public class ServoMotorHelper { @@ -14,8 +14,8 @@ public class ServoMotorHelper { private final Pwm servoMotor; /** - * - * @param servoMotor PWM + * The ServoMotorHelper constructor. + * @param servoMotor A {@link com.opensourcewithslu.utilities.PwmConfiguration} Object. */ public ServoMotorHelper(Pwm servoMotor) { @@ -23,7 +23,7 @@ public ServoMotorHelper(Pwm servoMotor) } /** - * + * Enables the servo motor by setting the duty cycle to 0 and the frequency to 50. */ public void enable() { @@ -33,7 +33,7 @@ public void enable() } /** - * + * Disables the servo motor. */ public void disable() { @@ -48,11 +48,13 @@ private float map(float value, int inMax, int outMin, int outMax) } /** - * - * @param angle integer + * Takes the angle as input and rotates the servo motor by that amount (between 0 and 180 degrees). + * @param angle An integer type */ public void setAngle(int angle) { + log.info("Rotating the servo motor by " + angle + " degrees."); + int MIN_ANGLE = 0; int MAX_ANGLE = 180; int SERVO_MIN_PULSE = 500; @@ -61,6 +63,7 @@ public void setAngle(int angle) angle = Math.max(MIN_ANGLE, Math.min(MAX_ANGLE, angle)); float pulse = map(angle, MAX_ANGLE, SERVO_MIN_PULSE, SERVO_MAX_PULSE); float pwm = map(pulse, 20000, 0, 100); + servoMotor.setDutyCycle(pwm); } } From 36f2598c9f78eb7b8c602a11517baba1d4f52dec Mon Sep 17 00:00:00 2001 From: Joe Folen Date: Sun, 14 Apr 2024 15:46:40 -0500 Subject: [PATCH 05/10] setAngle update in helper --- .../opensourcewithslu/outputdevices/ServoMotorHelper.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java index 30a975de..eb082275 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java @@ -53,6 +53,11 @@ private float map(float value, int inMax, int outMin, int outMax) */ public void setAngle(int angle) { + if (servoMotor.isOff()) + { + log.info("You must enable the servo motor first."); + return; + } log.info("Rotating the servo motor by " + angle + " degrees."); int MIN_ANGLE = 0; From 34f104cc534d03849220103aacf098337737ecdd Mon Sep 17 00:00:00 2001 From: ruthvikm Date: Sun, 21 Apr 2024 15:37:43 -0500 Subject: [PATCH 06/10] arm moves inaccurately --- .../outputdevices/ServoMotorHelper.java | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java index eb082275..efe89a11 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java @@ -10,7 +10,13 @@ public class ServoMotorHelper { private static final Logger log = LoggerFactory.getLogger(ServoMotorHelper.class); - + private static final int FREQUENCY = 50; // Frequency for PWM signal in Hz + private static final int PWM_CYCLE_MICROSECONDS = 2000; // Total cycle length in microseconds + private static final int MIN_ANGLE = 0; + private static final int MAX_ANGLE = 180; + private static final int SERVO_MIN_PULSE = 500; // Minimum pulse width in microseconds (0.5ms for 0 degrees) + private static final int SERVO_MAX_PULSE = 2500; // Maximum pulse width in microseconds (2.5ms for 180 degrees) + private boolean isEnabled = false; // State tracking variable for the servo motor private final Pwm servoMotor; /** @@ -27,9 +33,9 @@ public ServoMotorHelper(Pwm servoMotor) */ public void enable() { - log.info("enabling servo motor"); - - servoMotor.on(0, 50); + log.info("Enabling servo motor"); + servoMotor.on(0, FREQUENCY); + isEnabled = true; } /** @@ -37,9 +43,9 @@ public void enable() */ public void disable() { - log.info("disabling servo motor"); - + log.info("Disabling servo motor"); servoMotor.off(); + isEnabled = false; } private float map(float value, int inMax, int outMin, int outMax) @@ -51,24 +57,19 @@ private float map(float value, int inMax, int outMin, int outMax) * Takes the angle as input and rotates the servo motor by that amount (between 0 and 180 degrees). * @param angle An integer type */ - public void setAngle(int angle) - { - if (servoMotor.isOff()) - { + public void setAngle(int angle) { + if (!isEnabled) { log.info("You must enable the servo motor first."); return; } - log.info("Rotating the servo motor by " + angle + " degrees."); - - int MIN_ANGLE = 0; - int MAX_ANGLE = 180; - int SERVO_MIN_PULSE = 500; - int SERVO_MAX_PULSE = 2500; angle = Math.max(MIN_ANGLE, Math.min(MAX_ANGLE, angle)); - float pulse = map(angle, MAX_ANGLE, SERVO_MIN_PULSE, SERVO_MAX_PULSE); - float pwm = map(pulse, 20000, 0, 100); - servoMotor.setDutyCycle(pwm); + float pulseWidth = map(angle, MAX_ANGLE, SERVO_MIN_PULSE, SERVO_MAX_PULSE); + float dutyCycle = map(pulseWidth, PWM_CYCLE_MICROSECONDS, 0, 100); + + log.info("Setting servo to {} degrees, Pulse Width: {} us, Duty Cycle: {}%", angle, pulseWidth, dutyCycle); + + servoMotor.on(dutyCycle); } } From 185ec9b4863a06a26b1227b82648157e755f9796 Mon Sep 17 00:00:00 2001 From: Joe Folen Date: Sun, 21 Apr 2024 17:00:13 -0500 Subject: [PATCH 07/10] setAngle and map updates in helper --- .../outputdevices/ServoMotorHelper.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java index efe89a11..74b785fb 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java @@ -48,28 +48,34 @@ public void disable() isEnabled = false; } - private float map(float value, int inMax, int outMin, int outMax) + private float map(float value, float inMax, float outMin, float outMax) { - return (outMax - outMin) * (value) / (inMax) + outMin; + return ((outMax - outMin) * (value)) / ((inMax) + outMin); } /** * Takes the angle as input and rotates the servo motor by that amount (between 0 and 180 degrees). * @param angle An integer type */ - public void setAngle(int angle) { - if (!isEnabled) { + public void setAngle(int angle) + { + if (!isEnabled) + { log.info("You must enable the servo motor first."); return; } - angle = Math.max(MIN_ANGLE, Math.min(MAX_ANGLE, angle)); + if (angle < MIN_ANGLE || angle > MAX_ANGLE) + { + log.info("You must enter an angle between 0 and 180 degrees."); + return; + } - float pulseWidth = map(angle, MAX_ANGLE, SERVO_MIN_PULSE, SERVO_MAX_PULSE); + float pulseWidth = map((float)angle, MAX_ANGLE, SERVO_MIN_PULSE, SERVO_MAX_PULSE); float dutyCycle = map(pulseWidth, PWM_CYCLE_MICROSECONDS, 0, 100); log.info("Setting servo to {} degrees, Pulse Width: {} us, Duty Cycle: {}%", angle, pulseWidth, dutyCycle); - servoMotor.on(dutyCycle); + servoMotor.on(dutyCycle, FREQUENCY); } } From df679c4765b7ebe5f13efa4ce657c232b802f27c Mon Sep 17 00:00:00 2001 From: Joe Folen Date: Sun, 21 Apr 2024 17:17:16 -0500 Subject: [PATCH 08/10] updated map logic in helper --- .../outputdevices/ServoMotorHelper.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java index 74b785fb..10970207 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java @@ -50,15 +50,14 @@ public void disable() private float map(float value, float inMax, float outMin, float outMax) { - return ((outMax - outMin) * (value)) / ((inMax) + outMin); + return ((outMax - outMin) * (value - (float) ServoMotorHelper.MIN_ANGLE)) / ((inMax - (float) ServoMotorHelper.MIN_ANGLE) + outMin); } /** * Takes the angle as input and rotates the servo motor by that amount (between 0 and 180 degrees). * @param angle An integer type */ - public void setAngle(int angle) - { + public void setAngle(int angle) { if (!isEnabled) { log.info("You must enable the servo motor first."); @@ -77,5 +76,11 @@ public void setAngle(int angle) log.info("Setting servo to {} degrees, Pulse Width: {} us, Duty Cycle: {}%", angle, pulseWidth, dutyCycle); servoMotor.on(dutyCycle, FREQUENCY); + + try{ + Thread.sleep(1000); //Allow a full second for servo to rotate + } catch (InterruptedException e){ + Thread.currentThread().interrupt(); + } } } From 863a0d6d5d60db302e053aef33c9866cd282f49b Mon Sep 17 00:00:00 2001 From: ruthvikm Date: Wed, 24 Apr 2024 14:21:49 -0500 Subject: [PATCH 09/10] servo motor works for 0 to 180 --- .../outputdevices/ServoMotorHelper.java | 79 ++++++++++--------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java index 10970207..2ba710c0 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/outputdevices/ServoMotorHelper.java @@ -5,82 +5,87 @@ import org.slf4j.LoggerFactory; /** - * The ServoMotorHelper class contains methods which are used to implement and control a servo motor. + * Helper class to control a servo motor using PWM (Pulse Width Modulation). + * This class provides methods to enable, disable, and set the angle of a servo motor. */ public class ServoMotorHelper { private static final Logger log = LoggerFactory.getLogger(ServoMotorHelper.class); - private static final int FREQUENCY = 50; // Frequency for PWM signal in Hz - private static final int PWM_CYCLE_MICROSECONDS = 2000; // Total cycle length in microseconds - private static final int MIN_ANGLE = 0; - private static final int MAX_ANGLE = 180; - private static final int SERVO_MIN_PULSE = 500; // Minimum pulse width in microseconds (0.5ms for 0 degrees) - private static final int SERVO_MAX_PULSE = 2500; // Maximum pulse width in microseconds (2.5ms for 180 degrees) - private boolean isEnabled = false; // State tracking variable for the servo motor - private final Pwm servoMotor; + private static final int FREQUENCY = 50; // Frequency for PWM signal in Hz, typical for servo motors. + private static final int PWM_CYCLE_MICROSECONDS = 20000; // Total cycle length in microseconds, corresponding to 50 Hz. + private static final int MIN_ANGLE = 0; // Minimum angle for servo operation. + private static final int MAX_ANGLE = 180; // Maximum angle for servo operation. + private static final int SERVO_MIN_PULSE = 500; // Minimum pulse width in microseconds corresponding to 0 degrees. + private static final int SERVO_MAX_PULSE = 2500; // Maximum pulse width in microseconds corresponding to 180 degrees. + private boolean isEnabled = false; // State tracking variable for the servo motor. + private final Pwm servoMotor; // PWM interface for the servo motor. /** - * The ServoMotorHelper constructor. - * @param servoMotor A {@link com.opensourcewithslu.utilities.PwmConfiguration} Object. + * Constructs a new ServoMotorHelper. + * + * @param servoMotor A PWM interface to control the servo motor. */ - public ServoMotorHelper(Pwm servoMotor) - { + public ServoMotorHelper(Pwm servoMotor) { this.servoMotor = servoMotor; } /** - * Enables the servo motor by setting the duty cycle to 0 and the frequency to 50. + * Enables the servo motor by setting an initial duty cycle and frequency. + * The servo motor remains disabled until this method is called. */ - public void enable() - { + public void enable() { log.info("Enabling servo motor"); - servoMotor.on(0, FREQUENCY); + servoMotor.on(0, FREQUENCY); // Initializes PWM signal with 0% duty cycle. isEnabled = true; } /** - * Disables the servo motor. + * Disables the servo motor, effectively stopping any ongoing PWM signal. */ - public void disable() - { + public void disable() { log.info("Disabling servo motor"); - servoMotor.off(); + servoMotor.off(); // Stops the PWM signal. isEnabled = false; } - private float map(float value, float inMax, float outMin, float outMax) - { - return ((outMax - outMin) * (value - (float) ServoMotorHelper.MIN_ANGLE)) / ((inMax - (float) ServoMotorHelper.MIN_ANGLE) + outMin); + /** + * Maps the given angle to the corresponding pulse width for the servo motor. + * + * @param value the angle in degrees to be mapped to pulse width. + * @return the calculated pulse width in microseconds. + */ + private float map(float value) { + return SERVO_MIN_PULSE + ((value - MIN_ANGLE) * (SERVO_MAX_PULSE - SERVO_MIN_PULSE) / (MAX_ANGLE - MIN_ANGLE)); } /** - * Takes the angle as input and rotates the servo motor by that amount (between 0 and 180 degrees). - * @param angle An integer type + * Sets the servo motor to a specific angle. + * This method calculates the necessary pulse width and duty cycle to achieve the specified angle. + * + * @param angle the target angle for the servo motor, between 0 and 180 degrees. */ public void setAngle(int angle) { - if (!isEnabled) - { + if (!isEnabled) { log.info("You must enable the servo motor first."); return; } - if (angle < MIN_ANGLE || angle > MAX_ANGLE) - { + if (angle < MIN_ANGLE || angle > MAX_ANGLE) { log.info("You must enter an angle between 0 and 180 degrees."); return; } - float pulseWidth = map((float)angle, MAX_ANGLE, SERVO_MIN_PULSE, SERVO_MAX_PULSE); - float dutyCycle = map(pulseWidth, PWM_CYCLE_MICROSECONDS, 0, 100); + float pulseWidth = map(angle); + float dutyCycle = (pulseWidth / PWM_CYCLE_MICROSECONDS) * 100; log.info("Setting servo to {} degrees, Pulse Width: {} us, Duty Cycle: {}%", angle, pulseWidth, dutyCycle); servoMotor.on(dutyCycle, FREQUENCY); - - try{ - Thread.sleep(1000); //Allow a full second for servo to rotate - } catch (InterruptedException e){ - Thread.currentThread().interrupt(); + try { + Thread.sleep(100); // Pauses the thread to allow the servo to reach the set angle. + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); // Properly handle thread interruption. + log.info("Thread was interrupted, failed to complete rotation"); } } } From 9844ba17e3af1a52f8675fec7a56e8794af9add8 Mon Sep 17 00:00:00 2001 From: Joe Folen Date: Fri, 26 Apr 2024 11:31:19 -0500 Subject: [PATCH 10/10] added tag in controller --- .../components/controllers/ServoMotorController.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/src/main/java/com/opensourcewithslu/components/controllers/ServoMotorController.java b/components/src/main/java/com/opensourcewithslu/components/controllers/ServoMotorController.java index 05aa2ad6..2d433690 100644 --- a/components/src/main/java/com/opensourcewithslu/components/controllers/ServoMotorController.java +++ b/components/src/main/java/com/opensourcewithslu/components/controllers/ServoMotorController.java @@ -6,6 +6,7 @@ import io.micronaut.http.annotation.Get; import jakarta.inject.Named; +//tag::ex[] @Controller("/servoMotor") public class ServoMotorController { @@ -30,3 +31,4 @@ public void setAngle(int angle) { servoMotorHelper.setAngle(angle); } } +//end::ex[]