C++ Interface

This collection of basic examples are provided to ease and aid the introduction to programming applications using RobWork.

Template CMake Project

This tutorial will demonstrate how to create a simple RobWork application using CMake. First create a directory for your project. Now add a SampleTest.cpp file and type in a small main application:

RobWork/example/consoleapp/SampleTest.cpp
 1#include <rw/core/Log.hpp>
 2#include <rw/loaders/WorkCellLoader.hpp>
 3#include <rw/models/WorkCell.hpp>
 4
 5using rw::core::Log;
 6using rw::loaders::WorkCellLoader;
 7using rw::models::WorkCell;
 8
 9int main(int argc, char** argv)
10{
11    if (argc != 2) {
12        Log::errorLog() << "Usage: " << argv[0] << " <workcell>" << std::endl;
13        return 1;
14    }
15
16    const std::string file = argv[1];
17    WorkCell::Ptr workcell = WorkCellLoader::Factory::load(file);
18    if (workcell.isNull()) {
19        Log::errorLog() << "WorkCell could not be loaded." << std::endl;
20        return 1;
21    }
22
23    Log::infoLog() << "Workcell " << workcell->getName();
24    Log::infoLog() << " successfully loaded." << std::endl;
25    return 0;
26}

This example loads a WorkCell based on the argument given to the program. In the first lines, the header files are included for the types we use in the program. With the using statements we can avoid typing the full name when we use the Log, WorkCellLoader and WorkCell types in the code. Alternatively, one would have to write rw::core::Log, rw::loaders::WorkCellLoader and rw::models::WorkCell respectively. If you use more than one class from the same namespace, you should instead write:

using namespace rw::core;
using namespace rw::loaders;
using namespace rw::models;

When many different namespaces and classes from RobWork are used, it can be somewhat tedious to write the using namespace and includes in every file. Instead a general header file rw/rw.hpp and a macro can be used to assemble all classes into one namespace. To include everything from RobWork We can instead write:

#include <rw/rw.hpp>
USE_ROBWORK_NAMESPACE
using namespace robwork;

Notice that including all headers from RobWork can be quite heavy for the C++ preprocessor. This might cause a penalty for the time used to compile the program. For small programs like this, it is acceptable to import all the headers. Always avoid importing rw/rw.hpp in header files. Especially when developing libraries, as the header file might be included in many files that will each be hit by a compilation time penalty.

Notice that using and using namespace must never be used in header files. Only if used inside functions.

In this example we write the output to Log::errorLog() and Log::infoLog(). For a console application like this, it makes little difference if you instead output directly to std::cout. It is good practice to use the Log facility anyways. If you later need to use a snippet of code or call a library function from a RobWorkStudio plugin, the output will be shown in the RobWorkStudio Log plugin instead of the terminal. In case RobWorkStudio is not launched from a terminal, this is very convenient. The output would otherwise be invisible to the user.

Now create a CMakeLists.txt file with the following content:

RobWork/example/consoleapp/CMakeLists.txt
 1#####################################################
 2# Template for building RobWork dependent console application
 3#
 4# You should edit directly in this file to add extra source 
 5# files and when configuring your project.  
 6#####################################################
 7
 8# Test CMake version
 9cmake_minimum_required(VERSION 3.10  )
10
11# The name of the project. (EDIT THIS)
12project(ConsoleApp)
13
14# Used to resolve absolute path names
15set(ROOT ${CMAKE_CURRENT_SOURCE_DIR})
16
17# optionally: point the find package in the direction of the robwork.
18SET(RobWork_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../cmake)
19find_package(RobWork REQUIRED PATHS ${RobWork_DIR})
20
21link_directories( ${ROBWORK_LIBRARY_DIRS} )
22
23# Set the output dir for generated libraries and binaries
24if(MSVC)
25	SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${ROOT}/bin" CACHE PATH "Runtime directory" FORCE)
26	SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${ROOT}/libs" CACHE PATH "Library directory" FORCE)
27	SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${ROOT}/libs" CACHE PATH "Archive directory" FORCE)
28else()
29	SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${ROOT}/bin/${CMAKE_BUILD_TYPE}" CACHE PATH "Runtime directory" FORCE)
30	SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${ROOT}/libs/${CMAKE_BUILD_TYPE}" CACHE PATH "Library directory" FORCE)
31	SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${ROOT}/libs/${CMAKE_BUILD_TYPE}" CACHE PATH "Archive directory" FORCE)
32endif()
33
34# And now we add any targets that we want
35add_executable(SampleTest SampleTest.cpp)
36target_link_libraries(SampleTest PRIVATE ${ROBWORK_LIBRARIES})
37target_include_directories(SampleTest PRIVATE ${ROBWORK_INCLUDE_DIRS} )
38
39# if you have additional libraries or include dirs then add them here
40set(USER_LIBRARIES )
41target_link_libraries(SampleTest PRIVATE ${USER_LIBRARIES})
42target_include_directories(SampleTest PRIVATE ${USER_LIBRARIES})
43link_directories( )

Before running CMake, you should set the RW_ROOT environment variable to the path for RobWork. CMake will complain if it does not find RobWork.

Now make a build directory and from this directory run the command:

cmake -DCMAKE_BUILD_TYPE=Release path/to/project

On Windows you should also specify the generator based on your Visual Studio version:

cmake -DCMAKE_BUILD_TYPE=Release -G "Visual Studio 15 2017 Win64" path/to/project

You need to set the build type to the same build type that you used for compiling RobWork. Insert the correct path to the project code (the directory with the CMakeLists.txt file).

Build the project with make (on Linux):

make

In Windows, open the ConsoleApp solution in Visual Studio (solution was generated with CMake). Once the compilation succeeds, the executable will be put under the bin directory in the folder where CMakeLists.txt is located.

API documentation

The user is encouraged to use the C++ API documentation to look up functionality, classes and functions in RobWork. Please also consult the Manual for various examples of using the RobWork library.