1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* @file CleanRoleDecision.h
*
* @author <a href="mailto:schahin.tofangchi@hu-berlin.de">Schahin Tofangchi</a>
*/

#ifndef _CleanRoleDecision_h_
#define _CleanRoleDecision_h_

#include <functional>

#include <ModuleFramework/Module.h>

#include "Representations/Modeling/TeamMessage.h"
#include "Representations/Infrastructure/FrameInfo.h"
#include "Representations/Infrastructure/FieldInfo.h"
#include "Representations/Modeling/PlayerInfo.h"
#include "Representations/Modeling/RoleDecisionModel.h"
#include "Representations/Modeling/SoccerStrategy.h"
#include "Representations/Modeling/TeamMessagePlayersState.h"
#include "Representations/Modeling/TeamBallModel.h"
#include "Representations/Modeling/BodyState.h"
#include "Representations/Modeling/BallModel.h"

#include <Tools/DataStructures/ParameterList.h>
#include "Tools/Debug/DebugParameterList.h"
#include "Tools/Debug/DebugPlot.h"
#include "Tools/Debug/DebugRequest.h"

//////////////////// BEGIN MODULE INTERFACE DECLARATION ////////////////////

BEGIN_DECLARE_MODULE(CleanRoleDecision)
  PROVIDE(DebugRequest)
  PROVIDE(DebugPlot)
  PROVIDE(DebugDrawings)
  PROVIDE(DebugParameterList)

  REQUIRE(FrameInfo)
  REQUIRE(FieldInfo)
  REQUIRE(PlayerInfo)
  REQUIRE(SoccerStrategy)
  REQUIRE(TeamMessage)
  REQUIRE(TeamMessagePlayersState)
  REQUIRE(BodyState)
  REQUIRE(BallModel)
  REQUIRE(TeamBallModel)

  PROVIDE(RoleDecisionModel)
END_DECLARE_MODULE(CleanRoleDecision);

//////////////////// END MODULE INTERFACE DECLARATION //////////////////////

class CleanRoleDecision : public CleanRoleDecisionBase
{
public: 

  CleanRoleDecision();
  virtual ~CleanRoleDecision();

  /** executes the module */
  virtual void execute();

  void computeStrikers();
  
protected:
  bool amIactive() {
      return getBodyState().fall_down_state == BodyState::upright
          && getPlayerInfo().robotState != PlayerInfo::penalized
          && getBallModel().valid
          && (getFrameInfo().getTimeSince(getBallModel().getFrameInfoWhenBallWasSeen().getTime()) < params.maxBallLostTime + myStrikerBonusTime());
  }

  int myStrikerBonusTime() {
      return getRoleDecisionModel().firstStriker == getPlayerInfo().playerNumber ? params.strikerBonusTime:0;
  }

  /**
   * @brief Selects the first/second striker out of the possible striker based on their player number.
   *        The smallest player number gets the first striker role and second smallest the second striker role.
   *        If theres someone faster to the ball, this player dont "wantsToBeStriker".
   * @param possible_striker map of player number and their time to ball
   * @param ownTimeToBall the time to ball of this player
   */
  void strikerSelectionByNumber(std::map<unsigned int, unsigned int> &possible_striker, double &ownTimeToBall);

  /**
   * @brief Selects the first/second striker out of the possible striker based on their time to the ball.
   *        The fastest player to the ball gets the first striker role and the seceond fastest gets the second striker role.
   *        If theres someone faster to the ball, this player dont "wantsToBeStriker".
   * @param possible_striker map of player number and their time to ball
   * @param ownTimeToBall the time to ball of this player
   */
  void strikerSelectionByTime(std::map<unsigned int, unsigned int> &possible_striker, double &ownTimeToBall);

  /**
   * @brief Selects the first/second striker out of the possible striker based on their time to the ball.
   *        The fastest player to the ball gets the first striker role and the seceond fastest gets the second striker role.
   *        BUT, if the goalie is one of the possible striker, he is set to the first striker! And if someone is faster just
   *        for a small threshold (strikerSelectionDiffThreshold), the one with the smaller number is prefered.
   *        If theres someone faster to the ball, this player dont "wantsToBeStriker".
   * @param possible_striker map of player number and their time to ball
   * @param ownTimeToBall the time to ball of this player
   */
  void strikerSelectionByTimeExceptGoalie(std::map<unsigned int, unsigned int> &possible_striker, double &ownTimeToBall);

  /**
   * @brief Selects the first/second striker out of the possible striker based on their time to the ball.
   *        The fastest player to the ball gets the first striker role and the seceond fastest gets the second striker role, if the
   *        ball distance from the first and the "second" are greater than a given min distance.
   *        BUT, if the goalie is one of the possible striker, he is set to the first striker! And if someone is faster just
   *        for a small threshold (strikerSelectionDiffThreshold), the one with the smaller number is prefered.
   *        If there is someone faster to the ball, this player dont "wantsToBeStriker".
   * @param possible_striker map of player number and their time to ball
   * @param ownTimeToBall the time to ball of this player
   */
  void strikerSelectionByTimeExceptGoalieWithBallCompare(std::map<unsigned int, unsigned int> &possible_striker, double &ownTimeToBall);

