B100 //Clear existing program banks
L100 //Clear existing logic banks
K34=00 //Set Output 2 as analog output
K85=1 //Run logic bank 1 on power up
K87=1 //Logic bank scan time of 1ms
var Px V1.1 //Variable V1 is named Px (current Position)
V1.1="Px" //V1 is set to the current motor position
V2.1="AO2" //V2 is set to the analog output value
V5.1=0 //V5 is used as an accumulator in the calculations
var offset V6.1 //Variable V6 is named (used in calculations)
V6.1=0 //V6 is an offset in the event of negative position values
var P_Max V4.1 //Variable V4 is named P_Max (maximum position value)
P_Max=5000 //Set max position to 1000
var P_Min V3.1 //Variable V3 is named P_Min (minimum position value)
P_Min=-5000 //set min position to 0
N1.1=-1 //constant used in calculation
L1 //Logic bank 1
V5.1=P_Max-P_Min; //Subtract min position from max position to find range
P_Min<0,CL2,offset=0; //If the minimum position is negative, call logic bank 2, otherwise set the offset to 0
V5.1=V5.1/256; //Scale the range to the range of the analog output
V5.1=Px/V5.1; //Scale the output to the current position value
V5.1=V5.1+V6.1; //Add the offset to the current calculated value
V5.1>255,V5.1=255,T0; //If the value is above the analog range, set the analog output to max
V5.1<0,V5.1=0,T0; //If the value is below the analog range, set the analog output to 0
V2.1=V5.1; //Set analog output to the calculated value
END.1
L2 //L2 is used to scale the offset value in the event of negative positions
offset=V5.1/256;
offset=P_Min/offset;
offset=offset*N1.1;
End.1
$ //save program to EEPROM |