diff --git a/CMakeLists.txt b/CMakeLists.txt index 4bcf4f6d..8dd67e96 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,6 +96,8 @@ add_app_subdirectory(${ROVER_APPS_DIR}/pdb) add_app_subdirectory(${ROVER_APPS_DIR}/science) # Include Test Apps +add_app_subdirectory(${TEST_APPS_DIR}/tutorial-servo-can-control) +add_app_subdirectory(${TEST_APPS_DIR}/tutorial-servo-pot-control) add_app_subdirectory(${TEST_APPS_DIR}/test-actuator-controller) add_app_subdirectory(${TEST_APPS_DIR}/test-adafruitSTEMMA) add_app_subdirectory(${TEST_APPS_DIR}/test-aeat-6012) diff --git a/supported_build_configurations.yaml b/supported_build_configurations.yaml index 4121244a..f315ae7a 100644 --- a/supported_build_configurations.yaml +++ b/supported_build_configurations.yaml @@ -106,4 +106,10 @@ test-stress-can: - GIMBAL_REV2 - PDB_REV2 - SCIENCE_REV2 - - UWRT_NUCLEO \ No newline at end of file + - UWRT_NUCLEO + +tutorial-servo-pot-control: +- UWRT_NUCLEO + +tutorial-servo-can-control: +- UWRT_NUCLEO \ No newline at end of file diff --git a/test-apps/tutorial-servo-can-control/CMakeLists.txt b/test-apps/tutorial-servo-can-control/CMakeLists.txt new file mode 100644 index 00000000..f9d45330 --- /dev/null +++ b/test-apps/tutorial-servo-can-control/CMakeLists.txt @@ -0,0 +1,10 @@ +add_executable(tutorial-servo-can-control) +target_sources(tutorial-servo-can-control PRIVATE src/main.cpp) +target_link_libraries(tutorial-servo-can-control PRIVATE mbed-os) +mbed_set_post_build(tutorial-servo-can-control) + +add_library (TutorialServo STATIC) +target_sources (TutorialServo PRIVATE src/TutorialServo.cpp) +target_include_directories (TutorialServo PUBLIC include) +target_link_libraries (TutorialServo PRIVATE mbed-os) +target_link_libraries (tutorial-servo-can-control PRIVATE TutorialServo CANMsg) diff --git a/test-apps/tutorial-servo-can-control/include/TutorialServo.h b/test-apps/tutorial-servo-can-control/include/TutorialServo.h new file mode 100644 index 00000000..1b51d482 --- /dev/null +++ b/test-apps/tutorial-servo-can-control/include/TutorialServo.h @@ -0,0 +1,25 @@ +#pragma once +#include "mbed.h" + +class TutorialServo { +public : +// Constructor: Takes a servo pin name (ex. PA_1), and optionally a servo range +// that has a default value of 180.0 degrees, a minimum pulsewidth of 1ms, and a +// maximum pulsewidth of 2ms. +TutorialServo(PinName servoPin, float servoRangeInDegrees = 180.0, float +minPulsewidthInMs = 1, float maxPulsewidthInMs = 2) ; +// Set servo position (ex. 45 deg) +void setPositionInDegrees( const float degrees) ; +// Get the servo range in degrees (ex: 90 deg) +float getServoRangeInDegrees( ) const ; +// Get the min pulse width in ms (ex: 1ms) +float getMinPulseWidthInMs( ) const ; +// Get the max pulse width in ms (ex: 2ms) +float getMaxPulseWidthInMs( ) const ; + +private : + PwmOut m_servoPwmOut ; + const float m_servoRangeInDegrees ; + const float m_minPulsewidthInMs; + const float m_maxPulsewidthInMs; +}; \ No newline at end of file diff --git a/test-apps/tutorial-servo-can-control/src/TutorialServo.cpp b/test-apps/tutorial-servo-can-control/src/TutorialServo.cpp new file mode 100644 index 00000000..66a22686 --- /dev/null +++ b/test-apps/tutorial-servo-can-control/src/TutorialServo.cpp @@ -0,0 +1,35 @@ +#pragma once +#include "mbed.h" +#include "TutorialServo.h" + +TutorialServo::TutorialServo(PinName servoPin, float servoRangeInDegrees, float +minPulsewidthInMs, float maxPulsewidthInMs): +m_servoPwmOut(servoPin), +m_servoRangeInDegrees(servoRangeInDegrees), +m_minPulsewidthInMs(minPulsewidthInMs), +m_maxPulsewidthInMs(maxPulsewidthInMs){ + m_servoPwmOut.period_ms(20); +} + +void TutorialServo::setPositionInDegrees( const float degrees){ + if (degrees <= m_servoRangeInDegrees && degrees >= 0) + { + m_servoPwmOut.pulsewidth((degrees/m_servoRangeInDegrees)*(m_maxPulsewidthInMs-m_minPulsewidthInMs)); + }else if (degrees < 0){ + m_servoPwmOut.pulsewidth(m_minPulsewidthInMs); + }else{ + m_servoPwmOut.pulsewidth(m_maxPulsewidthInMs); + } +} + +float TutorialServo::getServoRangeInDegrees( ) const{ + return m_servoRangeInDegrees; +} + +float TutorialServo::getMinPulseWidthInMs( ) const { + return m_minPulsewidthInMs; +} + +float TutorialServo::getMaxPulseWidthInMs( ) const{ + return m_maxPulsewidthInMs; +} diff --git a/test-apps/tutorial-servo-can-control/src/main.cpp b/test-apps/tutorial-servo-can-control/src/main.cpp new file mode 100644 index 00000000..05548721 --- /dev/null +++ b/test-apps/tutorial-servo-can-control/src/main.cpp @@ -0,0 +1,18 @@ +#include "CANMsg.h" +#include "mbed.h" +#include "TutorialServo.h" + +TutorialServo servo(PA_1); //initialize servo variable from class we created using appropriate pin +CAN can(PB_8, PB_9); //rx and tx pins +CANMsg rxMsg; + + +int main() { + float percentage; + while (1) { + if (can.read(rxMsg)) { + rxMsg.getPayload(percentage); + servo.setPositionInDegrees(percentage * servo.getServoRangeInDegrees()); + } + } +} \ No newline at end of file diff --git a/test-apps/tutorial-servo-pot-control/CmakeLists.txt b/test-apps/tutorial-servo-pot-control/CmakeLists.txt new file mode 100644 index 00000000..efb4064e --- /dev/null +++ b/test-apps/tutorial-servo-pot-control/CmakeLists.txt @@ -0,0 +1,4 @@ +add_executable(tutorial-servo-pot-control) +target_sources(tutorial-servo-pot-control PRIVATE src/main.cpp) +target_link_libraries(tutorial-servo-pot-control PRIVATE mbed-os) +mbed_set_post_build(tutorial-servo-pot-control) \ No newline at end of file diff --git a/test-apps/tutorial-servo-pot-control/src/main.cpp b/test-apps/tutorial-servo-pot-control/src/main.cpp new file mode 100644 index 00000000..f54b213e --- /dev/null +++ b/test-apps/tutorial-servo-pot-control/src/main.cpp @@ -0,0 +1,12 @@ +#include "mbed.h" + +AnalogIn potVoltageIn(PA_0); +PwmOut servoPwmOut(PA_1); + +int main() { + servoPwmOut.period_ms(20); + while (1){ + float potVoltage = potVoltageIn.read(); + servoPwmOut.pulsewidth((1 + potVoltage/3.3) / 1000); + } +}