This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
rc2007-soccer/source/Concept/Framework/modules/executor/navigator.c

286 lines
8 KiB
C
Raw Normal View History

2007-02-18 00:14:00 +00:00
#include "navigator.h"
//-----------------------------------------------------------------------------
void Navigator::Stop()
{
2007-02-19 17:15:02 +00:00
this->direction = EMPTY_FLOAT;
this->targetAngle = EMPTY_FLOAT;
this->targetX = EMPTY_FLOAT;
this->targetY = EMPTY_FLOAT;
2007-02-18 17:57:03 +00:00
this->robotSpeed = 0;
2007-02-18 00:14:00 +00:00
this->rotationSpeed = 0;
for(uint8 i = IO_ENGINE_START; i < IO_ENGINE_END; i++)
{
(parent->GetModule<Engine>(i))->SetSpeed(0);
(parent->GetModule<Engine>(i))->SetEnabled(true);
}
2007-02-19 20:57:03 +00:00
}
2007-02-19 22:02:01 +00:00
//-----------------------------------------------------------------------------
void Navigator::Rotate(float rotationSpeed)
2007-02-19 20:57:03 +00:00
{
2007-02-19 22:02:01 +00:00
this->rotationSpeed = min(fabs(rotationSpeed), 255.0f) * sign(rotationSpeed);
2007-02-19 17:15:02 +00:00
this->direction = EMPTY_FLOAT;
this->targetAngle = EMPTY_FLOAT;
this->targetX = EMPTY_FLOAT;
this->targetY = EMPTY_FLOAT;
2007-02-18 17:57:03 +00:00
this->robotSpeed = 0;
2007-02-18 00:14:00 +00:00
for(uint8 i = IO_ENGINE_START; i < IO_ENGINE_END; i++)
{
(parent->GetModule<Engine>(i))->SetSpeed(rotationSpeed);
(parent->GetModule<Engine>(i))->SetEnabled(true);
}
2007-02-19 22:02:01 +00:00
}
2007-02-18 17:57:03 +00:00
//-----------------------------------------------------------------------------
void Navigator::Drive(float newDirection, float newAngle, float newSpeed, float rotationSpeed)
{
this->rotationSpeed = min(rotationSpeed, 255.0f);
2007-02-19 22:02:01 +00:00
this->direction = newDirection * PI / 180.0f;
this->targetAngle = newAngle * PI / 180.0f;
2007-02-19 17:15:02 +00:00
this->targetX = EMPTY_FLOAT;
this->targetY = EMPTY_FLOAT;
2007-02-18 17:57:03 +00:00
this->robotSpeed = newSpeed;
2007-02-19 17:15:02 +00:00
if(targetAngle == EMPTY_FLOAT)
2007-02-18 17:57:03 +00:00
{
2007-02-19 17:15:02 +00:00
rotationSpeed = 0;
2007-02-18 17:57:03 +00:00
}
else
{
2007-02-19 17:15:02 +00:00
rotationSpeed = fabs(rotationSpeed) * sign(PI - (targetAngle - (parent->GetModule<Position_Tracker>(IO_POSITION_TRACKER_MAIN))->GetOrientation()));
2007-02-18 17:57:03 +00:00
}
2007-02-19 17:15:02 +00:00
CalculateEngines();
//dings,,,
/*float xDrive = cos(direction + PI / 6.0f);
float yDrive = sin(direction + PI / 6.0f);
float vLeft = xDrive;
float vBack = (-xDrive + sqrt(3) * yDrive) / 2.0f;
float vRight = (-xDrive - sqrt(3) * yDrive) / 2.0f;
vLeft = vLeft * this->robotSpeed + rotationSpeed;
vBack = vBack * this->robotSpeed + rotationSpeed;
vRight = vRight * this->robotSpeed + rotationSpeed;
Engine* curEngine = parent->GetModule<Engine>(IO_ENGINE_DRIVE_LEFT);
curEngine->SetSpeed(vLeft);
curEngine->SetEnabled(true);
curEngine = parent->GetModule<Engine>(IO_ENGINE_DRIVE_BACK);
curEngine->SetSpeed(vBack);
curEngine->SetEnabled(true);
curEngine = parent->GetModule<Engine>(IO_ENGINE_DRIVE_RIGHT);
curEngine->SetSpeed(vRight);
curEngine->SetEnabled(true);
curEngine = NULL;*/
2007-02-19 22:02:01 +00:00
}
2007-02-18 17:57:03 +00:00
//-----------------------------------------------------------------------------
void Navigator::DriveTo(float newX, float newY, float newAngle, float newSpeed, float rotationSpeed)
{
if(newX < 0 || newY < 0) return;
Position_Tracker* locationeer = parent->GetModule<Position_Tracker>(IO_POSITION_TRACKER_MAIN);
this->rotationSpeed = min(rotationSpeed, 255.0f);
this->targetX = newX;
this->targetY = newY;
this->direction = direction2d(locationeer->GetPositionX(), locationeer->GetPositionY(), targetX, targetY);;
2007-02-19 22:02:01 +00:00
this->targetAngle = newAngle * PI / 180.0f;
2007-02-18 17:57:03 +00:00
this->robotSpeed = newSpeed;
if(targetAngle - locationeer->GetOrientation() > PI)
{
if(rotationSpeed > 0)
{
rotationSpeed = -rotationSpeed;
}
}
else
{
if(rotationSpeed < 0)
{
rotationSpeed = -rotationSpeed;
}
}
2007-02-19 17:15:02 +00:00
CalculateDirection();
2007-02-19 22:02:01 +00:00
}
2007-02-18 00:14:00 +00:00
//-----------------------------------------------------------------------------
void Navigator::Update()
{
Position_Tracker* locationeer = parent->GetModule<Position_Tracker>(IO_POSITION_TRACKER_MAIN);
2007-02-19 17:15:02 +00:00
bool targetReached = false;
bool targetAngleReached = false;
2007-02-18 00:14:00 +00:00
2007-02-19 17:15:02 +00:00
if(HasTarget() && distance2d(targetX, targetY, locationeer->GetPositionX(), locationeer->GetPositionY()) < 1.0f)
2007-02-18 00:14:00 +00:00
{
2007-02-19 17:15:02 +00:00
targetX = EMPTY_FLOAT;
targetY = EMPTY_FLOAT;
direction = EMPTY_FLOAT;
robotSpeed = 0;
2007-02-18 00:14:00 +00:00
2007-02-19 17:15:02 +00:00
targetReached = true;
2007-02-18 00:14:00 +00:00
}
2007-02-19 17:15:02 +00:00
if(targetAngle != EMPTY_FLOAT && fabs(targetAngle - locationeer->GetOrientation()) < 0.1f)
2007-02-18 00:14:00 +00:00
{
2007-02-19 17:15:02 +00:00
targetAngle = EMPTY_FLOAT;
rotationSpeed = 0;
2007-02-18 00:14:00 +00:00
2007-02-19 17:15:02 +00:00
targetAngleReached = true;
2007-02-18 00:14:00 +00:00
}
2007-02-19 17:15:02 +00:00
if(targetReached && targetAngleReached)
2007-02-18 00:14:00 +00:00
{
2007-02-19 17:15:02 +00:00
Stop();
}
else if(targetReached || targetAngleReached)
{
CalculateDirection();
2007-02-18 00:14:00 +00:00
}
2007-02-19 17:15:02 +00:00
if(!(correctionCountdown--))
2007-02-18 00:14:00 +00:00
{
2007-02-19 17:15:02 +00:00
CalculateDirection();
2007-02-18 00:14:00 +00:00
}
2007-02-19 22:02:01 +00:00
}
2007-02-19 17:15:02 +00:00
//-----------------------------------------------------------------------------
void Navigator::CalculateDirection()
{
correctionCountdown = CYCLES_PER_CORRECTION;
if(targetAngle == EMPTY_FLOAT && !HasTarget()) return;
Position_Tracker* locationeer = parent->GetModule<Position_Tracker>(IO_POSITION_TRACKER_MAIN);
if(HasTarget())
2007-02-18 00:14:00 +00:00
{
2007-02-19 17:15:02 +00:00
direction = direction2d(locationeer->GetPositionX(), locationeer->GetPositionY(), targetX, targetY);
2007-02-18 00:14:00 +00:00
}
2007-02-19 17:15:02 +00:00
if(targetAngle != EMPTY_FLOAT)
2007-02-18 00:14:00 +00:00
{
2007-02-19 17:15:02 +00:00
rotationSpeed = fabs(rotationSpeed) * sign(PI - (targetAngle - locationeer->GetOrientation()));
}
2007-02-18 00:14:00 +00:00
2007-02-19 17:15:02 +00:00
CalculateEngines();
2007-02-19 22:02:01 +00:00
}
2007-02-19 17:15:02 +00:00
//-----------------------------------------------------------------------------
void Navigator::CalculateEngines()
{
Position_Tracker* locationeer = parent->GetModule<Position_Tracker>(IO_POSITION_TRACKER_MAIN);
2007-02-19 17:15:02 +00:00
if(direction != EMPTY_FLOAT)
{
float relativeDirection = this->direction - locationeer->GetOrientation();
2007-02-18 00:14:00 +00:00
2007-02-19 17:15:02 +00:00
float xDrive = cos(relativeDirection + PI / 6.0f);
float yDrive = sin(relativeDirection + PI / 6.0f);
2007-02-18 00:14:00 +00:00
2007-02-19 17:15:02 +00:00
float vLeft = xDrive;
float vBack = (-xDrive + sqrt(3) * yDrive) / 2.0f;
float vRight = (-xDrive - sqrt(3) * yDrive) / 2.0f;
2007-02-18 00:14:00 +00:00
/*float speedCorrection = 1.0f;
2007-02-18 00:14:00 +00:00
2007-02-18 17:57:03 +00:00
float maxEngineSpeed = 255.0f;
float minEngineSpeed = -255.0f;
2007-02-18 00:14:00 +00:00
2007-02-19 17:15:02 +00:00
float maxSingleSpeed = max(max(fabs(vLeft), fabs(vBack)), fabs(vRight));
2007-02-18 00:14:00 +00:00
float calcSpeed = 1.0f;//sqrt(vLeft * vLeft - vLeft * vRight + vRight * vRight +
// vBack * (vBack + vLeft + vRight));
2007-02-18 17:57:03 +00:00
if(calcSpeed != 1.0f)
2007-02-18 00:14:00 +00:00
{
2007-02-18 17:57:03 +00:00
speedCorrection = 1.0f / calcSpeed;
2007-02-18 00:14:00 +00:00
}
2007-02-18 17:57:03 +00:00
float maxOverallSpeed = robotSpeed * maxSingleSpeed * speedCorrection;
if(maxOverallSpeed > maxEngineSpeed)
{
robotSpeed = maxEngineSpeed / (maxSingleSpeed * speedCorrection);
}*/
2007-02-18 17:57:03 +00:00
2007-02-18 00:14:00 +00:00
vLeft = vLeft * this->robotSpeed;// * speedCorrection;
vBack = vBack * this->robotSpeed;// * speedCorrection;
vRight = vRight * this->robotSpeed;// * speedCorrection;
2007-02-18 00:14:00 +00:00
/*maxSingleSpeed = max(max(fabs(vLeft), fabs(vBack)), fabs(vRight));
2007-02-18 17:57:03 +00:00
float minSingleSpeed = min(min(vLeft, vBack), vRight);
2007-02-18 00:14:00 +00:00
2007-02-19 17:15:02 +00:00
if(rotationSpeed)
2007-02-18 17:57:03 +00:00
{
if(this->rotationSpeed > 0)
{
if(maxEngineSpeed - maxSingleSpeed < this->rotationSpeed)
{
vLeft += maxEngineSpeed - maxSingleSpeed;
vBack += maxEngineSpeed - maxSingleSpeed;
vRight += maxEngineSpeed - maxSingleSpeed;
}
else
{
vLeft += this->rotationSpeed;
vBack += this->rotationSpeed;
vRight += this->rotationSpeed;
}
}
else
{
if((minEngineSpeed - minSingleSpeed) < this->rotationSpeed)
{
vLeft -= minEngineSpeed - minSingleSpeed;
vBack -= minEngineSpeed - minSingleSpeed;
vRight -= minEngineSpeed - minSingleSpeed;
}
else
{
vLeft -= this->rotationSpeed;
vBack -= this->rotationSpeed;
vRight -= this->rotationSpeed;
}
}
}*/
2007-02-18 00:14:00 +00:00
2007-02-18 17:57:03 +00:00
Engine* curEngine = parent->GetModule<Engine>(IO_ENGINE_DRIVE_LEFT);
curEngine->SetSpeed(vLeft);
curEngine->SetEnabled(true);
curEngine = parent->GetModule<Engine>(IO_ENGINE_DRIVE_BACK);
curEngine->SetSpeed(vBack);
curEngine->SetEnabled(true);
curEngine = parent->GetModule<Engine>(IO_ENGINE_DRIVE_RIGHT);
curEngine->SetSpeed(vRight);
curEngine->SetEnabled(true);
curEngine = NULL;
2007-02-18 00:14:00 +00:00
}
2007-02-19 17:15:02 +00:00
else if(rotationSpeed)
2007-02-18 00:14:00 +00:00
{
2007-02-18 17:57:03 +00:00
Engine* curEngine = parent->GetModule<Engine>(IO_ENGINE_DRIVE_LEFT);
curEngine->SetSpeed(this->rotationSpeed);
curEngine->SetEnabled(true);
curEngine = parent->GetModule<Engine>(IO_ENGINE_DRIVE_BACK);
curEngine->SetSpeed(this->rotationSpeed);
curEngine->SetEnabled(true);
curEngine = parent->GetModule<Engine>(IO_ENGINE_DRIVE_RIGHT);
curEngine->SetSpeed(this->rotationSpeed);
curEngine->SetEnabled(true);
curEngine = NULL;
}
2007-02-18 00:14:00 +00:00
else
{
2007-02-18 17:57:03 +00:00
Stop();
2007-02-18 00:14:00 +00:00
}
2007-02-19 22:02:01 +00:00
}