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

Firmware Training- Rukshi Thiyagayogan #380

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions apps/test-aeat-8800/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
15 changes: 15 additions & 0 deletions apps/tutorial-servo-can-control/CMakeLists.txt
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)
45 changes: 45 additions & 0 deletions apps/tutorial-servo-can-control/TutorialServo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

#include "mbed.h"
#include "TutorialServo.h"

Copy link
Collaborator

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"

#include <iostream>
using namespace std;


TutorialServo::TutorialServo(PinName servoPin, float servoRangeInDegrees = 180.0,
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

https://en.cppreference.com/w/cpp/language/constructor

float minPulsewidthInMs = 1, float maxPulsewidthInMs = 2)
{
//declares this pin as pwm using pwmout.
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

looking good


float TutorialServo::getMinPulseWidthInMs() const
{
return m_minPulsewidthInMs;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice


float TutorialServo::getMaxPulseWidthInMs() const
{
return m_maxPulsewidthInMs;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

good stuff

24 changes: 24 additions & 0 deletions apps/tutorial-servo-can-control/TutorialServo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.
image

#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;
} ;
26 changes: 26 additions & 0 deletions apps/tutorial-servo-can-control/main.cpp
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"
Copy link
Collaborator

Choose a reason for hiding this comment

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

For your include statements:

  • you dont need to include mbed.h (its already included in the header file)
  • make sure you include hw_bridge.h


int main()
{
CANBus can(CAN1_RX, CAN1_TX, HWBRIDGE::ROVERCONFIG::ROVER_CANBUS_FREQUENCY);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);

}

6 changes: 6 additions & 0 deletions apps/tutorial-servo-pot-control/src/CMakeLists.txt
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}
18 changes: 18 additions & 0 deletions apps/tutorial-servo-pot-control/src/main.cpp
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);
}

}
4 changes: 4 additions & 0 deletions supported_build_configurations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ test-quadrature64cpr:

test-watchdog:
- nucleo


tutorial-servo-pot-control:
- nucleo