You are not registered yet. Please click here to register!


 
 
plc storereviewsdownloads
This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc.
 
Try our online PLC Simulator- FREE.  Click here now to try it.

New Here? Please read this important info!!!


Go Back   PLCS.net - Interactive Q & A > PLCS.net - Interactive Q & A > LIVE PLC Questions And Answers

Reply
 
Thread Tools Display Modes
Old October 17th, 2020, 06:59 AM   #1
Sham
Member
Australia

Sham is offline
 
Join Date: Sep 2019
Location: Australia
Posts: 120
Time Stamps to Calculate Duration in Machine Expert

Hi All,

I have a rather simple task, but have no experience with the Timedate library so posting here. How can I calculate the time duration between two events ? for example, have to calculate the time duration of a motor running, by calculation the time duration between the time I press the Start button till I press the Stop button.

So, its like time_elapsed = TimeStop - TimeStart;
  Reply With Quote
Old October 17th, 2020, 03:17 PM   #2
kalabdel
Member
Canada

kalabdel is offline
 
Join Date: Feb 2015
Location: Ontario
Posts: 606
I'm also new and this is what I think should work:


Configure hardware events for each input, rising or falling.
Add two tasks and assign one for each event and program call in each task.

In each of the programs called by the tasks get system time.
In the cyclic program somewhere calculate the time difference.


Add SysTimeCore library to get the three functioins:
SusTimeGetms, SysTimeGetNs and SysTimeGetUs




Edit : I should add that I haven't tried it yet.



Have a look at the .gif in this link
__________________
Sometimes the best way to help a drowning person is to let him drown.
  Reply With Quote
Old October 17th, 2020, 03:29 PM   #3
AustralIan
Member
Germany

AustralIan is offline
 
Join Date: Jan 2013
Location: Germany
Posts: 1,167
Does TIME() work on Machine Expert?
  Reply With Quote
Old October 17th, 2020, 03:51 PM   #4
Sham
Member
Australia

Sham is offline
 
Join Date: Sep 2019
Location: Australia
Posts: 120
Quote:
Originally Posted by kalabdel View Post
I'm also new and this is what I think should work:


Configure hardware events for each input, rising or falling.
Add two tasks and assign one for each event and program call in each task.

In each of the programs called by the tasks get system time.
In the cyclic program somewhere calculate the time difference.


Add SysTimeCore library to get the three functioins:
SusTimeGetms, SysTimeGetNs and SysTimeGetUs



Edit : I should add that I haven't tried it yet.



Have a look at the .gif in this link
Thanks Kalabdel, I had similar thoughts. I wrote this:

--- --- --- VARIABLE DEFINITION --- --- ---
FUNCTION_BLOCK TimeDuration

VAR_INPUT
START: BOOL;
STOP: BOOL;
END_VAR

VAR_OUTPUT
OUTPUT:BOOL;
END_VAR

VAR
Trig_Start:R_Trig;
Trig_Stop:R_Trig;
stUnixStamp:SysTimeRtc.SysTimeCore.SYSTIME;
stdate: SysTimeRtc.RTS_SYSTIMEDATE;
Start_Stamp:ULINT;
Stop_Stamp:ULINT;
Time_ms: ULINT;
Runtime:ULINT;
END_VAR
--- --- --- LOGIC --- --- ---
Trig_Start(CLK:=START);
Trig_Stop(CLK:=STOP);



IF Trig_Start.Q THEN
OUTPUT:=TRUE;
Start_Stamp:=stUnixStamp;
END_IF

IF Trig_Stop.Q THEN
Stop_Stamp:=stUnixStamp;
Runtime:=(Stop_Stamp - Start_Stamp);
END_IF

This looks logically correct, but somehow is not giving any results.
  Reply With Quote
Old October 17th, 2020, 03:57 PM   #5
Sham
Member
Australia

Sham is offline
 
Join Date: Sep 2019
Location: Australia
Posts: 120
I am missing some part here which actually reads the time..
  Reply With Quote
Old October 17th, 2020, 04:09 PM   #6
Sham
Member
Australia

Sham is offline
 
Join Date: Sep 2019
Location: Australia
Posts: 120
Quote:
Originally Posted by AustralIan View Post
Does TIME() work on Machine Expert?
Yes it actually does. I am giving it a try now.
  Reply With Quote
Old October 17th, 2020, 04:14 PM   #7
Sham
Member
Australia

Sham is offline
 
Join Date: Sep 2019
Location: Australia
Posts: 120
Quote:
Originally Posted by AustralIan View Post
Does TIME() work on Machine Expert?
Got the result using TIME(); thanks a ton. Still curious to know the missing bit from my code using the Systime.RTC though!
  Reply With Quote
