
# Invivo AI challenge simulation environment
# Copyright 2009 Simon Funk
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from Interface import Interface

class Interface01(Interface):

	"""Interface01 is the i/o interface to a three-jointed bot
	with one touch sensor and a global sound sensor.

	Any position variable is an (x, y) tuple of floats representing a location,
	which may be in world or local coordinates as specified.  All positions
	are in the same units.

	Position-related velocities are also (x, y) tuples.  Angular velocities are scalars.

	Measurements (such as angles) are taken at the end of the most recent time step.
	Events (such as sounds) are accumulated during the timestep (exact time is not available).

	Static/Configuration info:

	   .position     = where the base of the robot is located in world coordinates.
	   .numjoints    = 3  (order is base, elbow, hand)
	   .cost         = score cost per second of (torque/torquemax)^2 (per each joint)
	   .torquesmax[n] = maxium abs torque joint n can exert.

	Sensors:

	   .angles[n] = current angle of joint n.
	   .angvel[n] = current angular velocity of joint n.

	   .handpos = position of the center of the hand relative to .position
	   .touch   = list of (position in hand coords, magnitude, reward) for any touch sensations on the hand.

	   .ballpos = position of the ball in world coordinates.
	   .ballvel = velocity of the ball.
	   .ballrot = angular velocity of the ball. [Currently unimplemented.]
	   .ballcol = "color" of ball, from -1 (prefers left) to 1 (prefers right)
	   .sound   = list of (worldpos, magnitude, reward) for any sound events

	Controls:

	   .torques[n] = current torque exerted by joint n.
	"""

	staticfields  = set(['numjoints', 'position', 'cost', 'torquesmax'])
	sensorfields  = set(['angles', 'angvel', 'handpos', 'ballpos', 'ballvel', 'ballcol'])
	controlfields = set(['torques'])

	def __init__(self, position=None, torquesmax=None, cost=None):

		self.numjoints  = 3
		self.position   = position
		self.cost       = cost
		self.torquesmax = torquesmax

		self.angles  = [0.] * self.numjoints
		self.angvel  = [0.] * self.numjoints
		self.handpos = None	# Unknown until set properly
		self.ballpos = None
		self.ballvel = None
		self.ballcol = None
		self.clearEvents()

		self.torques = [0.] * self.numjoints

	def clearEvents(self):
		"""(This is called automatically at the start of each new timestep.)"""
		self.sound = []
		self.touch = []

