37 lines
1.0 KiB
Dart
37 lines
1.0 KiB
Dart
class DiffSteerUseCase {
|
|
static const int polarityHigh = 0;
|
|
static const int polarityLow = 1;
|
|
|
|
double _turnSpeedScale = 1500 / 100;
|
|
double _forwardSpeedScale = 3000 / 100;
|
|
int _speedAmplLimit = 3000;
|
|
|
|
int _leftWheelDir = polarityHigh;
|
|
int _rightWheelDir = polarityHigh;
|
|
int _xDir = polarityHigh;
|
|
int _yDir = polarityHigh;
|
|
|
|
Map<String, int> calculate(int x, int y) {
|
|
int turnSpeed = _signOperator((x * _turnSpeedScale).toInt(), _xDir);
|
|
int forwardSpeed = _signOperator((y * _forwardSpeedScale).toInt(), _yDir);
|
|
|
|
// 倒车补偿
|
|
int adjustedTurn = (forwardSpeed >= 0 ? turnSpeed : -turnSpeed);
|
|
|
|
int left = _signOperator(forwardSpeed + adjustedTurn, _leftWheelDir);
|
|
int right = _signOperator(forwardSpeed - adjustedTurn, _rightWheelDir);
|
|
|
|
return {
|
|
'left': left.clamp(-_speedAmplLimit, _speedAmplLimit),
|
|
'right': right.clamp(-_speedAmplLimit, _speedAmplLimit),
|
|
};
|
|
}
|
|
|
|
int _signOperator(int num, int polarity) =>
|
|
polarity != polarityHigh ? -num : num;
|
|
|
|
|
|
|
|
|
|
}
|