SD/FAST C++ Interface and MATLAB Integration
Benjamin Stephens
Robotics Institute 2008
This document describes a hack that I have used to implement SD/FAST in C++ and in MATLAB. The motivation behind this was to find a simple way to use different models, or copies of the same model at the same time. The MATLAB script reads the output of SD/FAST and creates several new files that contain the source for the C++ and MATLAB interfaces.
The Files
Download a copy of all the files needed to run SD/FAST++ on an SD/FAST model here: sdfastplus.zip (updated 7/15/2011)
Below is a description of all of the files associated with this script.
- SD/FAST Input File
This is the standard model description file used by SD/FAST. The only requirement is that you DO NOT use a prefix.
- SD/FAST Output Files
SD/FAST outputs 4 files:
- information file (*_i) - gives you an overview of the model topology
- dynamics file (*_d.c) - c code for dynamics calculations
- simplified analysis routines (*_s.c) - c code for analysis, like integration
- standard library file (sdlib.c) - common functions, like vector operations
SD/FAST++ only uses the dynamics file. sdlib.c is the same for every model. I do not provide these files, you need to get them yourself.
- SD/FAST++ Files
These are the files that are used in conjunction with SD/FAST++ to make the magic:
- sdfastplus.m - the script that reads SD/FAST outputs and creates the SD/FAST++ files
- template.h - a template for the class definitions
- template.cpp - a template for the class implementations
- make_template.m - a template for the mex make file
- template_mex.cpp - a template for the mex interface file
- sdlib.h - definitions for the common library functions
- SDModel.h - an abstract base class for all models
- SDModel_common.cpp - common functions that work across models, like forward dynamics
- SDModel_common.h - definitions of the common functions
- SD/FAST++ Output Files
There are several files that SD/FAST++ outputs:
- model.h - the class definition file for the model object
- model.cpp - copies of the dynamics functions, now member functions of the class
- make_model.m - a make file that makes the mex interface
- model_mex - a mex file that returns a pointer to a model object
Instructions
- Installation
- Unzip the files from sdfastplus.zip to a directory
- Add this directory to your MATLAB path
- Compiling a model
- Write a model description file - "model"
- Run SD/FAST - "sdfast -lc -ge model"
- Open MATLAB and run the script (make sure the sdfastplus directory is in the path) - "sdfastplus('model')"
- Run the model mex make file - "make_model"
- An example matlab interface
- Create a matlab mex interface file (example: model_impulse.cpp - others are included in the zip file)
- Make sure you include "SDModel.h" and "SDModel_common.h"
- Compile the mex interface file - mex model_impulse.cpp [SDFAST++Dir]\SDModel.cpp [SDFAST++Dir]\SDModel_common.cpp [SDFAST++Dir]\sdlib.c -I[SDFAST++Dir]
- Create a model object and get the pointer - "m = model_mex;"
- Pass the pointer into your mex interface function - "x_new = model_impulse(m,x_0,body,force);"