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.
93 lines
1.4 KiB
C++
93 lines
1.4 KiB
C++
![]()
2 years ago
|
#include "AccurateMotor.h"
|
||
|
|
||
|
void AccurateMotorClass::Init()
|
||
|
{
|
||
|
car_running = false;
|
||
|
}
|
||
|
|
||
|
void AccurateMotorClass::SetCarRunning()
|
||
|
{
|
||
|
AccurateMotor.car_running = true;
|
||
|
}
|
||
|
|
||
|
void AccurateMotorClass::SetCarNotRunning()
|
||
|
{
|
||
|
AccurateMotor.car_running = false;
|
||
|
}
|
||
|
|
||
|
void AccurateMotorClass::TurnLeft(uint8_t degree)
|
||
|
{
|
||
|
if (car_running)
|
||
|
return;
|
||
|
DCMotor.TurnLeft(car_speed, car_speed);
|
||
|
SetCarRunning();
|
||
|
MsTimer2::set(degree * 50, []()
|
||
|
{
|
||
|
{
|
||
|
DCMotor.Stop();
|
||
|
SetCarNotRunning();
|
||
|
}
|
||
|
});
|
||
|
MsTimer2::start();
|
||
|
}
|
||
|
|
||
|
void AccurateMotorClass::TurnRight(uint8_t degree)
|
||
|
{
|
||
|
if (car_running)
|
||
|
return;
|
||
|
DCMotor.TurnRight(car_speed, car_speed);
|
||
|
SetCarRunning();
|
||
|
MsTimer2::set(degree * 50, []()
|
||
|
{
|
||
|
{
|
||
|
DCMotor.Stop();
|
||
|
SetCarNotRunning();
|
||
|
}
|
||
|
});
|
||
|
MsTimer2::start();
|
||
|
}
|
||
|
|
||
|
void AccurateMotorClass::RunForward(uint8_t distence)
|
||
|
{
|
||
|
if (car_running)
|
||
|
return;
|
||
|
DCMotor.Go(car_speed);
|
||
|
SetCarRunning();
|
||
|
MsTimer2::set(distence * 100, []()
|
||
|
{
|
||
|
{
|
||
|
DCMotor.Stop();
|
||
|
SetCarNotRunning();
|
||
|
}
|
||
|
});
|
||
|
MsTimer2::start();
|
||
|
}
|
||
|
|
||
|
bool AccurateMotorClass::IsCarRunning()
|
||
|
{
|
||
|
return car_running;
|
||
|
}
|
||
|
|
||
|
void AccurateMotorClass::DelayUntilCarStop()
|
||
|
{
|
||
|
while (car_running)
|
||
|
delay(10);
|
||
|
}
|
||
|
|
||
|
void AccurateMotorClass::RunBackward(uint8_t distence)
|
||
|
{
|
||
|
if (car_running)
|
||
|
return;
|
||
|
DCMotor.Back(car_speed);
|
||
|
SetCarRunning();
|
||
|
MsTimer2::set(distence * 100, []()
|
||
|
{
|
||
|
{
|
||
|
DCMotor.Stop();
|
||
|
SetCarNotRunning();
|
||
|
}
|
||
|
});
|
||
|
MsTimer2::start();
|
||
|
}
|
||
|
|
||
|
AccurateMotorClass AccurateMotor;
|