Skip to content

Commit f2fd632

Browse files
committed
chore: split ADC HAL from board mapping, add sitl board module
1 parent 0cf6117 commit f2fd632

22 files changed

Lines changed: 272 additions & 108 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* This program is free software under the GNU General Public License v3.
3+
* See <https://www.gnu.org/licenses/> for details.
4+
* Author: Dmitry Ponomarev <ponomarevda96@gmail.com>
5+
*/
6+
7+
#ifndef SRC_BOARDS_RL_MINI_V2_ADC_MAPPING_HPP_
8+
#define SRC_BOARDS_RL_MINI_V2_ADC_MAPPING_HPP_
9+
10+
#include <stdint.h>
11+
12+
namespace BoardAdc {
13+
14+
static constexpr uint8_t INVALID_RANK = 0xFF;
15+
static constexpr uint8_t DMA_CHANNEL_COUNT = 6;
16+
17+
static constexpr uint8_t RANK_VIN = 0;
18+
static constexpr uint8_t RANK_5V = 1;
19+
static constexpr uint8_t RANK_CURRENT = 2;
20+
static constexpr uint8_t RANK_VERSION = 3;
21+
static constexpr uint8_t RANK_TEMPERATURE = 4;
22+
23+
} // namespace BoardAdc
24+
25+
#endif // SRC_BOARDS_RL_MINI_V2_ADC_MAPPING_HPP_
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* This program is free software under the GNU General Public License v3.
3+
* See <https://www.gnu.org/licenses/> for details.
4+
* Author: Dmitry Ponomarev <ponomarevda96@gmail.com>
5+
*/
6+
7+
#ifndef SRC_BOARDS_RL_MINI_V3_ADC_MAPPING_HPP_
8+
#define SRC_BOARDS_RL_MINI_V3_ADC_MAPPING_HPP_
9+
10+
#include <stdint.h>
11+
12+
namespace BoardAdc {
13+
14+
static constexpr uint8_t INVALID_RANK = 0xFF;
15+
static constexpr uint8_t DMA_CHANNEL_COUNT = 5;
16+
17+
static constexpr uint8_t RANK_VIN = 0;
18+
static constexpr uint8_t RANK_5V = 1;
19+
static constexpr uint8_t RANK_CURRENT = 2;
20+
static constexpr uint8_t RANK_VERSION = 3;
21+
static constexpr uint8_t RANK_TEMPERATURE = 4;
22+
23+
} // namespace BoardAdc
24+
25+
#endif // SRC_BOARDS_RL_MINI_V3_ADC_MAPPING_HPP_

Src/boards/rl/sitl/adc_mapping.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* This program is free software under the GNU General Public License v3.
3+
* See <https://www.gnu.org/licenses/> for details.
4+
* Author: Dmitry Ponomarev <ponomarevda96@gmail.com>
5+
*/
6+
7+
#ifndef SRC_BOARDS_RL_SITL_ADC_MAPPING_HPP_
8+
#define SRC_BOARDS_RL_SITL_ADC_MAPPING_HPP_
9+
10+
#include <stdint.h>
11+
12+
namespace BoardAdc {
13+
14+
static constexpr uint8_t INVALID_RANK = 0xFF;
15+
static constexpr uint8_t DMA_CHANNEL_COUNT = 6;
16+
17+
static constexpr uint8_t RANK_VIN = 0;
18+
static constexpr uint8_t RANK_5V = 1;
19+
static constexpr uint8_t RANK_CURRENT = 2;
20+
static constexpr uint8_t RANK_VERSION = 3;
21+
static constexpr uint8_t RANK_TEMPERATURE = 4;
22+
23+
} // namespace BoardAdc
24+
25+
#endif // SRC_BOARDS_RL_SITL_ADC_MAPPING_HPP_

Src/boards/rl/sitl/cyphal.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ include(${ROOT_DIR}/Src/modules/system/CMakeLists.txt)
99
include(${ROOT_DIR}/Src/modules/circuit_status/cyphal/CMakeLists.txt)
1010
include(${ROOT_DIR}/Src/modules/feedback/cyphal/CMakeLists.txt)
1111
include(${ROOT_DIR}/Src/modules/rcout/CMakeLists.txt)
12+
include(${ROOT_DIR}/Src/modules/sitl/CMakeLists.txt)

Src/boards/rl/sitl/dronecan.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ include(${ROOT_DIR}/Src/modules/dronecan/arming/CMakeLists.txt)
99
include(${ROOT_DIR}/Src/modules/circuit_status/dronecan/CMakeLists.txt)
1010
include(${ROOT_DIR}/Src/modules/feedback/dronecan/CMakeLists.txt)
1111
include(${ROOT_DIR}/Src/modules/rcout/CMakeLists.txt)
12+
include(${ROOT_DIR}/Src/modules/sitl/CMakeLists.txt)
1213

