Results 1 to 15 of 15

Thread: QTimer

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default QTimer

    I'd like to know how to trigger my SLOT before QTimer actually times out.
    Here's an example scenario:

    we have a pump which if conditioned turns on. I have defined the pump life period in a timer. So, when the timer times out, pump turns off.
    Here, I need to check when 83% of the pump life period passes, then I need to do some instructions (after 83% life period of pump).
    finally when the whole pump life period passes it switches off.
    //-----------

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer

    Start another timer set at 83% of what you set the first timer to.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2012
    Location
    Paris, France
    Posts
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer

    Have you considered building a finite state machine to handle your pump? Something that would vaguely look like this:
    Qt Code:
    1. class pump {
    2. enum pump_state {
    3. on, // running
    4. wait_endcourse, // waiting to reach your "83%" course
    5. wait_off, // waiting to reach the point where pump shall be turned off
    6. off // stopped
    7. };
    8.  
    9. pump_state state;
    10.  
    11. pump::manage (MySplendidEventType event)
    12. {
    13. switch (state)
    14. {
    15. case on:
    16. if (event == command_shutdown)
    17. {
    18. whatever_I_need_to_do_to_start_shutdown();
    19. Timer.start (time_to_reach_83_pc_of_the_course);
    20. state = wait_endcourse;
    21. }
    22. break;
    23. case wait_endcourse:
    24. if (event == timeout)
    25. {
    26. whatever_I_need_to_do_at_83_pc_course();
    27. Timer.start(time_to_go_the_remaining_17_pc_of_the_course);
    28. state = wait_off;
    29. }
    30. break;
    31. case wait_off:
    32. if (event == timeout)
    33. {
    34. whatever_I_need_to_do_to_finish_shutdown();
    35. state = off;
    36. }
    37. }
    38. }
    39. };
    To copy to clipboard, switch view to plain text mode 

    Note that if you intend to command a real pump you are in dire need of feedback. I hope you don't assume sending the "on" command will prevent the pump to jam if, let's say, the janitor's broomstick has been sucked into the intake?
    This finite state machine technique allows to handle these feedbacks, for instance:
    Qt Code:
    1. case wait_endcourse:
    2. if (some_sensor_tells_me_that_the_pump_is_jammed)
    3. {
    4. fire_the_bloody_janitor();
    5. state = broomstick_stuck_in_intake;
    6. }
    7. else if (event == timeout)
    8. {
    9. whatever_I_need_to_do_at_83_pc_course();
    10. Timer.start(time_to_go_the_remaining_17_pc_of_the_course);
    11. state = wait_off;
    12. }
    13. break;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTimer

    Thank You for... trying to infer your good insight of this matter. It's nice to hear from an experienced expert.

    I merely check all these condition in my code, but your way of implementation looks more organized. I will try to change my code
    to look like this.

    about the first part of the code, at line 11, I want to know how you implemented the MySplendidEvent!
    I am considering it as kinda even a simple QString sent whenever the method manage is called. Or , it's different?

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer

    can be just another enum here
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Jun 2012
    Location
    Paris, France
    Posts
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer

    Well the idea behind 'MySplendidEvent' is to have a queue containing the events. The timer expiration would create such an event with the special type "timer expired", while other parts of the software would create events like "this sensor passed a given threshold", "this device has acknowledged a given command" or whatever your control system needs to trigger the finite state machine transitions.

    The finite state machine would then receive an unified stream of conditions likely to cause a transition to another state.

    As for the event type, it could be as simple as an enum (as amleto suggested), or more complex data structure if need be (not unlike Qt mouse or keyboard events, for instance).
    It all depends on the complexity of the process you're controlling. For instance, if you control only one pump, no need to bother with a pump identifier. If you have several different pumps in your system, it could be better to create an event structure with a "pump switched off" enum value associated with the actual pump identifier.
    Last edited by kuroi_neko; 15th June 2012 at 14:40.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer

    BTW. QStateMachine...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jun 2012
    Location
    Paris, France
    Posts
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer

    An humble newcomer like me would hardly dare to question such an authority as yours, pan wysota, but I've never found the Qt FSM very friendly as far as code maintenability and readability is concerned.
    Very well suited for GUI animation/synchronization, but not that good for low-level process control, in my opinion.
    The main trouble with QStateMachine is that the code does not reflect the state-transition diagram, just like widget/layouts code does not reflect a GUI structure in a visually understandable form.
    Maybe one day TrollTech will invest in a Qt designer-like tool for FSMs, but untill then I would rather use plain C switches, LUA (if a game developer tool does not seem too folish) or a custom-made FSM language for big machines.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer

    Quote Originally Posted by kuroi_neko View Post
    An humble newcomer like me would hardly dare to question such an authority as yours, pan wysota, but I've never found the Qt FSM very friendly as far as code maintenability and readability is concerned.
    I agree but for this particular approach you describe, the design fits well since all we need is events to trigger transitions and property changes/actions upon entering/exiting a state.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jun 2012
    Location
    Paris, France
    Posts
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer

    Indeed that could be an option. Some people are thinking visually, some other are more at ease with textual representations.

    However, from my experience in industrial software, graphical representations of FSM were pretty useful to discuss functional issues with non-programmers (customers, among others ).

    Using a coding paradigm close to this representation may allow to go easier back and forth between functional description and implementation.

    By the way, I wonder if someone actually produced a graphic design tool to generate QStateMachine-based FSMs?

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer

    Qt state machines are standard SCXML thus I'm sure there are tools for creating such charts.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Jun 2012
    Location
    Paris, France
    Posts
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer

    Yes! Good point. I missed that entirely. Still wet behind the ears, sorry .

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTimer

    Qt state machines are standard SCXML thus I'm sure there are tools for creating such charts.
    I'm not sure how useful this is. Apache has Visual SCXML for creating and editing state charts, but is there any Qt tool that will read the resulting SCXML file and either build the state machine at run time or generate source files at build time?

    I built a complex state machine a while ago to handle mouse and keyboard interactions with a graphics view; dozens of top level and sub-states and as many transitions. I ended up drawing the whole thing out on a very large sheet of graph paper. But it still required hand-coding everything, which was very tedious and resulted in many mistakes that needed to be tracked down. Thankfully, there is qDebug().

    Edit: I see there is a Qt Labs effort to develop a SCXML loader for the Qt state machine framework. This looks promising.
    Last edited by d_stranz; 20th June 2012 at 04:42.

  14. #14
    Join Date
    Jun 2012
    Location
    Paris, France
    Posts
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer

    Writing a code generator from SCXML should not be too difficult. That could be integrated into any IDE, lex/yacc style. Qt Designer-like interactivity would be a plus, but it's not mandatory.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer

    Since it is XML, all one needs is a proper XSL template
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QTimer or something else
    By .:saeed:. in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2011, 07:47
  2. Use a QTimer in a Qt-non-GUI app.
    By hakermania in forum Newbie
    Replies: 11
    Last Post: 29th August 2010, 10:36
  3. QTimer
    By dragon in forum Qt Programming
    Replies: 18
    Last Post: 16th November 2008, 14:15
  4. QTimer
    By Ahmad in forum Qt Programming
    Replies: 1
    Last Post: 22nd October 2007, 18:26
  5. QTimer
    By nirup in forum Qt Programming
    Replies: 1
    Last Post: 29th May 2007, 14:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.