Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pratham Sood Firmware Training Oct 2021 #399

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion supported_build_configurations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,10 @@ test-stress-can:
- GIMBAL_REV2
- PDB_REV2
- SCIENCE_REV2
- UWRT_NUCLEO
- UWRT_NUCLEO

tutorial-servo-pot-control:
- UWRT_NUCLEO

tutorial-servo-can-control:
- UWRT_NUCLEO
10 changes: 10 additions & 0 deletions test-apps/tutorial-servo-can-control/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 25 additions & 0 deletions test-apps/tutorial-servo-can-control/include/TutorialServo.h
Original file line number Diff line number Diff line change
@@ -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;
};
35 changes: 35 additions & 0 deletions test-apps/tutorial-servo-can-control/src/TutorialServo.cpp
Original file line number Diff line number Diff line change
@@ -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));
cindyli-13 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if m_minPulsewidthInMs is nonzero? you'll need the constant offset so that 0 degrees maps to 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;
}
18 changes: 18 additions & 0 deletions test-apps/tutorial-servo-can-control/src/main.cpp
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
4 changes: 4 additions & 0 deletions test-apps/tutorial-servo-pot-control/CmakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions test-apps/tutorial-servo-pot-control/src/main.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}