1314
if(USE_PLATFORM_NODE_V3 OR USE_PLATFORM_UBUNTU)
1415
include(${ROOT_DIR}/Src/modules/imu/CMakeLists.txt)

Src/common/application.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <array>
1010
#include <bitset>
1111
#include "flash_driver.h"
12-
#include "peripheral/adc/circuit_periphery.hpp"
12+
#include "drivers/board_monitor/board_monitor.hpp"
1313
#include "peripheral/gpio/gpio.hpp"
1414
#include "peripheral/iwdg/iwdg.hpp"
1515
#include "peripheral/led/led.hpp"
@@ -19,7 +19,7 @@
1919

2020
static int8_t init_board_periphery() {
2121
Board::Led::reset();
22-
CircuitPeriphery::init();
22+
BoardMonitor::init();
2323

2424
auto libparams_ints_num = (ParamIndex_t)IntParamsIndexes::INTEGER_PARAMS_AMOUNT;
2525
auto libparams_strs_num = NUM_OF_STR_PARAMS;

Src/peripheral/adc/circuit_periphery.cpp renamed to Src/drivers/board_monitor/board_monitor.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Author: Dmitry Ponomarev <ponomarevda96@gmail.com>
55
*/
66

7-
#include "circuit_periphery.hpp"
7+
#include "board_monitor.hpp"
88
#include <array>
99
#include "application.hpp"
1010
#include "main.h"
@@ -35,8 +35,12 @@ static const std::array<std::pair<const char*, uint16_t>, (int)BoardType::BOARDS
3535
{"co.rl.mini.v3", 14},
3636
}};
3737

