Last active
May 22, 2016 07:49
-
-
Save justbuchanan/8bf62d9a05c98a2ef6666474acfe7d37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# | |
# This script builds a ball-placement SSL Referee message for the yellow team | |
# and repeatedly broadcasts it. Usage: | |
# ./send_ssl_placement_cmd.py <x coord> <y coord> | |
# | |
# Install protobuf: | |
# pip2 install protobuf | |
# | |
# Compile protobuf files (this creates referee_pb2.py): | |
# protoc -I=./common/protobuf --python_out=./ ./common/protobuf/referee.proto | |
# | |
from __future__ import print_function | |
from referee_pb2 import * | |
import time | |
import sys | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('x', type=float) | |
parser.add_argument('y', type=float) | |
args = parser.parse_args() | |
# create packet | |
pkt = SSL_Referee() | |
pkt.stage = SSL_Referee.NORMAL_FIRST_HALF # needed? | |
pkt.command = SSL_Referee.BALL_PLACEMENT_YELLOW | |
pkt.command_counter = 1 | |
pkt.command_timestamp = int(time.time()) | |
# set position, converting from team coordinates to ssl ref global coordinates | |
pkt.designated_position.x = args.x * 1000 | |
pkt.designated_position.y = args.y * 1000 - 4500 | |
for team, name in [(pkt.yellow, 'RoboJackets'), (pkt.blue, 'Opponent')]: | |
team.name = name | |
team.score = 0 | |
team.red_cards = 0 | |
team.yellow_cards = 0 | |
team.timeouts = 5 | |
team.timeout_time = 5000 | |
team.goalie = 0 | |
print("Sending referee message: \n") | |
print("-" * 50) | |
print(pkt, end='') | |
print("-" * 50) | |
print() | |
# setup UDP broadcast | |
import socket | |
UDP_PORT = 10003 | |
UDP_ADDRESS = '224.5.23.1' | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
# Exit when ctrl+c is pressed | |
import signal | |
def handle_signal(signal, frame): | |
sock.close() | |
print('done') | |
sys.exit(0) | |
signal.signal(signal.SIGINT, handle_signal) | |
signal.signal(signal.SIGTERM, handle_signal) | |
# repeatedly send the packet | |
while True: | |
print('*', end='') | |
sys.stdout.flush() | |
pkt.packet_timestamp = int(time.time()) | |
pkt.command_counter += 1 | |
addr = (UDP_ADDRESS, UDP_PORT) | |
data = pkt.SerializeToString() | |
sock.sendto(data, addr) | |
time.sleep(0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment