/** \file
 *
 *  Contains the RudderServo class declaration.
 *
 *  RudderServo.h should only be included by RudderServo.cpp
 *  Other classes should include RudderServoIF.h
 *
 *  Copyright (c) 2007,2008,2009 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 */

#ifndef RUDDERSERVO_H_
#define RUDDERSERVO_H_

#include "EZServoServo.h"
#include "io/LoadControl.h"
#include "io/UartStream.h"

class UniversalDataWriter;

/**
 *  This is the device driver for the EZServo that controls the vehicle's
 *  rudder.  Since it shares the same RS-485 bus with the other EZServos,
 *  it is a child class of EZServoServo, which has a static instance of
 *  the StdUart class to interact with the bus.
 *
 *  RudderServo.h should only be included by RudderServo.cpp
 *  Other classes should include RudderServoIF.h
 *
 *  \ingroup modules_controller
 */
class RudderServo : public EZServoServo
{
public:
    RudderServo( const Module* module );
    virtual ~RudderServo();

    virtual void run();

    /// Do what needs to be done to run
    /// Similar to initialize, in old init/run/uninit sequence
    virtual RunState start();

    /// Might follow a STOP...START sequence
    virtual RunState starting();

    /// Pause for a short period (indicated by pauseTime)
    virtual RunState pause();

    /// Should eventually follow a PAUSE request: should set continueTime
    virtual RunState paused();

    /// Resume from PAUSE
    virtual RunState resume();

    /// Might follow a PAUSE...RESUME sequence
    virtual RunState resuming();

    /// Should eventually follow a START request or RESETTING
    virtual RunState runnable();

    /// Might occur in case of Error
    virtual RunState resetting()
    {
        return stop();
    }

    /// Initial state -- can be later followed by START
    virtual RunState stop();

    virtual RunState stopping();

    /// Initial state -- can be later followed by START
    virtual RunState stopped();

    // Shuts down motor controllers
    void uninitialize( void );

    /// Should return [myNamespace]::SIMULATE_HARDWARE, or [myNamespace]::POWER, etc
    virtual ConfigURI getConfigURI( ConfigOption configOption ) const;

private:

    // Note that the copy constructor below is private and not given a body.
    // Any attempt to call it will return a compiler error.
    RudderServo( const RudderServo& old ); // disallow copy constructor

    // Instance of load controller
    LoadControl loadControl_;

    // The outputs themselves
    UniversalDataWriter* rudderAngleWriter_;

    // The inputs
    DataReader* rudderAngleReader_;

    // returns true if data is requested from one of the readers
    bool isDataRequested();

    // returns true if a new position has been commanded
    bool isNeeded();

    typedef enum
    {
        INITIALIZE,   // Program basic initialization into EEPROM
        WAIT,         // Wait for EEPROM write to finish
        HOME,         // Do any centering/homing, etc if needed
        EXECUTE,      // Execute any stored programs
        VERIFY,       // Confirm operable and no errors
        DONE,         // Ready to run
    } StartupSequence;

    StartupSequence startup_;

    // indicates whether things are ok to run
    bool ok_;

    // Actual and commanded rudder angles
    float actualRudderAngle_;
    float cmdRudderAngle_;

    // Debugging outputs
    bool debug_;

    //-----------------------------------------------------------------*/

    float ruddDeadbandCfg_;

    //-----------------------------------------------------------------*/

    // Config readers
    ConfigReader* rudderDeadbandCfgReader_;

    // read vehicle config variables
    bool readConfig( void );

    // the given motor controller is ready to receive commands
    bool rudderRdy_;

    // Initialization routines. Returns true if initialization completed without error
    bool initRudder( void );
};

#endif /*RUDDERSERVO_H_*/
