Skip to content

Commit

Permalink
Setting GUI for Navigation Check
Browse files Browse the repository at this point in the history
<TBC>
  • Loading branch information
BrenoOR committed Mar 3, 2023
1 parent bb910f6 commit 7a43eac
Show file tree
Hide file tree
Showing 22 changed files with 447 additions and 30 deletions.
8 changes: 8 additions & 0 deletions src/entities/player/behavior/behavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ void Behavior::setPlayer(Player *player) {
}
}

void Behavior::setGUI(GUI *gui) {
_gui = gui;
}

void Behavior::runBehavior() {
// Check if behavior have at least one skill
if(_skillList.size() == 0) {
Expand Down Expand Up @@ -118,6 +122,10 @@ Player* Behavior::player() {
return _player;
}

GUI* Behavior::gui() {
return _gui;
}

WorldMap* Behavior::getWorldMap() {
if(_worldMap == nullptr) {
spdlog::error("[{}] WorldMap with nullptr value.", name().toStdString());
Expand Down
8 changes: 8 additions & 0 deletions src/entities/player/behavior/behavior.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define BEHAVIOR_H

#include <src/entities/basesuassuna.h>
#include <src/gui/gui.h>

class Behavior
{
Expand All @@ -41,6 +42,9 @@ class Behavior
void initialize(WorldMap* worldMap);
void setPlayer(Player* player);

void setGUI(GUI* gui);


// Method to run in role
void runBehavior();

Expand All @@ -53,6 +57,8 @@ class Behavior
WorldMap* getWorldMap();
Player* player();

GUI* gui();

private:

// Virtual implementation in inherited classes
Expand All @@ -71,6 +77,8 @@ class Behavior

// Initialize control
bool _initialized;

GUI *_gui;
};

#endif // BEHAVIOR_H
1 change: 1 addition & 0 deletions src/entities/player/behavior/default/behavior_default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void Behavior_Default::run() {
// Move the robot to other positions and see if it moves
// to the target position using the best face no move
_skill_goTo->setTargetPosition(_targetPosition);
_skill_goTo->setGUI(gui());
runSkill(SKILL_GOTO);
} else {
// Move the robot to positions where you can see both spin directions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void Behavior_Intercept::run() {
}

_skill_goTo->setTargetPosition(_interceptPosition);
_skill_goTo->setGUI(gui());
runSkill(SKILL_GOTO);
}

Expand Down
1 change: 1 addition & 0 deletions src/entities/player/behavior/move/behavior_move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void Behavior_Move::run() {
switch(_movementType) {
case MOVEMENT::MOVE:
_skill_goTo->setTargetPosition(_targetPosition);
_skill_goTo->setGUI(gui());
runSkill(SKILL_GOTO);
break;
case MOVEMENT::ROTATE:
Expand Down
5 changes: 5 additions & 0 deletions src/entities/player/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ void Player::setRole(Role *role) {
_mutexRole.unlock();
}

void Player::setGUI(GUI *gui) {
_gui = gui;
}

void Player::updatePlayer(Common::Types::Object playerData) {
setPosition(playerData.getPosition());
setVelocity(playerData.getVelocity());
Expand Down Expand Up @@ -111,6 +115,7 @@ void Player::loop() {
_playerRole->initialize(_worldMap);
}
_playerRole->setPlayer(this);
_playerRole->setGUI(_gui);
_playerRole->runRole();
}
else {
Expand Down
6 changes: 6 additions & 0 deletions src/entities/player/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include <src/entities/basesuassuna.h>
#include <src/common/types/robot/robot.h>
#include <src/entities/player/navigation/navigation.h>
#include <src/gui/gui.h>

class Player : public Suassuna::Robot, public Threaded::Entity
{
Expand All @@ -39,6 +41,7 @@ class Player : public Suassuna::Robot, public Threaded::Entity
* \param role
*/
void setRole(Role* role);
void setGUI(GUI* gui);

protected:
// Friend classes for update and internal control of the player
Expand Down Expand Up @@ -92,6 +95,9 @@ class Player : public Suassuna::Robot, public Threaded::Entity
// Idle control
Utils::Timer _idleTimer;
bool _firstIt;

GUI *_gui;

};

#endif // PLAYER_H
3 changes: 3 additions & 0 deletions src/entities/player/role/default/role_default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,21 @@ void Role_Default::run() {
// Move the robot to other positions and see if it moves to the target position using the best face no move
_behavior_move->setMovementType(Behavior_Move::MOVEMENT::MOVE);
_behavior_move->setTargetPosition(getWorldMap()->getBall().getPosition());
_behavior_move->setGUI(gui());
setBehavior(BEHAVIOR_MOVE);
} else if (player()->robotId() == 1) {
// Move the robot to other positions and see if it rotate to the target position using the smarter rotation diretion route
_behavior_move->setMovementType(Behavior_Move::MOVEMENT::ROTATE);
_behavior_move->setTargetAngle((getWorldMap()->getBall().getPosition() - player()->getPosition()).angle());
_behavior_move->setGUI(gui());
setBehavior(BEHAVIOR_MOVE);
} else {
// Move the robot to positions where you can see both spin directions
_behavior_intercept->setInterceptSegment(Geometry::LineSegment(Geometry::Vector2D(0.4f, -0.5f), Geometry::Vector2D(0.4f, 0.5f)));
_behavior_intercept->setObjectPosition(getWorldMap()->getBall().getPosition());
_behavior_intercept->setObjectVelocity(getWorldMap()->getBall().getVelocity());
_behavior_intercept->setIntersectionAccuracy(0.3f);
_behavior_intercept->setGUI(gui());
setBehavior(BEHAVIOR_INTERCEPT);
}
}
8 changes: 8 additions & 0 deletions src/entities/player/role/role.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ void Role::setPlayer(Player *player) {
_player = player;
}

void Role::setGUI(GUI *gui) {
_gui = gui;
}

QString Role::actualBehaviorName() {
if(_actualBehavior == nullptr) {
return "NONE";
Expand Down Expand Up @@ -133,3 +137,7 @@ WorldMap* Role::getWorldMap() {

return nullptr;
}

GUI* Role::gui() {
return _gui;
}
7 changes: 7 additions & 0 deletions src/entities/player/role/role.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define ROLE_H

#include <src/entities/basesuassuna.h>
#include <src/gui/gui.h>

class Role : public QObject
{
Expand All @@ -42,6 +43,8 @@ class Role : public QObject
void initialize(WorldMap *worldMap);
void setPlayer(Player *player);

void setGUI(GUI *gui);

// Actual Behavior name
QString actualBehaviorName();

Expand All @@ -57,6 +60,8 @@ class Role : public QObject
Player* player();
WorldMap* getWorldMap();

GUI* gui();

private:
// Virtual implementation in inherited classes
virtual void configure() = 0;
Expand All @@ -74,6 +79,8 @@ class Role : public QObject

// Initialize control
bool _initialized;

GUI *_gui;
};

#endif // ROLE_H
27 changes: 23 additions & 4 deletions src/entities/player/skill/goto/skill_goto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ float smallestAngleDiff(float target, float source) {
}

void Skill_GoTo::run() {
nav()->setGUI(gui());

float Kp = 20.0f;
float Ki = 0.0f;
float Kd = 2.5f;
Expand All @@ -55,16 +57,22 @@ void Skill_GoTo::run() {
bool reversed = false;

float robotAngle = player()->getOrientation().value();

//float angleToTarget = (_targetPosition - player()->getPosition()).angle();
float angleToTarget = atan2(_targetPosition.y() - player()->getPosition().y(),
_targetPosition.x() - player()->getPosition().x());

float angError = smallestAngleDiff(robotAngle, angleToTarget);
// float angleToTarget = atan2(_targetPosition.y() - player()->getPosition().y(),
// _targetPosition.x() - player()->getPosition().x());

// Navigation output here
// inputs: curr pos, goal pos, to avoid opponents, to avoid teammates, to avoid ball;
Geometry::Angle angleToTarget = nav()->angleToTarget(player()->getPosition(), _targetPosition, true, true, false);

float angError = smallestAngleDiff(robotAngle, angleToTarget.value());
if(fabs(angError) > M_PI/2.0 + M_PI/20.0) {
reversed = true;
robotAngle = Geometry::Angle(robotAngle + M_PI).value();
// Calculates the error and reverses the front of the robot
angError = smallestAngleDiff(robotAngle, angleToTarget);
angError = smallestAngleDiff(robotAngle, angleToTarget.value());
}

float motorSpeed = (Kp*angError) + (Ki*cumulativeError) + (Kd * (angError - lastError));// + 0.2 * sumErr;
Expand Down Expand Up @@ -96,6 +104,17 @@ void Skill_GoTo::run() {
}

setWheelsSpeed(leftMotorSpeed, rightMotorSpeed);

if (gui() != nullptr) {
QList<Geometry::Vector2D> interestPoints;
interestPoints.push_back(_targetPosition);
QVector<Geometry::Vector2D> univector;
Geometry::Vector2D projection = Geometry::Vector2D(angleToTarget, 1.0f);
projection = Geometry::Vector2D(player()->getPosition().x() + projection.x(), player()->getPosition().y() + projection.y());
univector.push_back(projection);
gui()->updateInterestPoints(player()->identifier().robotid(), interestPoints);
gui()->updateVectorsAngles(player()->identifier().robotid(), univector);
}
}

void Skill_GoTo::setTargetPosition(const Geometry::Vector2D &targetPosition) {
Expand Down
1 change: 1 addition & 0 deletions src/entities/player/skill/goto/skill_goto.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Skill_GoTo : public Skill
// Internal
Geometry::Vector2D _targetPosition;
bool _isSimulation;
void sendTargetPosition(const QList<Geometry::Vector2D>& targets);
};

#endif // SKILL_GOTO_H
16 changes: 16 additions & 0 deletions src/entities/player/skill/skill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Skill::Skill() {
_worldMap = nullptr;
_player = nullptr;
_initialized = false;
_nav = nullptr;
_gui = nullptr;
}

bool Skill::isInitialized() {
Expand All @@ -48,14 +50,28 @@ void Skill::setPlayer(Player *player) {
_player = player;
}

void Skill::setGUI(GUI *gui) {
_gui = gui;
}

void Skill::runSkill() {
_nav = new Navigation(_player->identifier().robotid(), _worldMap);
_nav->setGUI(gui());
run();
}

Player* Skill::player() {
return _player;
}

Navigation* Skill::nav() {
return _nav;
}

GUI* Skill::gui() {
return _gui;
}

void Skill::setWheelsSpeed(const float &wheelLeft, const float &wheelRight) {
_player->setWheelsSpeed(wheelLeft, wheelRight);
}
Expand Down
10 changes: 10 additions & 0 deletions src/entities/player/skill/skill.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#define SKILL_H

#include <src/entities/basesuassuna.h>
#include <src/entities/player/navigation/navigation.h>
#include <src/gui/gui.h>
#include <math.h>

class Skill
Expand All @@ -41,13 +43,18 @@ class Skill
void initialize(WorldMap* worldMap);
void setPlayer(Player *player);

void setGUI(GUI *gui);

// Run
void runSkill();

protected:
// Player and worldmap getters
WorldMap* getWorldMap();
Player* player();
Navigation* nav();

GUI* gui();

// Interface with Player
void setWheelsSpeed(const float& wheelLeft, const float& wheelRight);
Expand All @@ -65,6 +72,9 @@ class Skill

// Internal control
bool _initialized;

Navigation *_nav;
GUI *_gui;
};

#endif // SKILL_H
30 changes: 29 additions & 1 deletion src/entities/worldmap/worldmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include <src/common/constants/constants.h>
#include <src/common/utils/utils.h>
#include <src/entities/coach/team/team.h>

WorldMap::WorldMap() {
// Setup default field
Expand All @@ -52,6 +51,35 @@ Common::Types::Object WorldMap::getBall() {
return ball;
}

QPair<Geometry::Vector2D, bool> WorldMap::getPlayerPosition(const Common::Enums::Color& teamColor, quint8 playerId) {
_mutex.lockForWrite();
QList<Suassuna::Robot*> team = _teams[teamColor]->getPlayers();
_mutex.unlock();

// Return True in pair if robot found
for (Suassuna::Robot* player : team) {
if (player->identifier().robotid() == playerId) {
return QPair(player->getPosition(), true);
}
}

return QPair(Geometry::Vector2D(), false);
}

QList<quint8> WorldMap::getPlayersIds(const Common::Enums::Color &teamColor) {
QList<quint8> playersIds;

_mutex.lockForWrite();
QList<Suassuna::Robot*> team = _teams[teamColor]->getPlayers();
_mutex.unlock();

for (auto robot : team) {
playersIds.append(robot->identifier().robotid());
}

return playersIds;
}

void WorldMap::updatePlayers(const QList<Armorial::Robot>& robots) {
_mutex.lockForWrite();
for(const auto& r : robots) {
Expand Down
Loading

0 comments on commit 7a43eac

Please sign in to comment.