Skip to content

Commit

Permalink
Refactor command processing to seprate translation unit
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahiminfinite committed Jun 4, 2023
1 parent 8ae96fd commit 56fe805
Show file tree
Hide file tree
Showing 10 changed files with 553 additions and 326 deletions.
4 changes: 3 additions & 1 deletion moveit_ros/moveit_servo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ generate_parameter_library(

# This library provides a C++ interface for sending realtime twist or joint commands to a robot
add_library(moveit_servo_lib SHARED
src/collision_monitor.cpp
src/command_processor.cpp
src/servo.cpp
src/utils.cpp
src/collision_monitor.cpp

)
set_target_properties(moveit_servo_lib PROPERTIES VERSION "${moveit_servo_VERSION}")
ament_target_dependencies(moveit_servo_lib ${THIS_PACKAGE_INCLUDE_DEPENDS})
Expand Down
165 changes: 88 additions & 77 deletions moveit_ros/moveit_servo/config/servo_parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -264,80 +264,91 @@ servo:

############### POSE TRACKING PARAMETERS #########################

windup_limit: {
type: double,
default_value: 0.05,
description: "Maximum value of error integral for all PID controllers"
}

x_proportional_gain: {
type: double,
default_value: 1.5,
description: "Proportional gain value for the controller in x direction"
}

y_proportional_gain: {
type: double,
default_value: 1.5,
description: "Proportional gain value for the controller in y direction"
}

z_proportional_gain: {
type: double,
default_value: 1.5,
description: "Proportional gain value for the controller in z direction"
}

x_integral_gain: {
type: double,
default_value: 0.0,
description: "Integral gain value for the controller in x direction"
}

y_integral_gain: {
type: double,
default_value: 0.0,
description: "Integral gain value for the controller in y direction"
}

z_integral_gain: {
type: double,
default_value: 0.0,
description: "Integral gain value for the controller in z direction"
}

x_derivative_gain: {
type: double,
default_value: 0.0,
description: "Derivative gain value for the controller in x direction"
}

y_derivative_gain: {
type: double,
default_value: 0.0,
description: "Derivative gain value for the controller in y direction"
}

z_derivative_gain: {
type: double,
default_value: 0.0,
description: "Derivative gain value for the controller in z direction"
}

angular_proportional_gain: {
type: double,
default_value: 0.5,
description: "Proportional gain value for angular control"
}

angular_integral_gain: {
type: double,
default_value: 0.0,
description: "Integral gain value for angular control"
}

angular_derivative_gain: {
type: double,
default_value: 0.0,
description: "Derivative gain value for angular control"
}
pose_tracking:
x_pid:
kp: {
type: double,
default_value: 1.5,
description: "Proportional gain value for the controller in x direction"
}

ki: {
type: double,
default_value: 0.0,
description: "Integral gain value for the controller in x direction"
}

kd: {
type: double,
default_value: 0.0,
description: "Derivative gain value for the controller in x direction"
}

y_pid:
kp: {
type: double,
default_value: 1.5,
description: "Proportional gain value for the controller in y direction"
}

ki: {
type: double,
default_value: 0.0,
description: "Integral gain value for the controller in y direction"
}

kd: {
type: double,
default_value: 0.0,
description: "Derivative gain value for the controller in y direction"
}

z_pid:
kp: {
type: double,
default_value: 1.5,
description: "Proportional gain value for the controller in z direction"
}

ki: {
type: double,
default_value: 0.0,
description: "Integral gain value for the controller in z direction"
}

kd: {
type: double,
default_value: 0.0,
description: "Derivative gain value for the controller in z direction"
}

q_pid:
kp: {
type: double,
default_value: 0.5,
description: "Proportional gain value for angular control"
}

ki: {
type: double,
default_value: 0.0,
description: "Integral gain value for angular control"
}

kd: {
type: double,
default_value: 0.0,
description: "Derivative gain value for angular control"
}

windup_limit: {
type: double,
default_value: 0.05,
description: "Maximum value of error integral for all PID controllers"
}

use_anti_windup: {
type: bool,
default_value: true,
description: "If the controller should use anit windup"
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ int main(int argc, char* argv[])
auto servo_params = servo_param_listener->get_params();

// The publisher to send trajectory message to the robot controller.
rclcpp::Publisher<trajectory_msgs::msg::JointTrajectory>::SharedPtr trajectory_outgoing_cmd_pub_;
trajectory_outgoing_cmd_pub_ = servo_node->create_publisher<trajectory_msgs::msg::JointTrajectory>(
rclcpp::Publisher<trajectory_msgs::msg::JointTrajectory>::SharedPtr trajectory_outgoing_cmd_pub;
trajectory_outgoing_cmd_pub = servo_node->create_publisher<trajectory_msgs::msg::JointTrajectory>(
servo_params.command_out_topic, rclcpp::SystemDefaultsQoS());

// Create the servo object
Expand Down Expand Up @@ -91,7 +91,7 @@ int main(int argc, char* argv[])
if (servo.getStatus() != StatusCode::INVALID)
{
auto joint_trajectory = composeTrajectoryMessage(servo_params, joint_state);
trajectory_outgoing_cmd_pub_->publish(std::move(joint_trajectory));
trajectory_outgoing_cmd_pub->publish(joint_trajectory);
}

rate.sleep();
Expand Down
6 changes: 3 additions & 3 deletions moveit_ros/moveit_servo/demos/cpp_interface/demo_twist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ int main(int argc, char* argv[])
auto servo_params = servo_param_listener->get_params();

// The publisher to send trajectory message to the robot controller.
rclcpp::Publisher<trajectory_msgs::msg::JointTrajectory>::SharedPtr trajectory_outgoing_cmd_pub_;
trajectory_outgoing_cmd_pub_ = servo_node->create_publisher<trajectory_msgs::msg::JointTrajectory>(
rclcpp::Publisher<trajectory_msgs::msg::JointTrajectory>::SharedPtr trajectory_outgoing_cmd_pub;
trajectory_outgoing_cmd_pub = servo_node->create_publisher<trajectory_msgs::msg::JointTrajectory>(
servo_params.command_out_topic, rclcpp::SystemDefaultsQoS());

// Create the servo object
Expand Down Expand Up @@ -90,7 +90,7 @@ int main(int argc, char* argv[])
if (servo.getStatus() != StatusCode::INVALID)
{
auto joint_trajectory = composeTrajectoryMessage(servo_params, joint_state);
trajectory_outgoing_cmd_pub_->publish(std::move(joint_trajectory));
trajectory_outgoing_cmd_pub->publish(joint_trajectory);
}

rate.sleep();
Expand Down
146 changes: 146 additions & 0 deletions moveit_ros/moveit_servo/include/moveit_servo/command_processor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*******************************************************************************
* BSD 3-Clause License
*
* Copyright (c) 2019, Los Alamos National Security, LLC
* 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 copyright holder 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 HOLDER 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.
*******************************************************************************/

/* Title : command_processor.hpp
* Project : moveit_servo
* Created : 04/06/2023
* Author : Brian O'Neil, Andy Zelenak, Blake Anderson, V Mohammed Ibrahim
*/

#pragma once

#include <variant>
#include <control_toolbox/pid.hpp>

#include <rclcpp/rclcpp.hpp>
#include <tf2_eigen/tf2_eigen.hpp>

#include <moveit/kinematics_base/kinematics_base.h>

#include <moveit_servo/status_codes.hpp>
#include <moveit_servo/utils.hpp>

namespace moveit_servo
{

// Datatypes used by servo

typedef Eigen::VectorXd JointJog;

struct Pose
{
std::string frame_id;
Eigen::Isometry3d pose;
};

struct Twist
{
std::string frame_id;
Eigen::Vector<double, 6> velocities;
};

typedef std::variant<JointJog, Twist, Pose> ServoInput;

enum class CommandType
{
JOINT_JOG = 0,
TWIST,
POSE
};

class CommandProcessor
{
public:
CommandProcessor(const moveit::core::JointModelGroup* joint_model_group, moveit::core::RobotStatePtr& current_state,
servo::Params& servo_params, StatusCode& servo_status);

/**
* \brief Compute the change in joint position for the given joint jog command.
* @param command The joint jog command.
* @return The joint position change required (delta).
*/
Eigen::VectorXd jointDeltaFromCommand(const JointJog& command);

/**
* \brief Compute the change in joint position for the given twist command.
* @param command The twist command.
* @return The joint position change required (delta).
*/
Eigen::VectorXd jointDeltaFromCommand(const Twist& command);

/**
* \brief Compute the change in joint position for the given pose command.
* @param command The pose command.
* @return The joint position change required (delta).
*/
Eigen::VectorXd jointDeltaFromCommand(const Pose& command);

private:
/**
* \brief Set the IK solver that servo will use. If the robot does not have one, inverse jacobian will be used instead.
*/
void setIKSolver();

/**
* \brief Get the current status of servo.
* @return The current status.
*/
StatusCode getStatus();

/**
* \brief Get the message associated with the current servo status.
* @return The status message.
*/
const std::string getStatusMessage();

/**
* \brief Computes the required change in joint angles for given cartesian change, using the robots IK solver.
* @param carteisan_position_delta The change in cartesian position.
* @return The required joing angle deltas.
*/
Eigen::VectorXd detlaFromIkSolver(const Eigen::VectorXd& cartesian_position_delta);

// Variables
size_t num_joints_;
const moveit::core::JointModelGroup* joint_model_group_;
moveit::core::RobotStatePtr& current_state_;
servo::Params& servo_params_;
StatusCode& servo_status_;

kinematics::KinematicsBaseConstPtr ik_solver_ = nullptr;

uint64_t controller_period_;
std::map<std::string, control_toolbox::Pid> pid_controllers_;
};

} // namespace moveit_servo
Loading

0 comments on commit 56fe805

Please sign in to comment.