Old October 17th, 2020, 04:46 PM   #8
kalabdel
Member
Canada

kalabdel is offline
 
Join Date: Feb 2015
Location: Ontario
Posts: 606
The code I suggested is supposed to have nanosecond precision and not tied to scan time; it is based on generating hardware interrupts (trigger events) for digital inputs and using system function that retruns time in nanoseconds.
__________________
Sometimes the best way to help a drowning person is to let him drown.
  Reply With Quote
Old October 18th, 2020, 04:59 AM   #9
kallileo
Lifetime Supporting Member
Greece

kallileo is offline
 
Join Date: Jun 2008
Location: Thessaloniki, Macedonia, Hellas
Posts: 285
In most of my project I need to display time counters in the HMI. So I have created a library of different hour, minutes and seconds counters/timers.
The following FB is simple time counter that outputs the time in hours, minutes and seconds that has passed since xEnable was set high.

Code:
FUNCTION_BLOCK PUBLIC FB_TimeCount
VAR_INPUT
	xEnable:BOOL;
END_VAR
VAR_OUTPUT
	uiElapsedHour:UINT:=0;
	uiElapsedMin:UINT:=0;
	uiElapsedSec:UINT:=0;
END_VAR
VAR
	uiElapsedTime:UINT:=0;
	udNowTime : UDINT;
	udPrevTime : UDINT;
END_VAR
Code:
udNowTime := TIME_TO_UDINT(TIME());

IF xEnable THEN
	IF (udNowTime - udPrevTime > 1000) THEN
		uiElapsedTime := uiElapsedTime + 1;
		uiElapsedSec := uiElapsedTime MOD 60;
		uiElapsedMin := uiElapsedTime/60 MOD 60;
		uiElapsedHour := uiElapsedTime/3600 MOD 3600;
		udPrevTime := udNowTime;
	END_IF;
ELSE
	uiElapsedSec := 0;
	uiElapsedMin := 0;
	uiElapsedHour := 0;
	uiElapsedTime := 0;
END_IF;
__________________
automationofthings.blogspot.com
  Reply With Quote
Old October 18th, 2020, 05:30 AM   #10
Sham
Member
Australia

Sham is offline
 
Join Date: Sep 2019
Location: Australia
Posts: 120
Quote:
Originally Posted by kallileo View Post
In most of my project I need to display time counters in the HMI. So I have created a library of different hour, minutes and seconds counters/timers.
The following FB is simple time counter that outputs the time in hours, minutes and seconds that has passed since xEnable was set high.

Code:
FUNCTION_BLOCK PUBLIC FB_TimeCount
VAR_INPUT
	xEnable:BOOL;
END_VAR
VAR_OUTPUT
	uiElapsedHour:UINT:=0;
	uiElapsedMin:UINT:=0;
	uiElapsedSec:UINT:=0;
END_VAR
VAR
	uiElapsedTime:UINT:=0;
	udNowTime : UDINT;
	udPrevTime : UDINT;
END_VAR
Code:
udNowTime := TIME_TO_UDINT(TIME());

IF xEnable THEN
	IF (udNowTime - udPrevTime > 1000) THEN
		uiElapsedTime := uiElapsedTime + 1;
		uiElapsedSec := uiElapsedTime MOD 60;
		uiElapsedMin := uiElapsedTime/60 MOD 60;
		uiElapsedHour := uiElapsedTime/3600 MOD 3600;
		udPrevTime := udNowTime;
	END_IF;
ELSE
	uiElapsedSec := 0;
	uiElapsedMin := 0;
	uiElapsedHour := 0;
	uiElapsedTime := 0;
END_IF;
Thanks Kalileo, please check your inbox!
  Reply With Quote
Old October 18th, 2020, 12:51 PM   #11
kalabdel
Member
Canada

kalabdel is offline
 
Join Date: Feb 2015
Location: Ontario
Posts: 606
It looks like the SysTimeRtcHighResGet function is not supported in all CPUs, definitely not my M241.
Attached Images
File Type: png time delta4.PNG (86.9 KB, 21 views)
__________________
Sometimes the best way to help a drowning person is to let him drown.

Last edited by kalabdel; October 18th, 2020 at 01:16 PM.
  Reply With Quote
Old October 18th, 2020, 01:46 PM   #12
kallileo
Lifetime Supporting Member
Greece

kallileo is offline
 
Join Date: Jun 2008
Location: Thessaloniki, Macedonia, Hellas
Posts: 285
If you add the SysTime library SysTimeRtcHighResGet will work.
It works for me on SoMachine 4.3 but I think it's simpler to use TIME().

