Skip to content

Commit

Permalink
Add field for 2017
Browse files Browse the repository at this point in the history
Users can now load in there own image by setting 'pyfrc.field.image' to the name of the image file within the sim directory. If no image is specified, a defaultwill be used.
  • Loading branch information
ArchdukeTim committed Jan 12, 2017
1 parent 67162d5 commit a003ba5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 47 deletions.
9 changes: 5 additions & 4 deletions lib/pyfrc/mains/cli_sim.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@

import inspect
import json
import logging
from os.path import abspath, dirname, exists, join

import hal_impl.functions

from ..test_support import pyfrc_fake_hooks

import hal_impl.functions

import logging
logger = logging.getLogger('pyfrc.sim')

class PyFrcSim:
Expand Down Expand Up @@ -42,7 +43,7 @@ def _load_config(self, config_file):
config_obj['pyfrc']['field'].setdefault('w', 1)
config_obj['pyfrc']['field'].setdefault('h', 1)
config_obj['pyfrc']['field'].setdefault('px_per_ft', 10)
config_obj['pyfrc']['field'].setdefault('objects', [])
config_obj['pyfrc']['field'].setdefault('image', None)

config_obj['pyfrc'].setdefault('analog', {})
config_obj['pyfrc'].setdefault('CAN', {})
Expand Down Expand Up @@ -79,7 +80,7 @@ def run(self, options, robot_class, **static_options):
config_file = join(sim_path, 'config.json')

config_obj = self._load_config(config_file)

config_obj['simpath'] = sim_path
fake_time = sim.FakeRealTime()
hal_impl.functions.hooks = pyfrc_fake_hooks.PyFrcFakeHooks(fake_time)
hal_impl.functions.reset_hal()
Expand Down
Binary file added lib/pyfrc/sim/field/field.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
88 changes: 45 additions & 43 deletions lib/pyfrc/sim/field/field.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,81 @@

from os.path import abspath, dirname, join
from tkinter import PhotoImage

import tkinter as tk

from .elements import DrawableElement


class RobotField(object):

def __init__(self, root, manager, config_obj):
'''
initializes all default values and creates
initializes all default values and creates
a board, waits for run() to be called
to start the board
manager - sim manager class instance
board_size - a tuple with values (rows, cols)
'''

# TODO: support drawing an actual field?

self.manager = manager
self.elements = [] # robots, walls, missles, etc

field_size = config_obj['pyfrc']['field']['w'], \
config_obj['pyfrc']['field']['h']
config_obj['pyfrc']['field']['h']
px_per_ft = config_obj['pyfrc']['field']['px_per_ft']

# setup board characteristics -- cell size is 1ft
self.cols, self.rows = field_size
self.margin = 5
self.cellSize = px_per_ft
self.canvasWidth = 2*self.margin + self.cols*self.cellSize
self.canvasHeight = 2*self.margin + self.rows*self.cellSize
self.canvasWidth = 2 * self.margin + 485
self.canvasHeight = 2 * self.margin + 535

self.canvas = tk.Canvas(root, width=self.canvasWidth, height=self.canvasHeight)
self.canvas.bind("<Key>", self.on_key_pressed)
self.canvas.bind("<Button-1>", self.on_click)

self.text_id = None

# Draw the field initially
self.draw_field()

# Load elements from the config
# -> This probably belongs somewhere else
self._load_field_elements(px_per_ft, config_obj['pyfrc']['field']['objects'])

def _load_field_elements(self, px_per_ft, objects):

for obj in objects:

color = obj['color']
pts = [(self.margin + int(pt[0]*px_per_ft),
self.margin + int(pt[1]*px_per_ft)) for pt in obj['points']]
element = DrawableElement(pts, None, None, color)
self.add_moving_element(element)


self._load_field_elements(px_per_ft, config_obj)

def _load_field_elements(self, px_per_ft, config_obj):
image = config_obj['pyfrc']['field']['image']
if image is None:
self.photo = PhotoImage(file=abspath(join(dirname(__file__), 'field.gif')))
else:
self.photo = PhotoImage(file=join(config_obj['simpath'], config_obj['pyfrc']['field']['image']))
self.canvas.create_image((self.canvasWidth / 2, self.canvasHeight / 2), image=self.photo)

def add_moving_element(self, element):
'''Add elements to the board'''

element.initialize(self.canvas)
self.elements.append(element)

def grid(self, *args, **kwargs):
self.canvas.grid(*args, **kwargs)

def on_key_pressed(self, event):
'''
likely to take in a set of parameters to treat as up, down, left,
right, likely to actually be based on a joystick event... not sure
yet
'''

return

# TODO

if event.keysym == "Up":
self.manager.set_joystick(0.0, -1.0, 0)
elif event.keysym == "Down":
Expand All @@ -83,29 +84,30 @@ def on_key_pressed(self, event):
self.manager.set_joystick(-1.0, 0.0, 0)
elif event.keysym == "Right":
self.manager.set_joystick(1.0, 0.0, 0)

elif event.char == " ":
mode = self.manager.get_mode()
if mode == self.manager.MODE_DISABLED:
self.manager.set_mode(self.manager.MODE_OPERATOR_CONTROL)
else:
self.manager.set_mode(self.manager.MODE_DISABLED)

def on_click(self, event):
self.canvas.focus_set()

def update_widgets(self):

# TODO: process collisions and such too

for element in self.elements:
element.perform_move()

def draw_field(self):
for row in range(self.rows):
for col in range(self.cols):
self.draw_board_cell(row, col)

pass
# for row in range(self.rows):
# for col in range(self.cols):
# self.draw_board_cell(row, col)

def draw_board_cell(self, row, col):
left = self.margin + col * self.cellSize
right = left + self.cellSize
Expand Down

0 comments on commit a003ba5

Please sign in to comment.