Skip to content

Commit

Permalink
not finished, showcase for what hase to be changed
Browse files Browse the repository at this point in the history
  • Loading branch information
mamueluth committed Jan 29, 2024
1 parent 0d97fa4 commit 1187a90
Show file tree
Hide file tree
Showing 16 changed files with 424 additions and 300 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ TEST_F(ChainableControllerInterfaceTest, export_reference_interfaces)
EXPECT_EQ(reference_interfaces[0].get_prefix_name(), TEST_CONTROLLER_NAME);
EXPECT_EQ(reference_interfaces[0].get_interface_name(), "test_itf");

EXPECT_EQ(reference_interfaces[0].get_value(), INTERFACE_VALUE);
EXPECT_EQ(reference_interfaces[0].get_value<double>(), INTERFACE_VALUE);
}

TEST_F(ChainableControllerInterfaceTest, reference_interfaces_storage_not_correct_size)
Expand Down Expand Up @@ -88,7 +88,7 @@ TEST_F(ChainableControllerInterfaceTest, setting_chained_mode)
EXPECT_FALSE(controller.is_in_chained_mode());

// Fail setting chained mode
EXPECT_EQ(reference_interfaces[0].get_value(), INTERFACE_VALUE);
EXPECT_EQ(reference_interfaces[0].get_value<double>(), INTERFACE_VALUE);

