/***************************************************************************
 *   Copyright (C) 2005 by Robot Group Leipzig                             *
 *    martius@informatik.uni-leipzig.de                                    *
 *    fhesse@informatik.uni-leipzig.de                                     *
 *    der@informatik.uni-leipzig.de                                        *
 *                                                                         *
 *   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 2 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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                         *
 *   $Log: sinecontroller.h,v $
 *   Revision 1.16  2009/08/10 15:02:17  der
 *   Impuls->Impulse
 *
 *   Revision 1.15  2009/03/31 15:53:11  martius
 *   added amplitude
 *
 *   Revision 1.14  2009/02/02 15:37:44  martius
 *   added impulswidth
 *
 *   Revision 1.13  2009/02/02 15:23:50  martius
 *   added saw tooth and impuls oscillators
 *   sinerate is now period which is counted in control cycles
 *   changing the period does not change phase as it was before.
 *
 *   Revision 1.12  2008/04/17 14:54:45  martius
 *   randomGen added, which is a random generator with long period and an
 *    internal state. Each Agent has an instance and passed it to the controller
 *    and the wiring. This is good for
 *   a) repeatability on agent basis,
 *   b) parallel execution as done in ode_robots
 *
 *   Revision 1.11  2007/03/26 13:13:47  martius
 *   store and restore with params
 *
 *   Revision 1.10  2006/11/29 16:22:43  martius
 *   name is a variable of configurable and is used as such
 *
 *   Revision 1.9  2006/08/02 09:32:03  martius
 *   store and restore according to storable interface
 *
 *   Revision 1.8  2006/07/14 12:23:59  martius
 *   selforg becomes HEAD
 *
 *   Revision 1.6.6.5  2006/06/25 21:56:08  martius
 *   configureable has name and revision
 *
 *   Revision 1.6.6.4  2006/06/25 16:51:36  martius
 *   configureable has name and revision
 *   a robot is configureable by default
 *
 *   Revision 1.6.6.3  2006/03/30 12:35:13  martius
 *   documentation updated
 *
 *   Revision 1.6.6.2  2006/01/18 16:48:35  martius
 *   stored and restore
 *
 *   Revision 1.6.6.1  2005/11/14 15:38:17  martius
 *   moved to selforg
 *
 *   Revision 1.6  2005/11/14 12:46:19  martius
 *   added number of controlled motors
 *
 *   Revision 1.5  2005/11/09 13:56:34  martius
 *   const in instspectable functions
 *
 *   Revision 1.4  2005/10/06 17:06:57  martius
 *   switched to stl lists
 *
 *   Revision 1.3  2005/08/06 20:47:54  martius
 *   Commented
 *
 *   Revision 1.2  2005/07/07 10:25:47  martius
 *   added Phaseshift
 *
 *   Revision 1.1  2005/07/06 13:55:33  fhesse
 *   initial version, realising sine and cosine outputs
 *                                            *
 *                                                                         *
 ***************************************************************************/
#ifndef __SINECONTROLLER_H
#define __SINECONTROLLER_H


#include <stdio.h>
#include "abstractcontroller.h"

/**
 * class for robot control with sine, sawtooth and impuls  
 * 
 * period is the length of the period in steps and 
 * phaseshift is the phase difference between channels given in Pi/2
 */
class SineController : public AbstractController {
public:
  enum function {Sine, SawTooth, Impulse};

  /**     
     @param controlmask bitmask to select channels to control (default all)
     @param function controller function to use
   */
  SineController(unsigned long int controlmask = (~0), function func = Sine );

  /** initialisation of the controller with the given sensor/ motornumber 
      Must be called before use.
  */
  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
  
  /** @return Number of sensors the controller was initialised 
      with or 0 if not initialised */
  virtual int getSensorNumber() const {return number_sensors;}


  /** @return Number of motors the controller was initialised 
      with or 0 if not initialised */
  virtual int getMotorNumber() const {return number_motors;}

  /** performs one step ( the same as StepNoLearning).
      Calculates motor commands from sensor inputs.
      @param sensors sensors inputs scaled to [-1,1]
      @param sensornumber length of the sensor array
      @param motors motors outputs. MUST have enough space for motor values!
      @param motornumber length of the provided motor array
  */
  virtual void step(const sensor* sensors, int sensornumber, 
		    motor* motors, int motornumber);
  /** performs one step. 
      @see step
  */
  virtual void stepNoLearning(const sensor* , int number_sensors, 
			      motor* , int number_motors);
  

  /********* STORABLE INTERFACE ******/
  /// @see Storable
  virtual bool store(FILE* f) const { 
    Configurable::print(f,"");
    return true;
  }

  /// @see Storable
  virtual bool restore(FILE* f) { 
    Configurable::parse(f);    
    return true;
  }

  /// sine 
  static double sine(double x, double _unused);
  /// saw tooth shape oscillator
  static double sawtooth(double x, double _unused);
  /// impuls shaped oscillator (+-1 for impulsWidth part of the time)
  static double impuls(double x, double impulsWidth);

protected:

  std::string name;
  int number_sensors;
  int number_motors;
  unsigned long int controlmask; // bitmask to select channels. (the others are set to 0)

  paramval period;
  paramval phaseShift;
  paramval impulsWidth;
  double phase; // phase of oscillator
  paramval amplitude;

  double (*osci) (double x, double param); // oscillator function
};

#endif 
