Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: QEI Motordriver ros_lib_melodic
motor/Motor.cpp
- Committer:
- florine_van
- Date:
- 2019-11-13
- Revision:
- 11:35809512ec11
- Parent:
- 6:858a5116688e
- Child:
- 12:817da876ae2f
File content as of revision 11:35809512ec11:
#include "mbed.h"
#include "Motor.h"
#include "QEI.h"
///////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////
Motor::Motor(PinName in1, PinName in2, PinName pwm, QEI wheel)
: in1(in1), in2(in2), pwm(pwm), wheel(wheel)
{
this->pwm.period_ms(10);
}
///////////////////////////////////////////////////////////////////
// Public methods
///////////////////////////////////////////////////////////////////
void Motor::moveForward(int pulse)
{
moveDistance(-0.5, pulse, 1);
}
void Motor::moveBackward(int pulse)
{
moveDistance(0.5, pulse, -1);
}
void Motor::moveLeft(float speed)
{
moveDistance(speed, 3000, -1);
}
void Motor::moveRight(float speed)
{
moveDistance(speed, 3000, 1);
}
void Motor::setSpeed(float speed)
{
pwm.write(speed);
}
void Motor::moveDistance(float speed, int pulse, int pulse_direction)
{
// Variables to allow for any pulse reading
float initial_pulse_reading = wheel.getPulses();
float current_pulse_reading = wheel.getPulses();
// - is forward on our robot
setSpeed(speed);
// loop for moving forward
if (pulse_direction > 0)
{
while(current_pulse_reading <= (initial_pulse_reading + pulse * pulse_direction))
{
current_pulse_reading = wheel.getPulses();
}
}
else
{
while(current_pulse_reading >= (initial_pulse_reading + pulse * pulse_direction))
{
current_pulse_reading = wheel.getPulses();
}
}
setSpeed(0);
}
void Motor::move(float speed)
{
// - is forward on our robot
setSpeed(speed);
}