Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor driver library to follow patterns from PickNikRobotics/ros2_epick_gripper #34

Merged
merged 48 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
6b782ba
Refactoring for testability.
kineticsystem Sep 5, 2023
d32b44e
Added simple test.
kineticsystem Sep 5, 2023
1a8aeeb
Pre-commits.
kineticsystem Sep 5, 2023
ddda502
Added missing packages for testing.
kineticsystem Sep 5, 2023
79dfec4
In the on_configure method we try to connect to the hardware.
kineticsystem Sep 5, 2023
f371468
Added missing package.
kineticsystem Sep 5, 2023
c11d657
Refactoring.
kineticsystem Sep 5, 2023
76c5497
Removed not existing package.
kineticsystem Sep 5, 2023
8788593
Removed ros-linter. Reformatted code as per Studio conventions.
kineticsystem Sep 5, 2023
d7cfc1b
Revert "Auxiliary commit to revert individual files from 878859365914…
kineticsystem Sep 5, 2023
46b8b58
Linting packages.
kineticsystem Sep 5, 2023
92eefce
Remove ros linting clashing with .clang-format.
kineticsystem Sep 5, 2023
19569f8
Revert "Auxiliary commit to revert individual files from 92eefce7151f…
kineticsystem Sep 5, 2023
dad1114
Updated CMakeLists.txt
kineticsystem Sep 5, 2023
de63c83
Removed connection to the serial port in the contructor.
kineticsystem Sep 5, 2023
312ab08
Refactoring for testing.
kineticsystem Sep 6, 2023
c1dc459
Pre-commits.
kineticsystem Sep 6, 2023
4c3eafc
Added copyright.
kineticsystem Sep 6, 2023
e1089ad
Increased max line size to 120.
kineticsystem Sep 6, 2023
d283f18
Fixed ROS lints.
kineticsystem Sep 6, 2023
2239dd9
Try to exclude tests from linting.
kineticsystem Sep 6, 2023
011df4b
Removed cppcheck linter because it has problem recognizing macros in …
kineticsystem Sep 6, 2023
7414c37
Trying to add ros2_control_test_assets to test CMakeLists.txt
kineticsystem Sep 6, 2023
56ddcd4
Tryint to build everything on Ubuntu 22.04.
kineticsystem Sep 6, 2023
51e98f8
Moved gripper_interface_test command into its own package.
kineticsystem Sep 6, 2023
dfe6f79
Some refactoring.
kineticsystem Sep 6, 2023
21dc2c7
Some refactoring.
kineticsystem Sep 6, 2023
6ccf695
Some refactoring.
kineticsystem Sep 6, 2023
9691e54
Test command refactored.
kineticsystem Sep 6, 2023
685fe55
Ongoing refactoring.
kineticsystem Sep 6, 2023
5a5a62d
Ongoing refactoring.
kineticsystem Sep 6, 2023
91a08d0
Removed bringup package.
kineticsystem Sep 6, 2023
03e3701
Added fake driver for testing. config folder moved from robotiq_drive…
kineticsystem Sep 7, 2023
bcd5fe6
Hardware interface and driver refactoring.
kineticsystem Sep 8, 2023
7301c64
Driver refactoring.
kineticsystem Sep 8, 2023
d719ed2
Fixed pre-commits.
kineticsystem Sep 8, 2023
a434edf
Update .github/workflows/prerelease-check.yml
kineticsystem Sep 8, 2023
fb01131
Update .github/workflows/prerelease-check.yml
kineticsystem Sep 8, 2023
a5a9468
Update .github/workflows/prerelease-check.yml
kineticsystem Sep 8, 2023
21d1b4f
Update .github/workflows/prerelease-check.yml
kineticsystem Sep 8, 2023
c5819d8
Update .github/workflows/prerelease-check.yml
kineticsystem Sep 8, 2023
1658cdf
Adding ros2_control_test_assets to CMakeLists.txt
kineticsystem Sep 8, 2023
9f0a100
Removed unecessary dependencies.
kineticsystem Sep 8, 2023
660eec5
Added CRC-16/MODBUS test.
kineticsystem Sep 14, 2023
0f41ce9
Unit tests.
kineticsystem Sep 14, 2023
f5d7b7a
Update robotiq_driver/CMakeLists.txt
kineticsystem Sep 28, 2023
4c3fd18
Removed version from CMakeLists.txt file.
kineticsystem Oct 27, 2023
b4b73e6
Merge branch 'main' into pr-refactoring
moriarty Oct 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions robotiq_driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ add_library(
SHARED
src/crc.cpp
src/hardware_interface.cpp
src/robotiq_gripper_interface.cpp
src/default_driver.cpp
src/default_driver_factory.cpp
)
target_include_directories(
${PROJECT_NAME}
Expand All @@ -49,7 +50,9 @@ target_include_directories(gripper_interface_test PRIVATE include)
ament_target_dependencies(gripper_interface_test serial)
target_link_libraries(gripper_interface_test ${PROJECT_NAME})

###############################################################################
# INSTALL

install(
TARGETS ${PROJECT_NAME}
EXPORT export_${PROJECT_NAME}
Expand All @@ -74,26 +77,19 @@ install(
DESTINATION share/${PROJECT_NAME}
)

###############################################################################
# TESTS

# CTest module automatically creates a BUILD_TESTING option that selects
# whether to enable testing support (ON by default).
include(CTest)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
# the following skips uncrustify
# ament_uncrustify and ament_clang_format cannot both be satisfied
set(ament_cmake_uncrustify_FOUND TRUE)
# the following skips ament_flake8
# ament_flake8 and black fight over double or single quotes
# flake8 is run via pre-commit with a .flake8 configuration file
set(ament_cmake_flake8_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
add_subdirectory(tests)
endif()

## EXPORTS
###############################################################################
# EXPORTS

ament_export_include_directories(
include
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include <serial/serial.h>

#include <robotiq_driver/driver.hpp>
#include <string>
#include <vector>

Expand All @@ -38,32 +39,33 @@
* the gripper's current state.
*
*/
class RobotiqGripperInterface
namespace robotiq_driver
{
class DefaultDriver : public Driver
{
public:
explicit RobotiqGripperInterface(
const std::string & com_port = "/dev/ttyUSB0", uint8_t slave_id = 0x09);
explicit DefaultDriver(const std::string & com_port = "/dev/ttyUSB0", uint8_t slave_id = 0x09);

/**
* @brief Activates the gripper.
*
* @throw serial::IOException on failure to successfully communicate with gripper port
*/
void activateGripper();
void activate() override;

/**
* @brief Deactivates the gripper.
*
* @throw serial::IOException on failure to successfully communicate with gripper port
*/
void deactivateGripper();
void deactivate() override;

/**
* @brief Commands the gripper to move to the desired position.
*
* @param pos A value between 0x00 (fully open) and 0xFF (fully closed).
*/
void setGripperPosition(uint8_t pos);
void set_gripper_position(uint8_t pos) override;

/**
* @brief Return the current position of the gripper.
Expand All @@ -72,58 +74,31 @@ class RobotiqGripperInterface
*
* @return uint8_t A value between 0x00 (fully open) and 0xFF (fully closed).
*/
uint8_t getGripperPosition();
uint8_t get_gripper_position() override;

/**
* @brief Returns true if the gripper is currently moving, false otherwise.
*
*/
bool gripperIsMoving();
bool gripper_is_moving() override;

/**
* @brief Set the speed of the gripper.
*
* @param speed A value between 0x00 (stopped) and 0xFF (full speed).
*/
void setSpeed(uint8_t speed);
void set_speed(uint8_t speed) override;

/**
* @brief Set how forcefully the gripper opens or closes.
*
* @param force A value between 0x00 (no force) or 0xFF (maximum force).
*/
void setForce(uint8_t force);

enum class ActivationStatus
{
RESET,
ACTIVE
};

enum class ActionStatus
{
STOPPED,
MOVING
};

enum class GripperStatus
{
RESET,
IN_PROGRESS,
COMPLETED,
};

enum class ObjectDetectionStatus
{
MOVING,
OBJECT_DETECTED_OPENING,
OBJECT_DETECTED_CLOSING,
AT_REQUESTED_POSITION
};
void set_force(uint8_t force) override;

private:
std::vector<uint8_t> createReadCommand(uint16_t first_register, uint8_t num_registers);
std::vector<uint8_t> createWriteCommand(
std::vector<uint8_t> create_read_command(uint16_t first_register, uint8_t num_registers);
std::vector<uint8_t> create_write_command(
uint16_t first_register, const std::vector<uint16_t> & data);

/**
Expand All @@ -132,22 +107,22 @@ class RobotiqGripperInterface
* @param num_bytes Number of bytes to be read from device port.
* @throw serial::IOException on failure to successfully communicate with gripper port
*/
std::vector<uint8_t> readResponse(size_t num_bytes);
std::vector<uint8_t> read_response(size_t num_bytes);

/**
* @brief Send a command to the gripper.
*
* @param cmd The command.
* @throw serial::IOException on failure to successfully communicate with gripper port
*/
void sendCommand(const std::vector<uint8_t> & cmd);
void send_command(const std::vector<uint8_t> & cmd);

/**
* @brief Read the current status of the gripper, and update member variables as appropriate.
*
* @throw serial::IOException on failure to successfully communicate with gripper port
*/
void updateStatus();
void update_status();

serial::Serial port_;
uint8_t slave_id_;
Expand All @@ -162,3 +137,4 @@ class RobotiqGripperInterface
uint8_t commanded_gripper_speed_;
uint8_t commanded_gripper_force_;
};
} // namespace robotiq_driver
58 changes: 58 additions & 0 deletions robotiq_driver/include/robotiq_driver/default_driver_factory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2022 PickNik, Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the {copyright_holder} nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#pragma once

#include <hardware_interface/hardware_info.hpp>
#include <memory>
#include <robotiq_driver/default_driver_factory.hpp>
#include <robotiq_driver/driver_factory.hpp>

namespace robotiq_driver
{
/**
* This class is used to create a default driver to interact with the hardware.
*/
class DefaultDriverFactory : public DriverFactory
{
public:
DefaultDriverFactory() = default;

/**
* @brief Create a driver.
* @param info The hardware information.
* @return A driver to interact with the hardware.
*/
std::unique_ptr<Driver> create(const hardware_interface::HardwareInfo & info) const;

protected:
// Seam for testing.
virtual std::unique_ptr<Driver> create_driver(
const hardware_interface::HardwareInfo & info) const;
};
} // namespace robotiq_driver
113 changes: 113 additions & 0 deletions robotiq_driver/include/robotiq_driver/driver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (c) 2022 PickNik, Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the {copyright_holder} nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#pragma once

#include <cstdint>

/**
* @brief This interface describes how to communicate with the gripper hardware.
*/
namespace robotiq_driver
{
class Driver
{
public:
enum class ActivationStatus
{
RESET,
ACTIVE
};

enum class ActionStatus
{
STOPPED,
MOVING
};

enum class GripperStatus
{
RESET,
IN_PROGRESS,
COMPLETED,
};

enum class ObjectDetectionStatus
{
MOVING,
OBJECT_DETECTED_OPENING,
OBJECT_DETECTED_CLOSING,
AT_REQUESTED_POSITION
};

/**
* @brief Activates the gripper.
* @throw serial::IOException on failure to successfully communicate with gripper port
*/
virtual void activate() = 0;

/**
* @brief Deactivates the gripper.
* @throw serial::IOException on failure to successfully communicate with gripper port
*/
virtual void deactivate() = 0;

/**
* @brief Commands the gripper to move to the desired position.
* @param pos A value between 0x00 (fully open) and 0xFF (fully closed).
*/
virtual void set_gripper_position(uint8_t pos) = 0;

/**
* @brief Return the current position of the gripper.
*
* @throw serial::IOException on failure to successfully communicate with gripper port
*
* @return uint8_t A value between 0x00 (fully open) and 0xFF (fully closed).
*/
virtual uint8_t get_gripper_position() = 0;

/**
* @brief Returns true if the gripper is currently moving, false otherwise.
*/
virtual bool gripper_is_moving() = 0;

/**
* @brief Set the speed of the gripper.
*
* @param speed A value between 0x00 (stopped) and 0xFF (full speed).
*/
virtual void set_speed(uint8_t speed) = 0;

/**
* @brief Set how forcefully the gripper opens or closes.
* @param force A value between 0x00 (no force) or 0xFF (maximum force).
*/
virtual void set_force(uint8_t force) = 0;
};
} // namespace robotiq_driver
Loading
Loading