RoboidControl-cpp/Motor.cpp

32 lines
611 B
C++

#include "Motor.h"
#include <time.h>
#include <Arduino.h>
Motor::Motor() {
type = Type::Motor;
}
float Motor::GetSpeed() {
return this->currentSpeed;
}
void Motor::SetSpeed(float speed) {
this->currentSpeed = speed;
}
bool Motor::Drive(float distance) {
if (!this->driving) {
this->startTime = time(NULL);
this->targetDistance = abs(distance);
this->driving = true;
}
double duration = difftime(time(NULL), this->startTime);
if (duration >= this->targetDistance) {
this->driving = false;
return true;
}
SetSpeed(distance < 0 ? -1 : 1); // max speed
return false;
}