TwinCat Structured Text programming

Sohaib28

Member
Join Date
Aug 2015
Location
Cottbus
Posts
31
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);
 
Call this function from your PLC_PRG (main) and plug in your values.

Your "wInput" is raw (word) your "rProcesVar" is REAL.
Hope this helps.
 
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...
Replies
5
Views
1,920
Hello Everyone: I have been able to read value via canopen interface using ADS protocol. Actually my Structured Text Programming is weak. I can...
Replies
5
Views
3,980
Hi, i'm really struggling to get any decent examples of the above. Can anyone help??
Replies
3
Views
10,457
I am using twincat 3 to send some strings over TCP/IP. Where the server is a sensor and my PLC is the client. I noticed that the sensor didnt...
Replies
2
Views
48
I'm trying to control a device via MODBUS RTU and the ModbusRtuMasterV2_PcCOM in Twincat 3. I've configured a device with the right com port and...
Replies
7
Views
221
Back
Top Bottom