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{
54{
55protected:
56 std::atomic_bool* break_loops_; //Deprecated
57 std::shared_ptr<ShutdownSignaler> shutdown_signaler_;
58 std::tuple<VectorXd, VectorXd> joint_limits_;
59 VectorXd joint_velocities_;
60 VectorXd joint_torques_;
61
62 std::unique_ptr<sas::Clock> clock_;
63 std::unique_ptr<std::thread> watchdog_thread_;
64 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> time_point_from_the_client_;
65 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> time_point_from_the_server_;
66 bool watchdog_status_;
68 std::mutex mutex_watchdog_;
69 double max_acceptable_delay_ = 0.1;
70 double watchdog_period_;
71
72 RobotDriver(const std::shared_ptr<ShutdownSignaler>& shutdown_signaler_);
73 [[deprecated("Use RobotDriver(const std::shared_ptr<ShutdownSignaler>& shutdown_signaler_) instead.")]]
74 RobotDriver(std::atomic_bool* break_loops);
75
76 RobotDriver()=delete;
77 RobotDriver(const RobotDriver&)=delete;
78
79 std::exception_ptr watchdog_exception_{nullptr};
80 std::mutex watchdog_exception_mutex_;
81
82 public:
86 enum class Functionality{
87 None=0,
88 PositionControl,
89 VelocityControl,
90 ForceControl,
91 Homing,
92 ClearPositions,
93 Watchdog
94 };
95
100
105 virtual VectorXd get_joint_positions() = 0;
106
111 virtual void set_target_joint_positions(const VectorXd& set_target_joint_positions_rad) = 0;
112
117 virtual VectorXd get_joint_velocities();
118
123 virtual void set_target_joint_velocities(const VectorXd& set_target_joint_velocities);
124
129 virtual VectorXd get_joint_torques();
130
135 virtual void set_target_joint_torques(const VectorXd& set_target_joint_torques);
136
141 virtual std::tuple<VectorXd, VectorXd> get_joint_limits();
142
147 virtual void set_joint_limits(const std::tuple<VectorXd, VectorXd>& joint_limits);
148
153 void watchdog_start(const std::chrono::nanoseconds& period);
154
161 void watchdog_trigger(const std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>& time_point_from_the_client,
162 const std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>& time_point_from_the_server,
163 const bool& status);
164
169 void watchdog_set_maximum_acceptable_delay(const double& max_acceptable_delay);
170
175
179 virtual void connect()=0;
180
184 virtual void disconnect()=0;
185
189 virtual void initialize()=0;
190
194 virtual void deinitialize()=0;
195};
196}
197
198
199
Definition sas_robot_driver.hpp:54
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:68
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:185
void check_for_watchdog_exceptions()
Check for exceptions thrown by the watchdog thread and rethrow if present.
Definition sas_robot_driver.cpp:193
virtual void connect()=0
Connect to the underlying robot/hardware.
virtual void set_target_joint_torques(const VectorXd &set_target_joint_torques)
Set target joint torques.
Definition sas_robot_driver.cpp:73
virtual VectorXd get_joint_velocities()
Get current joint velocities.
Definition sas_robot_driver.cpp:58
~RobotDriver()
Virtual destructor for RobotDriver.
Definition sas_robot_driver.cpp:50
void _watchdog_thread_function()
RobotDriver::_watchdog_thread_function throws an exception if the elapsed time since the last trigger...
Definition sas_robot_driver.cpp:93
virtual std::tuple< VectorXd, VectorXd > get_joint_limits()
Get joint limits (min, max)
Definition sas_robot_driver.cpp:78
virtual void deinitialize()=0
Deinitialize 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:83
Functionality
Enumeration of optional driver functionalities.
Definition sas_robot_driver.hpp:86
virtual void set_target_joint_velocities(const VectorXd &set_target_joint_velocities)
Set target joint velocities.
Definition sas_robot_driver.cpp:63
void watchdog_start(const std::chrono::nanoseconds &period)
Start the watchdog thread with the given period.
Definition sas_robot_driver.cpp:144
virtual VectorXd get_joint_positions()=0
Get current joint positions.
virtual void initialize()=0
Initialize the driver resources.
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:166
Timing utilities for control loops and statistics collection.
Cross-module shutdown signaling helper.