// © RiceMotion ( Robert Carl Rice ) 2012-2016 - All rights reserved


// This software makes use of tools and libraries obtained from open source projects or released for

// use by relevant hardware manufactures. However, this software is NOT a part of any open source project.

// It is released only on a "need to know" basis for beta testers of the "RiceCNC Interpolation Engine".

// Recipents of this source code must respect the confidential nature of this software and prevent any

// distribution that could result in counterfeit copies of the "RiceCNC Interpolation Engine".


// © RiceMotion ( Robert Carl Rice ) 2012-2016 - All rights reserved

#ifndef HallFeedback_h

#define HallFeedback_h


#include "Energia.h"

#include <StepCounter.h>


class InputPin;

class BLDC3;

class DRV8305Axis;

class Machine;


class HallFeedback : public StepCounter {

     DRV8305Axis*     axis;

     BLDC3*               bldc3;


     InputPin*          hallA;

     InputPin*          hallB;

     InputPin*          hallC;

     

     enum     HallState {

          CW_A1,

          CW_C0,

          CW_B1,

          CW_A0,

          CW_C1,

          CW_B0,

          CCW_A1,

          CCW_B0,

          CCW_C1,

          CCW_A0,

          CCW_B1,

          CCW_C0,

          HallStart,

          S_A1,

          S_B1,

          S_C1,

          S_A0,

          S_B0,

          S_C0 };


     uint          hallState;


     void

          calculateFeedback();


     public:

          HallFeedback(

               char*               data,

               String*               msgPtr,

               DRV8305Axis*     axis );


          ~HallFeedback();


          bool          synchronized;

          uint          expirationCounter;


          // vector is direction, sensor, transition

          uint32_t     phaseTransition[ 12 ];

          uint32_t     stallDetectVoltage;     // 100% = 4096


          int32_t          error;

          int32_t          priorError;

          int32_t          integralError;


     // Angle PID parameter settings

          float          angleDerivative;

          float          angleProportion;

          float          angleIntegral;


     // Voltage PID parameter settings

          float          voltageIntegral;


          float          speedProportion;


          int32_t          feedbackAngle;

          int32_t          feedbackTorque;


          void

               initPhaseTransitionTable();


          void

               reportHallPinChange( String* msgPtr );


          void

               readHallPhasing(

                    char*          data,

                    String*          msgPtr );


          uint

               initCount();


          bool

               valid();


          virtual void

               pinISR( InputPin* );


          bool

               calculateHallFeedback(

                    Machine*     machine );

     };


#endif