As a mathematical solver, SNOPT is good enough to solve optimization problems such as Linear-Quadratic-Regulator and Model-Predictive-Controller. In ROS framework,
packing SNOPT as a service is an efficient method to get the optimized result. Here we will introduce the whole configuraiton and setting step by step.
Build a Server(C++)
In order to use SNOPT service, a server is required to receive the request and response the result.
Actually, a specified srv file is also needed to transfer information in the interactive process. The tutorial that creates a srv file could be seen at this link.
Here we will use the subroutines of snOptA in the book User’s Guide for SNOPT Version 7: Software for Large-Scale Nonlinear Programming as the example to build the SNOPT server.
The srv file is described as following:
1 2 3 4 5 6
# snopt_msgs/SnoptCal
float64 c1 float64 c2 --- float64[] x
Code
The full code of the server node could be seen at this link. Here we only describe some significant code blocks.
Head file and Static Variables
In order to use snOptA class, a specified head file is required and we also need to define some static variables for Callback function.
In the main function, a ROS node and a ROS server are created. The snOptA problem is also initialized here with defining the constraints according to our own optimization problems.
Callback function is the most import part in the server. In this function, we need to update the state of SNOPT problem through request from the client and solve the optimization problem.
At the end, return the solved result to the client through srv.
## Compile as C++11, supported in ROS Kinetic and newer add_compile_options(-std=c++11)
## Find catkin macros and libraries ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz) ## is used, also find other catkin packages find_package(catkin REQUIRED COMPONENTS roscpp std_msgs snopt_msgs )