Skip to content

Commit

Permalink
Use enum instead of std::optional
Browse files Browse the repository at this point in the history
  • Loading branch information
christophfroehlich committed Jan 8, 2024
1 parent 5f90914 commit 6967ed9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 32 deletions.
14 changes: 11 additions & 3 deletions hardware_interface/include/hardware_interface/hardware_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#ifndef HARDWARE_INTERFACE__HARDWARE_INFO_HPP_
#define HARDWARE_INTERFACE__HARDWARE_INFO_HPP_

#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
Expand Down Expand Up @@ -54,6 +53,14 @@ struct MimicJoint
double offset = 0.0;
};

/// @brief This enum is used to store the mimic attribute of a joint
enum class MimicAttribute
{
NOT_SET,
TRUE,
FALSE
};

/**
* This structure stores information about components defined for a specific hardware
* in robot's URDF.
Expand All @@ -65,8 +72,9 @@ struct ComponentInfo
/// Type of the component: sensor, joint, or GPIO.
std::string type;

/// If the component has a mimic joint tag, for opt-out
std::optional<bool> is_mimic{};
/// Hold the value of the mimic attribute if given, NOT_SET otherwise
MimicAttribute is_mimic = MimicAttribute::NOT_SET;

/**
* Name of the command interfaces that can be set, e.g. "position", "velocity", etc.
* Used by joints and GPIOs.
Expand Down
36 changes: 7 additions & 29 deletions hardware_interface/src/component_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,29 +102,6 @@ std::string get_attribute_value(
return element_it->Attribute(attribute_name);
}

/// Gets value of the attribute on an XMLelement.
/**
* If parameter is not found, returns specified default value
*
* \param[in] element_it XMLElement iterator to search for the attribute
* \param[in] attribute_name attribute name to search for and return value
* \param[in] default_value When the attribute is not found, this value is returned instead
* \return attribute value
* \throws std::runtime_error if attribute is not found
*/
std::string get_attribute_value_or(
const tinyxml2::XMLElement * element_it, const char * attribute_name,
const std::string default_value)
{
const tinyxml2::XMLAttribute * attr;
attr = element_it->FindAttribute(attribute_name);
if (!attr)
{
return default_value;
}
return element_it->Attribute(attribute_name);
}

/// Gets value of the attribute on an XMLelement.
/**
* If attribute is not found throws an error.
Expand Down Expand Up @@ -345,13 +322,14 @@ ComponentInfo parse_component_from_xml(const tinyxml2::XMLElement * component_it
{
try
{
component.is_mimic =
parse_bool(get_attribute_value(component_it, kMimicAttribute, kJointTag));
component.is_mimic = parse_bool(get_attribute_value(component_it, kMimicAttribute, kJointTag))
? MimicAttribute::TRUE
: MimicAttribute::FALSE;
}
catch (const std::runtime_error & e)
{
// mimic attribute not set
component.is_mimic = {};
component.is_mimic = MimicAttribute::NOT_SET;
}
}

Expand Down Expand Up @@ -387,7 +365,7 @@ ComponentInfo parse_component_from_xml(const tinyxml2::XMLElement * component_it
* and the interface may be an array of a fixed size of the data type.
*
* \param[in] component_it pointer to the iterator where component
* info should befound
* info should be found
* \throws std::runtime_error if a required component attribute or tag is not found.
*/
ComponentInfo parse_complex_component_from_xml(const tinyxml2::XMLElement * component_it)
Expand Down Expand Up @@ -703,12 +681,12 @@ std::vector<HardwareInfo> parse_control_resources_from_urdf(const std::string &
{
throw std::runtime_error("Joint " + joint.name + " not found in URDF");
}
if (!urdf_joint->mimic && joint.is_mimic.value_or(false))
if (!urdf_joint->mimic && joint.is_mimic == MimicAttribute::TRUE)
{
throw std::runtime_error(
"Joint '" + std::string(joint.name) + "' has no mimic information in the URDF.");
}
if (urdf_joint->mimic && joint.is_mimic.value_or(true))
if (urdf_joint->mimic && joint.is_mimic != MimicAttribute::FALSE)
{
if (joint.command_interfaces.size() > 0)
{
Expand Down

0 comments on commit 6967ed9

Please sign in to comment.