def on_new_gamestate(self, state):
        """ Is called with the new game state after receiving a package.
            The information is processed and published as a standard message to a ROS topic.
            :param state: Game State
        """
        if state.teams[0].team_number == self.team:
            own_team = state.teams[0]
            rival_team = state.teams[1]
        elif state.teams[1].team_number == self.team:
            own_team = state.teams[1]
            rival_team = state.teams[0]
        else:
            rospy.logerr('Team {} not playing, only {} and {}'.format(
                self.team, state.teams[0].team_number,
                state.teams[1].team_number))
            return

        try:
            me = own_team.players[self.player - 1]
        except IndexError:
            rospy.logerr('Robot {} not playing'.format(self.player))
            return

        msg = GameStateMsg()
        msg.header.stamp = rospy.Time.now()
        msg.gameState = state.game_state.intvalue
        msg.secondaryState = state.secondary_state.intvalue
        msg.secondaryStateMode = state.secondary_state_info[1]
        msg.firstHalf = state.first_half
        msg.ownScore = own_team.score
        msg.rivalScore = rival_team.score
        msg.secondsRemaining = state.seconds_remaining
        msg.secondary_seconds_remaining = state.secondary_seconds_remaining
        msg.hasKickOff = state.kick_of_team == self.team
        msg.penalized = me.penalty != 0
        msg.secondsTillUnpenalized = me.secs_till_unpenalized
        msg.secondaryStateTeam = state.secondary_state_info[0]
        msg.secondaryStateMode = state.secondary_state_info[1]
        msg.teamColor = own_team.team_color.intvalue
        msg.dropInTeam = state.drop_in_team
        msg.dropInTime = state.drop_in_time
        msg.penaltyShot = own_team.penalty_shot
        msg.singleShots = own_team.single_shots
        msg.coach_message = own_team.coach_message
        self.state_publisher.publish(msg)
Exemple #2
0
    def on_new_gamestate(self, state):
        """ Is called with the new game state after receiving a package
            Needs to be implemented or set
            :param state: Game State
        """
        if state.teams[0].team_number == self.team:
            own_team = state.teams[0]
            rival_team = state.teams[1]
        elif state.teams[1].team_number == self.team:
            own_team = state.teams[1]
            rival_team = state.teams[0]
        else:
            rospy.logerr('Team {} not playing, only {} and {}'.format(
                self.team, state.teams[0].team_number,
                state.teams[1].team_number))
            return

        try:
            me = own_team.players[self.player - 1]
        except IndexError:
            rospy.logerr('Robot {} not playing'.format(self.player))
            return

        msg = GameStateMsg()
        msg.header.stamp = rospy.Time.now()
        msg.gameState = state.game_state.intvalue
        msg.secondaryState = state.secondary_state.intvalue
        msg.firstHalf = state.first_half
        msg.ownScore = own_team.score
        msg.rivalScore = rival_team.score
        msg.secondsRemaining = state.seconds_remaining
        msg.secondary_seconds_remaining = state.secondary_seconds_remaining
        msg.hasKickOff = state.kick_of_team == self.team
        msg.penalized = me.penalty != 0
        msg.secondsTillUnpenalized = me.secs_till_unpenalized

        if me.penalty != 0:
            msg.allowedToMove = False
        elif state.game_state in ('STATE_INITIAL', 'STATE_SET'):
            msg.allowedToMove = False
        elif state.game_state == 'STATE_READY':
            msg.allowedToMove = True
        elif state.game_state == 'STATE_PLAYING':
            if state.kick_of_team >= 128:
                # Drop ball
                msg.allowedToMove = True
            elif state.secondary_state in ('STATE_DIRECT_FREEKICK',
                                           'STATE_INDIRECT_FREEKICK',
                                           'STATE_PENALTYKICK',
                                           'STATE_CORNER_KICK',
                                           'STATE_GOAL_KICK',
                                           'STATE_THROW_IN'):
                if state.secondary_state_info[1] in (0, 2):
                    msg.allowedToMove = False
                else:
                    msg.allowedToMove = True
                msg.secondaryStateTeam = state.secondary_state_info[0]
            elif state.secondary_state == 'STATE_PENALTYSHOOT':
                # we have penalty kick
                if state.kick_of_team == self.team:
                    msg.allowedToMove = True
                else:
                    msg.allowedToMove = False
            elif state.kick_of_team == self.team:
                msg.allowedToMove = True
            else:
                # Other team has kickoff
                if msg.secondary_seconds_remaining != 0:
                    msg.allowedToMove = False
                else:
                    # We have waited the kickoff time
                    msg.allowedToMove = True

        msg.teamColor = own_team.team_color.intvalue
        msg.dropInTeam = state.drop_in_team
        msg.dropInTime = state.drop_in_time
        msg.penaltyShot = own_team.penalty_shot
        msg.singleShots = own_team.single_shots
        msg.coach_message = own_team.coach_message
        self.state_publisher.publish(msg)