rm_control
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
hardware_interface.h
Go to the documentation of this file.
1/*******************************************************************************
2 * BSD 3-Clause License
3 *
4 * Copyright (c) 2021, Qiayuan Liao
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * * Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *******************************************************************************/
33
34//
35// Created by qiayuan on 12/21/20.
36//
37
38#pragma once
39
40#include <vector>
41#include <string>
42#include <memory>
43#include <unordered_map>
44
45// ROS
46#include <ros/ros.h>
47#include <urdf/model.h>
48#include <XmlRpcValue.h>
49
50// ROS control
51#include <hardware_interface/robot_hw.h>
52#include <hardware_interface/joint_command_interface.h>
53#include <hardware_interface/joint_state_interface.h>
54#include <transmission_interface/transmission_interface_loader.h>
55#include <joint_limits_interface/joint_limits_interface.h>
56
62#include <rm_msgs/ActuatorState.h>
63#include <rm_msgs/EnableImuTrigger.h>
64
65#include "can_bus.h"
66#include "gpio_manager.h"
67
68namespace rm_hw
69{
70class RmRobotHW : public hardware_interface::RobotHW
71{
72public:
73 RmRobotHW() = default;
83 bool init(ros::NodeHandle& root_nh, ros::NodeHandle& robot_hw_nh) override;
93 void read(const ros::Time& time, const ros::Duration& period) override;
94
103 void write(const ros::Time& time, const ros::Duration& period) override;
104
105 void setCanBusThreadPriority(int thread_priority);
106
107private:
115 bool parseActCoeffs(XmlRpc::XmlRpcValue& act_coeffs);
124 bool parseActData(XmlRpc::XmlRpcValue& act_datas, ros::NodeHandle& robot_hw_nh);
133 bool parseImuData(XmlRpc::XmlRpcValue& imu_datas, ros::NodeHandle& robot_hw_nh);
142 bool parseGpioData(XmlRpc::XmlRpcValue& gpio_datas, ros::NodeHandle& robot_hw_nh);
150 bool loadUrdf(ros::NodeHandle& root_nh);
151
152 bool parseTofData(XmlRpc::XmlRpcValue& tof_datas, ros::NodeHandle& robot_hw_nh);
160 bool setupTransmission(ros::NodeHandle& root_nh);
168 bool setupJointLimit(ros::NodeHandle& root_nh);
175 void publishActuatorState(const ros::Time& time);
176
177 bool enableImuTrigger(rm_msgs::EnableImuTrigger::Request& req, rm_msgs::EnableImuTrigger::Response& res);
178
179 ros::ServiceServer service_server_;
180
181 bool is_actuator_specified_ = false;
182 int thread_priority_;
183 // Interface
184 std::vector<CanBus*> can_buses_{};
185 GpioManager gpio_manager_{};
186 rm_control::GpioStateInterface gpio_state_interface_;
187 rm_control::GpioCommandInterface gpio_command_interface_;
188 hardware_interface::ActuatorStateInterface act_state_interface_;
189 rm_control::ActuatorExtraInterface act_extra_interface_;
190 hardware_interface::EffortActuatorInterface effort_act_interface_;
191 rm_control::RobotStateInterface robot_state_interface_;
192 hardware_interface::ImuSensorInterface imu_sensor_interface_;
193 rm_control::RmImuSensorInterface rm_imu_sensor_interface_;
194 std::unique_ptr<transmission_interface::TransmissionInterfaceLoader> transmission_loader_{};
195 transmission_interface::RobotTransmissions robot_transmissions_;
196 transmission_interface::ActuatorToJointStateInterface* act_to_jnt_state_{};
197 transmission_interface::JointToActuatorEffortInterface* jnt_to_act_effort_{};
198 joint_limits_interface::EffortJointSaturationInterface effort_jnt_saturation_interface_;
199 joint_limits_interface::EffortJointSoftLimitsInterface effort_jnt_soft_limits_interface_;
200 std::vector<hardware_interface::JointHandle> effort_joint_handles_{};
201 rm_control::TofRadarInterface tof_radar_interface_;
202
203 // URDF model of the robot
204 std::string urdf_string_; // for transmission
205 std::shared_ptr<urdf::Model> urdf_model_; // for limit
206
207 // Actuator
208 std::unordered_map<std::string, ActCoeff> type2act_coeffs_{};
209 std::unordered_map<std::string, std::unordered_map<int, ActData>> bus_id2act_data_{};
210
211 // Imu
212 std::unordered_map<std::string, std::unordered_map<int, ImuData>> bus_id2imu_data_{};
213
214 // TF radar
215 std::unordered_map<std::string, std::unordered_map<int, TofData>> bus_id2tof_data_{};
216
217 ros::Time last_publish_time_;
218 std::shared_ptr<realtime_tools::RealtimePublisher<rm_msgs::ActuatorState>> actuator_state_pub_;
219};
220
221} // namespace rm_hw
Definition actuator_extra_interface.h:132
Definition gpio_interface.h:94
Definition gpio_interface.h:89
Definition rm_imu_sensor_interface.h:36
Definition robot_state_interface.h:93
Definition tof_radar_interface.h:53
Definition gpio_manager.h:18
Definition hardware_interface.h:71
void write(const ros::Time &time, const ros::Duration &period) override
Comunicate with hardware. Publish command to robot.
Definition hardware_interface.cpp:152
void setCanBusThreadPriority(int thread_priority)
Definition hardware_interface.cpp:180
bool init(ros::NodeHandle &root_nh, ros::NodeHandle &robot_hw_nh) override
Get necessary params from param server. Init hardware_interface.
Definition hardware_interface.cpp:44
void read(const ros::Time &time, const ros::Duration &period) override
Comunicate with hardware. Get datas, status of robot.
Definition hardware_interface.cpp:121
RmRobotHW()=default
Definition control_loop.h:49