Results 1 to 10 of 10

Thread: Creating a qstate machine using QT

  1. #1
    Join Date
    Aug 2010
    Posts
    14
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Creating a qstate machine using QT

    0
    down vote
    favorite
    I'm beginner in qtstate machine and I'm trying to convert a state machine that is created using switch case, how would I model the state machine to the qt state machine. For example I have a state machine like the following

    Qt Code:
    1. switch (state)
    2. {
    3. case ZL_ZL_RINSINGCUP_UP:
    4. if(!GSet.defaultCleaningCup_LTC)
    5. {
    6. wtim=0;
    7. emit stateChanged(state=ZL_RINSE,resStat);
    8. break;
    9. }
    10. if(Fp.LidClose()||Global::UL>USERLEVEL_6){
    11. if(wtim)
    12. {
    13. mot.Up(); // dont move when custom cup is used!
    14. wtim=0;
    15. }
    16. else
    17. {
    18. if((!mot.isDown() && !mot.inMove())){
    19. emit stateChanged(state=ZL_RINSE,resStat);
    20. }
    21. }
    22. }
    23. else
    24. {
    25. if(!wtim){ //lid opened
    26. mot.Stop();
    27. wtim=1;
    28. }
    29. }
    30. break;
    31. case ZL_RINSE: // GUI
    32. break;
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2010
    Posts
    14
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default qtstate machine

    f I have a simple state machine like that

    Qt Code:
    1. switch(state)
    2. {
    3. case state_1:
    4. doSomething();
    5. state = state_2;
    6. break;
    7. case state_2:
    8. doSomething_2();
    9. state = state_finished;
    10. break;
    11. }
    To copy to clipboard, switch view to plain text mode 
    how would I convert this simple state machine into qstate with transitions ?

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qtstate machine

    QStateMachine, like most things in Qt, is event triggered.

    That can be low level events, signals like button clicks and also custom signals.

    There are a couple of options to execute things when a state is entered:
    - connect a slot to the state's entered() signal
    - use the QState's property assignment mechanism to set values on QObjects and have the property setters call further methods if necessary
    - connect to the transition's signals

    You could try to analyze your current state machine in order to craete a state diagram, i.e. identify the states, the transitions and what triggers each transition.
    Then model that with QStateMachine's classes.

    Cheers,
    _

  4. #4
    Join Date
    Aug 2010
    Posts
    14
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: qtstate machine

    can you convert the state machine above into code please, just to show the idea, am very beginner and I'm not understanding much of the stuff of the qtstate machine

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qtstate machine

    There is no information in your code on how the transitions are triggered.

    Hence my recommendation to first create a state diagram.

    Cheers,
    _

  6. #6
    Join Date
    Aug 2010
    Posts
    14
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: qtstate machine

    The transistion is here after doing some work in the state, I use state = state_2; which goes to the other transistion
    switch(state)
    {
    case state_1:
    doSomething();
    state = state_2;
    break;
    case state_2:
    doSomething_2();
    state = state_finished;
    break;
    }

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qtstate machine

    So, posting the same code again is helping how?
    Wouldn't it have been better to post the information I've asked about?
    Maybe there are no triggers at all, the switch is never executed?

    Cheers,
    _

  8. #8
    Join Date
    Aug 2010
    Posts
    14
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: qtstate machine

    the state machine is big I can't post it here, I want a code for moving from one state to another state, and each state is executing some codes

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qtstate machine

    Let me rephrase it then: what makes your program call the function that has this switch?
    What happens when the program is in state_1 that calls the code that this switch is in?

    Cheers,
    _

  10. #10
    Join Date
    Aug 2010
    Posts
    14
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default qt state machine

    I have a very complex state machine and I need to use the Qt framework state machine to do it. I know how to create a states using the qtframework, but I don't know where to put the code inside each state that should occur. what I only know is addtransition but where to put the code inside the switch case ??
    Qt Code:
    1. switch (state) {
    2. case Z_START:
    3.  
    4. state = Z_SAMPLE;
    5. emit stateChanged(Z_SAMPLE,0);
    6.  
    7. case Z_SAMPLE:
    8. {
    9.  
    10. DP_ANDY = false; // moved to Z sample
    11. GSet.absP = Fp.pxf; //s08 //remember ambient pressure for correction // moved to Z sample
    12.  
    13. ig.reset();
    14.  
    15. //setSecondlineibited(true); // moved to state=Z_SAMPLE;
    16. int next = nextSample();
    17. /*
    18.   for(;current_sample<FlashSampler;current_sample++)
    19.   {
    20.   currentMeasurement = SamplerTable.at(current_sample);
    21.   if(currentMeasurement->enabled)
    22.   break;
    23.   }
    24.   */
    25. if(next>=FlashSampler)
    26. {
    27. htim=1;
    28. wtim=1;
    29. emit stateChanged(state = Z_FINI,resStat /*0*/); // use state of last result instead of 0!
    30. break;
    31. }
    32. current_sample=next;
    33. currentMeasurement = SamplerTable.at(current_sample);
    34.  
    35. precool = Ta >currentMeasurement->Ta;
    36. #endif
    37.  
    38. MethSet* ms = currentMeasurement->getMethod()->methset;
    39. Ta = currentMeasurement->Ta;//s117
    40. Te = currentMeasurement->Te;
    41. Ts = Ta - GSet.stdT;
    42. Ti = Ta; //wirkungslos hier
    43. St = ms->Step;
    44. Rt = ms->Rate / 200.0f; // C/min alle 300 ms
    45. for(int i=0;i<6;i++)
    46. Ptim[i]=(int)(ms->airVol[i]*PUMPFACT);
    47. #ifndef FlashSampler
    48. if(Fp.powvers<200)
    49. stirval = (ms->stir?255:0); //stirrer was at fixed speed for first 5 units with old powerboard
    50. else
    51. #endif
    52. stirval=(int)((float)ms->stir/7.5f); //factor to be set
    53. if(stirval&&stirval<5)
    54. stirval=5;
    55.  
    56. state=Z_SAMPLE_wait,0);
    57. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QState
    By cszawisza in forum Newbie
    Replies: 7
    Last Post: 9th November 2014, 12:04
  2. Successful deployment on one machine does not work on a different machine
    By rakefet in forum Installation and Deployment
    Replies: 0
    Last Post: 6th May 2014, 15:57
  3. QState machine using multiple QThreads
    By PeterWurmsdobler in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2012, 10:55
  4. Replies: 4
    Last Post: 25th March 2010, 10:32
  5. rotate in QState
    By estel in forum Qt Programming
    Replies: 0
    Last Post: 21st October 2009, 12:39

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.