Skip to content
Snippets Groups Projects
Commit 4a56b658 authored by Alberto Nidasio's avatar Alberto Nidasio
Browse files

[Servo] Updated test entrypoint and some comments

parent 70c16a51
Branches
Tags
No related merge requests found
...@@ -103,7 +103,9 @@ ...@@ -103,7 +103,9 @@
"cSpell.words": [ "cSpell.words": [
"AVDD", "AVDD",
"Gpio", "Gpio",
"GPIOA",
"GPIOB", "GPIOB",
"GPIOC",
"miosix" "miosix"
] ]
} }
...@@ -30,8 +30,6 @@ PWM::PWM(TIM_TypeDef* const timer, unsigned int pwmFrequency, ...@@ -30,8 +30,6 @@ PWM::PWM(TIM_TypeDef* const timer, unsigned int pwmFrequency,
: timer(timer), pwmFrequency(pwmFrequency), : timer(timer), pwmFrequency(pwmFrequency),
dutyCycleResolution(dutyCycleResolution) dutyCycleResolution(dutyCycleResolution)
{ {
// TODO: Enable the peripheral clock
// Erase the previous timer configuration // Erase the previous timer configuration
this->timer.reset(); this->timer.reset();
...@@ -39,11 +37,7 @@ PWM::PWM(TIM_TypeDef* const timer, unsigned int pwmFrequency, ...@@ -39,11 +37,7 @@ PWM::PWM(TIM_TypeDef* const timer, unsigned int pwmFrequency,
setTimerConfiguration(); setTimerConfiguration();
} }
PWM::~PWM() PWM::~PWM() { timer.reset(); }
{
// TODO: Disable the peripheral clock
timer.reset();
}
void PWM::setFrequency(unsigned int pwmFrequency) void PWM::setFrequency(unsigned int pwmFrequency)
{ {
......
...@@ -35,13 +35,13 @@ namespace Boardcore ...@@ -35,13 +35,13 @@ namespace Boardcore
* when deleted) but no channels are enabled. This means that you will only need * when deleted) but no channels are enabled. This means that you will only need
* to use the channels without worrying about the underlying timer. * to use the channels without worrying about the underlying timer.
* *
* The PWM driver accepts a the pointer to the peripheral registers of a timer * The PWM driver accepts a pointer to the peripheral registers of a timer and
* and uses it as a 16bit general purpose timer. No checks are in place to this * uses it as a 16bit general purpose timer. No checks are in place to this
* pointer, thus make sure to pass a proper value! * pointer, thus make sure to pass a proper value!
* *
* Moreover, even 32bit general purpose timers are used as if they where 16bit. * Moreover, even 32bit general purpose timers are used as if they where 16bit.
* At the moment there is no need for further accuracy but if it ever will be, * At the moment there is no need for further accuracy but if it ever will be,
* exceeding this class is simble, just add a template parameter and pass it to * exceeding this class is simple, just add a template parameter and pass it to
* the GeneralPurposeTimer parameter. * the GeneralPurposeTimer parameter.
* *
* Check out the following spread sheet to visually see how the timers registers * Check out the following spread sheet to visually see how the timers registers
......
...@@ -22,45 +22,80 @@ ...@@ -22,45 +22,80 @@
#include <drivers/servo/Servo.h> #include <drivers/servo/Servo.h>
#include <miosix.h> #include <miosix.h>
#include <scheduler/TaskScheduler.h>
#include <utils/ClockUtils.h> #include <utils/ClockUtils.h>
#include <iostream>
/**
* The test uses 4 gpio pins:
* - PB4: TIM3-CH1
* - PA7: TIM3-CH2
* - PC8: TIM3-CH3
* - PB7: TIM4-CH2
*/
using namespace Boardcore; using namespace Boardcore;
using namespace miosix; using namespace miosix;
using ps1 = Gpio<GPIOB_BASE, 4>; GpioPin pin1(GPIOB_BASE, 4);
using ps2 = Gpio<GPIOA_BASE, 7>; GpioPin pin2(GPIOA_BASE, 7);
using ps3 = Gpio<GPIOB_BASE, 8>; GpioPin pin3(GPIOC_BASE, 8);
GpioPin pin4(GPIOB_BASE, 7);
int main() Servo s1(TIM3, TimerUtils::Channel::CHANNEL_1);
Servo s2(TIM3, TimerUtils::Channel::CHANNEL_2);
Servo s3(TIM3, TimerUtils::Channel::CHANNEL_3);
Servo s4(TIM4, TimerUtils::Channel::CHANNEL_2);
// Position to cycle through for the servo 1, 2 and 3
float positions[] = {0, 0.5, 1.0};
int lastPosition = 0;
void moveServo()
{ {
ps1::mode(Mode::ALTERNATE); s1.setPosition(positions[lastPosition % 3]);
ps2::mode(Mode::ALTERNATE); s2.setPosition(positions[(lastPosition + 1) % 3]);
ps3::mode(Mode::ALTERNATE); s3.setPosition(positions[(lastPosition + 2) % 3]);
ps1::alternateFunction(2); lastPosition++;
ps2::alternateFunction(2); }
ps3::alternateFunction(3);
Servo s1(TIM3, TimerUtils::Channel::CHANNEL_1); int main()
Servo s2(TIM3, TimerUtils::Channel::CHANNEL_2); {
Servo s3(TIM10, TimerUtils::Channel::CHANNEL_1); pin1.mode(Mode::ALTERNATE);
pin1.alternateFunction(2);
pin2.mode(Mode::ALTERNATE);
pin2.alternateFunction(2);
pin3.mode(Mode::ALTERNATE);
pin3.alternateFunction(2);
pin4.mode(Mode::ALTERNATE);
pin4.alternateFunction(2);
// Enable the timers
s1.enable(); s1.enable();
s2.enable(); s2.enable();
s3.enable(); s3.enable();
s4.enable();
// Start a periodic task to move the first three servos
TaskScheduler scheduler;
scheduler.addTask(&moveServo, 2 * 1000, 1);
scheduler.start();
float pos[] = {0, 0.5, 1.0}; // Control the fourth servo manually
float percentage;
s1.setPosition(pos[0]); printf("Please enter a percentage for the servo ");
s2.setPosition(pos[1]); printf("or anything else to exit: ");
s3.setPosition(pos[2]);
for (int i = 0;; i++) while (std::cin >> percentage)
{ {
s1.setPosition(pos[i % 3]); s4.setPosition(percentage / 100);
s2.setPosition(pos[(i + 1) % 3]);
s3.setPosition(pos[(i + 2) % 3]); printf("Position set to: %.2f%%\n", percentage);
Thread::sleep(2000); printf("Please enter a percentage for the servo ");
printf("or anything else to exit: ");
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment