Skip to content

Commit

Permalink
Refactor the Pid class to be completely ROS agnostic and added a ROS …
Browse files Browse the repository at this point in the history
…2 wrapper

Signed-off-by: ahcorde <[email protected]>
  • Loading branch information
ahcorde authored and bmagyar committed Jul 1, 2020
1 parent d227091 commit 205a1e2
Show file tree
Hide file tree
Showing 8 changed files with 631 additions and 328 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_library(${PROJECT_NAME} SHARED
src/dither.cpp
src/limited_proxy.cpp
src/pid.cpp
src/pidROS.cpp
src/sine_sweep.cpp
src/sinusoid.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC include)
Expand Down
87 changes: 17 additions & 70 deletions include/control_toolbox/pid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ namespace control_toolbox
<LI> \f$ p_{error} = p_{state} - p_{target} \f$.
</UL>
\section ROS ROS interface
\param p Proportional gain
\param d Derivative gain
Expand All @@ -86,8 +84,6 @@ namespace control_toolbox
\param i_clamp Min/max bounds for the integral windup, the clamp is applied to the \f$i_{term}\f$
\param publish_state Enable publishing internal controller state on the `state` topic. May break real-time guarantees due to clock_gettime system call.
\section Usage
To use the Pid class, you should first call some version of init()
Expand All @@ -110,10 +106,6 @@ namespace control_toolbox
*/
/***************************************************/

// alias for convenience
using NodeParamsIfacePtr = rclcpp::node_interfaces::NodeParametersInterface::SharedPtr;
using NodePtr = rclcpp::Node::SharedPtr;

class Pid
{
public:
Expand Down Expand Up @@ -150,9 +142,9 @@ class Pid
* initialize Pid-gains and integral term limits.
* Does not initialize dynamic reconfigure for PID gains
*
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param i_max The max integral windup.
* \param i_min The min integral windup.
*/
Expand All @@ -175,49 +167,24 @@ class Pid
* \brief Zeros out Pid values and initialize Pid-gains and integral term limits
* Does not initialize the node's parameter interface for PID gains
*
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param i_max The max integral windup.
* \param i_min The min integral windup.
*/
void initPid(double p, double i, double d, double i_max, double i_min, bool antiwindup = false);

/*!
* \brief Zeros out Pid values and initialize Pid-gains and integral term limits
* Initializes the node's parameter interface for PID gains
*
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param i_max The max integral windup.
* \param i_min The min integral windup.
*/
void initPid(
double p, double i, double d, double i_max, double i_min, NodeParamsIfacePtr node_param_iface);

void initPid(
double p, double i, double d, double i_max, double i_min, bool antiwindup,
NodeParamsIfacePtr node_param_iface);

/*!
* \brief Initialize real-time pid state publisher
*
* \param node A pointer to the node.
*/

void initPublisher(NodePtr node, std::string topic_prefix = "");

/*!
* \brief Reset the state of this PID controller
*/
void reset();

/*!
* \brief Get PID gains for the controller.
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param i_max The max integral windup.
* \param i_min The min integral windup.
*/
Expand All @@ -233,9 +200,9 @@ class Pid

/*!
* \brief Set PID gains for the controller.
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param i_max The max integral windup.
* \param i_min The min integral windup.
*/
Expand All @@ -253,11 +220,11 @@ class Pid
* and the timestep \c dt.
*
* \param error Error since last call (error = target - state)
* \param dt Change in time since last call
* \param dt Change in time since last call in seconds
*
* \returns PID command
*/
double computeCommand(double error, rclcpp::Duration dt);
double computeCommand(double error, double dt);

/*!
* \brief Set the PID error and compute the PID command with nonuniform
Expand All @@ -266,11 +233,11 @@ class Pid
*
* \param error Error since last call (error = target - state)
* \param error_dot d(Error)/dt since last call
* \param dt Change in time since last call
* \param dt Change in time since last call in seconds
*
* \returns PID command
*/
double computeCommand(double error, double error_dot, rclcpp::Duration dt);
double computeCommand(double error, double error_dot, double dt);

/*!
* \brief Set current command for this PID controller
Expand All @@ -288,12 +255,7 @@ class Pid
* \param ie The integral error.
* \param de The derivative error.
*/
void getCurrentPIDErrors(double * pe, double * ie, double * de);

/*!
* \brief Print to console the current parameters
*/
void printValues(const rclcpp::Logger & logger);
void getCurrentPIDErrors(double & pe, double & ie, double & de);

/*!
* @brief Custom assignment operator
Expand All @@ -315,30 +277,15 @@ class Pid
}

private:
void setParameterEventCallback();

private:
using ClockPtr = rclcpp::Clock::SharedPtr;
using PidStateMsg = control_msgs::msg::PidState;

// Store the PID gains in a realtime buffer to allow dynamic reconfigure to update it without
// blocking the realtime update loop
realtime_tools::RealtimeBuffer<Gains> gains_buffer_;

std::shared_ptr<rclcpp::Publisher<PidStateMsg>> state_pub_;
std::shared_ptr<realtime_tools::RealtimePublisher<PidStateMsg>> rt_state_pub_;
ClockPtr clock_;

double p_error_last_; /**< _Save position state for derivative state calculation. */
double p_error_; /**< Position error. */
double i_error_; /**< Integral of position error. */
double d_error_; /**< Derivative of position error. */
double cmd_; /**< Command to send. */

NodeParamsIfacePtr node_param_iface_;

using OnSetParamsCallbackPtr = rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr;
OnSetParamsCallbackPtr parameter_callback_;
};

} // namespace control_toolbox
Expand Down
176 changes: 176 additions & 0 deletions include/control_toolbox/pidROS.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) 2008, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the Willow Garage nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
#ifndef CONTROL_TOOLBOX__PIDROS_HPP_
#define CONTROL_TOOLBOX__PIDROS_HPP_

#include <memory>
#include <string>

#include "control_msgs/msg/pid_state.hpp"

#include "rclcpp/clock.hpp"
#include "rclcpp/duration.hpp"
#include "rclcpp/node.hpp"

#include "realtime_tools/realtime_buffer.h"
#include "realtime_tools/realtime_publisher.h"

#include "control_toolbox/pid.hpp"


namespace control_toolbox
{

class PidROS
{
public:
/*!
* \brief Destructor of PidROS class.
*
* The node is passed to this class to handler the ROS parameters, this class allows
* to add a prefix to the pid parameters
*
* \param node ROS node
* \param topic_prefix prefix to add to the pid parameters.
*/
explicit PidROS(rclcpp::Node::SharedPtr & node, std::string topic_prefix = std::string(""));

/*!
* \brief Destructor of PidROS class.
*/
~PidROS();

/*!
* \brief Initialize the PID controller and set the paramaters
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param i_max The max integral windup.
* \param i_min The min integral windup.
*/
void initPid(double p, double i, double d, double i_max, double i_min, bool antiwindup);

/*!
* \brief Initialize the PID controller based on already setted parameters
* \return True is all parameters are set (p, i, d, i_min and i_max), False otherwise
*/
bool initPid();

/*!
* \brief Reset the state of this PID controller
*/
void reset();

/*!
* \brief Set the PID error and compute the PID command with nonuniform time
* step size. The derivative error is computed from the change in the error
* and the timestep \c dt.
*
* \param error Error since last call (error = target - state)
* \param dt Change in time since last call in seconds
*
* \returns PID command
*/
double computeCommand(double error, rclcpp::Duration dt);

/*!
* \brief Get PID gains for the controller.
* \return gains A struct of the PID gain values
*/
Pid::Gains getGains();

/*!
* \brief Set PID gains for the controller.
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param i_max The max integral windup.
* \param i_min The min integral windup.
*/
void setGains(double p, double i, double d, double i_max, double i_min, bool antiwindup = false);

/*!
* \brief Set PID gains for the controller.
* \param gains A struct of the PID gain values
*/
void setGains(const Pid::Gains & gains);

/*!
* \brief Set current command for this PID controller
*/
void setCurrentCmd(double cmd);

/*!
* \brief Return current command for this PID controller
*/
double getCurrentCmd();

/*!
* \brief Return PID error terms for the controller.
* \param pe[out] The proportional error.
* \param ie[out] The integral error.
* \param de[out] The derivative error.
*/
void getCurrentPIDErrors(double & pe, double & ie, double & de);

/*!
* \brief Print to console the current parameters
*/
void printValues();

private:
void setParameterEventCallback();

void publishPIDState(double cmd, double error, rclcpp::Duration dt);

void declare_param(const std::string & param_name, rclcpp::ParameterValue param_value);

bool getDoubleParam(const std::string & param_name, double & value);

bool getBooleanParam(const std::string & param_name, bool & value);

rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr parameter_callback_;

rclcpp::Node::SharedPtr node_;

std::shared_ptr<realtime_tools::RealtimePublisher<control_msgs::msg::PidState>> rt_state_pub_;
std::shared_ptr<rclcpp::Publisher<control_msgs::msg::PidState>> state_pub_;

Pid pid_;
std::string topic_prefix_;
};

} // namespace control_toolbox

#endif // CONTROL_TOOLBOX__PIDROS_HPP_
Loading

0 comments on commit 205a1e2

Please sign in to comment.