From 4828b344d0ec5c425d86ad1ab0800ef6b424924c Mon Sep 17 00:00:00 2001 From: Anastasiia Stepanova Date: Mon, 9 Sep 2024 11:00:49 +0300 Subject: [PATCH] Add can terminator (#62) --- ...default_params.yml => default_params.yaml} | 0 Src/applications/cyphal/README.md | 7 ++- ...default_params.yml => default_params.yaml} | 0 Src/applications/dronecan/README.md | 57 ++++++++++--------- ...default_params.yml => default_params.yaml} | 2 + Src/common/application.cpp | 14 +++++ Src/common/params.yaml | 9 +++ Src/periphery/gpio/gpio.hpp | 34 +++++++++++ Src/platform/stm32g0b1/CMakeLists.txt | 1 + Src/platform/stm32g0b1/gpio.cpp | 54 ++++++++++++++++++ Src/platform/ubuntu/CMakeLists.txt | 1 + Src/platform/ubuntu/gpio.cpp | 27 +++++++++ 12 files changed, 175 insertions(+), 31 deletions(-) rename Src/applications/both/{default_params.yml => default_params.yaml} (100%) rename Src/applications/cyphal/{default_params.yml => default_params.yaml} (100%) rename Src/applications/dronecan/{default_params.yml => default_params.yaml} (90%) create mode 100644 Src/periphery/gpio/gpio.hpp create mode 100644 Src/platform/stm32g0b1/gpio.cpp create mode 100644 Src/platform/ubuntu/gpio.cpp diff --git a/Src/applications/both/default_params.yml b/Src/applications/both/default_params.yaml similarity index 100% rename from Src/applications/both/default_params.yml rename to Src/applications/both/default_params.yaml diff --git a/Src/applications/cyphal/README.md b/Src/applications/cyphal/README.md index 226813f..7fc2481 100644 --- a/Src/applications/cyphal/README.md +++ b/Src/applications/cyphal/README.md @@ -18,9 +18,10 @@ The node has the following registers: | № | Register name | Description | | -- | ----------------------- | ----------- | | 1 | uavcan.node.id | Defines a node-ID. Allowed values [0,127]. | -| 2 | system.name | Defines custom node name. If empty, the node will use the default name. | -| 3 | system.protocol | Auto, Cyphal/CAN or DroneCAN | -| 4 | uavcan.node.description | User/integrator-defined, human-readable description of this specific node. | +| 2 | system.can_terminator | Bitmask which switch can terminators (0 - both off, 1 - enables second, 2 - enables first, 3 - enables both). Works only for v3. | +| 3 | system.name | Defines custom node name. If empty, the node will use the default name. | +| 4 | system.protocol | Auto, Cyphal/CAN or DroneCAN | +| 5 | uavcan.node.description | User/integrator-defined, human-readable description of this specific node. | > This docs was automatically generated. Do not edit it manually. diff --git a/Src/applications/cyphal/default_params.yml b/Src/applications/cyphal/default_params.yaml similarity index 100% rename from Src/applications/cyphal/default_params.yml rename to Src/applications/cyphal/default_params.yaml diff --git a/Src/applications/dronecan/README.md b/Src/applications/dronecan/README.md index 07fdd99..6bf7ce6 100644 --- a/Src/applications/dronecan/README.md +++ b/Src/applications/dronecan/README.md @@ -3,34 +3,35 @@ The node has the following registers: | № | Register name | Description | | -- | ----------------------- | ----------- | | 1 | uavcan.node.id | Defines a node-ID. Allowed values [0,127]. | -| 2 | system.name | Defines custom node name. If empty, the node will use the default name. | -| 3 | system.protocol | Auto, Cyphal/CAN or DroneCAN | -| 4 | uavcan.node.description | User/integrator-defined, human-readable description of this specific node. | -| 5 | stats.engaged_time | Total engaged time of node in seconds. | -| 6 | pwm.cmd_ttl_ms | TTL of specified by pwm.cmd_type commands [ms]. | -| 7 | pwm.frequency | PWM frequency [Hz]. | -| 8 | pwm.cmd_type | 0 means RawCommand, 1 means ArrayCommand, 2 is reserved for hardpoint.Command. | -| 9 | pwm.1_ch | Index of setpoint channel. [-1; 255]. -1 means disabled, | -| 10 | pwm.1_min | PWM duration when setpoint is min (RawCommand is 0 or Command is -1.0) | -| 11 | pwm.1_max | PWM duration when setpoint is max (RawCommand is 8191 or Command is 1.0) | -| 12 | pwm.1_def | PWM duration when setpoint is negative or there is no setpoint at all. | -| 13 | pwm.1_feedback | Indicates the operational mode of the node. 0 means disabled. When set to 1, the command of corresponding Status type for cmd_type will be transmitted (esc.RawCommand - esc.Status, actuator.ArrayCommand - actuator.Status) with frequency 1 Hz. When set to 2 - 10 Hz. | -| 14 | pwm.2_ch | Index of setpoint channel. [-1; 255]. -1 means disabled, | -| 15 | pwm.2_min | PWM duration when setpoint is min (RawCommand is 0 or Command is -1.0) | -| 16 | pwm.2_max | PWM duration when setpoint is max (RawCommand is 8191 or Command is 1.0) | -| 17 | pwm.2_def | PWM duration when setpoint is negative or there is no setpoint at all. | -| 18 | pwm.2_feedback | Indicates the operational mode of the node. 0 means disabled. When set to 1, the command of corresponding Status type for cmd_type will be transmitted (esc.RawCommand - esc.Status, actuator.ArrayCommand - actuator.Status) with frequency 1 Hz. When set to 2 - 10 Hz. | -| 19 | pwm.3_ch | Index of setpoint channel. [-1; 255]. -1 means disabled, | -| 20 | pwm.3_min | PWM duration when setpoint is min (RawCommand is 0 or Command is -1.0) | -| 21 | pwm.3_max | PWM duration when setpoint is max (RawCommand is 8191 or Command is 1.0) | -| 22 | pwm.3_def | PWM duration when setpoint is negative or there is no setpoint at all. | -| 23 | pwm.3_feedback | Indicates the operational mode of the node. 0 means disabled. When set to 1, the command of corresponding Status type for cmd_type will be transmitted (esc.RawCommand - esc.Status, actuator.ArrayCommand - actuator.Status) with frequency 1 Hz. When set to 2 - 10 Hz. | -| 24 | pwm.4_ch | Index of setpoint channel. [-1; 255]. -1 means disabled, | -| 25 | pwm.4_min | PWM duration when setpoint is min (RawCommand is 0 or Command is -1.0) | -| 26 | pwm.4_max | PWM duration when setpoint is max (RawCommand is 8191 or Command is 1.0) | -| 27 | pwm.4_def | PWM duration when setpoint is negative or there is no setpoint at all. | -| 28 | pwm.4_feedback | Indicates the operational mode of the node. 0 means disabled. When set to 1, the command of corresponding Status type for cmd_type will be transmitted (esc.RawCommand - esc.Status, actuator.ArrayCommand - actuator.Status) with frequency 1 Hz. When set to 2 - 10 Hz. | -| 29 | imu.enable | Enable or disable IMU publisher. | +| 2 | system.can_terminator | Bitmask which switch can terminators (0 - both off, 1 - enables second, 2 - enables first, 3 - enables both). Works only for v3. | +| 3 | system.name | Defines custom node name. If empty, the node will use the default name. | +| 4 | system.protocol | Auto, Cyphal/CAN or DroneCAN | +| 5 | uavcan.node.description | User/integrator-defined, human-readable description of this specific node. | +| 6 | stats.engaged_time | Total engaged time of node in seconds. | +| 7 | pwm.cmd_ttl_ms | TTL of specified by pwm.cmd_type commands [ms]. | +| 8 | pwm.frequency | PWM frequency [Hz]. | +| 9 | pwm.cmd_type | 0 means RawCommand, 1 means ArrayCommand, 2 is reserved for hardpoint.Command. | +| 10 | pwm.1_ch | Index of setpoint channel. [-1; 255]. -1 means disabled, | +| 11 | pwm.1_min | PWM duration when setpoint is min (RawCommand is 0 or Command is -1.0) | +| 12 | pwm.1_max | PWM duration when setpoint is max (RawCommand is 8191 or Command is 1.0) | +| 13 | pwm.1_def | PWM duration when setpoint is negative or there is no setpoint at all. | +| 14 | pwm.1_feedback | Indicates the operational mode of the node. 0 means disabled. When set to 1, the command of corresponding Status type for cmd_type will be transmitted (esc.RawCommand - esc.Status, actuator.ArrayCommand - actuator.Status) with frequency 1 Hz. When set to 2 - 10 Hz. | +| 15 | pwm.2_ch | Index of setpoint channel. [-1; 255]. -1 means disabled, | +| 16 | pwm.2_min | PWM duration when setpoint is min (RawCommand is 0 or Command is -1.0) | +| 17 | pwm.2_max | PWM duration when setpoint is max (RawCommand is 8191 or Command is 1.0) | +| 18 | pwm.2_def | PWM duration when setpoint is negative or there is no setpoint at all. | +| 19 | pwm.2_feedback | Indicates the operational mode of the node. 0 means disabled. When set to 1, the command of corresponding Status type for cmd_type will be transmitted (esc.RawCommand - esc.Status, actuator.ArrayCommand - actuator.Status) with frequency 1 Hz. When set to 2 - 10 Hz. | +| 20 | pwm.3_ch | Index of setpoint channel. [-1; 255]. -1 means disabled, | +| 21 | pwm.3_min | PWM duration when setpoint is min (RawCommand is 0 or Command is -1.0) | +| 22 | pwm.3_max | PWM duration when setpoint is max (RawCommand is 8191 or Command is 1.0) | +| 23 | pwm.3_def | PWM duration when setpoint is negative or there is no setpoint at all. | +| 24 | pwm.3_feedback | Indicates the operational mode of the node. 0 means disabled. When set to 1, the command of corresponding Status type for cmd_type will be transmitted (esc.RawCommand - esc.Status, actuator.ArrayCommand - actuator.Status) with frequency 1 Hz. When set to 2 - 10 Hz. | +| 25 | pwm.4_ch | Index of setpoint channel. [-1; 255]. -1 means disabled, | +| 26 | pwm.4_min | PWM duration when setpoint is min (RawCommand is 0 or Command is -1.0) | +| 27 | pwm.4_max | PWM duration when setpoint is max (RawCommand is 8191 or Command is 1.0) | +| 28 | pwm.4_def | PWM duration when setpoint is negative or there is no setpoint at all. | +| 29 | pwm.4_feedback | Indicates the operational mode of the node. 0 means disabled. When set to 1, the command of corresponding Status type for cmd_type will be transmitted (esc.RawCommand - esc.Status, actuator.ArrayCommand - actuator.Status) with frequency 1 Hz. When set to 2 - 10 Hz. | +| 30 | imu.enable | Enable or disable IMU publisher. | > This docs was automatically generated. Do not edit it manually. diff --git a/Src/applications/dronecan/default_params.yml b/Src/applications/dronecan/default_params.yaml similarity index 90% rename from Src/applications/dronecan/default_params.yml rename to Src/applications/dronecan/default_params.yaml index 6e4a9f2..c7eca17 100644 --- a/Src/applications/dronecan/default_params.yml +++ b/Src/applications/dronecan/default_params.yaml @@ -1,4 +1,6 @@ uavcan.node.id: 39 +system.protocol : 0 +stats.engaged_time : 0 pwm.cmd_ttl_ms: 0 pwm.frequency: 50 pwm.cmd_type: 1 diff --git a/Src/common/application.cpp b/Src/common/application.cpp index e7b894c..b91c69c 100644 --- a/Src/common/application.cpp +++ b/Src/common/application.cpp @@ -6,6 +6,7 @@ #include "application.hpp" #include +#include #include "periphery/adc/circuit_periphery.hpp" #include "periphery/led/led.hpp" #include "params.hpp" @@ -13,6 +14,7 @@ #include "main.h" #include "periphery/led/led.hpp" +#include "periphery/gpio/gpio.hpp" #include "periphery/iwdg/iwdg.hpp" static int8_t init_board_periphery() { @@ -24,6 +26,18 @@ static int8_t init_board_periphery() { paramsInit(libparams_integers_amount, libparams_strings_amount, -1, 1); paramsLoad(); +#if defined(CAN1_TERMINATOR_Pin) && defined(CAN2_TERMINATOR_Pin) + auto teminator_parm = paramsGetIntegerValue(IntParamsIndexes::PARAM_SYSTEM_CAN_TEMINATOR); + + std::bitset<2> terminator_mask(teminator_parm); + for (uint8_t i = 0; i < 2; i++) { + if (terminator_mask[i]) { + GPIOPeriphery::set(GPIOPin(i)); + } else { + GPIOPeriphery::reset(GPIOPin(i)); + } + } +#endif return 0; } diff --git a/Src/common/params.yaml b/Src/common/params.yaml index 8ab67c1..0a11770 100644 --- a/Src/common/params.yaml +++ b/Src/common/params.yaml @@ -7,6 +7,15 @@ uavcan.node.id: min: 0 max: 255 +system.can_terminator: + type: Integer + note: Bitmask which switch can terminators (0 - both off, 1 - enables second, 2 - enables first, 3 - enables both). Works only for v3. + enum: PARAM_SYSTEM_CAN_TEMINATOR + flags: mutable + default: 0 + min: 0 + max: 3 + system.name: type: String note: Defines custom node name. If empty, the node will use the default name. diff --git a/Src/periphery/gpio/gpio.hpp b/Src/periphery/gpio/gpio.hpp new file mode 100644 index 0000000..d11ec70 --- /dev/null +++ b/Src/periphery/gpio/gpio.hpp @@ -0,0 +1,34 @@ +/*** + * Copyright (C) 2024 Anastasiia Stepanova + * Distributed under the terms of the GPL v3 license, available in the file LICENSE. +***/ + +#ifndef SRC_PERIPHERY_GPIO_HPP_ +#define SRC_PERIPHERY_GPIO_HPP_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +enum class GPIOPin { + CAN_TERMINATOR1, + CAN_TERMINATOR2, + GPIO_AMOUNT, +}; + +class GPIOPeriphery { +public: + static void set(const GPIOPin gpio_pin); + static bool get(const GPIOPin gpio_pin); + static void toggle(GPIOPin gpio_pin, uint32_t toggle_period_ms, uint32_t duty_cycle_ms); + static void reset(GPIOPin gpio_pin); + static void reset(); +}; + +#ifdef __cplusplus +} +#endif + +#endif // SRC_PERIPHERY_GPIO_HPP_ diff --git a/Src/platform/stm32g0b1/CMakeLists.txt b/Src/platform/stm32g0b1/CMakeLists.txt index e88deaf..b3d3bf7 100644 --- a/Src/platform/stm32g0b1/CMakeLists.txt +++ b/Src/platform/stm32g0b1/CMakeLists.txt @@ -17,6 +17,7 @@ add_executable(${EXECUTABLE} ${ROOT_DIR}/Src/periphery/adc/circuit_periphery.cpp ${ROOT_DIR}/Src/platform/stm32/pwm/pwm.cpp ${PLATFORM_DIR}/stm32f103/adc.cpp + ${CMAKE_CURRENT_LIST_DIR}/gpio.cpp ${PLATFORM_DIR}/stm32g0b1/pwm.cpp ${PLATFORM_DIR}/stm32g0b1/spi.cpp ${PLATFORM_DIR}/stm32f103/iwdg.cpp diff --git a/Src/platform/stm32g0b1/gpio.cpp b/Src/platform/stm32g0b1/gpio.cpp new file mode 100644 index 0000000..a8fa7fd --- /dev/null +++ b/Src/platform/stm32g0b1/gpio.cpp @@ -0,0 +1,54 @@ +/*** + * Copyright (C) 2024 Anastasiia Stepanova + * Distributed under the terms of the GPL v3 license, available in the file + *LICENSE. + ***/ + +#include "periphery/gpio/gpio.hpp" + +#include "main.h" + +void GPIOPeriphery::set(GPIOPin gpio_pin) { + switch (gpio_pin) { + case GPIOPin::CAN_TERMINATOR1: + HAL_GPIO_WritePin(CAN1_TERMINATOR_GPIO_Port, CAN1_TERMINATOR_Pin, GPIO_PIN_SET); + break; + + case GPIOPin::CAN_TERMINATOR2: + HAL_GPIO_WritePin(CAN2_TERMINATOR_GPIO_Port, CAN2_TERMINATOR_Pin, GPIO_PIN_SET); + break; + default: + break; + } +} + +bool GPIOPeriphery::get(GPIOPin gpio_pin) { + switch (gpio_pin) { + case GPIOPin::CAN_TERMINATOR1: + return HAL_GPIO_ReadPin(CAN1_TERMINATOR_GPIO_Port, CAN1_TERMINATOR_Pin); + + case GPIOPin::CAN_TERMINATOR2: + return HAL_GPIO_ReadPin(CAN2_TERMINATOR_GPIO_Port, CAN2_TERMINATOR_Pin); + + default: + return GPIO_PIN_SET; + } +} + +void GPIOPeriphery::reset() { + HAL_GPIO_WritePin(CAN1_TERMINATOR_GPIO_Port, CAN1_TERMINATOR_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(CAN2_TERMINATOR_GPIO_Port, CAN2_TERMINATOR_Pin, GPIO_PIN_RESET); +} + +void GPIOPeriphery::reset(GPIOPin gpio_pin) { + switch (gpio_pin) { + case GPIOPin::CAN_TERMINATOR1: + HAL_GPIO_WritePin(CAN1_TERMINATOR_GPIO_Port, CAN1_TERMINATOR_Pin, GPIO_PIN_RESET); + break; + case GPIOPin::CAN_TERMINATOR2: + HAL_GPIO_WritePin(CAN2_TERMINATOR_GPIO_Port, CAN2_TERMINATOR_Pin, GPIO_PIN_RESET); + break; + default: + break; + } +} diff --git a/Src/platform/ubuntu/CMakeLists.txt b/Src/platform/ubuntu/CMakeLists.txt index fd33414..91008ed 100644 --- a/Src/platform/ubuntu/CMakeLists.txt +++ b/Src/platform/ubuntu/CMakeLists.txt @@ -11,6 +11,7 @@ add_executable(${EXECUTABLE} ${ROOT_DIR}/Src/periphery/adc/circuit_periphery.cpp ${CMAKE_CURRENT_LIST_DIR}/adc.cpp + ${CMAKE_CURRENT_LIST_DIR}/gpio.cpp ${CMAKE_CURRENT_LIST_DIR}/pwm.cpp ${CMAKE_CURRENT_LIST_DIR}/iwdg.cpp ${CMAKE_CURRENT_LIST_DIR}/led.cpp diff --git a/Src/platform/ubuntu/gpio.cpp b/Src/platform/ubuntu/gpio.cpp new file mode 100644 index 0000000..d07bbf5 --- /dev/null +++ b/Src/platform/ubuntu/gpio.cpp @@ -0,0 +1,27 @@ +/*** + * Copyright (C) 2024 Anastasiia Stepanova + * Distributed under the terms of the GPL v3 license, available in the file + *LICENSE. + ***/ + +#include "periphery/gpio/gpio.hpp" + +#include +#include "main.h" + +static std::vector gpio((int)GPIOPin::GPIO_AMOUNT, false); + +void GPIOPeriphery::set(GPIOPin gpio_pin) { + gpio[(int)gpio_pin] = true; +} +bool GPIOPeriphery::get(GPIOPin gpio_pin) { + return gpio[(int)gpio_pin]; +} +void GPIOPeriphery::reset() { + for (auto gpio_val : gpio) { + gpio_val = false; + } +} +void GPIOPeriphery::reset(GPIOPin gpio_pin) { + gpio[(int)gpio_pin] = false; +}