#include "DifferentialDrive.h" #include "VectorAlgebra/FloatSingle.h" #include DifferentialDrive::DifferentialDrive(){}; DifferentialDrive::DifferentialDrive(Motor *leftMotor, Motor *rightMotor, float separation) { this->motorCount = 2; this->motors = new Motor *[2]; this->motors[0] = leftMotor; this->motors[1] = rightMotor; float distance = separation / 2; leftMotor->direction = Motor::Direction::CounterClockwise; leftMotor->position.angle = -90; leftMotor->position.distance = distance; rightMotor->direction = Motor::Direction::Clockwise; rightMotor->position.angle = 90; rightMotor->position.distance = distance; } void DifferentialDrive::SetTargetSpeeds(float leftSpeed, float rightSpeed) { for (unsigned int motorIx = 0; motorIx < this->motorCount; motorIx++) { Motor *motor = motors[motorIx]; if (motor == nullptr) continue; if (motor->type == Thing::UncontrolledMotorType) { float xPosition = motors[motorIx]->position.angle; if (xPosition < 0) motor->SetSpeed(leftSpeed); else if (xPosition > 0) motor->SetSpeed(rightSpeed); } }; } void DifferentialDrive::SetTwistSpeed(float forward, float yaw) { float leftSpeed = Float::Clamp(forward - yaw, -1, 1); float rightSpeed = Float::Clamp(forward + yaw, -1, 1); SetTargetSpeeds(leftSpeed, rightSpeed); } void DifferentialDrive::SetTwistSpeed(Vector2 linear, float yaw) { SetTwistSpeed(linear.y, yaw); } void DifferentialDrive::SetTwistSpeed(Vector3 linear, float yaw, float pitch, float roll) { SetTwistSpeed(linear.z, yaw); } Polar DifferentialDrive::GetVelocity() { Motor *leftMotor = motors[0]; Motor *rightMotor = motors[1]; float leftSpeed = leftMotor->GetSpeed(); float rightSpeed = rightMotor->GetSpeed(); float speed = (leftSpeed + rightSpeed) / 2; float direction = speed >= 0 ? 0.0F : 180.0F; float distance = fabsf(speed); Polar velocity = Polar(direction, distance); return velocity; } float DifferentialDrive::GetAngularVelocity() { Motor *leftMotor = motors[0]; Motor *rightMotor = motors[1]; float leftSpeed = leftMotor->GetSpeed(); float rightSpeed = rightMotor->GetSpeed(); float angularVelocity = leftSpeed - rightSpeed; return angularVelocity; }