MCA2 Calculator Tutorial Chapter 3

From Mca2
Revision as of 15:41, 27 November 2010 by Badruduja (Talk | contribs)

Jump to: navigation, search

At this point your program should compile successfully. You can run the compiled program by typing

     pcalc

in your console. You can also start mcabrowser to see the structure of the modular architecture – of course there is only one group and one module without any conneting edges by now (see figure 3). But we will change this within the next few minutes.

To do so we first open the mAdd.h file. Note that there are several enums in the public area of the header file. For the moment we focus on the four enums for the ContollerInputs, Controller- Outputs, SensorInputs and SensorOutputs. For each of those enums there is a description DESCR (a macro expansion) which will help the mca2-framework to name the inputs and outputs of your module. Note the name convention: eCI stands for ControllerInput, eCO for ControllerOutput and so on. Be sure to always name your in- and outputs that way.

The only functionality our module has is to sum up the two ControllerInputs Value1 and Value2 and write the result to a SensorOutput named Result. So what we do now is add the Controller- Inputs and the SensorOutput.

ControllerInputs:

     enum {
            eCI VALUE1 ,
            eCI VALUE2 ,
            eCI DIMENSION /∗!< Endmarker and Dimension ∗/
          };

Figure 3: Within the tool mcabrowser you can inspect running mca2 programs and even interact with them by setting ControllerInputs or SensorInputs

Figure 3 Caption Text: Within the tool mcabrowser you can inspect running mca2 programs and even interact with them by setting ControllerInputs or SensorInputs. Here you see our module “Add” within the group PocketCalc.


SensorOutput:

     enum {
            eSO RESULT ,
            eSO DIMENSION /∗!< Endmarker and Dimension ∗/
          };

At the bottom of the header file in the private-declaration area we create a member variable in usual C++-manner:

     double mresult ;

Save the changes and open the mAdd.cpp file. Go to the Control()-function and add the following code:

        if(ControllerInputChanged())
        {
             mresult = ControllerInput (eCI VALUE1 ) + ControllerInput (eCI VALUE2);
             ClearControllerInput ();
        }
        else
        {
            ClearControllerOutput ();
        }

Every module in mca2 has a flag for each the ControllerIn- and Output and the SensorIn- and Output to signalize the module below respectively above that there is new data at the inputs to read (and manipulate on demand). The function

       ControllerInputChanged ()

checks if is the CI-flag is true. If so, we know that data at the ControllerInputs has changed and we have to re-compute whatever our module is written for – here we have to re-add the two values and update the Result-SensorOutput. So every mca2-control-loop we check if the CIs have changed and if so re-compute the result of the addition. This is done by

      mresult = ControllerInput ( eCI VALUE1 ) + ControllerInput ( eCI VALUE2 );

The function ControllerInput(enum index) reads data at the enum index-th ControllerInput and returns the data so it can be used for calculations. Note that in mca2 all data at Controller- and SensorIn- and -Outputs has the C++ data type double. If there was no new data at the ControllerInput we only clear the ControllerOutput (which means we set the ControllerOutput-changed-flag to false) to show modules below in the control-loop that we did not create new information for them.

To write the result of our calculation to the sensor output, add following code to the Sense()- function:

      SensorOutput (eSO_RESULT) = mresult ;
      SetSensorOutputChanged ();

This means: Every time in the mca2-sense-loop the Sense()-function of our module is called the content of our m_result variable is written to the SensorOutput denoted by the enum-index eSO RESULT. We then signalize the modules above that are connected with the SensorOutput of our module that there is new data at their SensorInput(s).

Now the complete functionality of our module is implemented. What comes next is to create the ControllerInputs and the SensorOutput of the group and then connect the communication edges. To do so open the gPocketCalc.h and add the missing ContollerInputs and SensorOutput as we did before for the module.

ControllerInputs:

     enum {
            eCI_VALUE1,
            eCI_VALUE2,
            eCI_DIMENSION /*!< Endmarker and Dimension 
          };

SensorOutput:

     enum {
            eSO_RESULT ,
            eSO_DIMENSION /∗!< Endmarker and Dimension ∗/
          };

Save the changes. Now we are ready to connect the edges. Open the gPocketCalc.cpp and go to the place where we called the constructor of our Add-module before. Below add:

     AddEdgeDown ( this ,addition , 2 ,
                      eCI_VALUE1 , mAdd::eCI_VALUE1 ,
                      eCI_VALUE2 , mAdd::eCI VALUE2 );

This creates a 2-dimensional down-edge between the group (this) and our module (addition). You have to define which number of the group’s ControllerInputs you want to connect with which number of the module’s ControllerInput. This is done by listing the enumeration indices in a “from – to” manner. Here we connect the eCI VALUE1 of the group with the eCI VALUE1 (in namespace mAdd) of our module and so on. Similarly we connect the Sense-side of our module:

     AddEdgeUp (addition, this , 1 ,
                   mAdd :: eSO RESULT , eSO RESULT) ;

This creates a 1-dimensional up-edge between our addition-module and the SensorOutputs of the group. Again: the edge is connected from mAdd::eSO RESULT to eSO RESULT. Recompile and start the program. Connect the mcabrowser to the running program. There you can see the groups and modules organized in a tree. Click on the group and you will see the connected edges. Go ahead and examine the browser a bit (see figure 4). For example click on the connection lines (the edges) between the Add-module and the group.

Figure 4(a): group PocketCalc Add modules

Figure 4 (a) Caption Text: In image (a) you see the group PocketCalc with the module Add. The edge from the ControllerInput of the group to the ControllerInput of the module is drawn in red, the edge from the SensorOutput of the module to the SensorOutput of the group in yellow. In image



We will test the program now: Open the ControllerInput of the group and enter two numbers intthe ControllerInputs. Press “Apply”. Open the SensorOutput of the group. Press “AutoUpdate”You should see the result of the addition there (see figure 5).


< Back=== Forward>

Personal tools