PDA

View Full Version : How to trig an action at reaching of defined value (with fast sampling)?



sr1s
2nd October 2019, 17:03
Hello,
I have to face a problem with alarm sensor. I have a qLineEdit where values are modified by an external widget (it manages data from a serial port).
Now I need that, when values exceed a defined level, an action is trigged (just like an alarm, or a motor power on, etc.).
I tried to use a signal/slot for the qLineEdit (_textChanged event with a simple "if ()" condition), it's ok for slow data flow, but obviously, for an high data rate ( as from a fast voltage sensor, a position device, a piece counter), it's freeze my code. So I'm searching a better strategy, did you have any suggestion? Thanks!!

Lesiok
2nd October 2019, 17:53
Signalize the transition through a given level and not every change in value above that level.
Simple code of the slote :

if(value > alarm_value)
{
if(!alarm_state)
{
alarm_state = true;
//do what You need to alarm ON
}
}
else
{
if(alarm_state)
{
alarm_state = false;
//do what You need to alarm OFF
}
}

d_stranz
2nd October 2019, 18:00
It seems like a very strange design to use a change in the text of a GUI widget to trigger an alarm signal. Why aren't you monitoring the sensor directly and using the sensor value to trigger the alarm?

If your basic problem is that the data rate from the sensor is too high, then you have at least two options:

- don't sample the sensor so often. If you don't need microsecond or millisecond response, then sample once per second.

- if you can't change the sampling rate, then change the rate that you report the signal to the rest of the app. Compute the average of all of the samples received over a 1 second period (for example), and once every second emit a signal with the average value. Connect that signal to other parts of your app.

And change your design so you aren't relying on looking at a QLineEdit's value to tell your app when the alarm should be raised. Take the signal from the monitoring widget and connect it to two slots - one that changes the text in the QLineEdit, and another that checks the value to see if the alarm should be raised.

Your current design is like looking in the mirror to see if your face is sweating to tell you it is too hot instead of just looking at the thermometer.

sr1s
4th October 2019, 17:01
"Your current design is like looking in the mirror to see if your face is sweating to tell you it is too hot instead of just looking at the thermometer." It's a very clear comparison!
I decided to review the design of my app, in order to have the widget only as GUI, while data process and alarm will be done with a flow limit (it's unuseful having a ton of aquiring, when 10 per sec is enough!)