WOLFRAM SYSTEMMODELER

GettingStarted

Step-by-step guide to running your first model using ModelPlug

Information

This guide assumes that you have an Arduino (or Firmata-compatible board) and basic knowledge on how to use it. If you are just starting with the Arduino, we recommend you to first visit the Arduino webpage, http://arduino.cc, and have a look at the “Getting Started” guide. Before continuing, please ensure that you can upload Arduino examples to your board and that it is working correctly.

Preparing Your Board

Basic knowledge of SystemModeler is assumed for this guide. If you do not have ModelPlug installed on your computer, please go ahead and install it. We recommend that you have a look at the learning resources at http://www.wolfram.com/system-modeler/resources if you are not familiar with SystemModeler.

The first thing you need to do is to upload the Firmata code to your board. The code is included with the Arduino distribution (v1.0.5 or higher) as part of the examples. In the menu, click Files > Examples > Firmata > StandardFirmata (see Figure 1). Open this sketch and upload it to your board.

Figure 1. Location of the Firmata sketch.

Once the Firmata upload has fininshed, write down the serial port name as you will need to give the port name to ModelPlug later in order to communicate with the board. The serial port name can be found under the menu Tools > Serial Port or in the bottom-right corner of the Arduino software window (see Figure 2). In Windows, the serial port name is something like “COM5”, while in MacOS and Linux the name will be something like “/dev/ttyACM0”. You are now ready to create your first model.

Figure 2. Finding the serial port being used.

Blinking LED

As a first exercise, with the purpose of introducing ModelPlug, you are going to recreate the ModelPlug blinking LED example model. To build the model, create a new model and drag & drop the following components from the Class Browser to the diagram view of your model:

  • ModelPlug.Pins.DigitalOutput
  • ModelPlug.Boards.Arduino
  • Modelica.Blocks.Sources.BooleanPulse
Connect the components as in Figure 3. The 'DigitalOutput' component is connected to the 'Arduino' component in order to associate a specific pin to a specific board. This is necessary as multiple boards with multiple pins can be used at the same time.

Figure 3. Diagram of the blinking LED.

Next, specify the serial port that the board is going to be using. Select the 'Arduino' component in the diagram view to list its parameters. In the parameter view, locate the 'Port' parameter and click the icon to the right of its text box to bring up the Serial Port Selector window. Select the port you obtained from section “Preparing your board”. If you manually type the name of the port, ensure that the name is surrounded by quotation marks, as shown in Figure 4.

Note that some Arduino boards, such as the Leonardo, require that the 'UseDTR' parameter is set to true. If you are not sure whether your board requires it, you can always go back and change it later if the board would not be responding.

Figure 4. Specifying the serial port name. 

Earlier you associated an output pin to the board by creating a connection between two components. Now you need to specify what output pin you are going to use. Select the 'DigitalOutput' component and set the 'Pin' parameter to the number of the pin. Arduino boards usually have an LED attached to pin 13.

Figure 5. Specifying the pin to use.

Finally, for the 'BooleanPulse' component, set the 'period' parameter to 1. The model is now ready to simulate. Press the simulate button in the tool bar to build and simulate the model.

The first time you run the simulation it will probably finish without you noticing the LED blinking even once. The reason for this is that SystemModeler performs the simulation of the model as fast as possible. In order to interact with your models using ModelPlug, it is necessary to synchronize the simulation time with real-time. This is done in Simulation Center. After the first simulation of the model, check the the checkbox 'Synchronize with real-time' in the Settings view of the Experiment Browser, as shown in Figure 6.

Figure 6. Synchronizing your simulation with real time.

If you plan on creating many models with the ModelPlug library, you probably want to make the “Synchronize with real-time” setting the default setting. This is done in Simulation Center under the menu Tools > Options in the section Simulation Center > Default Experiment (see Figure 7).

Figure 7. Setting "Synchronize with real-time" as default.

Run the simulation again and you should see the LED blinking until the simulation reaches the stop time. If you want to keep the simulation running continuously, you need to change the stop time to infinity as shown in Figure 8.

Figure 8. Setting the stop time to infinity.

If you run the simulation again, you should see the LED blinking continuously.

One thing that you may have noticed is that ModelPlug prints messages in the Simulation Log view when the simulation is started (see Figure 9). The messages are explained below:

  • A. A list of all available serial ports.
  • B. The name and speed of the currently used serial port.
  • C. A confirmation that the board was successfully initialized. The board's Firmata version is usually shown here.
  • D. The sampling interval for the board is set.
  • E. A list of all pin configurations. In this example, pin 13 is set to be an output as a result of the configuration of the 'DigitalOutput' component done earlier.
  • F. A list of all available pins and their capabilities. This list contains the numbers of the pins and the modes in which they can be used.

Figure 9. Messages shown by ModelPlug.

Troubleshooting

Here is checklist that you can follow in order to solve most of the problems that can occur when using the ModelPlug library:

  1. Verify that you can upload Arduino examples to your board.
  2. Verify that you have uploaded the StandardFirmata example to your board.
  3. Verify that the port name in the board component in the model matches the port name of the connected board.
  4. Verify that the port name is surrounded by quotation marks.
  5. ModelPlug will not be able to connect to the board if there is another application using the port. Ensure that no other application is using the port.
  6. If the simulation log does not show the board capabilities, try uploading the Firmata to your board again.
  7. When using multiple boards, your operating system may have changed the port names. Verify that the port names in the board components match to port names of the hardware boards.
  8. Some Arduino boards, such as the Leonardo, require the 'UseDTR' parameter to be set to true. Change the parameter and try again.
If you are still having problems, we recommend a visit to http://community.wolfram.com where you ask questions and connect with other users of ModelPlug.

What Next?

ModelPlug contains a series of basic examples showing the functionality of the components. The examples can be found under ModelPlug.Examples. Once you have learned how to use the basic components of ModelPlug, visit the Arduino Playground to learn how to connect other sensors and actuators.

Wolfram Language

In[1]:=
Click for copyable input
SystemModel["ModelPlug.GettingStarted"]
Out[1]:=