Skip to content

Commit

Permalink
Add can terminator (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
AsiiaPine authored Sep 9, 2024
1 parent 01d3fbd commit 4828b34
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 31 deletions.
File renamed without changes.
7 changes: 4 additions & 3 deletions Src/applications/cyphal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
File renamed without changes.
57 changes: 29 additions & 28 deletions Src/applications/dronecan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 14 additions & 0 deletions Src/common/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

#include "application.hpp"
#include <array>
#include <bitset>
#include "periphery/adc/circuit_periphery.hpp"
#include "periphery/led/led.hpp"
#include "params.hpp"
#include "module.hpp"
#include "main.h"

#include "periphery/led/led.hpp"
#include "periphery/gpio/gpio.hpp"
#include "periphery/iwdg/iwdg.hpp"

static int8_t init_board_periphery() {
Expand All @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions Src/common/params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 34 additions & 0 deletions Src/periphery/gpio/gpio.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/***
* Copyright (C) 2024 Anastasiia Stepanova <[email protected]>
* 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 <stdint.h>

#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_
1 change: 1 addition & 0 deletions Src/platform/stm32g0b1/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions Src/platform/stm32g0b1/gpio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/***
* Copyright (C) 2024 Anastasiia Stepanova <[email protected]>
* 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;
}
}
1 change: 1 addition & 0 deletions Src/platform/ubuntu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions Src/platform/ubuntu/gpio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/***
* Copyright (C) 2024 Anastasiia Stepanova <[email protected]>
* Distributed under the terms of the GPL v3 license, available in the file
*LICENSE.
***/

#include "periphery/gpio/gpio.hpp"

#include <vector>
#include "main.h"

static std::vector<bool> 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;
}

0 comments on commit 4828b34

Please sign in to comment.