How do I put a delay in a case structure without delaying my loop? - timer

Sorry, I am not allowed to post my full program - company rules.
I've got a portion of my program that reads a script file and reads in a timer value to the file. I need that value to pause the reading in of the next line from the script file, and the only way I'm aware of doing that is like this:
where the scan from string function retrieves the time in milliseconds and inserts it into the delay function. The problem with this is, it delays my main loop, and I have other timed functions running simultaneously based on previous commands issued from the same script file that may take more time to run. IS there any way to instantiate this, within a case structure, that will pause the reading of the next command, and let the main loop continue?
My script reader has an event handler so that the rest of my program will run without it until called.
This feeds directly into the nested case statements that feeds data directly to the timer. Any help is appreciated!

If I understand correctly:
You have a loop running some tasks all the time without interruption
Sometimes, a button is pressed, which will cause a special task being executed between two iterations of the loop.
A special task is to disable the button for some seconds
This is accomplished by the following simplified code.
When the button is clicked and the command from the file is WAIT 30, the current time plus 30s is written to the shift register, and the button is disabled grayed out. That is, the user can't click it, and he even sees this!
When the button is not clicked within 100ms, the Timeout case is executed. (again and again and again...) There, all the tasks which should run always, live. That case also checks if the current time is greater than the time in the shift register, and re-enables the button again.

Related

Is there an algorithm to schedule overlapping turn-on/turn-off commands?

The problem I want to solve is as follow:
Each task (the green bar) represents a pair of turn-on (green-dashed-line) and turn-off (red-dashed-line) commands. Tasks may or may not overlap with one another. This a real-time application where we don't know when or if another task is coming. The goal is to turn the valve on if it's not already on and avoid turn off the valve prematurely.
What I mean by not turn off the valve prematurely is, for example, if we turn off the valve at time off-1, that's wrong because the valve should still stay on at that point in time, the correct action is to turn off the valve at time off-4.
Regarding the implementation details, each task is an async task (via. GLib async API). I simulate the waiting for task-duration with the sleep function, a timer is probably more appropriate. Right now, the tasks run independently and there is no coordination between them, thus the valve is being turn-off prematurely.
I searched around to find a similar problem but the closest I found is interval scheduling whose goal is different. Has anyone encountered a similar problem before and can give me some pointers on how to solve this problem?
It seems like this could be solved with a simple counter. You increment the counter for each opening command, and decrement it for each closing command - when the count reaches zero, you close the valve.

How to send a signal to a function in embedded c

I have a wireless device and can send commands from my phone to the device. A command executes a bunch of steps to complete an action. At the moment, this action function is blocking, ie until the call completes the user needs to wait, with no option to quit. If for any reason, the call doesn't complete then the user is stuck with that screen. A simple pseudo looks like this:
do_action()
{
int result = 0;
result |= step_a();
result |= step_b();
result |= step_c();
result |= step_d();
return result;
}
How can I make this process "interruptible", ie use a signal/flag to tell this function call that the user has terminated and that this action needs to be terminated/cleaned up. Is there a way I can "time bound" this function, ie exit the action if not completed within expected time? How can I implement such a feature? One of the issues also is that some of the steps, such as step_a, step_b functions are blackbox functions, ie implemented by the manufacturer and are blocking and I have no way modify their interface.
The easiest option is to see if the API has a way to pass a timeout to individual calls (or ask the manufacturer to implement one). If this is possible, then you could structure your code to take one step, check if you should quit (timeout reached or user interrupted), then take another step.
Should you be stuck with blocking vendor code you cannot modify, you will need to put something in place that can terminate an in-progress "action". Exactly what this interruption can be will depend environment is running on your device.
If your device is running more feature complete operating system then you could either add a second thread to your process that monitors the first (lookup pthreads), or you could execute your action in one process and have a separate monitor process that kills the first if it takes too long or if the user cancels the action (lookup the "fork" and "kill" system calls).
If your code is running on a more bare-bones environment then your options are more limited. One way to do this is to manually set up a hardware timer and an interrupt handler to check state. The specifics of how to do this will depend entirely on what hardware you are using.

How to correctly log the signals of a long running function in node red? Getting heap-memory full error and file stays empty

