コード例 #1
0
    def testlookAt(self):
        self.warrior = Warrior()

        # Case 1: when the warrior needs to turn in some given orientation
        self.warrior.action.append("lookAt")
        self.warrior.action.append("orientation")
        self.warrior.orientation = 0
        self.warrior.targetOrientation = pi
        warrior = self.actions.run(self.warrior)

        self.assertEqual(warrior.cmdType, "ORIENTATION")

        # Other cases: when the warrior needs to look at some given target(a point)
        # If the target is in front of him, the function is equal to PI
        self.warrior.targetOrientation = None
        self.warrior.cmdType = None
        self.warrior.action[1] = "target"
        self.warrior.position = (100, 200)
        self.warrior.target = (300, 200)
        warrior = self.actions.run(self.warrior)

        self.assertEqual(warrior.cmdType, 'ORIENTATION')
        self.assertEqual(warrior.targetOrientation, pi)

        # If the target is in front of him, the function is equal to 0
        self.warrior.targetOrientation = None
        self.warrior.cmdType = None
        self.warrior.action[1] = "target"
        self.warrior.position = (300, 200)
        self.warrior.target = (100, 200)
        warrior = self.actions.run(self.warrior)

        self.assertEqual(warrior.cmdType, 'ORIENTATION')
        self.assertEqual(warrior.targetOrientation, 0)

        # If the target is up, the function is equal to PI/2
        self.warrior.targetOrientation = None
        self.warrior.cmdType = None
        self.warrior.action[1] = "target"
        self.warrior.position = (100, 100)
        self.warrior.target = (100, 300)
        warrior = self.actions.run(self.warrior)

        self.assertEqual(warrior.cmdType, 'ORIENTATION')
        self.assertEqual(warrior.targetOrientation, pi/2)

        # And if the target is down, the function is equal to -Pi/2
        self.warrior.targetOrientation = None
        self.warrior.cmdType = None
        self.warrior.action[1] = "target"
        self.warrior.position = (100, 300)
        self.warrior.target = (100, 100)
        warrior = self.actions.run(self.warrior)

        self.assertEqual(warrior.cmdType, 'ORIENTATION')
        self.assertEqual(warrior.targetOrientation, -(pi/2))
        del self.warrior
コード例 #2
0
    def testStop(self):
        self.warrior = Warrior()

        self.warrior.action.append("stop")
        self.warrior.before = 0
        warrior = self.actions.run(self.warrior)

        self.assertEqual(warrior.cmdType, "SPEED")
        self.assertEqual(warrior.vLeft, 0)
        self.assertEqual(warrior.vRight, 0)

        del self.warrior
コード例 #3
0
    def testGoTo(self):
        self.warrior = Warrior()

        self.warrior.action.append("goTo")
        self.warrior.position = (200, 200)
        self.warrior.orientation = 0
        # self.warrior.targetOrientation = -((pi/2.0) + ((pi/2.0)/2.0))
        self.warrior.targetOrientation = (500, 500)
        self.warrior.target = (400, 400)
        self.warrior.vMax = 1.0

        warrior = self.actions.run(self.warrior)
        self.assertEqual(warrior.cmdType, "VECTOR")

        del self.warrior
コード例 #4
0
class TestTranslate(unittest.TestCase):
    warrior = Warrior()
    translate = Dice()

    def testRun(self):
        self.warrior.cmdType = "SPEED"
        self.warrior.vLeft = 0.8
        self.warrior.vRight = 0.8

        warrior = self.translate.run(self.warrior)
        self.assertIsNotNone(warrior)

    def testUvfControl(self):
        pass

    def testVectorControl(self):
        pass

    def testPositionControl(self):
        '''
        translate = Translate()
        warrior = warrior([100, 200], [100, 200], 0, math.pi, 0, "POSITION", 0.8, 1, 1, None)
        warrior = translate.setup(warrior)
        self.assertEqual([warrior[0], warrior[1]], [0, 0])
        '''
        pass

    def testOrientationControl(self):
        self.warrior.cmdType = "ORIENTATION"
        self.warrior.orientation = 0
        self.warrior.targetOrientation = pi
        self.warrior.vMax = 0.8
        warrior = self.translate.run(self.warrior)

        self.assertEqual([warrior[0], warrior[1]], [0, 0])

        self.warrior.cmdType = "ORIENTATION"
        self.warrior.orientation = pi
        self.warrior.targetOrientation = pi
        warrior = self.translate.run(self.warrior)

        self.assertEqual([warrior[0], warrior[1]], [0, 0])
コード例 #5
0
    def testSpin(self):
        self.warrior = Warrior()

        self.warrior.action.append("spin")
        self.warrior.action.append("clockwise")
        self.warrior.vMax = 0.8
        warrior = self.actions.run(self.warrior)

        self.assertEqual(warrior.cmdType, "SPEED")
        self.assertEqual(warrior.vLeft, 0.8)
        self.assertEqual(warrior.vRight, -0.8)

        self.warrior.action[1] = "counter"
        warrior = self.actions.run(self.warrior)

        self.assertEqual(warrior.cmdType, "SPEED")
        self.assertEqual(warrior.vLeft, -0.8)
        self.assertEqual(warrior.vRight, 0.8)

        del self.warrior
