Menu

Example of Defer and Recall for QPCPP on Arduino

2014-06-13
2020-07-14
  • Anton Morgan

    Anton Morgan - 2014-06-13

    I am newer to using the QP Framework and I am trying to understand how to implement the Defer Event state pattern.

    Can anyone lead me towards or provide an example using defer and recall? I will soon be moving to QPC, so an example in either QPC or QPCPP is helpful, though ultimately QPC will be preferred over everything else.

    Thank you very much!

     
  • Quantum Leaps

    Quantum Leaps - 2014-06-13

    The code examples in QP/C++ baseline distribution are applicable to Arduino as well.

    So, I would recommend that you download the latest QP/C++ baseline code from http://sourceforge.net/projects/qpc/files/QP-Cpp/ and look in the directory: qpcpp\examples\win32\mingw\defer.

    Regarding the QP/C framework, you need to download QP/C baseline code from http://sourceforge.net/projects/qpc/files/QP-C and look in the directory: qpc\examples\win32\mingw\defer.

    For completeness, for QP-nano, you need to download QP-nano baseline code from http://sourceforge.net/projects/qpc/files/QP-nano/ and look in the directory: qpn\examples\win32\mingw\defer.


    Update as of July 2020:

    In QP/C/C++ 6.x the examples have been reorganized. Specifically, the folder examples\win32\mingw has been renamed to examples\workstation. This is because all console-style examples can now be built and run on any workstation, including Windows, Linux and MacOS. The provided Makefiles in the examples\workstation folder should work on all these operating systems.

    Also, the recommended way of downloading QP is now directly from state-machine.com in form of "QP-bundle", which includes all QP frameworks, QM modeling tool and QTools collection.

    --MMS

     

    Last edit: Quantum Leaps 2020-07-14
  • Anton Morgan

    Anton Morgan - 2014-06-13

    Thank you for the quick response! This is really helpful.

    I will take a look at this. In addition I am using the QM modeling tool, are there resources similar to the one you just posted, that have QM model examples? I am also looking through the sourceforge directory for this.

    Thank you

     
  • Quantum Leaps

    Quantum Leaps - 2014-06-14

    Many QM models are already available in the QP baseline distributions. You can just grep for model files (*.qm).

    For example, here is the listing of *.qm files from the QP/C++ baseline distribution:

    C:\qp\qpcpp>fart -r -c *.qm
    examples\arm-cm\qk\arm_keil\dpp-qk_ek-lm3s811\dpp.qm
    examples\arm-cm\qk\arm_keil\dpp-qk_ek-tm4c123gxl\dpp.qm
    examples\arm-cm\qk\arm_keil\game-qk_ek-lm3s811\game.qm
    examples\arm-cm\qk\gnu\dpp-qk_ek-lm3s811-lint\dpp.qm
    examples\arm-cm\qk\gnu\dpp-qk_ek-tm4c123gxl\dpp.qm
    examples\arm-cm\qk\gnu\game-qk_ek-lm3s811\game.qm
    examples\arm-cm\qk\iar\dpp-qk_ek-lm3s811-lint\dpp.qm
    examples\arm-cm\qk\iar\dpp-qk_ek-tm4c123gxl\dpp.qm
    examples\arm-cm\qk\iar\game-qk_ek-lm3s811\game.qm
    examples\arm-cm\vanilla\arm_keil\dpp_ek-lm3s811\dpp.qm
    examples\arm-cm\vanilla\arm_keil\dpp_ek-tm4c123gxl\dpp.qm
    examples\arm-cm\vanilla\arm_keil\game_ek-lm3s811\game.qm
    examples\arm-cm\vanilla\gnu\dpp_ek-lm3s811\dpp.qm
    examples\arm-cm\vanilla\gnu\dpp_ek-tm4c123gxl\dpp.qm
    examples\arm-cm\vanilla\gnu\game_ek-lm3s811\game.qm
    examples\arm-cm\vanilla\iar\dpp_ek-lm3s811\dpp.qm
    examples\arm-cm\vanilla\iar\dpp_ek-tm4c123gxl\dpp.qm
    examples\arm-cm\vanilla\iar\game_ek-lm3s811\game.qm
    examples\posix\gnu\dpp\dpp.qm
    examples\posix\gnu\qhsmtst\qhsmtst.qm
    examples\win32\mingw\calc\calc.qm
    examples\win32\mingw\calc2\calc2.qm
    examples\win32\mingw\dpp\dpp.qm
    examples\win32\mingw\dpp-gui\dpp.qm
    examples\win32\mingw\game-gui\game.qm
    examples\win32\mingw\history\history.qm
    examples\win32\mingw\history_qm\history_qm.qm
    examples\win32\mingw\qhsmtst\qhsmtst.qm
    examples\win32\mingw\qmsmtst\qmsmtst.qm
    examples\win32\vc\dpp\dpp.qm
    examples\win32\vc\dpp-gui\dpp.qm
    examples\win32\vc\game-gui\game.qm
    Found 32 file(s).
    

    --MMS

     
  • Anton Morgan

    Anton Morgan - 2014-06-17

    Do any of the QM models have a defer/recall example? If so can you point me to which one that is?

    Thanks for the help.

     
  • Panopticon

    Panopticon - 2014-06-17

    I don't believe there is currently a model for the deferred event design pattern.

    It wouldn't be hard to do, if I can find the time I'll try to throw one together, I would copy Miro's implementation as closely as possible.

     
  • Anton Morgan

    Anton Morgan - 2014-06-17

    That would be awesome. Thanks!

     
  • Panopticon

    Panopticon - 2014-06-17

    OK so this is just a really quick model (attached), I haven't even tested it yet, but hopefully it gives you an idea. (Also, it's modeled after the MinGW version, not tailored to Arduino)

     
  • Anton Morgan

    Anton Morgan - 2014-06-17

    Thank you! I'm actually migrating to our final controller, so not a problem. I will give feedback once I am able to test it.

     
  • Anton Morgan

    Anton Morgan - 2014-06-18

    There is still one thing I am not quite understanding. If idle were it's own Active Object, how would you change the New_Request Transitions to make this work? Right now I am trying to make a test model using QPCPP and Arduino, though after I understand this and the other hardware is ready I will be taking it to QPC.

    Thanks again.

     
  • Panopticon

    Panopticon - 2014-06-18

    There is still one thing I am not quite understanding. If idle were it's own Active Object, how would you change the New_Request Transitions to make this work? Right now I am trying to make a test model using QPCPP and Arduino, though after I understand this and the other hardware is ready I will be taking it to QP.

    In this example, Idle is one state within one state machine within the TServer active object. In other words, all of this processing takes place inside a single active object.

    Someone (presumably another active object, or perhaps an ISR) sends the New Request to the TServer AO. And the sender doesn't even know that the request was deferred. The TServer just comes back and handles it later.

    So I guess what I'm saying is that I don't quite understand when you say, "If idle were its own active object", what that means? In this example, the deferring (in superstate Busy) and the recall (in Idle) both take place within the same active object. I'm not sure how this could be split apart without adding a lot of handshaking. And that kind of defeats the purpose of the design pattern, which hides the whole defer/recall processing.

    Does that make sense, or can you add a bit of clarification to what you're describing?

    Thanks.

     
  • Anton Morgan

    Anton Morgan - 2014-06-18

    Yep that makes sense that idle and busy have to be within the same Active Object, after looking at it further I understand why. Thanks for baring with me. If and when I get my example working, I will post it for completion.

     

    Last edit: Anton Morgan 2014-06-18
  • Dana Good

    Dana Good - 2020-07-14

    I have the same question (how to implement the defer pattern in QP/C using QActive_defer() while working in QM), but unfortunately the model that was so generously posted in 2014 is no longer compatible with QM.

    Is there information about this anywhere online, or could anyone help me with a set of steps? The question uppermost in my mind is whether QM supports this natively or whether I need to insert code of my own.

     
    • Panopticon

      Panopticon - 2020-07-14

      Hi Dana,

      I assume you have a modern (i.e. 6.x) version of QP/C?

      I have 6.7.0 and there is a QM model for the Defer Pattern that can be found under: qpc\examples\workstation\defer\defer.qm

      Hope that helps.

       
      • Panopticon

        Panopticon - 2020-07-14

        Yikes. Apologies. I just realized that the model in my QP distribution is one I create (reverse-engineered from the code). Sorry.

         
        • Dana Good

          Dana Good - 2020-07-14

          Thanks for looking into this.

           
  • Quantum Leaps

    Quantum Leaps - 2020-07-14

    Hi Dana,

    The "defer" example in QP/C/C++ 6.8.1 is still coded manually and the example directory has no QM model file. But the models have been created and will be included in the upcoming QP/C/C++ 6.8.2 release. For now, I simply attach the model file defer.qm for QP/C++. This model should be copied to the directory qpcpp\examples\workstation\defer, where it can be built with the Makefile. The following snippet shows the command-prompt output from a build and run of the defer example:

    C:\qp_lab\qpcpp\examples\workstation\defer>make
    g++ -MM -MT build/defer.o -c -g -O -fno-pie -std=c++11 -pedantic -Wall -Wextra -fno-rtti -fno-exceptions -I.  -I../../../include -I../../../src -I../../../ports/win32-qv -DQP_API_VERSION=9999 defer.cpp > build/defer.d
    g++ -MM -MT build/bsp.o -c -g -O -fno-pie -std=c++11 -pedantic -Wall -Wextra -fno-rtti -fno-exceptions -I.  -I../../../include -I../../../src -I../../../ports/win32-qv -DQP_API_VERSION=9999 bsp.cpp > build/bsp.d
    g++ -c -g -O -fno-pie -std=c++11 -pedantic -Wall -Wextra -fno-rtti -fno-exceptions -I.  -I../../../include -I../../../src -I../../../ports/win32-qv -DQP_API_VERSION=9999 bsp.cpp -o build/bsp.o
    g++ -c -g -O -fno-pie -std=c++11 -pedantic -Wall -Wextra -fno-rtti -fno-exceptions -I.  -I../../../include -I../../../src -I../../../ports/win32-qv -DQP_API_VERSION=9999 defer.cpp -o build/defer.o
    g++ -c -g -O -fno-pie -std=c++11 -pedantic -Wall -Wextra -fno-rtti -fno-exceptions -I.  -I../../../include -I../../../src -I../../../ports/win32-qv -DQP_API_VERSION=9999 ../../../include/qstamp.cpp -o build/qstamp.o
    g++    -no-pie  -L../../../ports/win32-qv/dbg -o build/defer.exe build/bsp.o build/defer.o build/qstamp.o  -lqp -lws2_32
    
    C:\qp_lab\qpcpp\examples\workstation\defer>build\defer.exe
    Deferred Event state pattern
    QP version: 6.8.2
    Press 'n' to generate a new request
    Press ESC to quit...
    idle-ENTRY;
    No deferred requests
    Processing request #1
    receiving-ENTRY; active request: #1
    Request #2 deferred;
    authorizing-ENTRY; active request: #1
    Request #3 deferred;
    Request #4 deferred;
    Request #5 deferred;
    Request #6 IGNORED;
    busy-EXIT; done processing request #1
    idle-ENTRY;
    Request recalled
    Processing request #2
    receiving-ENTRY; active request: #2
    authorizing-ENTRY; active request: #2
    busy-EXIT; done processing request #2
    idle-ENTRY;
    Request recalled
    Processing request #3
    receiving-ENTRY; active request: #3
    authorizing-ENTRY; active request: #3
    busy-EXIT; done processing request #3
    idle-ENTRY;
    Request recalled
    Processing request #4
    receiving-ENTRY; active request: #4
    authorizing-ENTRY; active request: #4
    busy-EXIT; done processing request #4
    idle-ENTRY;
    Request recalled
    Processing request #5
    receiving-ENTRY; active request: #5
    authorizing-ENTRY; active request: #5
    busy-EXIT; done processing request #5
    idle-ENTRY;
    No deferred requests
    final-ENTRY;
    
    C:\qp_lab\qpcpp\examples\workstation\defer>
    
     

    Last edit: Quantum Leaps 2020-07-14
  • Dana Good

    Dana Good - 2020-07-14

    Many thanks - I'll start here.

     

Log in to post a comment.