Implementing a non blocking delay

For general Flowcode discussion that does not belong in the other sections.
mvictor
Posts: 28
http://meble-kuchenne.info.pl
Joined: Thu Aug 25, 2022 7:46 am
Has thanked: 3 times
Been thanked: 1 time

Re: Implementing a non blocking delay

Post by mvictor »

Hey

Really sorry for this as it’s something that I realised when putting this in practice. So it goes like this:

I managed to make count variable numbers like when I press button A, the LED lights after 3 seconds and shuts off at about 3.3seconds since the button is pressed. Like a 300ms pulse is given.

But if I press the button A again before this 3.3 seconds, then there is no second output since the first on is not completed. What made me realise this is, there could be two signals of the same category (consider it as button A being pressed again 1 second apart) from the PLC, depending on the reading picked from the camera.

Regards

chipfryer27
Valued Contributor
Posts: 1874
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 409 times
Been thanked: 632 times

Re: Implementing a non blocking delay

Post by chipfryer27 »

Hi

If you press and hold the button for one-second before releasing will the Led come on for one-second after a delay? If it was the pressed and held for two-seconds would the on-time then be two seconds?

If you could describe a possible sequence of events over a given period it would help.

e.g

Time Action
00:00
00:01
00:02
00:03 Button A pressed#1
00:04 Button A released
00:05
00:06 Button A pressed#2
00:07 Button A released
00:08 Led A On (#1)
00:09
00:10 Led A Off (#1)
00:11 Led A On (#2)
00:12
00:13 led A Off (#2)

When the button is pressed, how long is it actually pressed for? You said it is under MCU control so I appreciate "button press" isn't an actual button, but how long is the signal high / active for?

Regards

mvictor
Posts: 28
Joined: Thu Aug 25, 2022 7:46 am
Has thanked: 3 times
Been thanked: 1 time

Re: Implementing a non blocking delay

Post by mvictor »

Hello,

The signal would be high for approx 300ms. It is a pulse trigger which controls a 12v push pull solenoid, so when a pulse is give the solenoid would go forward and when its low the solenoid goes back to its place.

Regards

chipfryer27
Valued Contributor
Posts: 1874
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 409 times
Been thanked: 632 times

Re: Implementing a non blocking delay

Post by chipfryer27 »

HI

So in other words you are looking for a digital delay? What happens on input A is reflected on output A after x-seconds?

What sort of delay periods are you lo0king at? Variable or fixed and how many triggers could arrive before the first trigger has completed it's delay?

Regards

mvictor
Posts: 28
Joined: Thu Aug 25, 2022 7:46 am
Has thanked: 3 times
Been thanked: 1 time

Re: Implementing a non blocking delay

Post by mvictor »

Hi

Yes, like what happens on input A will reflect on output A after 3 seconds. My delay period would be fixed.

The thing is the triggers arriving before the first one is completed isn’t fixed, it depends on the grade that my camera would detect and signal the PLC. Most cases it would be like what has already been done but in case there would be objects belonging to the same grade then input A would be triggered more than once.

chipfryer27
Valued Contributor
Posts: 1874
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 409 times
Been thanked: 632 times

Re: Implementing a non blocking delay

Post by chipfryer27 »

Hi

Not sure that chip and your connections would be best suited.

My thoughts (and I'm sure others will have better) are that if you had your inputs on bits 0/1/2 then you could sample the entire port using a mask to give values for the three inputs.

A timer interrupt would sample every x-mS and store the value in buffer

For example variable "input" could equal 0 - 7 depanding on the status of the inputs when triggered. Doesn't then matter which input is active.

The inputs would be sampled every x-mS and the result stored in a circular buffer large enough to store three seconds worth of data. Each sample would push current value into the buffer and also read the oldest value out (i.e. what was pushed three seconds ago). This value would the be outputed to another port bits 0/1/2.

Your RAM would need to be big enough to not only run the program but also store the buffer.

Probably a better way to do it, but that is just "first thought" :idea:

Regards

chipfryer27
Valued Contributor
Posts: 1874
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 409 times
Been thanked: 632 times

Re: Implementing a non blocking delay

Post by chipfryer27 »

Hi

Further thought

I you had a fixed number of potential inputs e.g. 5 triggers in the period of first on first off you could perhaps again use circular buffer.

A counter increments as before.

When triggered at say 1000, it puts a value in ON-buffer of 1225 and in OFF-buffer of 1250 (it adds 3 seconds and 3.333 seconds to current count).
Program compares current count with values in Buffe and if found removes and actions.

In your current connections / chip you would need buffers per trigger input. This would reduce the amount of RAM need to a few tens.

I'm sure someone better than I can come up with something better.

Regards

mvictor
Posts: 28
Joined: Thu Aug 25, 2022 7:46 am
Has thanked: 3 times
Been thanked: 1 time

Re: Implementing a non blocking delay

Post by mvictor »

Hi

Actually I could try this out because as per my experience goes there won’t be too many trigger instances. 5 could help out.

Could I have some guidance or examples on how to implement circular buffers ?

Thanks a million for the help.

Regards

chipfryer27
Valued Contributor
Posts: 1874
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 409 times
Been thanked: 632 times

Re: Implementing a non blocking delay

Post by chipfryer27 »

Hi

Simulating will be difficult if trying out real values but it may be OK if you have delays of five seconds and "on" times of a couple or so. The WiKi has many example of using, but briefly the steps would be something like:-

Under storage you will find circular buffers and you will need 16-bit ones which take up a double byte in the buffer. In properties select an appropriate size for the number of entries.

Have a buffer for both On and Off.

When you trigger you will add on 375 (5 x 75) to current counter value to give a delay of 5s and store in ON CB, and add on 525 ((5 x 75)+ (2x75)) to give an on time of 2s and store in Off CB.

In your chart you will look in On buffer for current counter value and if met remove and action. Same for OFF buffer.

Arrays could do similar but possibly with more code than CB.

Reluctant to create an example just now as the Wiki has good ones and as I mention above this may not be the best way to achieve.

Regards

medelec35
Valued Contributor
Posts: 2247
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 690 times
Been thanked: 761 times

Re: Implementing a non blocking delay

Post by medelec35 »

Hi.
For my two pence worth.
Timer interrupts can be precise in simulation as well as on hardware
All you do is use the timed interval component instated of the timer interrupt command icon.
Then you set both simulation and embedded time within the the properties.
Timer Interval component 1.png
Timer Interval component 1.png (248.04 KiB) Viewed 66 times
There is a bug within the timed interval for simulation only and I have added the simple get around you will see within the ISR call macro.
Try the attached. I have just modified Chipfryer27 example.
Attachments
Thermaltimingjan_Example v2.fcfx
(27.13 KiB) Downloaded 9 times
Martin

Post Reply