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

#ifndef THRUSTERSERVO_H_
#define THRUSTERSERVO_H_

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

class UniversalDataWriter;

/**
 *  This is the device driver for the EZServo that controls the vehicle's
 *  thruster.  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.
 *
 *  ThrusterServo.h should only be included by ThrusterServo.cpp
 *  Other classes should include ThrusterServoIF.h
 *
 *  \ingroup modules_controller
 */
class ThrusterServo : public EZServoServo
{
public:

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

    ThrusterServo( const Module* module );
    virtual ~ThrusterServo();

    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:

    // Instance of load controller
    LoadControl loadControl_;

    // The outputs themselves
    UniversalDataWriter* propOmegaWriter_;

    // The inputs
    DataReader* propOmegaReader_;

    // Configuration readers
    ConfigReader* encoderTksCfgReader_;           // encoder tick/second multiplier
    ConfigReader* tksPerRevCfgReader_;            // encoder ticks/revolution as seen by the controller from HALL A&B
    ConfigReader* deviationCfgReader_;            // Number of encoder ticks deviation allowed between expected and actual
    ConfigReader* allowableBadVelocityCfgReader_; // Allowable Number of bad velocity reads


    // 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_;

    // Debugging outputs
    bool debug_;

    // Stores the previous command
    float lastCmdPropOmega_;
    // Stores the commanded propeller rotational velocity
    float cmdPropOmega_;

    // Stores the commanded motor velocity
    float motorPosCmd_;

    // Stores the commanded velocity
    float controllerVelocityCmd_;

    //------------------- vehicle parameters --------------------------*/
    // Thruster
    float encoderTksCfg_;         // encoder tick/second multiplier
    float tksPerRevCfg_;          // encoder ticks/revolution as seen by the controller from HALL A&B
    float deviationCfg_;          // Number of encoder ticks deviation allowed between expected and actual
    int allowableBadVelocityCfg_; // Allowable Number of bad velocity reads

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

    // Number of bad velocity reads
    int badVelocityCount_;

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

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

    // Current speed is within the bounds of what was commanded
    bool thrustSpdGood_;

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

    // Velocity good after the command has been issues and the thruster is run this long without fault
    Timespan velocityTimeout_;

};

#endif /*THRUSTERSERVO_H_*/
