Skip to content

Instantly share code, notes, and snippets.

@fangeugene
Last active January 13, 2020 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fangeugene/5af3a52c2ac19ffad6cdfdee8e61d8af to your computer and use it in GitHub Desktop.
Save fangeugene/5af3a52c2ac19ffad6cdfdee8e61d8af to your computer and use it in GitHub Desktop.
Convert old Zebra MotionWorks data and upload to TBA using the Trusted API
import csv
import glob
import hashlib
import json
import numpy as np
import requests
EVENT_KEY = '2019cc'
AUTH_ID = 'ID'
AUTH_SECRET = 'SECRET'
TIMESTEP = 0.1
def parseMatch(rows):
# Determine TBA match key
matchID = rows[0][2].split(' - ')
level = matchID[0].strip()
if level == 'Qualification':
matchKey = 'qm{}'.format(matchID[1].strip())
else:
matchKey = matchID[1].lower()
if matchKey.split('-')[0] == 'f':
matchKey = matchKey.replace('f-', 'f1m')
else:
matchKey = matchKey.replace('-', 'm')
# Find start and end times
startTime = float('inf')
endTime = 0
for row in rows[4:]:
if row[2]:
time = float(row[2])
startTime = min(startTime, time)
endTime = max(endTime, time)
nSteps = int(round((endTime - startTime) / TIMESTEP + 1))
times = np.arange(0, nSteps * TIMESTEP, TIMESTEP)
times = [round(time, 1) for time in times]
# Construct team data
redTeams = rows[1]
redTeams[0] = redTeams[0][1:] # Strip leading '#'
blueTeams = rows[2]
blueTeams[0] = blueTeams[0][1:] # Strip leading '#'
red = [{
'team_key': 'frc{}'.format(teamNum),
'xs': [None] * nSteps,
'ys': [None] * nSteps,
} for teamNum in redTeams]
blue = [{
'team_key': 'frc{}'.format(teamNum),
'xs': [None] * nSteps,
'ys': [None] * nSteps,
} for teamNum in blueTeams]
# Setup a way to find the team when filling in data
teamMap = {}
for i, team in enumerate(red):
teamMap[team['team_key']] = (True, i)
for i, team in enumerate(blue):
teamMap[team['team_key']] = (False, i)
# Fill in team data
teamKey = None
curTime = 0
for row in rows[3:]:
if row[0].startswith('#'):
teamKey = 'frc{}'.format(row[0][1:])
curTime = 0
continue
if teamKey is not None:
isRed, idx = teamMap[teamKey]
if isRed:
teamData = red[idx]
else:
teamData = blue[idx]
x, y, time = row
time = float(time) - startTime
timeIdx = int(round(time / TIMESTEP))
teamData['xs'][timeIdx] = float(x)
teamData['ys'][timeIdx] = float(y)
return {
'key': '{}_{}'.format(EVENT_KEY, matchKey),
'times': times,
'alliances': {
'red': red,
'blue': blue,
},
}
# Parse all matches in directory
for file in glob.glob("./*.csv"):
with open(file) as csvfile:
match = parseMatch(list(csv.reader(csvfile)))
matches = [match]
# Make TBA Trusted API call to add data
request_body = json.dumps(matches)
request_path = '/api/trusted/v1/event/{}/zebra_motionworks/add'.format(EVENT_KEY)
m = hashlib.md5()
m.update('{}{}{}'.format(AUTH_SECRET, request_path, request_body).encode('utf-8'))
r = requests.post('https://www.thebluealliance.com' + request_path, headers={'X-TBA-Auth-Id': AUTH_ID, 'X-TBA-Auth-Sig': m.hexdigest()}, data=request_body)
print(r.status_code)
print(r.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment