Exemplo n.º 1
0
def test_input():
    message = pb_controller.Input()
    message.move.left = 0.2
    message.move.right = -0.5
    message.fire.weapon1 = False
    message.fire.weapon2 = True
    payload = message.SerializeToString()
    message2 = pb_controller.Input()
    message2.ParseFromString(payload)
    assert (message2.move.left == 0.2)
    assert (message2.move.right == -0.5)
    assert (not message2.fire.weapon1)
    assert (message2.fire.weapon2)
Exemplo n.º 2
0
 def get_input(self, payload):
     message = pb_messages.Input()
     message.ParseFromString(payload)
     assert (-1 <= message.move.left <= 1)
     assert (-1 <= message.move.right <= 1)
     return (message.move.left, message.move.right, message.fire.weapon1,
             message.fire.weapon2)
Exemplo n.º 3
0
 def __init__(self, left, right, fire_weapon1, fire_weapon2):
     pb_input = pb_controller.Input()
     pb_input.move.left = left
     pb_input.move.right = right
     pb_input.fire.weapon1 = fire_weapon1
     pb_input.fire.weapon2 = fire_weapon2
     self._payload = pb_input.SerializeToString()
Exemplo n.º 4
0
 def send_input(self, data):
     factor = 0.5
     left = 0
     right = 0
     fire_weapon1 = False
     fire_weapon2 = False
     if ("LEFT" == data):
         left = -1 * factor
         right = 1 * factor
     elif ("FORWARD" == data):
         left = 1 * factor
         right = 1 * factor
     elif ("RIGHT" == data):
         left = 1 * factor
         right = -1 * factor
     elif ("BACKWARD" == data):
         left = -1 * factor
         right = -1 * factor
     elif ("FIRE1" == data):
         fire_weapon1 = True
     elif ("FIRE2" == data):
         fire_weapon2 = True
     pb_input = pb_controller.Input()
     pb_input.move.left = left
     pb_input.move.right = right
     pb_input.fire.weapon1 = fire_weapon1
     pb_input.fire.weapon2 = fire_weapon2
     payload = pb_input.SerializeToString()
     message = self._routing_id + ' Input ' + payload
     # self._subscribe_stream.send(hello)
     self._push_stream.send(message)
Exemplo n.º 5
0
    def test_4(self):
        """Nested message."""
        message = pb_controller.Input()
        message.move.left = 0.2
        message.move.right = -0.5
        message.fire.weapon1 = False
        message.fire.weapon2 = True
        yaml_content = """
message: !CaptureInput
    message:
        move:
            left: {left}
            right: {right}
        fire:
            weapon1: {weapon1}
            weapon2: {weapon2}
""".format(left=message.move.left,
           right=message.move.right,
           weapon1=message.fire.weapon1,
           weapon2=message.fire.weapon2)
        data = yaml.load(yaml_content, Loader=yaml.FullLoader)
        message2 = data["message"]
        assert_equal(message.move.left, message2.protobuf_message.move.left)
        assert_equal(message.move.right, message2.protobuf_message.move.right)
        assert_equal(message.fire.weapon1,
                     message2.protobuf_message.fire.weapon1)
        assert_equal(message.fire.weapon2,
                     message2.protobuf_message.fire.weapon2)
Exemplo n.º 6
0
 def get_input_message(self, left, right, fire1, fire2):
     assert (-1 <= left <= 1)
     assert (-1 <= right <= 1)
     message = pb_messages.Input()
     message.move.left = left
     message.move.right = right
     message.fire.weapon1 = fire1
     message.fire.weapon2 = fire2
     payload = message.SerializeToString()
     return self.recipient + " Input " + payload
Exemplo n.º 7
0
    def test_key_map():
        pb_message = pb_controller.Input()
        left = 0.2
        right = 0.5
        pb_message.move.left = left
        pb_message.move.right = right
        pb_message.fire.weapon1 = False
        pb_message.fire.weapon2 = True
        yaml_content = """
message: !CaptureInput
    destination: TEST1
    message:
        move:
            left: {left}
            right: "{{right}}"
        fire:
            weapon1: {weapon1}
            weapon2: {weapon2}
""".format(left=pb_message.move.left,
           weapon1=pb_message.fire.weapon1,
           weapon2=pb_message.fire.weapon2)
        data = yaml.load(yaml_content, Loader=yaml.FullLoader)
        message1 = data["message"]
        sys.stderr.write("\n" + str(message1.key_map) + "\n")
        assert_equal(message1.key_map["/move/left"], pb_message.move.left)
        assert_equal(message1.key_map["/move/right"], '{right}')
        assert_equal(message1.key_map["/fire/weapon1"],
                     pb_message.fire.weapon1)
        assert_equal(message1.key_map["/fire/weapon2"],
                     pb_message.fire.weapon2)
        new_left = 1.0
        pb_message.move.left = new_left
        message2 = y2p.Input(pb_message.SerializeToString())
        sys.stderr.write(str(message2.key_map) + "\n")
        assert_equal(message2.key_map["/move/left"], pb_message.move.left)
        assert_equal(message2.key_map["/move/right"], pb_message.move.right)
        assert_equal(message2.key_map["/fire/weapon1"],
                     pb_message.fire.weapon1)
        assert_equal(message2.key_map["/fire/weapon2"],
                     pb_message.fire.weapon2)
        diffs = message1.compute_differences(message2)
        sys.stderr.write(str(diffs) + "\n")
        # the name differs because we did not go thourgh the standard path
        # that removes the Capture part in the name.
        assert_equal([('@name', u'!CaptureInput', u'!Input'),
                      ('@destination', 'TEST1', None),
                      ('/move/left', left, new_left)], diffs)
        sys.stderr.write(str(message1.captured) + "\n")
        assert_equal([{'right': right}], message1.captured)
        pb_message = message1.fill(message1.captured[-1])
        sys.stderr.write('filled:' + str(pb_message) + "\n")
        message2.protobuf_message.move.left = left
        assert_equal(str(pb_message), str(message2.protobuf_message))
        assert_equal(pb_message, message2.protobuf_message)
Exemplo n.º 8
0
 def send_input(self, joystick, force_ping):
     if (joystick.has_new_values):
         pb_input = pb_controller.Input()
         pb_input.move.left = joystick.left
         pb_input.move.right = joystick.right
         pb_input.fire.weapon1 = joystick.fire_weapon1
         pb_input.fire.weapon2 = joystick.fire_weapon2
         payload = pb_input.SerializeToString()
         message = self._routing_id + ' Input ' + payload
         LOGGER.debug("message sent: " + repr(message))
         self._push_socket.send(message)
         if (joystick.ping or force_ping):
             pb_ping = pb_controller.Ping()
             timing_event = pb_ping.timing.add()
             timing_event.logger = NAME
             timestamp = int(round(time.time() * 1000))
             timing_event.timestamp = timestamp
             payload = pb_ping.SerializeToString()
             message = self._routing_id + ' Ping ' + payload
             LOGGER.info("message sent: " + repr(message))
             self._push_socket.send(message)
Exemplo n.º 9
0
 def test_3(self):
     """Play with the underlying library (not really a test)."""
     message = pb_controller.Hello()
     name = "JAMBON"
     message.name = name
     dico = pb2dict(message)
     message = pb_controller.Input()
     left = 0.2
     right = -0.5
     weapon1 = False
     weapon2 = True
     message.move.left = left
     message.move.right = right
     message.fire.weapon1 = weapon1
     message.fire.weapon2 = weapon2
     dico = pb2dict(message)
     message2 = dict2pb(pb_controller.Input, dico)
     assert_equal(left, message2.move.left)
     assert_equal(right, message2.move.right)
     assert_equal(weapon1, message2.fire.weapon1)
     assert_equal(weapon2, message2.fire.weapon2)
Exemplo n.º 10
0
    def test_5(self):
        """Use the inline notation (json like)"""
        message = pb_controller.Input()
        message.move.left = 0.2
        message.move.right = -0.5
        message.fire.weapon1 = False
        message.fire.weapon2 = True
        yaml_content = """
message: !CaptureInput {{ "message": {{ "move": {{ "left": {left},
"right": {right} }}, "fire": {{ "weapon1": {weapon1}, "weapon2": {weapon2} }}
}} }}""".format(left=message.move.left,
                right=message.move.right,
                weapon1=message.fire.weapon1,
                weapon2=message.fire.weapon2)
        data = yaml.load(yaml_content, Loader=yaml.FullLoader)
        message2 = data["message"]
        assert_equal(message.move.left, message2.protobuf_message.move.left)
        assert_equal(message.move.right, message2.protobuf_message.move.right)
        assert_equal(message.fire.weapon1,
                     message2.protobuf_message.fire.weapon1)
        assert_equal(message.fire.weapon2,
                     message2.protobuf_message.fire.weapon2)
Exemplo n.º 11
0
import orwell.messages.server_game_pb2 as server_game_messages
import orwell.messages.controller_pb2 as controller_messages
import collections
from enum import Enum


class Messages(Enum):
    Register = 'Register'
    Registered = 'Registered'
    Input = 'Input'


REGISTRY = {
    Messages.Register.name: lambda: robot_messages.Register(),
    Messages.Registered.name: lambda: server_game_messages.Registered(),
    Messages.Input.name: lambda: controller_messages.Input(),
}


class Subscriber(object):
    def __init__(self, address, port, context):
        self._socket = context.socket(zmq.SUB)
        self._socket.setsockopt(zmq.LINGER, 0)
        self._socket.setsockopt(zmq.SUBSCRIBE, "")
        url = "tcp://%s:%i" % (address, port)
        self._socket.connect(url)

    def read(self):
        return self._socket.recv(flags=zmq.DONTWAIT)