From 13b5b735c6bdd8c3ebc83ea4dbc34adfabad4c1f Mon Sep 17 00:00:00 2001 From: Tom Lonergan <43603330+TomLonergan03@users.noreply.github.com> Date: Fri, 24 May 2024 18:00:01 +0100 Subject: [PATCH 1/4] SNS - Keyence (#118) --- CMakeLists.txt | 1 + Dockerfile | 10 +- config/debug.toml | 4 + lib/debug/commands/gpio_commands.cpp | 26 +-- lib/debug/commands/keyence_commands.cpp | 66 +++++++ lib/debug/commands/keyence_commands.hpp | 27 +++ lib/debug/repl.cpp | 10 + lib/debug/repl.hpp | 9 +- lib/io/CMakeLists.txt | 1 + lib/io/gpio.hpp | 12 +- lib/io/hardware_gpio.cpp | 235 +++++------------------ lib/io/hardware_gpio.hpp | 55 ++---- lib/motors/time_frequency_calculator.hpp | 1 + lib/sensors/keyence.cpp | 27 ++- lib/sensors/keyence.hpp | 14 +- lib/utils/dummy_gpio.cpp | 6 +- lib/utils/dummy_gpio.hpp | 6 +- src/debugger/main.cpp | 4 +- test/utils/dummy_gpio.cpp | 23 +-- 19 files changed, 251 insertions(+), 286 deletions(-) create mode 100644 lib/debug/commands/keyence_commands.cpp create mode 100644 lib/debug/commands/keyence_commands.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a32d058..0a051fc4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ include_directories(${CURSES_INCLUDE_DIR}) find_package(Boost REQUIRED) find_package(PahoMqttCpp REQUIRED) include_directories(${PahoMqttCpp_INCLUDE_DIRS}) +find_library(GPIOD_LIBRARY REQUIRED NAMES libgpiodcxx.so) include(FetchContent) FetchContent_Declare( rapidjson GIT_REPOSITORY https://github.com/Tencent/rapidjson.git diff --git a/Dockerfile b/Dockerfile index 8d7da661..89c300ec 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ ARG DEBIAN_FRONTEND=noninteractive # Install dependencies RUN apt-get update && \ - apt-get install -y git clang clang-format clang-tidy libboost-all-dev libeigen3-dev rapidjson-dev build-essential gcc make cmake libssl-dev libncurses5-dev libncursesw5-dev sudo + apt-get install -y git clang clang-format clang-tidy libboost-all-dev libeigen3-dev rapidjson-dev build-essential gcc make cmake libssl-dev libncurses5-dev libncursesw5-dev sudo autoconf-archive # Install Paho C++ WORKDIR /home @@ -12,6 +12,14 @@ WORKDIR /home/paho.mqtt.cpp RUN git checkout v1.3.2 RUN cmake -Bbuild -H. -DPAHO_WITH_MQTT_C=ON -DPAHO_BUILD_STATIC=ON -DPAHO_BUILD_DOCUMENTATION=OFF -DPAHO_BUILD_SAMPLES=OFF RUN sudo cmake --build build/ --target install + +# Install libgpiod +WORKDIR /home +RUN git clone https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git +WORKDIR /home/libgpiod +RUN git checkout v2.1.x +RUN ./autogen.sh --enable-tools=yes --enable-bindings-cxx --prefix=/usr && make -j && sudo make install + RUN sudo ldconfig WORKDIR /home/hyped_entrypoint diff --git a/config/debug.toml b/config/debug.toml index c5b32299..80388389 100644 --- a/config/debug.toml +++ b/config/debug.toml @@ -35,6 +35,10 @@ address = 0x38 enabled = false bus = "can1" +[sensors.keyence] +enabled = false +pin = 1 + [motors] enabled = false bus = "can1" diff --git a/lib/debug/commands/gpio_commands.cpp b/lib/debug/commands/gpio_commands.cpp index 0e8c608b..5af79193 100644 --- a/lib/debug/commands/gpio_commands.cpp +++ b/lib/debug/commands/gpio_commands.cpp @@ -1,13 +1,15 @@ #include "gpio_commands.hpp" +#include + namespace hyped::debug { core::Result GpioCommands::addCommands(core::ILogger &logger, std::shared_ptr &repl) { { - const auto *const gpio_read_command_name = "gpio read"; - const auto *const gpio_read_command_description = "Read from GPIO pins"; - const auto *const gpio_read_command_usage = "gpio read "; + const std::string gpio_read_command_name = "gpio read"; + const std::string gpio_read_command_description = "Read from GPIO pins"; + const std::string gpio_read_command_usage = "gpio read "; const auto gpio_read_command_handler = [&logger, repl](std::vector args) { if (args.size() != 1) { logger.log(core::LogLevel::kFatal, "Invalid number of arguments"); @@ -15,12 +17,12 @@ core::Result GpioCommands::addCommands(core::ILogger &logger, std::shared_ptrgetGpio(); const auto pin = std::stoi(args[0]); - auto optional_gpio_reader = gpio->getReader(pin, io::Edge::kNone); + auto optional_gpio_reader = gpio->getReader(pin); if (!optional_gpio_reader) { - logger.log(core::LogLevel::kFatal, "Failed to get GPIO reader on pin %d", pin); + logger.log(core::LogLevel::kFatal, "Failed to create GPIO reader for pin %d", pin); return; } - const auto gpio_reader = std::move(*optional_gpio_reader); + const auto gpio_reader = *optional_gpio_reader; const auto value = gpio_reader->read(); if (!value) { logger.log(core::LogLevel::kFatal, "Failed to read from GPIO pin %d", pin); @@ -38,9 +40,9 @@ core::Result GpioCommands::addCommands(core::ILogger &logger, std::shared_ptraddCommand(std::move(gpio_read_command)); } { - const auto *const gpio_write_command_name = "gpio write"; - const auto *const gpio_write_command_description = "Write to GPIO pin (0 or 1)"; - const auto *const gpio_write_command_usage = "gpio write "; + const std::string gpio_write_command_name = "gpio write"; + const std::string gpio_write_command_description = "Write to GPIO pin (0 or 1)"; + const std::string gpio_write_command_usage = "gpio write "; const auto gpio_write_command_handler = [&logger, repl](std::vector args) { if (args.size() != 2) { logger.log(core::LogLevel::kFatal, "Invalid number of arguments"); @@ -48,12 +50,12 @@ core::Result GpioCommands::addCommands(core::ILogger &logger, std::shared_ptrgetGpio(); const auto pin = std::stoi(args[0]); - auto optional_gpio_writer = gpio->getWriter(pin, io::Edge::kNone); + auto optional_gpio_writer = gpio->getWriter(pin); if (!optional_gpio_writer) { - logger.log(core::LogLevel::kFatal, "Failed to create GPIO writer on pin %d", pin); + logger.log(core::LogLevel::kFatal, "Failed to create GPIO writer for pin %d", pin); return; } - const auto gpio_writer = std::move(*optional_gpio_writer); + const auto gpio_writer = *optional_gpio_writer; const auto value = std::stoi(args[1]); core::DigitalSignal signal; switch (value) { diff --git a/lib/debug/commands/keyence_commands.cpp b/lib/debug/commands/keyence_commands.cpp new file mode 100644 index 00000000..eb017ac2 --- /dev/null +++ b/lib/debug/commands/keyence_commands.cpp @@ -0,0 +1,66 @@ +#include "keyence_commands.hpp" + +#include +#include + +#include "core/logger.hpp" +#include "core/time.hpp" +#include "core/types.hpp" +#include "sensors/keyence.hpp" + +namespace hyped::debug { + +core::Result KeyenceCommands::addCommands(core::ILogger &logger, + std::shared_ptr &repl, + core::ITimeSource &time, + toml::v3::node_view config) +{ + const auto optional_pin = config["pin"].value(); + if (!optional_pin) { + logger.log(core::LogLevel::kFatal, "Invalid pin"); + return core::Result::kFailure; + } + const auto pin = *optional_pin; + const auto gpio = repl->getGpio(); + auto optional_keyence = sensors::Keyence::create(logger, gpio, pin); + if (!optional_keyence) { + logger.log(core::LogLevel::kFatal, "Failed to create keyence sensor"); + return core::Result::kFailure; + } + const auto keyence = *optional_keyence; + const std::string keyence_command_name = "keyence read"; + const std::string keyence_command_usage = "keyence read