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.
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.
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:
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:
Figure 9. Messages shown by ModelPlug.
Here is checklist that you can follow in order to solve most of the problems that can occur when using the ModelPlug library:
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.