RoboidControl-cpp/Motor.cpp
Pascal Serrarens b2914e437b Improvements
2023-12-04 10:38:07 +01:00

26 lines
653 B
C++

#include "Motor.h"
#include <time.h>
Motor::Motor() { type = (int)Thing::UncontrolledMotorType; }
float Motor::GetSpeed() { return this->currentTargetSpeed; }
void Motor::SetSpeed(float targetSpeed) {
this->currentTargetSpeed = targetSpeed;
}
bool Motor::Drive(float distance) {
if (!this->driving) {
this->startTime = time(NULL);
this->targetDistance = distance >= 0 ? distance : -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;
}