My Flow has following structure:
StartNode(InjectNode) -> LongRunningFunction(Custom) -> LogNode(File-Write set to Append)
StartNode: just a Inject node that I use to start the flow
LongRunningFunction: the function creates inside a longer for-loop a signal via
node.send(msg)
for(let i = 0; i < 5000000; i++){
node.send({payload: i});
}
this is the full code of the LongRunningNode. Later I want to calculate some message string based on the counter inside the loop, but problem can be reproduced by above code
The file stays empty and the script stops execution after some time. If I connect a debug node as second output to LongRunningFunction the last console output is around 3236831, because the heap-memory is full
Is there a general problem with the structure of my program?
i even tried abusing context by putting
flow.set("pause", true);
while(flow.get("pause")){
}
into the for loop of enter code hereLongRunningFunction and setting "pause" to false again, when a file watch node triggers.
theoretically, this way it should send only 1 signal at a time and wait until the logging finished.
tldr: it never writes anything to the file and heap crashes
Edit: This may possibly be related to the problem:
https://nodejs.org/es/docs/guides/backpressuring-in-streams/
the text is for node.js, so not sure how this is applies to node-red or how to fix it
The problem with your approach of a long running function is that is never yields back to the node.js event loop. That stalls all other activity in the runtime and nothing can make any progress.
The while loop approach is particularly bad; the only thing the runtime can do is sit in that while loop. Nothing else can happen outside of the while loop to change the context value you are testing.
There are lots of different ways to write long running functions using timers and callbacks that allow the node.js event loop to continue processing other events along side. It all depends on what you actually want to do in your flow.

Increment an output signal in labview

![enter image description here][1]I have a high voltage control VI and I'd like it to increase the output voltage by a user set increment every x number of seconds. At the moment I have a timed sequence outside the main while loop but it never starts. When it's inside the while loop it delays all other functions. I'm afraid I'm such a beginner at this that I can't post a picture yet. All that needs to happen is an increase in voltage by x amount every y seconds. Is there a way to fix this or a better way of doing it? I'm open to suggestions! Thanks!
Eric,
Without seeing the code I am guessing that you have the two loops in series (i.e. the starting of the while loop depends upon an output of the timed loop; this is the only way that one loop might block another). If this is the case, then decouple the two loops so that they are not directly dependent on each other.
If the while loop is dependent on user input, then use an event structure and then pass the new parameters via a queue (this would be your producer-consumer pattern).
Also, get rid of the timed loop and replace with a while loop. The timed loop is only simulated on non-real time machines and it can disrupt determinisitic features of a real-time system. Given that you are looking for sending out a a signal on the order of seconds, it is absolutely not necessary.
Anyways, if I am off base, please throw the code in question up so that we can review it.
Cheers, Matt

run func() based on what time it is

i wrote some code that monitors a directory DIR with inotify() and when a file gets moved in DIR i get a .txt output of that file(its an nfcapd file with flows of my network interface). This happens every 5 minutes.
After that, i used Snort's DPX starter kit, with which you can extend Snort by writing your own preprocessor. This preprocessor,like all the others, is just a function that's executed every time a new packet is available. My problem is that i want, when a new file gets exported from my previous code(so every 5 minutes), to read that file inside the preprocessor's function.
So, is there any way of getting the time and executing only if it's the desired time?
if (time is 15:36){
func(output.txt);}
i'm writing in c.
Thanks
You can do something like the following:
#include <time.h>
...
time_t t = time(NULL); //obtain current time in seconds
struct tm broken_time;
localtime_r(&t, &broken_time); // split time into fields
if(broken_time.tm_hour == 15 && broken_time.tm_min == 36) { //perform the check
func(output.txt);
}
Since you're using inotify, I'm assuming your environment supports POSIX signals.
You can use alarm() to raise a signal after a predetermined amount of time has passed and have the appropriate signal handler do whatever work you need to do. It would avoid what I think is going to end up being a very ugly infinite loop in your code.
So, in your case, the function handling SIGALRM would not need to worry what time it was, it would know that a predetermined amount of time has passed by the fact that it was entered. However, you'll need to provide some context that function can access to know what to do, kind of hard to suggest how without seeing your code.
I'm not entirely sure you're going down the right path with this, but using alarm() would probably be the sanest approach given what you described.

Resources