// NOTE: Some of the variables used within the below methods
// are created in the file "eddl_sk_methodvariables.h"


//***********************************
// METHODS
//***********************************

METHOD CURRENT_LIMIT_MAX_method
{
    LABEL "Calculate CURRENT_LIMIT_MAX";
    DEFINITION
    {
		double	dMaxCurrent;
		double	dNormalDutyRating;  // What to do for this value?
		double	dHeavyDutyRating;
		double	dMotorRatedCurrent;
		double	dPowerFactor;
		double	dR1;
		double	dR2;
		double	dR3;
		double	dR4;
		double	dR5;
		double	dR6;
		double dSqr;
		
		int		iCnt;		
		int		iHeavyDutyRating;
		int		iMotorRatedCurrent;
		int		iPowerFactor;
		int		iResultPos;
		int		iResultNeg;
		
		// Get Drive Heavy Duty Current Rating
		iHeavyDutyRating = M11_P32;
		dHeavyDutyRating = iHeavyDutyRating / 100.00;
		ACKNOWLEDGE("dHeavyDutyRating = %{dHeavyDutyRating}");
		
		// Get Normal Duty Current Rating
		dNormalDutyRating = dHeavyDutyRating * 1.2;  // This is just a guestimate since I can't read the value
		ACKNOWLEDGE("dNormalDutyRating = %{dNormalDutyRating}");
		
		
		iMotorRatedCurrent = M5_P7;
		dMotorRatedCurrent = iMotorRatedCurrent / 100.00;
		ACKNOWLEDGE("dMotorRatedCurrent = %{dMotorRatedCurrent}");
				
		iPowerFactor = M5_P10;
		dPowerFactor = iPowerFactor / 1000.000;
		ACKNOWLEDGE("dPowerFactor = %{dPowerFactor}");
		
		if (dMotorRatedCurrent <= dHeavyDutyRating)
		{
			dMaxCurrent = 1.5 * dHeavyDutyRating;
		}
		else
		{
			dMaxCurrent = 1.1 * dNormalDutyRating;
		}
		ACKNOWLEDGE("dMaxCurrent = %{dMaxCurrent}");

		dR1 = dMaxCurrent / dMotorRatedCurrent;
		ACKNOWLEDGE("dR1 = %{dR1}");
		
		//dR2 = pow(dR1, 2);
		dR2 = dR1 * dR1;
		ACKNOWLEDGE("dR2 = %{dR2}");
		
		//dR3 = pow(dPowerFactor, 2);
		dR3 = dPowerFactor * dPowerFactor;
		ACKNOWLEDGE("dR3 = %{dR3}");
		
		dR4 = (dR2 / dPowerFactor) + dR3 - 1;
		ACKNOWLEDGE("dR4 = %{dR4}");
		
		iCnt = 0;
		dSqr = 2187;
		while (iCnt <= 4)
		{
			dSqr = ( (dSqr + (dR4 / dSqr)) / 2);
			iCnt = iCnt + 1;
		}
		
		//dR5 = sqrt(dR4);		
		dR5 = dSqr;
		ACKNOWLEDGE("dR5 = %{dR5}");
				
		dR6 = dR5 * 100;
		ACKNOWLEDGE("dR6 = %{dR6}");
		
		iResultPos = dR6 * 10;
		ACKNOWLEDGE("iResultPos = %{iResultPos}");
		
		iResultNeg = -1 * iResultPos;
		ACKNOWLEDGE("iResultNeg = %{iResultNeg}");
		
		CURRENT_LIMIT_MAX = iResultPos;
		NEG_CURRENT_LIMIT_MAX = iResultNeg;
				
		save_values();
	}
}

METHOD ENABLE_MOTORIZED_POT_method
{
    LABEL "Enable Motorized Pot";
    DEFINITION
    {
		if (MOTORIZED_POT_STATUS == 0)
		{
			iassign(M1_P14, 0);		// Reference Select set to A1.A2
			iassign(M7_P14, 0);		// Analog 2 Destination set to Unassigned
			iassign(M8_P41, 8);		// Digital Output Control set to "User"
			save_values();			
			iassign(M8_P21, 926);	// B3 Digital I/O Destination set to Motorized Pot Up
			iassign(M8_P24, 927);	// B6 Digital Input Destination set to Motorized Pot Down
			iassign(M8_P25, 141);	// B7 Digital Input Destination set to Analog Ref 2 Select
			iassign(M8_P31, 0);		// Terminal B3 Mode set to Digital Input Mode
			iassign(M9_P4, 922);	// Logic Function 1 Source 1 set to Motorized Pot Bipolar Select
			iassign(M9_P7, 1);		// Logic Function 1 Source 2 Invert set to ON
			iassign(M9_P10, 110);	// Logic Function 1 Destination set to Allow Negative References
			iassign(M9_P14, 802);	// Logic Function 2 Source 1 set to Digital Input 2 state
			iassign(M9_P17, 1);		// Logic Function 2 Source 2 Invert set to ON
			iassign(M9_P20, 928);	// Logic Function 2 Destination set to Motorized Pot Reset
			iassign(M9_P25, 137);	// Motorized Pot Destination set to Analog Reference 2
			ACKNOWLEDGE("Configuring necessary parameters for Motorized Pot Mode.");
		}
		else
		{
			ACKNOWLEDGE("Motorized Pot Mode is already enabled.");
		}
		
		MOTORIZED_POT_STATUS = 1;

		save_values();
	}
}

METHOD DISABLE_MOTORIZED_POT_method
{
    LABEL "Disable Motorized Pot";
    DEFINITION
    {
		if (MOTORIZED_POT_STATUS == 1)
		{
			iassign(M7_P14, 137);	// Analog 2 Destination set to Analog 2 Reference
			iassign(M8_P41, 0);		// Digital Output Control set to "At zero speed"
			save_values();
			iassign(M8_P21, 1003);	// B3 Digital I/O Destination set to zero speed
			iassign(M8_P24, 6.32);	// B6 Digital Input Destination set to Run Reverse
			iassign(M8_P25, 141);	// B7 Digital Input Destination set to Analog Ref 2 Select
			iassign(M8_P31, 1);		// Terminal B3 Mode set to Digital Output Mode
			iassign(M9_P4, 0);		// Logic Function 1 Source 1 set to Unassigned
			iassign(M9_P7, 0);		// Logic Function 1 Source 2 Invert set to OFF
			iassign(M9_P10, 0);		// Logic Function 1 Destination set to Unassigned
			iassign(M9_P14, 0);		// Logic Function 2 Source 1 set to Unassigend
			iassign(M9_P17, 0);		// Logic Function 2 Source 2 Invert set to OFF
			iassign(M9_P20, 0);		// Logic Function 2 Destination set to Unassigned
			iassign(M9_P25, 0);		// Motorized Pot Destination set to Unassigned


			ACKNOWLEDGE("Setting required parameters back to default values.");
		}
		else
		{
			ACKNOWLEDGE("Motorized Pot Mode is already disabled.");
		}
		
		MOTORIZED_POT_STATUS = 0;

		save_values();
	}
}

METHOD PID_CONFIGURE_method
{
    LABEL "Configure PID Parameters";
    DEFINITION
    {
		if (M14_P8 == 1)
		{
			ACKNOWLEDGE("Configuring parameters to enable PID functionality.");
			iassign(M1_P14, 0);
			iassign(M7_P14, 0);
			iassign(M8_P41, 8);
			save_values();
			iassign(M8_P25, 141);
			iassign(M14_P3, 1801);
			iassign(M14_P4, 702);
			iassign(M14_P9, 805);
			iassign(M14_P16, 137);
		}
		else
		{
			ACKNOWLEDGE("Restoring default values to remove PID functionality.");
			iassign(M1_P14, 0);
			iassign(M7_P14, 137);
			iassign(M8_P41, 0);
			save_values();
			iassign(M14_P3, 0);
			iassign(M14_P4, 0);
			iassign(M14_P9, 0);
			iassign(M14_P16, 0);
		}
		
		save_values();
	}
}

METHOD RESTORE_DEFAULTS_method
{
	LABEL "Restore Drive Parameter Default Values";
	DEFINITION
    {
		int		iDefaultSelect;
		int		iM1P0Feedback;

		iDefaultSelect = SELECT_FROM_LIST("Which regional default values do you wish to load?", "US/North America; Europe");

		if (iDefaultSelect == 0)
		{
			iassign(M1_P0, 1244);		
		}
		else
		{
			iassign(M1_P0, 1233);	
		}

		M10_P38 = 100;

		do
		{
			if (iDefaultSelect == 0)
			{
				DELAY(1, "Applying US/North American defaults.  Please wait...");		
			}
			else
			{
				DELAY(1, "Applying European defaults.  Please wait...");	
			}
		} while (M1_P0 != 0)


		ACKNOWLEDGE("Default values have been loaded.  It is now necessary to reload the user values from each of the setup views.");

		save_values();
	}	
}

METHOD RESET_method
{
	LABEL "Reset the Drive";
	DEFINITION
    {
		int		iSelectedOptn;
		
		MenuDisplay(RESET_PAGE1_window, "Next", iSelectedOptn);

		M10_P38 = 100;
		
		do
		{		 
			DELAY(1, "Reseting drive.  Please wait...");
		} while (M10_P38 == 100)
		
		ACKNOWLEDGE("The Drive Reset method is complete.  The drive has been reset.");
	}	
}

METHOD SAVE_DRV_PARAM_SET_method
{
	LABEL "Save Drive Parameters";
	DEFINITION
    {
		int		iSelectedOptn;
		
		MenuDisplay(SAVE_TO_NVM_PAGE1_window, "Next" , iSelectedOptn);

		M0_P0 = 1001;
		M10_P38 = 100;
		
		do
		{		 
			DELAY(1, "Saving the drive database and resetting drive.  Please wait...");
		} while (M0_P0 == 1001)
		
		ACKNOWLEDGE("The Save to NVM method is complete.  The entire drive parameter set has been saved to Non-Volatile Memory.");
	}	
}