コード例 #6
0
    def setup(self, nWarriors):
        """Zeus first movements

        This method must be called before using Zeus properly.
        Here is instantiated the Eunomia() and Dice() as well as the amount nWarriors of Warriors to be used.

        Args:
            nWarriors: Num of warriors in game

        Returns:

        """
        self.actions.setup()
        self.nWarriors = nWarriors

        for i in range(0, nWarriors):
            self.warriors.append(Warrior())
            self.robotsSpeed.append(0.0)

        print("Zeus is set up")
        return self
コード例 #7
0
    def getWarriors(self, strategia):
        """Transforms a list of dictionaries into a Warrior() list.

        Seta os atributos do object Warrior() baseado nas informações passadas pela estratégia. Ações que podem ser escolhidas:
        - {
            "command": "goTo",
            "data": {
                "obstacles": [(x, y)] # opcional - se passado, desviar de tais obstaclos
                "pose": { "position": (x, y), "orientation": θ radianos },
                "target": {
                    "position": (x, y),
                    "orientation": θ radianos | (x, y)  # opcional - pode ser uma orientação final ou uma posição de lookAt
                    },
                "velocity": X m/s,  # opcional - se passado, sem before, é a velocidade constante / com before é velocidade padrão
                "before": X s  # se passado sem o velocity, usa a velocidade máxima do robô como teto
            }
        }
        - {
            "command": "spin",
            "data": { "velocity": X m/s, "direction": "clockwise" | "counter" }
        }
        - {
            "command": "lookAt",
            "data": {
                "pose": {
                    "position": (x, y),  # opcional - é passado se o target for um ponto
                    "orientation": θ radianos
                },
                "target": θ radianos | (x, y)
            }
        }
        - {
            "command": stop,
            "data": {before: 0}
        }

        Args:
            strategia (list): List of dictionaries with information generated by Strategy0

        Returns:
            list: Object Warrior() list

        Raises:
            ValueError:

        """

        warriors = []
        if type(strategia) is not list or \
                len(strategia) != self.nWarriors:
            raise ValueError("Invalid data object received.")

        for i in range(0, self.nWarriors):
            if type(strategia[i]) is not dict:
                raise ValueError("Invalid data object received.")

            if ("command" in strategia[i]) is False or \
                    ("data" in strategia[i]) is False:
                raise ValueError("Invalid data object received.")

            warriors.append(Warrior())

        for x in range(0, len(strategia)):
            if strategia[x]["command"] is not "goTo" and \
                    strategia[x]["command"] is not "spin" and \
                    strategia[x]["command"] is not "lookAt" and \
                    strategia[x]["command"] is not "stop":
                raise ValueError("Invalid command.")

            warriors[x].action.append(strategia[x]["command"])
            info = strategia[x]["data"]

            if strategia[x]["command"] == "goTo":
                warriors[x].position = numpy.asarray(info["pose"]["position"],
                                                     dtype=float)
                warriors[x].orientation = float(info["pose"]["orientation"])

                warriors[x].target = numpy.asarray(info["target"]["position"],
                                                   dtype=float)
                if type(info["target"]["orientation"]) is tuple:
                    warriors[x].targetOrientation = numpy.asarray(
                        info["target"]["orientation"], dtype=float)
                else:
                    warriors[x].targetOrientation = float(
                        info["target"]["orientation"])

                if "velocity" in info:
                    warriors[x].vMax = numpy.asarray(info["velocity"],
                                                     dtype=float)
                else:
                    warriors[x].vMax = self.robotsSpeed[x]

                if "before" in info:
                    warriors[x].before = float(info["before"])

                if "obstacles" in info:
                    warriors[x].obstacles = numpy.asarray(info["obstacles"],
                                                          dtype=float)
                    warriors[x].obstaclesSpeed = numpy.asarray(
                        info["obstaclesSpeed"], dtype=float)

            elif strategia[x]["command"] == "spin":
                warriors[x].action.append(info["direction"])

                if "velocity" in info:
                    warriors[x].vMax = numpy.asarray(info["velocity"],
                                                     dtype=float)
                else:
                    warriors[x].vMax = self.robotsSpeed[x]

            elif strategia[x]["command"] == "lookAt":
                warriors[x].orientation = float(info["pose"]["orientation"])

                if "velocity" in info:
                    warriors[x].vMax = numpy.asarray(info["velocity"],
                                                     dtype=float)
                else:
                    warriors[x].vMax = self.robotsSpeed[x]

                if type(info["target"]) is not tuple:
                    warriors[x].targetOrientation = float(info["target"])
                    warriors[x].action.append("orientation")
                else:
                    warriors[x].position = numpy.asarray(
                        info["pose"]["position"], dtype=float)
                    warriors[x].target = numpy.asarray(info["target"],
                                                       dtype=float)
                    warriors[x].action.append("target")

            elif strategia[x]["command"] == "stop":
                warriors[x].vMax = 0.0
                warriors[x].before = float(info["before"])

            warriors[x].backward = self.warriors[x].backward
            warriors[x].front = self.warriors[x].front
            warriors[x].velAcc = self.warriors[x].velAcc

        return warriors