﻿<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1">
  <POU Name="fb_SheaveMesurements" Id="{48ed6351-6f5d-403f-bc44-4ab913f4d7b0}" SpecialFunc="None">
    <Declaration><![CDATA[FUNCTION_BLOCK PUBLIC fb_SheaveMesurements
VAR_INPUT
	ID : INT;
	config : REFERENCE TO ConfigSheaveMeasurement;
	encoder : REFERENCE TO IncrementalEncoder;			// must be run on separate high-speed task
END_VAR
VAR
	Loadcell_IO_in 		AT %I*  : p6com.EL30xxIO_in;
END_VAR]]></Declaration>
    <Implementation>
      <ST><![CDATA[// this page is intentionally left blank]]></ST>
    </Implementation>
    <Method Name="ResetLength" Id="{04970739-da79-473b-b851-0470c69b5a1b}">
      <Declaration><![CDATA[{attribute 'TcRpcEnable'}
METHOD PUBLIC ResetLength : BOOL
VAR_INPUT
END_VAR
]]></Declaration>
      <Implementation>
        <ST><![CDATA[encoder.reset();]]></ST>
      </Implementation>
    </Method>
    <Property Name="Length" Id="{41318a35-30fc-4b18-b31e-a91b0ad1ef45}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
{attribute 'TcRpcEnable'}
// (m)
PROPERTY PUBLIC Length : LREAL]]></Declaration>
      <Get Name="Get" Id="{13aafd3c-cd14-416e-912e-145e5a5bf287}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[Length := config.SheaveDiameter * math.Pi * encoder.revolutions;]]></ST>
        </Implementation>
      </Get>
      <Set Name="Set" Id="{4c2485cf-2a70-47e3-aed4-733ba4f565fa}">
        <Declaration><![CDATA[
VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[ // maybe add code to set a specific length by adding/subtracting an offset?]]></ST>
        </Implementation>
      </Set>
    </Property>
    <Method Name="CalibrateZeroLoad" Id="{8b2c0629-a267-44df-a258-eca92d45af7c}">
      <Declaration><![CDATA[{attribute 'TcRpcEnable'}
// Call to store raw value to zero load config
METHOD PUBLIC CalibrateZeroLoad : BOOL
VAR_INPUT
END_VAR
]]></Declaration>
      <Implementation>
        <ST><![CDATA[IF Loadcell_IO_in.status.bits.Error THEN
	DebugMessage(concat('Loadcell ',INT_TO_STRING(ID)),config.name,' Calibration failed: Sensor error.');
	RETURN;
END_IF
config.ZeroLoadRawValue := Loadcell_IO_in.input ;
DebugMessage(concat('Loadcell ',INT_TO_STRING(ID)),config.name,' Zero calibration OK');]]></ST>
      </Implementation>
    </Method>
    <Property Name="Tension" Id="{9f17722a-cde3-4101-b380-3cb546828e98}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
{attribute 'TcRpcEnable'}
// Calibrated Load in kg
PROPERTY Tension : LREAL]]></Declaration>
      <Get Name="Get" Id="{3da441bf-665b-4c25-bb39-7568f40f5a75}">
        <Declaration><![CDATA[VAR
	_tension : LREAL;
	_input : LREAL;
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[IF Loadcell_IO_in.status.bits.Error THEN
	Tension := -1;
	RETURN;
END_IF

_tension := scale(
	Value :=  Loadcell_IO_in.input, 
	InputMin := config.ZeroLoadRawValue, 
	InputMax := config.CalibratedLoadRawValue,
	OutputMin := 0,
	OutputMax := config.CalibrationLoad,
	LimitOutput := FALSE);
	
Tension := MAX(0,_tension);
	]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="Error" Id="{a24403a0-06b5-47c0-b741-d9e238f90a49}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
{attribute 'TcRpcEnable'}
// true if Sensor ERROR
PROPERTY Error : BOOL]]></Declaration>
      <Get Name="Get" Id="{7322579e-258b-4e6d-86a3-f6c8663c4b17}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[Error := Loadcell_IO_in.status.bits.Error ;]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="LoadCellRawValue" Id="{c6a354ad-5d76-477b-87c3-4fac9ddb86de}">
      <Declaration><![CDATA[{attribute 'monitoring' := 'call'}
{attribute 'TcRpcEnable'}
PROPERTY PUBLIC LoadCellRawValue : INT]]></Declaration>
      <Get Name="Get" Id="{fa5bb6ed-4101-4bdf-953c-70d3da18b4aa}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[LoadCellRawValue := Loadcell_IO_in.input;
]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Method Name="CalibrateReferenceLoad" Id="{fb74ff14-29db-423b-a249-2a01a51d033c}">
      <Declaration><![CDATA[{attribute 'TcRpcEnable'}
// Call to store Raw value at specified load
METHOD PUBLIC CalibrateReferenceLoad : BOOL
VAR_INPUT
END_VAR
]]></Declaration>
      <Implementation>
        <ST><![CDATA[IF Loadcell_IO_in.status.bits.Error THEN
	DebugMessage(concat('Loadcell ',INT_TO_STRING(ID)),config.name,': Calibration failed, sensor error.');
	RETURN;
END_IF
config.CalibratedLoadRawValue := Loadcell_IO_in.input ;
DebugMessage(concat('Loadcell ',INT_TO_STRING(ID)),config.name,' Load calibration OK');]]></ST>
      </Implementation>
    </Method>
  </POU>
</TcPlcObject>