Message Boards Message Boards

Importing Systemmodeler models into Mathematica

Posted 10 years ago

I am comfortable creating and saving my models within SystemModeler.

I'd like to load my saved model (/home/cjpembo/Documents/SimpleFourBarLinkage.mo) into Mathematica. I've tried the following:

Needs["WSMLink`"];
myModel = WSMSimulate[
 SystemOpen["/home/cjpembo/Documents/SimpleFourBarLinkage.mo"]];

This will launch SystemModeler and load my saved model within that external program, but Mathematica is not yet ready to access my model. It returns the following:

WSMSimulate[Null]

I must run the command a second time, now using the model name only:

WSMSimulate["SimpleFourBarLinkage"]

I can now fully access my model from within Mathematica.

I'm certain there is a simple way to accomplish this... but the documentation seems to only address loading models that are already included as examples in SystemModeler, or models that are already loaded in a running SystemModeler session.

So to be more clear: I want to open a saved model from within Mathematica without having to load it twice; just like in the example documentation.

Thanks!

POSTED BY: Chris Pemberton
3 Replies

SystemModeler and your Mathematica session actually share a common state of loaded models, if they are both running.

To load a .mo file and then simulate it in Mathematica without launching SystemModeler:

(* Load SystemModeler functionality *)
Needs["WSMLink`"];
(* Load the model you want to simulate *)
myModel = Import["/home/cjpembo/Documents/SimpleFourBarLinkage.mo", "ModelicaModel"];
(* Simulate the model *)
WSMSimulate[myModel]

Documentation is here: http://reference.wolfram.com/system-modeler/WSMLink/ref/format/MO.html

POSTED BY: Malte Lenz
Posted 10 years ago

Thanks; that is what I was looking for.

I've been expiramenting with an arduino (uno and mega) and Wolfram's ModelPlug addon. I'm in linux and SystemModeler gave me the following errors when I ran the BlinkLED example:

Starting simulation of experiment "Model1 1" at 07:53:22 
[ModelPlug.Firmata]: Available ports:
- /dev/ttyACM0
[ModelPlug.Firmata]: ERROR The port ttyACM0 does not exist
[GENERAL]: Server listening on 127.0.0.1:38993
[GENERAL]: Initialization finished.
[GENERAL]: Simulation stopped at time = 10.
[GENERAL]: Simulation took 0.028844 seconds of CPU time.
[GENERAL]: Total number of function evaluations: 8024
[GENERAL]: Total number of events: 1000
[GENERAL]: Total number of step events (dynamic state switches): 0
Simulation exited at 07:53:23

Port /dev/ttyACM0 does exist and my user account has the correct permissions to use it. I've solved the problem: I "think" that linux (in my case Archlinux) sets the port to 9600 baud by default and this borks SystemModeler somehow. I've tried setting/unsetting the arduino component parameter "ShowPinCapabilities", but it still fails. The following command will set the baud higher and then SystemModeler seems to work fine (in my limited testing).

stty -F /dev/ttyACM0 115200

I've been reading the source code that comes with ModelPlug and it seems the author has several comments stating that serial emulation in linux is very challenging. Perhaps ModelPlug could include a parameter to manually set the baud?

POSTED BY: Chris Pemberton
Posted 10 years ago

Here is the output after setting the baud to 115200:

Starting simulation of experiment "Model1 1" at 08:16:13 
[ModelPlug.Firmata]: Available ports:
- /dev/ttyACM0
[ModelPlug.Firmata]: Using port /dev/ttyACM0 with baud rate 57600
[GENERAL]: Server listening on 127.0.0.1:37645
[GENERAL]: Initialization finished.
[ModelPlug.Firmata]: /dev/ttyACM0 StandardFirmata.ino-2.3
[ModelPlug.Firmata]: Setting sampling interval to 10 ms for board /dev/ttyACM0
[ModelPlug.Firmata]: /dev/ttyACM0 setting pin 13 to mode 1
[ModelPlug.Firmata]: Board /dev/ttyACM0 capabilities
- Pin 2 supports:  DigitalInput - DigitalOutput - Servo
- Pin 3 supports:  DigitalInput - DigitalOutput - AnalogOutput - Servo
- Pin 4 supports:  DigitalInput - DigitalOutput - Servo
- Pin 5 supports:  DigitalInput - DigitalOutput - AnalogOutput - Servo
- Pin 6 supports:  DigitalInput - DigitalOutput - AnalogOutput - Servo
- Pin 7 supports:  DigitalInput - DigitalOutput - Servo
- Pin 8 supports:  DigitalInput - DigitalOutput - Servo
- Pin 9 supports:  DigitalInput - DigitalOutput - AnalogOutput - Servo
- Pin 10 supports:  DigitalInput - DigitalOutput - AnalogOutput - Servo
- Pin 11 supports:  DigitalInput - DigitalOutput - AnalogOutput - Servo
- Pin 12 supports:  DigitalInput - DigitalOutput - Servo
- Pin 13 supports:  DigitalInput - DigitalOutput - Servo
- Pin 14 supports:  DigitalInput - DigitalOutput - AnalogInput(A0)
- Pin 15 supports:  DigitalInput - DigitalOutput - AnalogInput(A1)
- Pin 16 supports:  DigitalInput - DigitalOutput - AnalogInput(A2)
- Pin 17 supports:  DigitalInput - DigitalOutput - AnalogInput(A3)
- Pin 18 supports:  DigitalInput - DigitalOutput - AnalogInput(A4)
- Pin 19 supports:  DigitalInput - DigitalOutput - AnalogInput(A5)
- Pin 20 supports: 
[GENERAL]: Simulation stopped at time = 10.
[GENERAL]: Simulation took 9.99968 seconds of CPU time.
[GENERAL]: Total number of function evaluations: 6127
[warning]: Real-time synchronization overshot 0.1 % of all deadlines with an average of 0.005 ms.
[GENERAL]: Total number of events: 1000
[GENERAL]: Total number of step events (dynamic state switches): 0
Simulation exited at 08:16:23

In this example, 1/2 second on/off pulses are sent to the arduino's pin 13 LED starting at t=0 to t=10 seconds. You would expect 20 flashes; but in reality it tales approximately 4 seconds to upload the code to the arduino and querry its capabilities. If you set the booleanPulse component to use a startTime of 5 seconds, that seems to be enough time to upload the code and query the capabilities, and you will see all 10 flashes of the LED from time t=5 to 10 sec. I would suppose this is the case in Windows as well; and not an issue with my serial port - just the arduino taking an amount of time to initilize?

POSTED BY: Chris Pemberton
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract