PDA

View Full Version : Creating a qstate machine using QT



andreahmed
13th April 2016, 19:54
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


switch (state)
{
case ZL_ZL_RINSINGCUP_UP:
if(!GSet.defaultCleaningCup_LTC)
{
wtim=0;
emit stateChanged(state=ZL_RINSE,resStat);
break;
}
if(Fp.LidClose()||Global::UL>USERLEVEL_6){
if(wtim)
{
mot.Up(); // dont move when custom cup is used!
wtim=0;
}
else
{
if((!mot.isDown() && !mot.inMove())){
emit stateChanged(state=ZL_RINSE,resStat);
}
}
}
else
{
if(!wtim){ //lid opened
mot.Stop();
wtim=1;
}
}
break;
case ZL_RINSE: // GUI
break;
}

andreahmed
14th April 2016, 08:02
f I have a simple state machine like that


switch(state)
{
case state_1:
doSomething();
state = state_2;
break;
case state_2:
doSomething_2();
state = state_finished;
break;
}
how would I convert this simple state machine into qstate with transitions ?

anda_skoa
14th April 2016, 08:45
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,
_

andreahmed
14th April 2016, 08:52
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

anda_skoa
14th April 2016, 09:17
There is no information in your code on how the transitions are triggered.

Hence my recommendation to first create a state diagram.

Cheers,
_

andreahmed
14th April 2016, 09:22
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;
}

anda_skoa
14th April 2016, 10:38
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,
_

andreahmed
14th April 2016, 10:48
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

anda_skoa
14th April 2016, 11:22
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,
_

andreahmed
15th April 2016, 19:50
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 ??


switch (state) {
case Z_START:

state = Z_SAMPLE;
emit stateChanged(Z_SAMPLE,0);

case Z_SAMPLE:
{

DP_ANDY = false; // moved to Z sample
GSet.absP = Fp.pxf; //s08 //remember ambient pressure for correction // moved to Z sample

ig.reset();

//setSecondlineibited(true); // moved to state=Z_SAMPLE;
int next = nextSample();
/*
for(;current_sample<FlashSampler;current_sample++)
{
currentMeasurement = SamplerTable.at(current_sample);
if(currentMeasurement->enabled)
break;
}
*/
if(next>=FlashSampler)
{
htim=1;
wtim=1;
emit stateChanged(state = Z_FINI,resStat /*0*/); // use state of last result instead of 0!
break;
}
current_sample=next;
currentMeasurement = SamplerTable.at(current_sample);

precool = Ta >currentMeasurement->Ta;
#endif

MethSet* ms = currentMeasurement->getMethod()->methset;
Ta = currentMeasurement->Ta;//s117
Te = currentMeasurement->Te;
Ts = Ta - GSet.stdT;
Ti = Ta; //wirkungslos hier
St = ms->Step;
Rt = ms->Rate / 200.0f; // C/min alle 300 ms
for(int i=0;i<6;i++)
Ptim[i]=(int)(ms->airVol[i]*PUMPFACT);
#ifndef FlashSampler
if(Fp.powvers<200)
stirval = (ms->stir?255:0); //stirrer was at fixed speed for first 5 units with old powerboard
else
#endif
stirval=(int)((float)ms->stir/7.5f); //factor to be set
if(stirval&&stirval<5)
stirval=5;

state=Z_SAMPLE_wait,0);
}