Tutorial : Using Signals and Slots


Table of Contents

Using Signals and Slots
Overview
Building the Signal/Slot Example
Implementing Signals and Slots
Index

Using Signals and Slots

Overview

To provide simple, error-free state connections between objects, OmeChron provides a “signal/slot” mechanism whereby the output state of one object is tied to one or more input state “slots” in other objects. The value of an input state slot is never changed through a connected signal until the signaled state is valid. Since OmeChron state signals can be connected to any other object state slot, objects can be coupled freely without regard to type as occurs when using OmeChron references. In this tutorial, we'll walk through creating and connecting models with signals and slots in the OmeChronGUI. A simple output sine wave will be connected to a slot, and the value of the signal plotted on a strip chart. Then we'll go through the details of programming the models in C++ and building the example plugin library.

Building the Signal/Slot Example

We'll demonstrate OmeChron signals and slots using the signal_generator project under the $OMECHRON/examples/signal_generator directory. To build the plugin, follow the usual qmake/make command sequence in that directory.

Start the OmeChron GUI and load the signal_generator plugin library. Drag and drop an examples::SignalGenerator object onto the top-level system palette. Do the same to create an examples::SamplePort object. Click right on the SignalGenerator object and select Signals/Slots to start the Signal/Slot Editor dialog shown below.

The OmeChron Signal/Slot Editor

Figure 1. The OmeChron Signal/Slot Editor


Select Add Slot to start the Add Connection dialog shown below.

Connecting a Signal to a Slot

Figure 2. Connecting a Signal to a Slot


Under the Object selector, choose examples::SamplePort_0 and on the Slot selector choose the input slot. Then, select OK to create the connection. A signal/slot connection appears as a green line between two objects in the OmeChron GUI as shown below. On each update of the signal generator, the current value of the sine wave is "fired" or sent out through the signal to all its connected ports. (A signal can be connected to any number of slots.) On each update of the sample port, it stores the current value in its input slot into a member attribute variable. Next, we'll create and configure a strip chart object to monitor and plot the sample port's value.

Signals and Slots in the OmeChron GUI

Figure 3. Signals and Slots in the OmeChron GUI


This example has the OmeChron plotting package built into it, so we can add a strip chart to visualize the signal as the simulation runs. Drag and drop a StripChart onto the system pallet and set its reference to point to the SamplePort. Double-click on the StripChart to open the inspector and edit plotattrs[0]. This will display the available SamplePort attributes for plotting. Choose the value attribute. The StripChart inspector should appear as shown below.

Editing the StripChart Attributes

Figure 4. Editing the StripChart Attributes


Save your system and run the simulation. You should see the sine wave plotted on the strip chart as shown below.

The Sine Wave Signal Plot

Figure 5. The Sine Wave Signal Plot


Implementing Signals and Slots

[Note]Note

This section assumes that your are familiar with the basics of building OmeChron plugins and C++ programming.

In the section, we'll walk through coding, compiling, and linking the Signal/Slot example using the OmeChron framework. We'll skip the details on building plugin classes since that has already been covered elsewhere. Like attributes and references, signals and slots are exported to the OmeChron system by overriding special base-class functions inherited from the SimModel base class. Let's examine how the SignalGenerator defines and exports its output signal. The SignalGenerator class declaration appears in the following code listing.

 
class SignalGenerator : public SimModel
{
	public:
   ....
   /// Return this object's signals.
      SignalMap getSignals();  1

    private:

      sim::StateSignal m_output_signal;   2

};// end class SignalGenerator

Note the following features..

1

Override the getSignals function to export the model's signals.

2

Declare a signal member variable.

The implementation of the getSignals function appears below.

 
SignalMap SignalGenerator::getSignals()
{
// Get any base class signals...
   SignalMap ans = SimModel::getSignals(); 1

// Add ours...
   add_signal( ans, &m_output_signal ); 2

   return ans;

}// end getSignals() ~~~~~~~~~~~~~~~~~~~~~~~

Note the following features..

1

Call the base class getSignals function to add any base class signals.

2

Add the member signal to the returned signal map.

Input slots are handled in the similar manner as shown below in the listing for the SamplePort declaration.


class SamplePort : public SimModel
{
   public:
  ...
   /// Default constructor.
      SamplePort();   1

   /// Get this object's slots.
      SlotMap getSlots();  2

   private:

      sim::StateSlot m_input_slot;  3

};// end class SamplePort

Note the following features..

1

Declare a constructor to initialize all members including the initial value of the slot.

2

Override the getSlots function to export the slots.

3

Declare the slot as a member variable.


SlotMap SamplePort::getSlots()
{
// Get any base class slots...
   SlotMap ans = SimModel::getSlots();  1

/// Add ours...
   add_slot( ans, &m_input_slot );  2

   return ans;

}// end getSlots() ~~~~~~~~~~~~~~~~~~~~~~~~~

Note the following features..

1

Call the base class getSlots to return any inherited slots.

2

Add the local slot to the return slot map.


Compiling and Linking the Model

CONFIG += ome_plugin   1
TEMPLATE = lib
TARGET = signal_generator

DESTDIR = $(OMECHRON)/bin/plugins

HEADERS = SignalGenerator.h \
                       SamplePort.h

SOURCES = SignalGenerator.cpp \
                       SamplePort.cpp

##
## Make the OmeChron plotting classes part of this plugin model...
##   NOTE: must be included *before* omechron.conf
##
include( $(OMECHRON)/oplot/oplot.pri )

include( $(OMECHRON)/config/omechron.conf )

OBJECTS_DIR = ./obj

Note the following features of the qmake build file for plugin libraries:

1

Add the item ome_plugin to the CONFIG variable to turn on the default plugin build settings. This will automatically configure the required libraries and paths for you.

 

Index

S

Signals
slots and, Using Signals and Slots