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 @@
"cSpell.words": [
"AVDD",
"Gpio",
"GPIOA",
"GPIOB",
"GPIOC",
"miosix"
]
}
......@@ -30,8 +30,6 @@ PWM::PWM(TIM_TypeDef* const timer, unsigned int pwmFrequency,
: timer(timer), pwmFrequency(pwmFrequency),
dutyCycleResolution(dutyCycleResolution)
{
// TODO: Enable the peripheral clock
// Erase the previous timer configuration
this->timer.reset();
......@@ -39,11 +37,7 @@ PWM::PWM(TIM_TypeDef* const timer, unsigned int pwmFrequency,
setTimerConfiguration();
}
PWM::~PWM()
{
// TODO: Disable the peripheral clock
timer.reset();
}
PWM::~PWM() { timer.reset(); }
void PWM::setFrequency(unsigned int pwmFrequency)
{
......
......@@ -35,13 +35,13 @@ namespace Boardcore
* when deleted) but no channels are enabled. This means that you will only need
* to use the channels without worrying about the underlying timer.
*
* The PWM driver accepts a the pointer to the peripheral registers of a timer
* and uses it as a 16bit general purpose timer. No checks are in place to this
* The PWM driver accepts a pointer to the peripheral registers of a timer and
* uses it as a 16bit general purpose timer. No checks are in place to this
* pointer, thus make sure to pass a proper value!
*
* 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,
* 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.
*
* Check out the following spread sheet to visually see how the timers registers
......
......@@ -22,45 +22,80 @@
#include <drivers/servo/Servo.h>
#include <miosix.h>
#include <scheduler/TaskScheduler.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 miosix;
using ps1 = Gpio<GPIOB_BASE, 4>;
using ps2 = Gpio<GPIOA_BASE, 7>;
using ps3 = Gpio<GPIOB_BASE, 8>;
GpioPin pin1(GPIOB_BASE, 4);
GpioPin pin2(GPIOA_BASE, 7);
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);
ps2::mode(Mode::ALTERNATE);
ps3::mode(Mode::ALTERNATE);
s1.setPosition(positions[lastPosition % 3]);
s2.setPosition(positions[(lastPosition + 1) % 3]);
s3.setPosition(positions[(lastPosition + 2) % 3]);
ps1::alternateFunction(2);
ps2::alternateFunction(2);
ps3::alternateFunction(3);
lastPosition++;
}
Servo s1(TIM3, TimerUtils::Channel::CHANNEL_1);
Servo s2(TIM3, TimerUtils::Channel::CHANNEL_2);
Servo s3(TIM10, TimerUtils::Channel::CHANNEL_1);
int main()
{
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();
s2.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]);
s2.setPosition(pos[1]);
s3.setPosition(pos[2]);
printf("Please enter a percentage for the servo ");
printf("or anything else to exit: ");
for (int i = 0;; i++)
while (std::cin >> percentage)
{
s1.setPosition(pos[i % 3]);
s2.setPosition(pos[(i + 1) % 3]);
s3.setPosition(pos[(i + 2) % 3]);
s4.setPosition(percentage / 100);
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