Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 11 additions & 5 deletions hydrv_thruster/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)

if(MCU_FAMILY STREQUAL "F4")
add_library(${LIBRARY_NAME} INTERFACE)
target_link_libraries(${LIBRARY_NAME} INTERFACE HydrolibCommon)
target_link_libraries(${LIBRARY_NAME} INTERFACE HydrodriversGPIO)
target_link_libraries(${LIBRARY_NAME} INTERFACE HydrodriversTimerLow)
target_link_libraries(${LIBRARY_NAME} INTERFACE CMSIS)
target_link_libraries(${LIBRARY_NAME}
INTERFACE
HydrolibCommon
HydrodriversGPIO
HydrodriversTimerLow
HydrolibThrustGenerator
HydrolibThrustersControl
CMSIS
)
target_include_directories(${LIBRARY_NAME} INTERFACE include)

add_subdirectory(example)

message(STATUS "${LIBRARY_NAME} target added for MCU_FAMILY: ${MCU_FAMILY}")
else()
message(WARNING "${LIBRARY_NAME} target is not supported for MCU_FAMILY: ${MCU_FAMILY}")
Expand Down
1 change: 1 addition & 0 deletions hydrv_thruster/example/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "hydrv_tim_low.hpp"

#include "hydrv_thruster.hpp"
#include "hydrv_thrusters_control.hpp"

extern "C"
{
Expand Down
28 changes: 14 additions & 14 deletions hydrv_thruster/include/hydrv_thruster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,36 @@ class Thruster
static constexpr unsigned SpeedToPWM_(int speed);

private:
int speed;
unsigned tim_channel_;
timer::TimerLow &tim_;
GPIO::GPIOLow &tim_pin_;

timer::TimerLow &tim;
unsigned tim_channel;
GPIO::GPIOLow &tim_pin;
int speed_;
};

inline constexpr Thruster::Thruster(unsigned thruster_tim_channel,
hydrv::timer::TimerLow &thruster_tim,
hydrv::GPIO::GPIOLow &thruster_tim_pin)
: tim_channel(thruster_tim_channel), tim(thruster_tim),
tim_pin(thruster_tim_pin),
speed(speed_null)
: tim_channel_(thruster_tim_channel), tim_(thruster_tim),
tim_pin_(thruster_tim_pin),
speed_(speed_null)
{
}

inline void Thruster::Init()
{
tim.Init();
tim.ConfigurePWM(tim_channel, tim_pin);
tim.SetCaptureCompare(tim_channel, SpeedToPWM_(speed_null));
tim.StartTimer();
tim_.Init();
tim_.ConfigurePWM(tim_channel_, tim_pin_);
tim_.SetCaptureCompare(tim_channel_, SpeedToPWM_(speed_null));
tim_.StartTimer();
}

inline hydrolib_ReturnCode Thruster::SetSpeed(int thruster_speed)
{
if (thruster_speed <= max_speed && thruster_speed >= -max_speed)
{
speed = thruster_speed;
tim.SetCaptureCompare(tim_channel, SpeedToPWM_(speed));
speed_ = thruster_speed;
tim_.SetCaptureCompare(tim_channel_, SpeedToPWM_(speed_));
return HYDROLIB_RETURN_OK;
}
else
Expand All @@ -73,7 +73,7 @@ inline hydrolib_ReturnCode Thruster::SetSpeed(int thruster_speed)
}
}

inline int Thruster::GetSpeed() { return speed; }
inline int Thruster::GetSpeed() { return speed_; }

inline constexpr unsigned Thruster::SpeedToPWM_(int speed)
{
Expand Down
77 changes: 77 additions & 0 deletions hydrv_thruster/include/hydrv_thrusters_control.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#pragma once

#include "hydrolib_thrust_generator.hpp"
#include "hydrolib_thrusters_control.hpp"
#include "hydrv_thruster.hpp"

namespace hydrv::thruster
{

template <unsigned THRUSTERS_COUNT>
class ThrusterControl
{

public:
constexpr ThrusterControl(
int *x_rotation_gain, int *y_rotation_gain, int *z_rotation_gain,
int *x_linear_gain, int *y_linear_gain, int *z_linear_gain,
int single_clamp, int sum_clamp,
unsigned thruster_tim_channel[THRUSTERS_COUNT],
hydrv::timer::TimerLow *thruster_tim[THRUSTERS_COUNT],
hydrv::GPIO::GPIOLow *thruster_tim_pin[THRUSTERS_COUNT]);

void Init();
void SetControl(
hydrolib::controlling::ThrustersControlData thruster_control_data);

private:
hydrv::thruster::Thruster thrusters_[THRUSTERS_COUNT];

hydrolib::controlling::ThrustGenerator<THRUSTERS_COUNT> thrust_generator_;
};

template <unsigned THRUSTERS_COUNT>
inline constexpr ThrusterControl<THRUSTERS_COUNT>::ThrusterControl(
int *x_rotation_gain, int *y_rotation_gain, int *z_rotation_gain,
int *x_linear_gain, int *y_linear_gain, int *z_linear_gain,
int single_clamp, int sum_clamp,
unsigned thruster_tim_channel[THRUSTERS_COUNT],
hydrv::timer::TimerLow *thruster_tim[THRUSTERS_COUNT],
hydrv::GPIO::GPIOLow *thruster_tim_pin[THRUSTERS_COUNT])
: thrust_generator_{*x_rotation_gain, *y_rotation_gain, *z_rotation_gain,
*x_linear_gain, *y_linear_gain, *z_linear_gain,
single_clamp, sum_clamp}
{
{
for (int i = 0; i < THRUSTERS_COUNT; i++)
{
//TODO: Check if we can do so
thrusters_[i](thruster_tim_channel[i], *thruster_tim[i],
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вот это вообще интересная штука, не знал, что так можно. Это точно компилируется?

*thruster_tim_pin[i]);
}
}
}

template <unsigned THRUSTERS_COUNT>
void ThrusterControl<THRUSTERS_COUNT>::Init()
{
for (int i = 0; i < THRUSTERS_COUNT; i++)
{
thrusters_[i].Init();
}
}

template <unsigned THRUSTERS_COUNT>
void ThrusterControl<THRUSTERS_COUNT>::SetControl(
hydrolib::controlling::ThrustersControlData thruster_control_data)
{
int dest[THRUSTERS_COUNT];
thrust_generator_.ProcessWithFeedback(thruster_control_data, *dest);

for (int i = 0; i < THRUSTERS_COUNT; i++)
{
thrusters_[i].SetSpeed(dest[i]);
}
}

} // namespace hydrv::thruster