  class Parameters: public ParameterList
  {
  public:
    Parameters(): ParameterList("CleanRoleDecision")
    {
      PARAMETER_REGISTER(strikerBonusTime) = 4000;
      PARAMETER_REGISTER(maxBallLostTime) = 1000;
      PARAMETER_REGISTER(strikerSelectionDiffThreshold) = 500; // ms
      PARAMETER_REGISTER(firstSecondStrikerMinBallDistance) = 500.0; // mm
      PARAMETER_REGISTER(useSecondStriker) = true;
      PARAMETER_REGISTER(strikerSelection, &Parameters::setStrikerSelectionFunction) = 3;
      PARAMETER_REGISTER(strikerBallRadiusFunction, &Parameters::setStrikerBallRadiusFunction) = 3;
      PARAMETER_REGISTER(firstSecondStrikerLinBallDistanceM) = 0.35;
      PARAMETER_REGISTER(firstSecondStrikerLinBallDistanceN) = -40.0;

      // load from the file after registering all parameters
      syncWithConfig();
    }

    bool useSecondStriker;
    int strikerBonusTime;
    int maxBallLostTime;
    int strikerSelectionDiffThreshold;

    int strikerSelection;
    void (CleanRoleDecision::*strikerSelectionFunction)(std::map<unsigned int, unsigned int>&, double&) = &CleanRoleDecision::strikerSelectionByNumber;

    int strikerBallRadiusFunction;
    double (CleanRoleDecision::*ballDifferenceRadius)(double) = &CleanRoleDecision::ballDifferenceRadiusConstant;

    double firstSecondStrikerMinBallDistance;
    double firstSecondStrikerLinBallDistanceM;
    double firstSecondStrikerLinBallDistanceN;

    /**
     * @brief Update function, if another radius function should be used
     * @param selection
     */
    void setStrikerBallRadiusFunction(int selection) {
        // select a radius function based on the given new parameter
        switch (selection) {
            // simply uses a firstSecondStrikerMinBallDistance as min radius between first and second striker's ball
            case 1: ballDifferenceRadius = &CleanRoleDecision::ballDifferenceRadiusConstant; break;
            // simply uses a linear 'function f(x) = x*m + n' based on the distance to the ball of the second striker
            case 2: ballDifferenceRadius = &CleanRoleDecision::ballDifferenceRadiusLinear; break;
            // uses the firstSecondStrikerMinBallDistance or a linear 'function f(x) = x*m + n' based on the distance to the ball of the second striker to determine the min radius between first and second striker's ball
            case 3: ballDifferenceRadius = &CleanRoleDecision::ballDifferenceRadiusConstantLinear; break;
            // simply uses a firstSecondStrikerMinBallDistance as min radius between first and second striker's ball
            default: ballDifferenceRadius = &CleanRoleDecision::ballDifferenceRadiusConstant;
        }
    }

    /**
     * @brief Update function, if another striker selection function should be used
     * @param selection
     */
    void setStrikerSelectionFunction(int selection) {
        // select a selection function based on the given new parameter
        switch (selection) {
            case 2:  strikerSelectionFunction = &CleanRoleDecision::strikerSelectionByTimeExceptGoalie; break;
            case 3:  strikerSelectionFunction = &CleanRoleDecision::strikerSelectionByTimeExceptGoalieWithBallCompare; break;
            case 1:
            default: strikerSelectionFunction = &CleanRoleDecision::strikerSelectionByNumber; break;
        }
    }
  } params;

private:
  /**
   * @brief Calculates, if the ball position of the given player are different from each other.
   *        Different in this context means, the ball positions are greater than "firstSecondStrikerDistance".
   * @param first the "first" player number
   * @param second the "second" player number
   * @return true, if the balls are further away, otherwise false
   */
  bool inline isSecondStrikerDifferentFromFirst(unsigned int first, unsigned int second);<--- Function 'isSecondStrikerDifferentFromFirst' argument 1 names different: declaration 'first' definition 'firstNumber'.<--- Function 'isSecondStrikerDifferentFromFirst' argument 2 names different: declaration 'second' definition 'secondNumber'.

  /**
   * @brief Sets the dynamic role of the player to the striker.
   * @param number  the player, which role should be set
   */
  void inline setStrikerRole(unsigned int number);

  /* Some radius functions to determine similiar balls. */
  inline double ballDifferenceRadiusConstant(double /*d*/) { return params.firstSecondStrikerMinBallDistance; }
  inline double ballDifferenceRadiusLinear(double d) { return d * params.firstSecondStrikerLinBallDistanceM + params.firstSecondStrikerLinBallDistanceN; }
  inline double ballDifferenceRadiusConstantLinear(double d) { return std::max(ballDifferenceRadiusConstant(d), ballDifferenceRadiusLinear(d)); }
};

#endif //__CleanRoleDecision_h_