Interfacing ArtiSynth to MATLAB

5 Scripting commands

The ArtisynthHandle object contains a number of methods that make it possible to load models and control simulation directly from MATLAB, rather than using the ArtiSynth GUI. A full summary of these methods is given Section 11.

In particular, it is possible to load a model and then run or single step a simulation.

To load a model, one may use the method

  loadModel (name, args...)

where name is either the fully-qualified classname of the model’s RootModel, or one of the shorter names that appears under the ArtiSynth Models menu, and args... is an optional variable-length list of string arguments that are used to form the args argument of the model’s build() method.

Once loaded, simulation may be controlled using methods such as play(), pause(), step(), or reset(). The following example shows loading a model called RigidBodyDemo and then having it simulate for 2.5 seconds:

   >> ah.loadModel (’RigidBodyDemo’);
   >> ah.play (2.5);

A particularly powerful feature is the ability to single step execution in a loop, allowing MATLAB to be used to control or inspect the simulation while it is in progress:

   >> % single step the simulation for 100 steps
   >> for i=1:100
   >>    ... adjust inputs if desired ...
   >>    ah.step();
   >>    ... monitor outputs if desired ...
   >> end

This allows MATLAB code to surround each simulation step, assuming a role analogous to the Controller or Monitor objects that can be added to the RootModel. The adjustment of inputs or monitoring of outputs can be accomplished by setting or querying input or output variables of appropriate model components. One way to obtain access to these components is through the find() method discussed above. As a simple example, consider the SimpleMuscleWithController demo describe the ArtiSynth Modeling Guide. The same effect can be achieved by loading SimpleMuscle and then adjusting target position of point p1 directly in MATLAB:

   >> import maspack.matrix.Point3d
   >> ah.loadModel (’SimpleMuscle’);
   >> p1 = ah.find(’models/0/particles/p1’);
   >> for i=1:100
   >>    ang = ah.getTime()*pi/2;
   >>    targ = 0.5*[sin(ang), 0, 1-cos(ang)];
   >>    p1.setTargetPosition (Point3d (avec (targ)));
   >>    ah.step();
   >>    pause (0.01); % slow down simulation if desired
   >> end

The call to pause() in the above code simply slows down the simulation so that it appears to run in real time.