PROGRAM VFD_ConfigMap
(* READING PARAMS *)

(* Nameplate HP *)
VFD_R_NPHP(VFD_Read, VFD_IPAddress, 500, 37);
IF VFD_R_NPHP.MSGParRead_Q_RTRIG.Q THEN
	cfg_NameplateHP := ANY_TO_REAL(VFD_R_NPHP.ParameterValue) / 74.6;
END_IF;

(* Nameplate FLA *)
VFD_R_NPFLA(VFD_Read, VFD_IPAddress, 500, 34);
IF VFD_R_NPFLA.MSGParRead_Q_RTRIG.Q THEN
	cfg_NameplateFLA := ANY_TO_REAL(VFD_R_NPFLA.ParameterValue) / 10.0;
END_IF;

(* Nameplate Volts *)
VFD_R_NPVolts(VFD_Read, VFD_IPAddress, 500, 31);
IF VFD_R_NPVolts.MSGParRead_Q_RTRIG.Q THEN
	cfg_NameplateVolts := ANY_TO_REAL(VFD_R_NPVolts.ParameterValue);
END_IF;

(* Nameplate Hertz *)
VFD_R_NPHz(VFD_Read, VFD_IPAddress, 500, 32);
if VFD_R_NPHz.MSGParRead_Q_RTRIG.Q THEN
	cfg_NameplateHz := ANY_TO_REAL(VFD_R_NPHz.ParameterValue);
END_IF;

(* Nameplate RPM *)
VFD_R_NPRPM(VFD_Read, VFD_IPAddress, 500, 36);
IF VFD_R_NPRPM.MSGParRead_Q_RTRIG.Q THEN
	cfg_NameplateRPM := ANY_TO_REAL(VFD_R_NPRPM.ParameterValue);
END_IF;

(* Nameplate OL Current *)
VFD_R_NPOLCurrent(VFD_Read, VFD_IPAddress, 500, 33);
IF VFD_R_NPOLCurrent.MSGParRead_Q_RTRIG.Q THEN
	cfg_NameplateOLCurrent := ANY_TO_REAL(VFD_R_NPOLCurrent.ParameterValue) / 10.0;
END_IF;

(* Motor Poles *)
VFD_R_MotorPoles(VFD_Read, VFD_IPAddress, 500, 35);
IF VFD_R_MotorPoles.MSGParRead_Q_RTRIG.Q THEN
	cfg_MotorPoles := VFD_R_MotorPoles.ParameterValue;
END_IF;

(* Max Frequency *)
VFD_R_MaxFreq(VFD_Read, VFD_IPAddress, 500, 44);
IF VFD_R_MaxFreq.MSGParRead_Q_RTRIG.Q THEN
	cfg_MaxFreq := ANY_TO_REAL(VFD_R_MaxFreq.ParameterValue) / 100.0;
END_IF;

(* Min Frequency *)
VFD_R_MinFreq(VFD_Read, VFD_IPAddress, 500, 43);
IF VFD_R_MinFreq.MSGParRead_Q_RTRIG.Q THEN
	cfg_MinFreq := ANY_TO_REAL(VFD_R_MinFreq.ParameterValue) / 100.0;
END_IF;

(* Stop Mode [HARD CODED TO COAST] *)
VFD_R_StopMode(VFD_Read, VFD_IPAddress, 500, 45);
IF VFD_R_StopMode.MSGParRead_Q_RTRIG.Q THEN
	cfg_StopMode := VFD_R_StopMode.ParameterValue;
END_IF;

(* Torque Perf Mode [HARD CODED TO V/Hz] *)
VFD_R_TorquePerfMode(VFD_Read, VFD_IPAddress, 500, 39);
IF VFD_R_TorquePerfMode.MSGParRead_Q_RTRIG.Q THEN
	cfg_TorquePerfMode := VFD_R_TorquePerfMode.ParameterValue;
END_IF;
	
