You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
2.3 KiB
C++
139 lines
2.3 KiB
C++
#include "AccurateMotor.h"
|
|
|
|
void AccurateMotorClass::SetCarRunning()
|
|
{
|
|
AccurateMotor.car_running = true;
|
|
}
|
|
|
|
void AccurateMotorClass::SetCarNotRunning()
|
|
{
|
|
AccurateMotor.car_running = false;
|
|
}
|
|
|
|
void AccurateMotorClass::Init()
|
|
{
|
|
car_running = false;
|
|
}
|
|
|
|
bool AccurateMotorClass::IsCarRunning()
|
|
{
|
|
return car_running;
|
|
}
|
|
|
|
void AccurateMotorClass::DelayUntilCarStop()
|
|
{
|
|
while (car_running)
|
|
delay(10);
|
|
delay(100); //缓冲,防止因惯性导致位置偏差
|
|
}
|
|
|
|
void AccurateMotorClass::ForceStop()
|
|
{
|
|
DCMotor.Stop();
|
|
SetCarNotRunning();
|
|
MsTimer2::stop();
|
|
}
|
|
|
|
void AccurateMotorClass::TurnLeft(uint8_t degree)
|
|
{
|
|
if (car_running)
|
|
return;
|
|
DCMotor.TurnLeft(car_turn_speed, car_turn_speed);
|
|
SetCarRunning();
|
|
MsTimer2::set(degree * turn_multplyer, []()
|
|
{
|
|
{
|
|
DCMotor.Stop();
|
|
SetCarNotRunning();
|
|
MsTimer2::stop();
|
|
}
|
|
});
|
|
MsTimer2::start();
|
|
}
|
|
|
|
void AccurateMotorClass::TurnRight(uint8_t degree)
|
|
{
|
|
if (car_running)
|
|
return;
|
|
DCMotor.TurnRight(car_turn_speed, car_turn_speed);
|
|
SetCarRunning();
|
|
MsTimer2::set(degree * turn_multplyer, []()
|
|
{
|
|
{
|
|
DCMotor.Stop();
|
|
SetCarNotRunning();
|
|
MsTimer2::stop();
|
|
}
|
|
});
|
|
MsTimer2::start();
|
|
}
|
|
|
|
void AccurateMotorClass::RunForward(uint8_t distence)
|
|
{
|
|
if (car_running)
|
|
return;
|
|
DCMotor.Go(car_speed);
|
|
SetCarRunning();
|
|
MsTimer2::set(distence * run_multplyer, []()
|
|
{
|
|
{
|
|
DCMotor.Stop();
|
|
SetCarNotRunning();
|
|
MsTimer2::stop();
|
|
}
|
|
});
|
|
MsTimer2::start();
|
|
}
|
|
|
|
void AccurateMotorClass::RunBackward(uint8_t distence)
|
|
{
|
|
if (car_running)
|
|
return;
|
|
DCMotor.Back(car_speed);
|
|
SetCarRunning();
|
|
MsTimer2::set(distence * run_multplyer, []()
|
|
{
|
|
{
|
|
DCMotor.Stop();
|
|
SetCarNotRunning();
|
|
MsTimer2::stop();
|
|
}
|
|
});
|
|
MsTimer2::start();
|
|
}
|
|
|
|
void AccurateMotorClass::RunForwardSlow(uint8_t distence)
|
|
{
|
|
if (car_running)
|
|
return;
|
|
DCMotor.Go(car_slow_speed);
|
|
SetCarRunning();
|
|
MsTimer2::set(distence * run_multplyer * (car_speed / car_slow_speed), []()
|
|
{
|
|
{
|
|
DCMotor.Stop();
|
|
SetCarNotRunning();
|
|
MsTimer2::stop();
|
|
}
|
|
});
|
|
MsTimer2::start();
|
|
}
|
|
|
|
void AccurateMotorClass::RunBackwardSlow(uint8_t distence)
|
|
{
|
|
if (car_running)
|
|
return;
|
|
DCMotor.Back(car_slow_speed);
|
|
SetCarRunning();
|
|
MsTimer2::set(distence * run_multplyer * (car_speed / car_slow_speed), []()
|
|
{
|
|
{
|
|
DCMotor.Stop();
|
|
SetCarNotRunning();
|
|
MsTimer2::stop();
|
|
}
|
|
});
|
|
MsTimer2::start();
|
|
}
|
|
|
|
AccurateMotorClass AccurateMotor; |