Generating code from an existing ROS Simulink model![Conflict Detection Enabled Conflict Detection Enabled](/sites/all/themes/redux/css/images/icons/conflict_enabled_icon.png)
Overview
The toal of this tutorial is to demonstrate how to generate code from Simulink that will run in your ROS workspace for the catvehicle.
Load the Simulink model
Start by loading the Simulink model. I typically start up MATLAB and have it running from the root of my workspace.
cd catvehicle_ws/ source devel/setup.bash matlab
Navigate to src/catvehicle/simulink and open up the catvehicle_hoffmannFollower.slx file
We could just run this model, but that will result in Simulink's overhead operating while the other simulator bits are running (plus, we already did it in Following a circular path): so we'll take advantage of the powerful code generation capabilities in MATLAB/Simulink.
Ensure roscore is running
In a new tab:
roscore
If roscore is not running, you will get strange messages about missing message types and other things that go wrong when you try to have MATLAB generate code for you. If you are running something already through roslaunch, then roscore is running automatically.
Check out the Model Parameters
In the simulink model, press ^E or select from the menu: "Simulation->Model configuration and parameters"
Under Hardware Implementation make "Hardware board: Robot Operating System (ROS)" is selected (it should be already). This will ensue that the generated code will be for ROS and not the MATLAB/Simulink default.
Build the code
Press ^B or from the menu, "Code->C/C++ Code->Build Model". After some time you should see information about model building success!
Note: if you get an error here about ROS master, don't forget to have roscore running.
Copy the generated code to your catvehicle_ws workspace
The generated code is now in whatever directory you were navigated into in MATLAB. I was in the catvehicle_ws directory already, so my code can quickly be extracted without providing path information. In addition to generated the output code, a file called build_ros_model.sh was produced. I will now run it:
./build_ros_model.sh catvehicle_hoffmannFollower.tgz . Catkin project directory: ./src/catvehicle_hoffmannfollower Base path: /home/sprinkle/catvehicle_ws Source space: /home/sprinkle/catvehicle_ws/src Build space: /home/sprinkle/catvehicle_ws/build Devel space: /home/sprinkle/catvehicle_ws/devel Install space: /home/sprinkle/catvehicle_ws/install #### #### Running command: "cmake /home/sprinkle/catvehicle_ws/src -DCATKIN_DEVEL_PREFIX=/home/sprinkle/catvehicle_ws/devel -DCMAKE_INSTALL_PREFIX=/home/sprinkle/catvehicle_ws/install -G Unix Makefiles" in "/home/sprinkle/catvehicle_ws/build" #### -- Using CATKIN_DEVEL_PREFIX: /home/sprinkle/catvehicle_ws/devel -- Using CMAKE_PREFIX_PATH: /home/sprinkle/tmp/catvehicle_ws/devel;/home/sprinkle/cvchallenge-task2/devel;/opt/ros/indigo -- This workspace overlays: /home/sprinkle/tmp/catvehicle_ws/devel;/home/sprinkle/cvchallenge-task2/devel;/opt/ros/indigo -- Using PYTHON_EXECUTABLE: /usr/bin/python -- Using Debian Python package layout -- Using empy: /usr/bin/empy -- Using CATKIN_ENABLE_TESTING: ON -- Call enable_testing() -- Using CATKIN_TEST_RESULTS_DIR: /home/sprinkle/catvehicle_ws/build/test_results -- Found gtest sources under '/usr/src/gtest': gtests will be built -- Using Python nosetests: /usr/bin/nosetests-2.7 -- catkin 0.6.18 -- BUILD_SHARED_LIBS is on -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- ~~ traversing 3 packages in topological order: -- ~~ - catvehicle_hoffmannfollower -- ~~ - obstaclestopper -- ~~ - catvehicle -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- +++ processing catkin package: 'catvehicle_hoffmannfollower' -- ==> add_subdirectory(catvehicle_hoffmannfollower) -- Using these message generators: gencpp;genlisp;genpy -- +++ processing catkin package: 'obstaclestopper' -- ==> add_subdirectory(obstaclestopper) -- +++ processing catkin package: 'catvehicle' -- ==> add_subdirectory(catvehicle) -- Using these message generators: gencpp;genlisp;genpy -- Boost version: 1.54.0 -- Found the following Boost libraries: -- system -- Configuring done -- Generating done -- Build files have been written to: /home/sprinkle/catvehicle_ws/build #### #### Running command: "make catvehicle_hoffmannfollower_node -j2 -l2" in "/home/sprinkle/catvehicle_ws/build"
etc. I cut out a bunch of stuff after this. The essence is that the build script both extracts that .tgz file to my catvehicle_ws/src directory, and then calls catkin_make.
ls src catvehicle catvehicle_hoffmannfollower CMakeLists.txt obstaclestopper
If I were to try to run the catvehicle_hoffmannfollower node now, it would fail because I have not refreshed my path:
source devel/setup.bash
Now, I'm ready to start up the simulation again, and run the hoffmannfollower node from generated code, rather than from Simulink.
roslaunch catvehicle catvehicle_skidpan.launch
and in another tab:
gzclient
and in yet another tab:
source devel/setup.bash rosrun catvehicle_hoffmannfollower catvehicle_hoffmannfollower_node
You should now see your car driving around. How does your real-time factor compare to the previous tutorial?