38-
uint16_t CircuitPeriphery::temperature() {
39-
auto adc_12b = HAL::Adc::get(HAL::AdcChannel::ADC_TEMPERATURE);
38+
uint16_t BoardMonitor::temperature() {
39+
if (BoardAdc::RANK_TEMPERATURE == BoardAdc::INVALID_RANK) {
40+
return 0;
41+
}
42+
43+
auto adc_12b = HAL::Adc::get(BoardAdc::RANK_TEMPERATURE);
4044
uint16_t temperature_kelvin;
4145
#ifdef STM32G0B1xx
4246
temperature_kelvin = __HAL_ADC_CALC_TEMPERATURE(3300, adc_12b, ADC_RESOLUTION_12B) + 273;
@@ -49,8 +53,8 @@ uint16_t CircuitPeriphery::temperature() {
4953
return temperature_kelvin;
5054
}
5155

52-
BoardType CircuitPeriphery::detect_board_type() {
53-
auto hardware_version = CircuitPeriphery::hardware_version();
56+
BoardType BoardMonitor::detect_board_type() {
57+
auto hardware_version = BoardMonitor::hardware_version();
5458

5559
int board_counter = 0;
5660

@@ -65,6 +69,6 @@ BoardType CircuitPeriphery::detect_board_type() {
6569
return (detected_board < BoardType::BOARDS_AMOUNT) ? detected_board : BoardType::MINI_V2_1_1;
6670
}
6771

68-
std::pair<const char*, uint8_t> CircuitPeriphery::get_board_name() {
72+
std::pair<const char*, uint8_t> BoardMonitor::get_board_name() {
6973
return names[(uint8_t)detect_board_type()];
7074
}

Src/peripheral/adc/circuit_periphery.hpp renamed to Src/drivers/board_monitor/board_monitor.hpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
// Copyright (C) 2024 Anastasiia Stepanova <asiiapine@gmail.com>
2-
// Distributed under the terms of the GPL v3 license, available in the file LICENSE.
1+
/**
2+
* This program is free software under the GNU General Public License v3.
3+
* See <https://www.gnu.org/licenses/> for details.
4+
* Author: Dmitry Ponomarev <ponomarevda96@gmail.com>
5+
*/
36

4-
#ifndef SRC_DRIVER_CIRCUIT_PERIPHERY_HPP_
5-
#define SRC_DRIVER_CIRCUIT_PERIPHERY_HPP_
7+
#ifndef SRC_DRIVERS_BOARD_MONITOR_BOARD_MONITOR_HPP_
8+
#define SRC_DRIVERS_BOARD_MONITOR_BOARD_MONITOR_HPP_
69

710
#include <stdint.h>
811
#include <limits>
912
#include <utility>
1013
#include "peripheral/adc/adc.hpp"
14+
#include "adc_mapping.hpp"
1115

1216
#ifdef __cplusplus
1317
extern "C" {
@@ -25,10 +29,10 @@ enum class BoardType : uint8_t {
2529
BOARDS_AMOUNT,
2630
};
2731

28-
class CircuitPeriphery{
32+
class BoardMonitor{
2933
public:
3034
static int8_t init(){
31-
return HAL::Adc::init();
35+
return HAL::Adc::init(BoardAdc::DMA_CHANNEL_COUNT);
3236
}
3337

3438
/**
@@ -40,6 +44,10 @@ class CircuitPeriphery{
4044
* @return The current in Amperes if the hardware supports it, otherwise NaN.
4145
*/
4246
static float current() {
47+
if (BoardAdc::RANK_CURRENT == BoardAdc::INVALID_RANK) {
48+
return std::numeric_limits<float>::quiet_NaN();
49+
}
50+
4351
if (auto hw_version = hardware_version(); hw_version < 2403 || hw_version > 2450) {
4452
return std::numeric_limits<float>::quiet_NaN();
4553
}
@@ -49,24 +57,35 @@ class CircuitPeriphery{
4957
constexpr float MAX_SENSOR_CURRENT = 10.0f;
5058
constexpr float CALIBRATION_COEF = 0.6666667f;
5159
constexpr float ADC_CURRENT_MULTIPLIER = MAX_SENSOR_CURRENT * CALIBRATION_COEF / 4095.0f;
52-
uint16_t curr = HAL::Adc::get(HAL::AdcChannel::ADC_CURRENT);
60+
uint16_t curr = HAL::Adc::get(BoardAdc::RANK_CURRENT);
5361
return curr * ADC_CURRENT_MULTIPLIER;
5462
}
5563

5664
static float voltage_vin() {
65+
if (BoardAdc::RANK_VIN == BoardAdc::INVALID_RANK) {
66+
return std::numeric_limits<float>::quiet_NaN();
67+
}
68+
5769
constexpr float ADC_VIN_MULTIPLIER = 1.0f / 64.0f;
58-
uint16_t volt = HAL::Adc::get(HAL::AdcChannel::ADC_VIN);
70+
uint16_t volt = HAL::Adc::get(BoardAdc::RANK_VIN);
5971
return volt * ADC_VIN_MULTIPLIER;
6072
}
6173

6274
static float voltage_5v() {
75+
if (BoardAdc::RANK_5V == BoardAdc::INVALID_RANK) {
76+
return std::numeric_limits<float>::quiet_NaN();
77+
}
78+
6379
constexpr float ADC_5V_MULTIPLIER = 1.0f / 640.0f;
64-
uint16_t volt = HAL::Adc::get(HAL::AdcChannel::ADC_5V);
80+
uint16_t volt = HAL::Adc::get(BoardAdc::RANK_5V);
6581
return volt * ADC_5V_MULTIPLIER;
6682
}
6783

6884
static uint16_t hardware_version() {
69-
return HAL::Adc::get(HAL::AdcChannel::ADC_VERSION);
85+
if (BoardAdc::RANK_VERSION == BoardAdc::INVALID_RANK) {
86+
return 0;
87+
}
88+
return HAL::Adc::get(BoardAdc::RANK_VERSION);
7089
}
7190

7291
static bool overvoltage() {
@@ -97,4 +116,4 @@ class CircuitPeriphery{
97116
}
98117
#endif
99118

100-
#endif // SRC_DRIVER_CIRCUIT_PERIPHERY_HPP_
119+
#endif // SRC_DRIVERS_BOARD_MONITOR_BOARD_MONITOR_HPP_

Src/modules/circuit_status/cyphal/circuit_status.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "circuit_status.hpp"
88
#include <algorithm>
99
#include "libcpnode/cyphal.hpp"
10-
#include "peripheral/adc/circuit_periphery.hpp"
10+
#include "drivers/board_monitor/board_monitor.hpp"
1111
#include "params.hpp"
1212

1313
REGISTER_MODULE(CyphalCircuitStatus)
@@ -42,19 +42,19 @@ void CyphalCircuitStatus::update_params() {
4242

4343
void CyphalCircuitStatus::spin_once() {
4444
if (voltage_5v_pub.isEnabled()) {
45-
voltage_5v_pub.msg.volt = CircuitPeriphery::voltage_5v();
45+
voltage_5v_pub.msg.volt = BoardMonitor::voltage_5v();
4646
voltage_5v_pub.publish();
4747
}
4848

4949
if (voltage_vin_pub.isEnabled()) {
50-
voltage_vin_pub.msg.volt = CircuitPeriphery::voltage_vin();
50+
voltage_vin_pub.msg.volt = BoardMonitor::voltage_vin();
5151
voltage_vin_pub.publish();
5252
}
5353

5454
if (temperature_pub.isEnabled()) {
55-
temperature_pub.msg.kelvin = CircuitPeriphery::temperature();
55+
temperature_pub.msg.kelvin = BoardMonitor::temperature();
5656
temperature_pub.publish();
5757
}
5858

59-
set_health(CircuitPeriphery::is_failure() ? Status::MINOR_FAILURE : Status::OK);
59+
set_health(BoardMonitor::is_failure() ? Status::MINOR_FAILURE : Status::OK);
6060
}

Src/modules/circuit_status/dronecan/circuit_status.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "circuit_status.hpp"
99
#include "params.hpp"
10-
#include "peripheral/adc/circuit_periphery.hpp"
10+
#include "drivers/board_monitor/board_monitor.hpp"
1111

1212
REGISTER_MODULE(DronecanCircuitStatus)
1313

@@ -24,26 +24,26 @@ void DronecanCircuitStatus::spin_once() {
2424
if (bitmask & static_cast<uint8_t>(Bitmask::ENABLE_DEV_TEMPERATURE_PUB)) {
2525
dev_temperature.msg = {
2626
.device_id = node_id,
27-
.temperature = static_cast<float>(CircuitPeriphery::temperature()),
27+
.temperature = static_cast<float>(BoardMonitor::temperature()),
2828
.error_flags = TemperatureErrorFlags_t(0),
2929
};
3030
dev_temperature.publish();
3131
}
3232

3333
uint16_t error_flags = 0;
34-
if (CircuitPeriphery::overvoltage()) {
34+
if (BoardMonitor::overvoltage()) {
3535
error_flags |= (uint16_t)ERROR_FLAG_OVERVOLTAGE;
3636
if (circuit_status.msg.error_flags == 0) {
3737
logger.log_warn("overvoltage");
3838
}
3939
}
40-
if (CircuitPeriphery::undervoltage()) {
40+
if (BoardMonitor::undervoltage()) {
4141
error_flags |= (uint16_t)ERROR_FLAG_UNDERVOLTAGE;
4242
if (circuit_status.msg.error_flags == 0) {
4343
logger.log_warn("undervoltage");
4444
}
4545
}
46-
if (CircuitPeriphery::overcurrent()) {
46+
if (BoardMonitor::overcurrent()) {
4747
error_flags |= (uint16_t)ERROR_FLAG_OVERCURRENT;
4848
if (circuit_status.msg.error_flags == 0) {
4949
logger.log_warn("overcurrent");
@@ -53,8 +53,8 @@ void DronecanCircuitStatus::spin_once() {
5353
if (bitmask & static_cast<uint8_t>(Bitmask::ENABLE_5V_PUB)) {
5454
circuit_status.msg = {
5555
.circuit_id = static_cast<uint16_t>(node_id * 10),
56-
.voltage = CircuitPeriphery::voltage_5v(),
57-
.current = CircuitPeriphery::current(),
56+
.voltage = BoardMonitor::voltage_5v(),
57+
.current = BoardMonitor::current(),
5858
.error_flags = static_cast<CircuitStatusErrorFlags_t>(error_flags),
5959
};
6060
circuit_status.publish();
@@ -63,15 +63,15 @@ void DronecanCircuitStatus::spin_once() {
6363
if (bitmask & static_cast<uint8_t>(Bitmask::ENABLE_VIN_PUB)) {
6464
circuit_status.msg = {
6565
.circuit_id = static_cast<uint16_t>(node_id * 10 + 1),
66-
.voltage = CircuitPeriphery::voltage_vin(),
67-
.current = CircuitPeriphery::current(),
66+
.voltage = BoardMonitor::voltage_vin(),
67+
.current = BoardMonitor::current(),
6868
.error_flags = static_cast<CircuitStatusErrorFlags_t>(error_flags),
6969
};
7070
circuit_status.publish();
7171
}
7272

7373
if (bitmask & static_cast<uint8_t>(Bitmask::ENABLE_HW_CHECKS)) {
74-
set_health(CircuitPeriphery::is_failure() ? Status::MINOR_FAILURE : Status::OK);
74+
set_health(BoardMonitor::is_failure() ? Status::MINOR_FAILURE : Status::OK);
7575
} else {
7676
set_health(Status::OK);
7777
}

0 commit comments

Comments
 (0)