https://product-help.schneider-elect..._Library-5.htm
__________________
automationofthings.blogspot.com
  Reply With Quote
Old October 18th, 2020, 01:55 PM   #13
kalabdel
Member
Canada

kalabdel is offline
 
Join Date: Feb 2015
Location: Ontario
Posts: 606
Thanks Kallileo. I can add the library and have access to the instruciton; I do not get any errors it just doesn't output the result.

You are right though, it is supported in M241 I will spend some time later to get it to work.
__________________
Sometimes the best way to help a drowning person is to let him drown.
  Reply With Quote
Old October 18th, 2020, 02:41 PM   #14
kallileo
Lifetime Supporting Member
Greece

kallileo is offline
 
Join Date: Jun 2008
Location: Thessaloniki, Macedonia, Hellas
Posts: 285
Almost every piece of code I write in ST is a state machine.
The following code counts motor operating hours add them to the old values values that can should be declared as Persistent variables in SoMachine/Machine Expert/Codesys.
You can also convert the operating minutes to hours and add them as well.

Code:
FUNCTION_BLOCK PUBLIC FB_TimeCount
VAR_INPUT
	xEnable:BOOL;
END_VAR
VAR_OUTPUT
	uiElapsedHour:UINT:=0;
	uiElapsedMin:UINT:=0;
	uiElapsedSec:UINT:=0;
END_VAR
VAR
	uiElapsedTime:UINT:=0;
	udNowTime : UDINT;
	udPrevTime : UDINT;
	iStatus : INT :=0;
END_VAR

udNowTime := TIME_TO_UDINT(TIME());
IF xEnable THEN
	CASE iStatus OF
	0:
		uiElapsedSec := 0;
		uiElapsedMin := 0;
		uiElapsedHour := 0;
		uiElapsedTime := 0;
		iStatus := 10;
	10:		
		IF (udNowTime - udPrevTime > 1000) THEN
			uiElapsedTime := uiElapsedTime + 1;
			uiElapsedSec := uiElapsedTime MOD 60;
			uiElapsedMin := uiElapsedTime/60 MOD 60;
			uiElapsedHour := uiElapsedTime/3600 MOD 3600;
			udPrevTime := udNowTime;
		END_IF;
	END_CASE
ELSE	
		iStatus := 0;
END_IF;

Code:
PROGRAM PLC_PRG
VAR
	motorOperHours : FB_TimeCount;
	uiOperHours : UINT;
	uiOperMinutes : UINT;
	uiOperSecs : UINT;
	xMotorStart : BOOL;
	ftMotor : F_TRIG;
	uiOldMotorHours : UINT := 10;
END_VAR

motorOperHours(xEnable:=xMotorStart, uiElapsedHour=>uiOperHours, uiElapsedMin=>uiOperMinutes, uiElapsedSec=>uiOperSecs);

ftMotor(CLK:=xMotorStart);
IF ftMotor.Q THEN
	uiOldMotorHours := uiOldMotorHours + uiOperHours;
END_IF
__________________
automationofthings.blogspot.com
  Reply With Quote
Old October 18th, 2020, 08:34 PM   #15
kalabdel
Member
Canada

kalabdel is offline
 
Join Date: Feb 2015
Location: Ontario
Posts: 606
Thanks kallileo. I'm sure your code will come in handy.



Quote:
Originally Posted by Sham View Post
Thanks Kalabdel, I had similar thoughts. I wrote this:



This looks logically correct, but somehow is not giving any results.

I think you need to call the function to actually access the time.


Result := SysTimeRtcHighResGet (pTimeStamp := StartTime);


That worked for me.


Thanks for asking the question, it was a nice exercise.
Attached Images
File Type: png time delta5.PNG (100.9 KB, 13 views)
__________________
Sometimes the best way to help a drowning person is to let him drown.
  Reply With Quote
Reply
Jump to Live PLC Question and Answer Forum

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Topics
Thread Thread Starter Forum Replies Last Post
Schneider machine expert baisc: Retentive timer Varri LIVE PLC Questions And Answers 0 April 14th, 2020 04:28 PM
Input Time to Schedule an Action on PanelView Bunt23 LIVE PLC Questions And Answers 31 January 12th, 2020 08:58 AM
How AB Timers work. PhilipW LIVE PLC Questions And Answers 0 January 28th, 2005 03:47 PM
Micrologix 1500 used to calculate tak time? rkeller LIVE PLC Questions And Answers 3 January 27th, 2005 12:19 AM
lambda tuning – how is it calculated? Ron Beaufort LIVE PLC Questions And Answers 4 August 5th, 2003 12:45 PM


All times are GMT -4. The time now is 06:21 AM.


.