6/11/2014 12:20 PM | |
Posts: 2 Rating: (0) |
Hi, Someone made this code for me, but I'm strugling with understanding the FOR sequence in the folowing code: The purpose with this code is that we have 4x motors, where the total running hours from SBE blocks are linked to the inputs. The block should then place a number 1-4 on the out-pins according to fewest to most run hours. If I understand the code right, when Iloop=1 it does 3 IF compares, same again when iloop =2 and 3. can this code be compared withbubblesort? only with 4 compares instead of 8 as in the .gif? If someone could help me out with this, i would really appreciate your help! FUNCTION_BLOCK FB97 VAR_INPUT diMotor1Hours:DINT; diMotor2Hours:DINT; diMotor3Hours:DINT; diMotor4hours:DINT; END_VAR VAR_OUTPUT Y_dev1:INT; Y_dev2:INT; Y_dev3:INT; Y_dev4:INT; END_VAR VAR Y_Dev:INT; iLoop:INT; adiMotor:ARRAY[1..4] OF DINT; diMotor:DINT; END_VAR BEGIN adiMotor[1]:=diMotor1Hours; adiMotor[2]:=diMotor2Hours; adiMotor[3]:=diMotor3Hours; adiMotor[4]:=diMotor4Hours; //assume order Y_Dev1:=1; Y_Dev2:=2; Y_Dev3:=3; Y_Dev4:=4; FOR iLoop:=1 TO 3 DO IF adiMotor[2] < adiMotor[1] THEN Y_Dev:=Y_Dev1; Y_Dev1:=Y_Dev2; Y_Dev2:=Y_Dev; diMotor:=adiMotor[1]; adiMotor[1]:=adiMotor[2]; adiMotor[2]:=diMotor; END_IF; IF adiMotor[3] < adiMotor[2] THEN Y_Dev:=Y_Dev2; Y_Dev2:=Y_Dev3; Y_Dev3:=Y_Dev; diMotor:=adiMotor[2]; adiMotor[2]:=adiMotor[3]; adiMotor[3]:=diMotor; END_IF; IF adiMotor[4] < adiMotor[3] THEN Y_Dev:=Y_Dev3; Y_Dev3:=Y_Dev4; Y_Dev4:=Y_Dev; diMotor:=adiMotor[3]; adiMotor[3]:=adiMotor[4]; adiMotor[4]:=diMotor; END_IF; END_FOR; end_function_Block |
6/16/2014 2:07 PM | |
Posts: 2348 Rating: (264)
|
Its a code to ensure equal motor usage - i.e. most used motors will be used less frequent. |
Follow us on