-
Notifications
You must be signed in to change notification settings - Fork 57
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
Firmware Training- Rukshi Thiyagayogan #380
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
#include "mbed.h" | ||
#include "TutorialServo.h" | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
|
||
TutorialServo::TutorialServo(PinName servoPin, float servoRangeInDegrees = 180.0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of providing default values as you have done we do it by providing something called a member initializer list. Below is a link explaining what that means. Try implementing this in your code. |
||
float minPulsewidthInMs = 1, float maxPulsewidthInMs = 2) | ||
{ | ||
//declares this pin as pwm using pwmout. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inside your constructor there doesn't need to be any code. Really in this application the only purpose of the constructor is to use the member initializer list to initialize the member variables of the object. |
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What you are doing here is changing the member variable, this isn't going to change the angle of the motor at all. What you want to do is to call the method of setting the pulsewidth on the servoPwmOut i.e including the following line of code instead: m_servoPwmOut.pulsewidth_ms(some_argument); Think about what should the argument for this function be and dont hesitate to hit me up on discord for more help. |
||
} | ||
|
||
float TutorialServo::getServoRangeInDegrees() const | ||
{ | ||
return m_servoRangeInDegrees; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looking good |
||
|
||
float TutorialServo::getMinPulseWidthInMs() const | ||
{ | ||
return m_minPulsewidthInMs; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
|
||
float TutorialServo::getMaxPulseWidthInMs() const | ||
{ | ||
return m_maxPulsewidthInMs; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good stuff |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check your indenting on this file, depending on your code editor you can probably select a setting that can format your code every time you save. If that's not an option make sure you go through and manually indent. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a section in our repo readme called "Clang-Format" that walks you through setting up the formatter we use. Please go through it and use it to format the training files. Our formatting style uses an indentation of 2 spaces so you'll know you've formatted your file properly if the indentation is 2 spaces. |
||
#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; | ||
} ; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "TutorialServo.h" | ||
#include "CANBus.h" | ||
#include "CANMsg.h" | ||
#include "mbed.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For your include statements:
|
||
|
||
int main() | ||
{ | ||
CANBus can(CAN1_RX, CAN1_TX, HWBRIDGE::ROVERCONFIG::ROVER_CANBUS_FREQUENCY); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these declarations are well done (since they are right at the top of your main function), make sure to declare your servo object as one of the first things you do |
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. be careful because these lines of code will never be run (your code will enter the while loop and never leave) |
||
//controls the servo using float value stored in CAN message. | ||
servoObject TutorialServo(PA_1, rangeOfMotion); | ||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,3 +92,7 @@ test-quadrature64cpr: | |
|
||
test-watchdog: | ||
- nucleo | ||
|
||
|
||
tutorial-servo-pot-control: | ||
- nucleo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only thing you need to include is TutorialServo.h, also we dont normally use "using namespace std"