Boost logo

Boost Users :

Subject: [Boost-users] Boost MSM: Can not get out of the submachine
From: gauravk (gaurav_kulkarni_at_[hidden])
Date: 2011-06-01 19:50:57


Hi,

I have been playing around with the boost MSM libraries, to know more about
them. I am creating a vending machine simulation with a sub-machine for
accepting payment from the user. The vending machine has 3 states: waiting
(where the machine just waits for the user to start processing), selection
(where user selects a product. The machine makes a transition to this state
from "waiting" state if the event "key_press" occurs and if the key pressed
is the start button (checked by a guard condition).). After a product is
selected the machine goes in payment state, which is a submachine that
accepts payment in quarters. After the payment is received (it is fixed $1
for all products, just to make it simple) the machine again goes in the
waiting state dispensing the product (action: dispatch_item).

The submachine Payment has 4 states: Payment_init which just displays a
message and then makes an immediate automatic transition to the next state
"Paying". In this state the machine just waits for the "coin_insert" event
to occur (which represents inserting a quarter). Once the "coin_insert"
event occurs it makes a transition to "check_payment" state which checks if
sufficient amount was paid. If not it again makes an automatic transition to
"Paying" state, else it moves to the "Payment_done" state which is the last
state in the submachine. This state generates an event "last_state" which is
an indicator that the payment has been made and should cause a transition
from the submachine "Payment" to waiting state (which I am not observing).
The transition tables for both the machines are:
(btw I am using the functors as the front end.)
Table for vending machine:
//
+---------------------+-----------------------+----------------------+---------------------+-------
   Row < waiting, key_press, selection, none, is_start>, /// If start button
pressed, go to selection state
   Row < waiting, key_press, waiting, none, is_cancel>, // If cancel pressed
stay in the waiting state
   Row < selection, key_press, Payment, none, is_number>, //If a product
number pressed go to payment
   Row < selection, key_press, waiting, none, is_cancel>, // If cancel
pressed go to
   Row < Payment, key_press, waiting, none, is_cancel>,
   Row < Payment, last_state, waiting, dispatch_item, none> //the last_state
event is generated in the
                                                                        //
"payment_done" state of the Payment submachine
//
+---------+-------------+---------+---------------------+----------------------+

Table for Payment machine:
//
+---------------------+-----------------------+----------------------+---------------------+-------
      Row < Payment_init, none, Paying, none, none >, // Just make an auto
transition after displaying a msg
      Row < Paying, coin_insert, check_payment, none, none>,
      Row < check_payment, none, Paying, none, is_not_paid>, //If enough
money received
      Row < check_payment, none, payment_done, none, is_paid>
//
+---------+-------------+---------+---------------------+----------------------+

The code for payment_done state is:
struct payment_done: public msm::front::state
{
   template<class Event, class FSM>
   void on_entry(Event const &, FSM &fsm)
   {
      cout << "---------------" << endl;
      cout << "Payment_done" << endl;
      fsm.process_event(last_state());
   }
};

The events that I gave as an input to the state machine are:
Event inputs:
//vm = vending machine
  vm->start();
   vm->process_event(key_press(START)); //press start
   vm->process_event(key_press('5')); // select a product
   vm->process_event(coin_insert(0.25)); // Make payment in quarters and "50
cents"!
   vm->process_event(coin_insert(0.25));
   vm->process_event(coin_insert(0.5));

I am printing out the event/state/action/guard condition name on entry. Here
is the output:
Output:
Trying out the Boost Meta State Machine
---------------
waiting
Please press start button
---------------
Event key_press
Entered key: s // s = start
---------------
Guard condition is_cancel
---------------
Guard condition is_start
---------------
selection
Select a product:
---------------
Event key_press
Entered key: 5
---------------
Guard condition is_cancel
---------------
Guard condition is_number
entering: Payment // submachine
---------------
Payment_Init
---------------
Paying
template Insert coins: //msg displayed in the state //plz ignore the word
"template" in the line
---------------
Event coin_insert
Inserted $0.25
---------------
check_payment
Checking if full payment made in check_payment.
---------------
Guard condition is_paid //check if full payment received
---------------
Guard condition is_not_paid //check if full payment not received (in which
case move to Paying state)
---------------
Paying
template Insert coins: //msg displayed in the state //plz ignore the word
"template" in the line
---------------
Event coin_insert
Inserted $0.25
---------------
check_payment
Checking if full payment made in check_payment.
---------------

Guard condition is_paid //check if full payment received
---------------
Guard condition is_not_paid //check if full payment not received (in which
case move to Paying state)
---------------
Paying
template Insert coins: //msg displayed in the state //plz ignore the word
"template" in the line
---------------
Event coin_insert
Inserted $0.5
---------------
check_payment
Checking if full payment made in check_payment.
---------------
Guard condition is_paid //check if full payment received (which is, in this
case. so move the payment_done state)
---------------
Payment_done
---------------
Event last state //The event occurs but the transition does not

As you can see the event "last_state" is occurring in the payment_done state
but the transition is not taking place. I can post the entire code is
someone is interested but I am just playing around with the libraries so the
code (and some logic too) is not sophisticated at all. Still I can post it
if someone wants to take a look at it.

Thanks!

--
View this message in context: http://boost.2283326.n4.nabble.com/Boost-MSM-Can-not-get-out-of-the-submachine-tp3567263p3567263.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net