EXPECT_FALSE(controller.set_chained_mode(true));
EXPECT_FALSE(controller.is_in_chained_mode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ class ActuatorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNod

double get_state(const std::string & interface_name) const
{
return actuator_states_.at(interface_name)->get_value();
return actuator_states_.at(interface_name)->get_value<double>();
}

void set_command(const std::string & interface_name, const double & value)
Expand All @@ -408,37 +408,60 @@ class ActuatorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNod

double get_command(const std::string & interface_name) const
{
return actuator_commands_.at(interface_name)->get_value();
return actuator_commands_.at(interface_name)->get_value<double>();
}

void set_emergency_stop(const double & emergency_stop)
void set_emergency_stop(const bool & emergency_stop)
{
emergency_stop_->set_value(emergency_stop);
}

double get_emergency_stop() const { return emergency_stop_->get_value(); }
bool get_emergency_stop() const { return emergency_stop_->get_value<bool>(); }

void set_error_code(const double & error_code) { error_signal_->set_value(error_code); }
void set_error_code(std::array<uint8_t, hardware_interface::error_signal_count> error_codes)
{
error_signal_->set_value(error_codes);
}

double get_error_code() const { return error_signal_->get_value(); }
std::array<uint8_t, hardware_interface::error_signal_count> get_error_code() const
{
return error_signal_->get_value<std::array<uint8_t, hardware_interface::error_signal_count>>();
}

void set_error_message(const double & error_message)
void set_error_message(
std::array<std::string, hardware_interface::error_signal_count> error_messages)
{
error_signal_message_->set_value(error_message);
error_signal_message_->set_value(error_messages);
}

double get_error_message() const { return error_signal_message_->get_value(); }
std::array<std::string, hardware_interface::error_signal_count> get_error_message() const
{
return error_signal_message_
->get_value<std::array<std::string, hardware_interface::error_signal_count>>();
}

void set_warning_code(const double & warning_codes) { warning_signal_->set_value(warning_codes); }
void set_warning_code(std::array<int8_t, hardware_interface::warning_signal_count> warning_codes)
{
warning_signal_->set_value(warning_codes);
}

double get_warning_code() const { return warning_signal_->get_value(); }
std::array<int8_t, hardware_interface::warning_signal_count> get_warning_code() const
{
return warning_signal_
->get_value<std::array<int8_t, hardware_interface::warning_signal_count>>();
}

void set_warning_message(const double & error_message)
void set_warning_message(
std::array<std::string, hardware_interface::warning_signal_count> error_message)
{
warning_signal_message_->set_value(error_message);
}

double get_warning_message() const { return warning_signal_message_->get_value(); }
std::array<std::string, hardware_interface::error_signal_count> get_warning_message() const
{
return warning_signal_message_
->get_value<std::array<std::string, hardware_interface::error_signal_count>>();
}

protected:
HardwareInfo info_;
Expand Down
40 changes: 22 additions & 18 deletions hardware_interface/include/hardware_interface/handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <limits>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <variant>

Expand All @@ -30,7 +31,19 @@
namespace hardware_interface
{

typedef std::variant<double> HANDLE_DATATYPE;
using HANDLE_DATATYPE = std::variant<
bool, double, hardware_interface::WARNING_SIGNALS, hardware_interface::ERROR_SIGNALS,
hardware_interface::WARNING_MESSAGES>;

// Define a type trait for allowed types
template <typename T>
struct HANDLE_DATATYPE_TYPES : std::disjunction<
std::is_same<T, bool>, std::is_same<T, double>,
std::is_same<T, hardware_interface::WARNING_SIGNALS>,
std::is_same<T, hardware_interface::ERROR_SIGNALS>,
std::is_same<T, hardware_interface::WARNING_MESSAGES>>
{
};

/// A handle used to get and set a value on a given interface.
class Handle
Expand All @@ -49,10 +62,10 @@ class Handle
: prefix_name_(interface_description.prefix_name),
interface_name_(interface_description.interface_info.name)
{
// TODO(Manuel): implement this.
// As soon as multiple datatypes are used in HANDLE_DATATYPE
// we need to initialize according the type passed in interface description
value_ = std::numeric_limits<double>::quiet_NaN();
value_ptr_ = std::get_if<double>(&value_);
// value_ = std::numeric_limits<double>::quiet_NaN();
}

[[deprecated("Use InterfaceDescription for initializing the Interface")]]
Expand Down Expand Up @@ -95,32 +108,23 @@ class Handle

const std::string & get_prefix_name() const { return prefix_name_; }

double get_value() const
template <typename T, typename std::enable_if<HANDLE_DATATYPE_TYPES<T>::value, int>::type = 0>
T get_value() const
{
// BEGIN (Handle export change): for backward compatibility
// TODO(Manuel) return value_ if old functionality is removed
THROW_ON_NULLPTR(value_ptr_);
return *value_ptr_;
// END
return std::get<T>(value_);
}

void set_value(double value)
template <typename T, typename std::enable_if<HANDLE_DATATYPE_TYPES<T>::value, int>::type = 0>
void set_value(T value)
{
// BEGIN (Handle export change): for backward compatibility
// TODO(Manuel) set value_ directly if old functionality is removed
THROW_ON_NULLPTR(this->value_ptr_);
*this->value_ptr_ = value;
// END
value_ = value;
}

protected:
std::string prefix_name_;
std::string interface_name_;
HANDLE_DATATYPE value_;
// BEGIN (Handle export change): for backward compatibility
// TODO(Manuel) redeclare as HANDLE_DATATYPE * value_ptr_ if old functionality is removed
double * value_ptr_;
// END
};

class StateInterface : public Handle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class LoanedCommandInterface

void set_value(double val) { command_interface_.set_value(val); }

double get_value() const { return command_interface_.get_value(); }
double get_value() const { return command_interface_.get_value<double>(); }

protected:
CommandInterface & command_interface_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class LoanedStateInterface

const std::string & get_prefix_name() const { return state_interface_.get_prefix_name(); }

double get_value() const { return state_interface_.get_value(); }
double get_value() const { return state_interface_.get_value<double>(); }

protected:
StateInterface & state_interface_;
Expand Down
43 changes: 33 additions & 10 deletions hardware_interface/include/hardware_interface/sensor_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,30 +265,53 @@ class SensorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI

double get_state(const std::string & interface_name) const
{
return sensor_states_.at(interface_name)->get_value();
return sensor_states_.at(interface_name)->get_value<double>();
}

void set_error_code(const double & error_code) { error_signal_->set_value(error_code); }
void set_error_code(std::array<uint8_t, hardware_interface::error_signal_count> error_codes)
{
error_signal_->set_value(error_codes);
}

double get_error_code() const { return error_signal_->get_value(); }
std::array<uint8_t, hardware_interface::error_signal_count> get_error_code() const
{
return error_signal_->get_value<std::array<uint8_t, hardware_interface::error_signal_count>>();
}

void set_error_message(const double & error_message)
void set_error_message(
std::array<std::string, hardware_interface::error_signal_count> error_messages)
{
error_signal_message_->set_value(error_message);
error_signal_message_->set_value(error_messages);
}

double get_error_message() const { return error_signal_message_->get_value(); }
std::array<std::string, hardware_interface::error_signal_count> get_error_message() const
{
return error_signal_message_
->get_value<std::array<std::string, hardware_interface::error_signal_count>>();
}

void set_warning_code(const double & warning_codes) { warning_signal_->set_value(warning_codes); }
void set_warning_code(std::array<int8_t, hardware_interface::warning_signal_count> warning_codes)
{
warning_signal_->set_value(warning_codes);
}

double get_warning_code() const { return warning_signal_->get_value(); }
std::array<int8_t, hardware_interface::warning_signal_count> get_warning_code() const
{
return warning_signal_
->get_value<std::array<int8_t, hardware_interface::warning_signal_count>>();
}

void set_warning_message(const double & error_message)
void set_warning_message(
std::array<std::string, hardware_interface::warning_signal_count> error_message)
{
warning_signal_message_->set_value(error_message);
}

double get_warning_message() const { return warning_signal_message_->get_value(); }
std::array<std::string, hardware_interface::error_signal_count> get_warning_message() const
{
return warning_signal_message_
->get_value<std::array<std::string, hardware_interface::error_signal_count>>();
}

protected:
HardwareInfo info_;
Expand Down
50 changes: 36 additions & 14 deletions hardware_interface/include/hardware_interface/system_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,14 @@ class SystemInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
* \return state.
*/
void set_state(const rclcpp_lifecycle::State & new_state) { lifecycle_state_ = new_state; }

void set_state(const std::string & interface_name, const double & value)
{
system_states_.at(interface_name)->set_value(value);
}

double get_state(const std::string & interface_name) const
{
return system_states_.at(interface_name)->get_value();
return system_states_.at(interface_name)->get_value<double>();
}

void set_command(const std::string & interface_name, const double & value)
Expand All @@ -448,37 +447,60 @@ class SystemInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI

double get_command(const std::string & interface_name) const
{
return system_commands_.at(interface_name)->get_value();
return system_commands_.at(interface_name)->get_value<double>();
}

void set_emergency_stop(const double & emergency_stop)
void set_emergency_stop(const bool & emergency_stop)
{
emergency_stop_->set_value(emergency_stop);
}

double get_emergency_stop() const { return emergency_stop_->get_value(); }
bool get_emergency_stop() const { return emergency_stop_->get_value<bool>(); }

void set_error_code(const double & error_code) { error_signal_->set_value(error_code); }
void set_error_code(std::array<uint8_t, hardware_interface::error_signal_count> error_codes)
{
error_signal_->set_value(error_codes);
}

double get_error_code() const { return error_signal_->get_value(); }
std::array<uint8_t, hardware_interface::error_signal_count> get_error_code() const
{
return error_signal_->get_value<std::array<uint8_t, hardware_interface::error_signal_count>>();
}

void set_error_message(const double & error_message)
void set_error_message(
std::array<std::string, hardware_interface::error_signal_count> error_messages)
{
error_signal_message_->set_value(error_message);
error_signal_message_->set_value(error_messages);
}

double get_error_message() const { return error_signal_message_->get_value(); }
std::array<std::string, hardware_interface::error_signal_count> get_error_message() const
{
return error_signal_message_
->get_value<std::array<std::string, hardware_interface::error_signal_count>>();
}

void set_warning_code(const double & warning_codes) { warning_signal_->set_value(warning_codes); }
void set_warning_code(std::array<int8_t, hardware_interface::warning_signal_count> warning_codes)
{
warning_signal_->set_value(warning_codes);
}

double get_warning_code() const { return warning_signal_->get_value(); }
std::array<int8_t, hardware_interface::warning_signal_count> get_warning_code() const
{
return warning_signal_
->get_value<std::array<int8_t, hardware_interface::warning_signal_count>>();
}

void set_warning_message(const double & error_message)
void set_warning_message(
std::array<std::string, hardware_interface::warning_signal_count> error_message)
{
warning_signal_message_->set_value(error_message);
}

double get_warning_message() const { return warning_signal_message_->get_value(); }
std::array<std::string, hardware_interface::error_signal_count> get_warning_message() const
{
return warning_signal_message_
->get_value<std::array<std::string, hardware_interface::error_signal_count>>();
}

protected:
HardwareInfo info_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
#define HARDWARE_INTERFACE__TYPES__HARDWARE_INTERFACE_ERROR_SIGNALS_HPP_

#include <cstdint>
#include <string>
#include <vector>

namespace hardware_interface
{

// Count of how many different error signals there are that can be reported.
const size_t error_signal_count = 32;
using ERROR_SIGNALS = std::array<uint8_t, hardware_interface::error_signal_count>;

constexpr char ERROR_SIGNAL_INTERFACE_NAME[] = "ERROR_SIGNAL";
// Available error signal names
Expand Down Expand Up @@ -62,6 +64,8 @@ enum class error_signal : std::uint8_t
ERROR_SIGNAL_31 = 31
};

using ERROR_MESSAGES = std::array<std::string, hardware_interface::error_signal_count>;

constexpr char ERROR_SIGNAL_MESSAGE_INTERFACE_NAME[] = "ERROR_SIGNAL_MESSAGE";
// Available WARNING signal message names
enum class error_signal_message : std::uint8_t
Expand Down
Loading

0 comments on commit 1187a90

Please sign in to comment.