From 7930a49ba09bb1fe3f2c38a59fc0c966ffb03720 Mon Sep 17 00:00:00 2001 From: Rukshi101 Date: Wed, 26 May 2021 18:26:08 -0400 Subject: [PATCH] Firmware Training Rukshi Thiyagayogan --- apps/test-aeat-8800/CMakeLists.txt | 1 + .../tutorial-servo-can-control/CMakeLists.txt | 15 +++++++ .../TutorialServo.cpp | 45 +++++++++++++++++++ .../TutorialServo.h | 24 ++++++++++ apps/tutorial-servo-can-control/main.cpp | 26 +++++++++++ .../src/CMakeLists.txt | 6 +++ apps/tutorial-servo-pot-control/src/main.cpp | 18 ++++++++ supported_build_configurations.yaml | 4 ++ 8 files changed, 139 insertions(+) create mode 100644 apps/tutorial-servo-can-control/CMakeLists.txt create mode 100644 apps/tutorial-servo-can-control/TutorialServo.cpp create mode 100644 apps/tutorial-servo-can-control/TutorialServo.h create mode 100644 apps/tutorial-servo-can-control/main.cpp create mode 100644 apps/tutorial-servo-pot-control/src/CMakeLists.txt create mode 100644 apps/tutorial-servo-pot-control/src/main.cpp diff --git a/apps/test-aeat-8800/CMakeLists.txt b/apps/test-aeat-8800/CMakeLists.txt index 36f6efa92..c1103dc4d 100644 --- a/apps/test-aeat-8800/CMakeLists.txt +++ b/apps/test-aeat-8800/CMakeLists.txt @@ -2,3 +2,4 @@ add_executable(test-aeat-8800.${TARGET}-board.elf) target_sources(test-aeat-8800.${TARGET}-board.elf PRIVATE src/main.cpp) target_link_libraries(test-aeat-8800.${TARGET}-board.elf PRIVATE Logger) target_set_firmware_properties(test-aeat-8800.${TARGET}-board.elf) +add_subdirectory(apps/tutorial-servo-pot-control) \ No newline at end of file diff --git a/apps/tutorial-servo-can-control/CMakeLists.txt b/apps/tutorial-servo-can-control/CMakeLists.txt new file mode 100644 index 000000000..f7f8d39a7 --- /dev/null +++ b/apps/tutorial-servo-can-control/CMakeLists.txt @@ -0,0 +1,15 @@ +add_executable(tutorial-servo-pot-control.${TARGET}-board.elf) +target_sources(tutorial-servo-pot-control.${TARGET}-board.elf +PRIVATE src/main.cpp) +target_set_firmware_properties(tutorial-servo-pot-control.${TARGET} +-b oard.elf) + +add_subdirectory(apps/tutorial-servo-pot-control) + +add_library (TutorialServo STATIC) +target_sources (TutorialServo PRIVATE src/TutorialServo.cpp) +target_link_libraries (tutorial-servo-can-control. ${TARGET} -board.elf PRIVATE +TutorialServo +) +target_include_directories (TutorialServo PUBLIC include) +target_set_mbed_dependency (TutorialServo) \ No newline at end of file diff --git a/apps/tutorial-servo-can-control/TutorialServo.cpp b/apps/tutorial-servo-can-control/TutorialServo.cpp new file mode 100644 index 000000000..6139c3c06 --- /dev/null +++ b/apps/tutorial-servo-can-control/TutorialServo.cpp @@ -0,0 +1,45 @@ + +#include "mbed.h" +#include "TutorialServo.h" + +#include +using namespace std; + + +TutorialServo::TutorialServo(PinName servoPin, float servoRangeInDegrees = 180.0, +float minPulsewidthInMs = 1, float maxPulsewidthInMs = 2) +{ + //declares this pin as pwm using pwmout. + PwmOut servoPwmOut(servoPin); + + //set the PWM period for that pin to 20ms + servoPwmOut.period_ms(20); + + + while(1) + { + servoPwmOut.pulsewidth_ms(minPulsewidthInMs); + servoPwmOut.pulsewidth_ms(maxPulsewidthInMs); + } +} + +void TutorialServo::setPositionInDegrees(const float degrees) +{ + + m_servoRangeInDegrees = degrees; +} + +float TutorialServo::getServoRangeInDegrees() const +{ + return m_servoRangeInDegrees; +} + +float TutorialServo::getMinPulseWidthInMs() const +{ + return m_minPulsewidthInMs; +} + +float TutorialServo::getMaxPulseWidthInMs() const +{ + return m_maxPulsewidthInMs; +} \ No newline at end of file diff --git a/apps/tutorial-servo-can-control/TutorialServo.h b/apps/tutorial-servo-can-control/TutorialServo.h new file mode 100644 index 000000000..b0e23acee --- /dev/null +++ b/apps/tutorial-servo-can-control/TutorialServo.h @@ -0,0 +1,24 @@ +#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/apps/tutorial-servo-can-control/main.cpp b/apps/tutorial-servo-can-control/main.cpp new file mode 100644 index 000000000..21d1f57cf --- /dev/null +++ b/apps/tutorial-servo-can-control/main.cpp @@ -0,0 +1,26 @@ +#include "TutorialServo.h" +#include "CANBus.h" +#include "CANMsg.h" +#include "mbed.h" + +int main() +{ + CANBus can(CAN1_RX, CAN1_TX, HWBRIDGE::ROVERCONFIG::ROVER_CANBUS_FREQUENCY); + CANMsg rxMsg; + float rangeOfMotion = NULL; + + //listens on can for any incoming can messages and if any places into rxMsg object + while(true) + { + if(can.read(rxMsg)) + { + rxMsg.getPayload(rangeOfMotion); + } + } + + TutorialServo servoObject; + //controls the servo using float value stored in CAN message. + servoObject TutorialServo(PA_1, rangeOfMotion); + +} + diff --git a/apps/tutorial-servo-pot-control/src/CMakeLists.txt b/apps/tutorial-servo-pot-control/src/CMakeLists.txt new file mode 100644 index 000000000..aff01561f --- /dev/null +++ b/apps/tutorial-servo-pot-control/src/CMakeLists.txt @@ -0,0 +1,6 @@ + +//letting cmake know how our app is compiled. +add_executable(tutorial-servo-pot-control.${TARGET}-board.elf) +target_sources(tutorial-servo-pot-control.${TARGET}-board.elf +PRIVATE src/main.cpp) +target_set_firmware_properties(tutorial-servo-pot-control.${TARGET} \ No newline at end of file diff --git a/apps/tutorial-servo-pot-control/src/main.cpp b/apps/tutorial-servo-pot-control/src/main.cpp new file mode 100644 index 000000000..5434ff3a5 --- /dev/null +++ b/apps/tutorial-servo-pot-control/src/main.cpp @@ -0,0 +1,18 @@ +#include "mbed.h" + +//initilaize potentiometer voltage input on pin PA_0 +AnalogIn potVoltageIn(PA_0); +PwmOut servoPwmOut(PA_1); + +int main() { + +//loop runs infinitely. + while(1){ + //sets pulse width modulation period to 20ms(50hz) + servoPwmOut.period_ms(20); + + float potVoltage = potVoltageIn.read(); + servoPwmOut.pulsewidth_ms(1 + potVoltage/3.3); + } + +} \ No newline at end of file diff --git a/supported_build_configurations.yaml b/supported_build_configurations.yaml index 2e89bf8a1..1f41df734 100644 --- a/supported_build_configurations.yaml +++ b/supported_build_configurations.yaml @@ -92,3 +92,7 @@ test-quadrature64cpr: test-watchdog: - nucleo + + +tutorial-servo-pot-control: +- nucleo \ No newline at end of file