//#############################################################################
// $Copyright:
// Copyright (C) 2017-2023 Texas Instruments Incorporated - http://www.ti.com/
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//   Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
//
//   Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the
//   distribution.
//
//   Neither the name of Texas Instruments Incorporated nor the names of
//   its contributors may be used to endorse or promote products derived
//   from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//#############################################################################


//! \file   //solutions/tidm_02010_dmpfc/common/include/motor1_drive.h
//!
//! \brief  header file to be included in all labs
//!         support for HVAC reference board with F28002x/F28003x/F280013x
//!


#ifndef MOTOR1_DRIVE_H
#define MOTOR1_DRIVE_H


//*****************************************************************************
//
// If building with a C++ compiler, make all of the definitions in this header
// have a C binding.
//
//*****************************************************************************
#ifdef __cplusplus
extern "C"
{
#endif

//*****************************************************************************
//
//! \defgroup MOTOR1_DRIVE MOTOR1_DRIVE
//! @{
//
//*****************************************************************************

// includes
#include "libraries/math/include/math.h"
#include <math.h>

#include "userParams.h"
#include "motor_common.h"

// *****************************************************************************
// the defines


// *****************************************************************************
// the typedefs


// *****************************************************************************
// the globals
extern float32_t angleFOCM1_rad;     //!< the rotor angle from FOC modules

#ifdef MOTOR1_ESMO
extern ESMO_Handle esmoM1Handle;
extern ESMO_Obj    esmoM1;

extern SPDFR_Handle spdfrM1Handle;
extern SPDFR_Obj    spdfrM1;

extern float32_t angleCompM1_rad;
extern float32_t anglePLLM1_rad;
extern float32_t angleSMOM1_rad;
extern float32_t speedPLLM1_Hz;
extern float32_t frswPosM2_sf;
#endif  // MOTOR1_ESMO

#if defined(MOTOR1_FAST)
extern EST_Handle    estM1Handle;     //!< the handle for the estimator
extern EST_InputData_t estInputDataM1;
extern EST_OutputData_t estOutputDataM1;

extern float32_t angleDeltaM1_rad;   //!< the rotor angle compensation value
extern float32_t angleESTM1_rad;     //!< the rotor angle from EST modules
extern float32_t speedESTM1_Hz;     //!< the speed from EST modules
#endif  // MOTOR1_FAST

#if((DMC_BUILDLEVEL == DMC_LEVEL_2) || (DMC_BUILDLEVEL == DMC_LEVEL_3) || \
        defined(MOTOR1_ESMO))
//!< the handles for Angle Generate for open loop control
extern ANGLE_GEN_Handle angleGenM1Handle;

//!< the Angle Generate onject for open loop control
extern ANGLE_GEN_Obj    angleGenM1;
extern float32_t angleGenM1_rad;     //!< the rotor angle from Generator modules
extern float32_t speedRefM1_Hz;
#endif  // ((DMC_BUILDLEVEL <= DMC_LEVEL_3) || defined(MOTOR1_ESMO))

#if defined(MOTOR1_DCLINKSS)
extern DCLINK_SS_Handle dclinkM1Handle;
extern DCLINK_SS_Obj    dclinkM1;
#endif  // MOTOR1_DCLINKSS
#if defined(MOTOR1_SSIPD)
extern SSIPD_Handle ssipdHandle;
extern SSIPD_Obj    ssipd;

// IPD is only for compressor
extern bool flagEnableIPD;
extern float32_t angleOffsetIPD_rad;
extern float32_t angleDetectIPD_rad;
#endif  // MOTOR1_SSIPD

#ifdef MOTOR1_MTPA  // MPTA and FWC are only for compressor
extern float32_t mtpaKconst;
extern float32_t LsOnline_d_H;
extern float32_t LsOnline_q_H;
extern float32_t fluxOnline_Wb;
extern float32_t angleMTPA_rad;

extern MTPA_Handle mtpaHandle;
extern MTPA_Obj    mtpa;
#endif  // MOTOR1_MTPA

#if defined(MOTOR1_FWC) // MPTA and FWC are only for compressor
extern float32_t Kp_fwc;
extern float32_t Ki_fwc;
extern float32_t angleFWCMax_rad;
extern float32_t angleFWC_rad;
extern float32_t VsRef_pu;
extern float32_t VsRef_V;

extern PI_Handle piHandle_fwc;
extern PI_Obj    pi_fwc;
#endif  // MOTOR1_FWC

#if defined(MOTOR1_VIBCOMPA)
extern VIB_COMP_Handle vibCompHandle;
extern VIB_COMP_Obj    vibComp;

extern float32_t       vibCompAlpha;
extern float32_t       vibCompGain;
extern int16_t         vibCompIndexDelta;
extern bool            vibCompFlagReset;
extern bool            vibCompFlagEnable;
#elif defined(MOTOR1_VIBCOMPT)
extern VIB_COMP_Handle vibCompHandle;
extern VIB_COMP_Obj    vibComp;

extern float32_t       compressorAngle;
extern float32_t       vibCompAlpha0;
extern float32_t       vibCompAlpha120;
extern float32_t       vibCompAlpha240;
#endif  // MOTOR1_VIBCOMPA || MOTOR1_VIBCOMPT


extern float32_t angleCurrentM1_rad;

extern bool flagEnableFWCM1;
extern bool flagEnableMTPAM1;
extern bool flagUpdateMTPAParamsM1;


// *****************************************************************************
// the function prototypes

//! \brief The main interrupt service (ISR) routine
extern __interrupt void motor1CtrlISR(void);

//! \brief  Initialize motor control parameters
extern void initMotor1CtrlParameters(void);

//! \brief  Run motor control in main loop
extern void runMotor1Control(void);

//! \brief update eSMO control parameters
extern void updateM1SMOParams(void);

//! \brief  Updates the FWC parameters
extern void updateFWCParams(void);

//! \brief  Updates the MTPA parameters
extern void updateMTPAParams(void);

//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************

//*****************************************************************************
//
// Mark the end of the C bindings section for C++ compilers.
//
//*****************************************************************************
#ifdef __cplusplus
}
#endif // extern "C"

#endif // end of MOTOR1_DRIVE_H definition
