TwinCat Structured Text programming

Hello everyone:

I have been able to read and write data via canopen interface through ADS read and write functional blocks in Twincat PLC control. I have coonected a high voltage power supply and I can read and write value of voltage through it. As you can see in the screen shots that I have been able to scale the value of read voltage.

Now the problem is that I want to write the value of voltage. But I dont know which value I have to write.
Actually I am bit weak at structured text programming. The scaled max=765 and scaled min=0. output max= 52428 and output min=0.
I already tried this code ' wVoltage=(wVolatge)*52428/765;'. But it does not work. wVoltage is a UINT. Please help me so that I am able to scale the value and write the required value. I think it will just work like analog output scaling.

Thanks
Regards

Unbenannt.jpg Unbenannt2.jpg
 
it is known that codesys must have correct types.
so declare all numbers (here in real) then make the calc in real, now the numbers are a mix of real and int.
 
FUNCTION ScaleAnalogOutput : WORD
VAR_INPUT
rInput:REAL;
rInputMin:REAL;
rInputMax:REAL;
wOutputMin:WORD;
wOutputMax:WORD;
END_VAR
VAR
rOutputMin:REAL;
rOutputMax:REAL;
rInputRange:REAL;
rOutpuRange:REAL;
rScaleFactor: REAL;
rInputWithoutOffset: REAL;
rOutputWithoutOffset: REAL;
END_VAR
---------------------------------------------------------

IF rInput < rInputMin THEN
rInput := rInputMin;
END_IF

IF rInput > rInputMax THEN
rInput := rInputMax;
END_IF

rOutputMin:=WORD_TO_REAL(wOutputMin);
rOutputMax:=WORD_TO_REAL(wOutputMax);

rInputRange:=rInputMax-rInputMin;
rOutpuRange:=rOutputMax-rOutputMin;

rInputWithoutOffset:=rInput-rInputMin;

rScaleFactor:=rOutpuRange/rInputRange;

rOutputWithoutOffset:=rInputWithoutOffset*rScaleFactor;

ScaleAnalogOutput:=REAL_TO_WORD(rOutputWithoutOffset+rOutputMin);
 
Thanks bkottaras.

So you are saying that ScaleAnalogOutput is equal to wVoltage which is UINT now. And I have to define it as WORD. And what is rInput?
Actually problem is that it is not a analogue output. It is a digital output via canopen. When I change UINT into REAL or WORD the whole value changes. And I have to figure out new scaling values when I define wVoltage as REAL or WORD. Because the scaling factor 52428/750 does not work there.
 
so you have an wvoltage (word as uint)
change it to a real with WORD_TO_REAL to rvoltage
52428 into a rnumber1
765 to rnumber2
rresult=rvoltage*rnumber1/rnumber2
then wresult = REAL_TO_WORD (rresult);
 
Thanks Shooter.

If you have Twincat installed then can you please send me a screen shot? It would be very helpful for me. By defining all the variables and writing the code. Thanks again.
 

Similar Topics

Hi all, I've inherited someone else's program and can't for the life of me figure out his use of the # symbol in one of his structured text...
5
Replies
3,333
Views
Hello Everyone: I have been able to read value via canopen interface using ADS protocol. Actually my Structured Text Programming is weak. I can...
5
Replies
5,192
Views
Hi, i'm really struggling to get any decent examples of the above. Can anyone help??
3
Replies
11,539
Views
Scope: I'm trying to access SEW inverters over EtherCAT by using the EtherCAT master mailbox. I can ping the mailbox from inside the PLC, but not...
0
Replies
139
Views
Hello everyone. I recently changed my work laptop. I installed the latest version of Twincat 3 on it however I'm running in the following...
4
Replies
539
Views
Top