Program Listing for File spring_actuator_controller.hpp

↰ Return to documentation for file (include/spring_actuator_controller/spring_actuator_controller.hpp)

// Copyright 2024 ROBOTIS CO., LTD.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Woojin Wie

#ifndef SPRING_ACTUATOR_CONTROLLER__SPRING_ACTUATOR_CONTROLLER_HPP_
#define SPRING_ACTUATOR_CONTROLLER__SPRING_ACTUATOR_CONTROLLER_HPP_

#include <spring_actuator_controller/visibility_control.h>

#include <memory>
#include <string>
#include <vector>
#include <map>
#include <sstream>

#include "controller_interface/controller_interface.hpp"
#include "hardware_interface/types/hardware_interface_type_values.hpp"
#include "rclcpp/rclcpp.hpp"
#include "rclcpp_lifecycle/state.hpp"

// Visibility and parameter definitions
#include <ffw_spring_actuator_controller/spring_actuator_controller_parameters.hpp>

namespace spring_actuator_controller
{

class SpringActuatorController : public controller_interface::ControllerInterface
{
public:
  SPRING_ACTUATOR_CONTROLLER_PUBLIC
  SpringActuatorController();

  SPRING_ACTUATOR_CONTROLLER_PUBLIC
  controller_interface::InterfaceConfiguration command_interface_configuration() const override;

  SPRING_ACTUATOR_CONTROLLER_PUBLIC
  controller_interface::InterfaceConfiguration state_interface_configuration() const override;

  SPRING_ACTUATOR_CONTROLLER_PUBLIC
  controller_interface::return_type update(
    const rclcpp::Time & time, const rclcpp::Duration & period) override;

  //----------------------------------------------------------------------------
  // Lifecycle node interface
  //----------------------------------------------------------------------------

  SPRING_ACTUATOR_CONTROLLER_PUBLIC
  controller_interface::CallbackReturn on_init() override;

  SPRING_ACTUATOR_CONTROLLER_PUBLIC
  controller_interface::CallbackReturn on_configure(
    const rclcpp_lifecycle::State & previous_state) override;

  SPRING_ACTUATOR_CONTROLLER_PUBLIC
  controller_interface::CallbackReturn on_activate(
    const rclcpp_lifecycle::State & previous_state) override;

  SPRING_ACTUATOR_CONTROLLER_PUBLIC
  controller_interface::CallbackReturn on_deactivate(
    const rclcpp_lifecycle::State & previous_state) override;

  SPRING_ACTUATOR_CONTROLLER_PUBLIC
  controller_interface::CallbackReturn on_cleanup(
    const rclcpp_lifecycle::State & previous_state) override;

  SPRING_ACTUATOR_CONTROLLER_PUBLIC
  controller_interface::CallbackReturn on_error(
    const rclcpp_lifecycle::State & previous_state) override;

  SPRING_ACTUATOR_CONTROLLER_PUBLIC
  controller_interface::CallbackReturn on_shutdown(
    const rclcpp_lifecycle::State & previous_state) override;

protected:
  std::string formatVector(const std::vector<double> & vec);

  //----------------------------------------------------------------------------
  // Parameters and internal state
  //----------------------------------------------------------------------------

  std::shared_ptr<ParamListener> param_listener_;
  Params params_;

  bool dither_switch_;

  //----------------------------------------------------------------------------
  // Hardware interfaces
  //----------------------------------------------------------------------------

  std::vector<std::string> state_interface_types_ = {
    hardware_interface::HW_IF_POSITION,
    hardware_interface::HW_IF_VELOCITY,
  };

  std::vector<std::string> command_interface_types_ = {
    hardware_interface::HW_IF_EFFORT
  };

  size_t dof_;

  std::vector<std::string> command_joint_names_;

  template<typename T>
  using InterfaceReferences = std::vector<std::vector<std::reference_wrapper<T>>>;

  InterfaceReferences<hardware_interface::LoanedCommandInterface> joint_command_interface_;
  InterfaceReferences<hardware_interface::LoanedStateInterface> joint_state_interface_;

  //----------------------------------------------------------------------------
  // Joint information
  //----------------------------------------------------------------------------

  std::vector<std::string> joint_names_;

  size_t n_joints_;

  std::vector<double> joint_positions_;
  std::vector<double> joint_velocities_;
};

}  // namespace spring_actuator_controller

#endif  // SPRING_ACTUATOR_CONTROLLER__SPRING_ACTUATOR_CONTROLLER_HPP_