IF VFD_Read AND 
	VFD_R_MaxFreq.MSGParRead.Q AND
	VFD_R_MinFreq.MSGParRead.Q AND
	VFD_R_MotorPoles.MSGParRead.Q AND
	VFD_R_NPFLA.MSGParRead.Q AND
	VFD_R_NPHP.MSGParRead.Q AND
	VFD_R_NPHz.MSGParRead.Q AND
	VFD_R_NPOLCurrent.MSGParRead.Q AND
	VFD_R_NPRPM.MSGParRead.Q AND
	VFD_R_NPVolts.MSGParRead.Q AND
	VFD_R_StopMode.MSGParRead.Q AND
	VFD_R_TorquePerfMode.MSGParRead.Q
THEN
	VFD_read := FALSE;
END_IF;



(* Nameplate HP *)
temp_nphp :=  ANY_TO_UINT(cfg_NameplateHP * 74.6);
VFD_W_NPHP(VFD_Write, VFD_IPAddress, 3, 37, temp_nphp);

(* Nameplate FLA *)
temp_fla := ANY_TO_UINT(cfg_NameplateFLA * 10.0);
VFD_W_NPFLA(VFD_Write, VFD_IPAddress, 3, 34, temp_fla);

(* Nameplate Volts *)
temp_volts := ANY_TO_UINT(cfg_NameplateVolts);
VFD_W_NPVolts(VFD_Write, VFD_IPAddress, 3, 31, temp_volts);

(* Nameplate Hertz *)
temp_hz :=ANY_TO_UINT(cfg_NameplateHz);
VFD_W_NPHz(VFD_Write, VFD_IPAddress, 3, 32, temp_hz);

(* Nameplate RPM *)
temp_rpm :=  ANY_TO_UINT(cfg_NameplateRPM);
VFD_W_NPRPM(VFD_Write, VFD_IPAddress, 3, 36,temp_rpm);

(* Nameplate OL Current *)
temp_olcurrent := ANY_TO_UINT(cfg_NameplateOLCurrent * 10.0);
VFD_W_NPOLCurrent(VFD_Write, VFD_IPAddress, 3, 33, temp_olcurrent);

(* Motor Poles *)
VFD_W_MotorPoles(TRUE, VFD_IPAddress, 3, 35, cfg_MotorPoles);

(* Max Frequency *)
temp_maxfreq :=  ANY_TO_UINT(cfg_MaxFreq * 100.0);
VFD_W_MaxFreq(VFD_Write, VFD_IPAddress, 3, 44, temp_maxfreq);

(* Min Frequency *)
temp_minfreq := ANY_TO_UINT(cfg_MinFreq * 100.0);
VFD_W_MinFreq(VFD_Write, VFD_IPAddress, 3, 43, temp_minfreq);

(* Stop Mode [HARD CODE TO COAST] *)
VFD_W_StopMode(VFD_Write, VFD_IPAddress, 3, 45, 1);

(* Torque Perf Mode [HARD COAD TO V/Hz] *)
VFD_W_TorquePerfMode(VFD_Write, VFD_IPAddress, 3, 39, 0);


IF VFD_Write AND
	VFD_W_MaxFreq.MSGParWrite.Q AND
	VFD_W_MinFreq.MSGParWrite.Q AND
	VFD_W_MotorPoles.MSGParWrite.Q AND
	VFD_W_NPFLA.MSGParWrite.Q AND
	VFD_W_NPHP.MSGParWrite.Q AND
	VFD_W_NPHz.MSGParWrite.Q AND
	VFD_W_NPOLCurrent.MSGParWrite.Q AND
	VFD_W_NPRPM.MSGParWrite.Q AND
	VFD_W_NPVolts.MSGParWrite.Q AND
	VFD_W_StopMode.MSGParWrite.Q AND
	VFD_W_TorquePerfMode.MSGParWrite.Q
THEN
	VFD_Write := FALSE;
END_IF;

VFD_WriteTimer(VFD_Write, VFD_RW_Time);
VFD_ReadTimer(VFD_Read, VFD_RW_Time);

IF VFD_ClearFault AND NOT(VFD_Fault OR VFD_Fault_CommError OR VFD_Fault_DriveFault) THEN
	VFD_ClearFault := FALSE;
END_IF;


END_PROGRAM