Main Content

Generate Code to Manually Deploy a ROS Node from金宝app

This example shows you how to generate C++ code from a Simulink®model to deploy as a standalone ROS node. The code is generated on your computer and must be manually transferred to the target ROS device. No connection to the hardware is necessary for generated the code. For an automated deployment of a ROS node, seeGenerate a Standalone ROS Node from Simulink®.

Prerequisites

  • This example requiresSimulink Coder™and Embedded Coder™ .

  • A Ubuntu Linux system with ROS is necessary for building and running the generated C++ code. You can use your own Ubuntu ROS system, or you can use the Linux virtual machine used for ROS Toolbox™ examples. SeeGet Started with Gazebo and Simulated TurtleBotfor instructions on how to install and use the virtual machine.

  • Review theFeedback Control of a ROS-Enabled Robotexample, which details the Simulink model that the code is being generated from.

Configure A Model for Code Generation

Configure a model to generate C++ code for a standalone ROS node using theConfiguration Parameters. The model used here is the proportional controller introduced in theFeedback Control of a ROS-Enabled Robotexample.

Open the proportional controller model.

edit robotROSFeedbackControlExample

Copy the entire model to a new blank Simulink model.

Delete theSimulation Rate Controlblock.

Simulink model that publishes the command velocity using the pose feedback and desired position input for a proportional controller.

On the Apps tab, underControl Systems, clickRobot Operating System (ROS).

In theRobot Operating System (ROS)刻度盘og box that opens up, selectRobot Operating System (ROS)from theROS Networkdrop-down. This opens up theROStab in the toolstrip which shows the specified ROS Network in theConnectsection.

In thePreparesection underROStab, clickHardware Settingsto open the model configuration parameters dialog box.

TheHardware board settingssection contains settings specific to the generated ROS package, such as information included in thepackage.xmlfile. ChangeMaintainer nametoROS Example User并点击OK.

In theSolverpane of theConfiguration Parameters刻度盘og, ensure theTypeis set toFixed-step, theSolveris set toode3 (Bogacki-Shampine)and theFixed-step sizeis set to0.05. In generated code, the fixed-step size defines the actual time step that is used for the model update loop. SeeExecution of Code Generated from a Model(Simulink Coder)for more information.

In theCode Generation窗格中,确保variable-size signalsis enabled.

ClickOKto close theConfiguration Parameters刻度盘og. Save the model asRobotController.slx.

Configure the Build Options for Code Generation

After configuring the model, you must specify the build options for the target hardware and set the folder or building the generated code.

ClickDeployunder theROStab. Then underDeployment, clickBuild Model.This setting ensures that code is generated for the ROS node without building it on an external ROS device.

Generate and Deploy the Code

Start a ROS master in MATLAB®. This ROS master is used by Simulink for the code generation steps.

In the MATLAB command window type:

rosinit

Set the current folder to a writable directory. This folder is the location that generate code will be stored when you build the model.

Under theC Codetab, clickGenerate Codeor pressCtrl+Bto start code generation for the model.

Once the build completes, two new files are written to your folder.

  • RobotController.tgz–– An archive containing the C++ code

  • build_ros_model.sh–– A shell script for extracting and building the C++ code

Files in the working folder after the build is complete.

Manually transfer the two files to the target machine. If you connect to a ROS device usingrosdevice, you can send files usingputFile. Otherwise, this step assumes you are using the Linux virtual machine used for Robotics System Toolbox™ examples. The virtual machine is configured to accept SSH and SCP connections.If you are using your own Linux system, consult your system administrator for a secure way to transfer files.

Ensure your host system (the system with yourRobotController.tgzandbuild_ros_model.shfiles) has an SCP client. For Windows®systems, the next step assumes that PuTTY SCP client (pcsp.exe) is installed.

Use SCP to transfer the files to the user home director on the Linux virtual machine. Username isuserand password ispassword. Replacewith your virtual machines IP address.

  • Windows host systems:

    pscp.exe RobotController.tgz build_ros_model.sh user@:
  • Linux ormacOShost systems:

    scp RobotController.tgz build_ros_model.sh user@:

Thebuild_ros_model.shfile is not specific to this model. It only needs to be transferred once for multiple models.

On the Linux system, execute the following commands to create a Catkin workspace. You may use an existing Catkin workspace.

mkdir -p ~/catkin_ws_simulink/src cd ~/catkin_ws_simulink/src catkin_init_workspace

Decompress and build the node there using the following command in Linux. Replacewith the path to your catkin workspace.

cd ~ ./build_ros_model.sh RobotController.tgz 

If that does not work, ensure thatbuild_ros_model.shis set up as an executable by entering the following command.

chmod +x build_ros_model.sh

The generated source code is under~/catkin_ws_simulink/src/robotcontroller/. Review the contents of thepackage.xmlfile. Verify that the node executable was created using:

file ~/catkin_ws_simulink/devel/lib/robotcontroller/robotcontroller_node

If the executable was created successfully, the command lists information about the file.

The model is now ready to be run as a standalone ROS node on your device.

Optional:You can then run the node using this command. Replacewith the path to your catkin workspace.

~//devel/lib/robotcontroller/robotcontroller_node

Related Topics