sas
Modularised monitoring, logging, and control of robots.
Loading...
Searching...
No Matches
sas_robot_driver.hpp
Go to the documentation of this file.
1#pragma once
2/*
3# Copyright (c) 2016-2025 Murilo Marques Marinho
4#
5# This file is part of sas_robot_driver.
6#
7# sas_robot_driver is free software: you can redistribute it and/or modify
8# it under the terms of the GNU Lesser General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# sas_robot_driver is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU Lesser General Public License for more details.
16#
17# You should have received a copy of the GNU Lesser General Public License
18# along with sas_robot_driver. If not, see <https://www.gnu.org/licenses/>.
19#
20# ################################################################
21#
22# Author: Murilo M. Marinho, email: murilomarinho@ieee.org
23#
24# ################################################################
25# Contributors:
26#
27# 1. Juan Jose Quiroz Omana (juanjose.quirozomana@manchester.ac.uk)
28# Added the Watchdog functionaly initially proposed in
29# https://github.com/SmartArmStack/sas_core/pull/1
30*/
31
40#include <atomic>
41#include <mutex>
42#include <thread>
43#include <memory>
44#include <chrono>
47#include <eigen3/Eigen/Dense>
48
49using namespace Eigen;
50
51namespace sas
52{
59class RobotDriver
60{
61protected:
62 std::atomic_bool* break_loops_; //Deprecated
63 std::shared_ptr<ShutdownSignaler> shutdown_signaler_;
64 std::tuple<VectorXd, VectorXd> joint_limits_;
65 VectorXd joint_velocities_;
66 VectorXd joint_torques_;
67
68 std::unique_ptr<sas::Clock> clock_;
69 std::unique_ptr<std::thread> watchdog_thread_;
70 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> time_point_from_the_client_;
71 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> time_point_from_the_server_;
72 bool watchdog_status_;
74 std::mutex mutex_watchdog_;
75 double max_acceptable_delay_ = 0.1;
76 double watchdog_period_;
77
78 RobotDriver(const std::shared_ptr<ShutdownSignaler>& shutdown_signaler_);
79 [[deprecated("Use RobotDriver(const std::shared_ptr<ShutdownSignaler>& shutdown_signaler_) instead.")]]
80 RobotDriver(std::atomic_bool* break_loops);
81
82 RobotDriver()=delete;
83 RobotDriver(const RobotDriver&)=delete;
84
85 std::exception_ptr watchdog_exception_{nullptr};
86 std::mutex watchdog_exception_mutex_;
87
88
89 std::function<void()> control_loop_callback_;
90
91 public:
95 enum class Functionality{
96 None=0,
97 PositionControl,
98 VelocityControl,
99 ForceControl,
100 Homing,
101 ClearPositions,
102 Watchdog
103 };
104
108 ~RobotDriver();
109
114 virtual VectorXd get_joint_positions() = 0;
115
120 virtual void set_target_joint_positions(const VectorXd& set_target_joint_positions_rad) = 0;
121
127 virtual VectorXd get_joint_velocities();
128
134 virtual void set_target_joint_velocities(const VectorXd& set_target_joint_velocities);
135
141 virtual VectorXd get_joint_torques();
142
148 virtual void set_target_joint_torques(const VectorXd& set_target_joint_torques);
149
154 virtual std::tuple<VectorXd, VectorXd> get_joint_limits();
155
160 virtual void set_joint_limits(const std::tuple<VectorXd, VectorXd>& joint_limits);
161
166 void watchdog_start(const std::chrono::nanoseconds& period);
167
174 void watchdog_trigger(const std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>& time_point_from_the_client,
175 const std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>& time_point_from_the_server,
176 const bool& status);
177
182 void watchdog_set_maximum_acceptable_delay(const double& max_acceptable_delay);
183
190
194 virtual void connect()=0;
195
199 virtual void disconnect()=0;
200
204 virtual void initialize()=0;
205
209 virtual void deinitialize()=0;
210
211
217 void set_control_loop_callback(std::function<void()> callback);
218
219
228
229
235};
236}
237
238
239
~RobotDriver()
Virtual destructor for RobotDriver.
Definition sas_robot_driver.cpp:55
virtual void disconnect()=0
Disconnect from the underlying robot/hardware.
virtual void set_target_joint_positions(const VectorXd &set_target_joint_positions_rad)=0
Set target joint positions.
virtual VectorXd get_joint_torques()
Get current joint torques.
Definition sas_robot_driver.cpp:73
virtual VectorXd get_joint_velocities()
Get current joint velocities.
Definition sas_robot_driver.cpp:63
virtual void connect()=0
Connect to the underlying robot/hardware.
void check_for_watchdog_exceptions()
Check for exceptions thrown by the watchdog thread and rethrow if present.
Definition sas_robot_driver.cpp:198
void execute_control_loop_callback()
Execute the control loop callback if it has been set.
Definition sas_robot_driver.cpp:222
void set_control_loop_callback(std::function< void()> callback)
Set the control loop callback function.
Definition sas_robot_driver.cpp:209
bool control_loop_callback_is_set()
Check if a control loop callback has been set.
Definition sas_robot_driver.cpp:241
virtual void set_target_joint_torques(const VectorXd &set_target_joint_torques)
Set target joint torques.
Definition sas_robot_driver.cpp:78
virtual void deinitialize()=0
Deinitialize the driver resources.
void watchdog_set_maximum_acceptable_delay(const double &max_acceptable_delay)
Set the maximum acceptable delay for the watchdog (seconds).
Definition sas_robot_driver.cpp:190
Functionality
Enumeration of optional driver functionalities.
Definition sas_robot_driver.hpp:95
virtual std::tuple< VectorXd, VectorXd > get_joint_limits()
Get joint limits (min, max).
Definition sas_robot_driver.cpp:83
virtual VectorXd get_joint_positions()=0
Get current joint positions.
virtual void initialize()=0
Initialize the driver resources.
virtual void set_joint_limits(const std::tuple< VectorXd, VectorXd > &joint_limits)
Set joint limits (min, max).
Definition sas_robot_driver.cpp:88
virtual void set_target_joint_velocities(const VectorXd &set_target_joint_velocities)
Set target joint velocities.
Definition sas_robot_driver.cpp:68
void _watchdog_thread_function()
RobotDriver::_watchdog_thread_function throws an exception if the elapsed time since the last trigger...
Definition sas_robot_driver.cpp:98
void watchdog_trigger(const std::chrono::time_point< std::chrono::system_clock, std::chrono::nanoseconds > &time_point_from_the_client, const std::chrono::time_point< std::chrono::system_clock, std::chrono::nanoseconds > &time_point_from_the_server, const bool &status)
Trigger the watchdog with timestamps from client/server and the current status.
Definition sas_robot_driver.cpp:171
void watchdog_start(const std::chrono::nanoseconds &period)
Start the watchdog thread with the given period.
Definition sas_robot_driver.cpp:149
Timing utilities for control loops and statistics collection.
Cross-module shutdown signaling class.