Skip to content

Commit

Permalink
feat: maximum_change_duty_cycle() と各関数の引数の範囲確認を追加 (#244)
Browse files Browse the repository at this point in the history
* feat: maximum_change_duty_cycle() と各関数の引数の範囲確認を追加

* refactor: duty_cycle() で範囲外を指定した時に警告が出ることを確認
  • Loading branch information
yutotnh authored Jul 25, 2023
1 parent 5d0b1c6 commit 48865a4
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 74 deletions.
16 changes: 10 additions & 6 deletions src/spirit/include/Motor.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,26 @@ class Motor {
* @brief デューティ比の変化具合を設定する ChangeLevelTarget
* @param target 上昇時: ChangeLevelTarget::Rise 、下降時: ChangeLevelTarget::Fall
* @param level ChangeLevel::Manual, ChangeLevel::OFF, ChangeLevel::Low, ChangeLevel::Middle, ChangeLevel::High, ChangeLevel::Max のいずれかの設定したい変化具合
* @attention この関数を実行すると、それより前に Motor::maximum_change_duty_cycle(ChangeLevelTarget, float) で設定した値は無効になり、
* Motor::get_maximum_change_duty_cycle(ChangeLevelTarget) は 設定した ChangeLevel に対応した値を返す
*/
void change_level(ChangeLevelTarget target, ChangeLevel level);

/**
* @brief デューティ比の変化具合を設定する ChangeLevelTarget
* @brief デューティ比の変化具合を返す
* @param target 上昇時: ChangeLevelTarget::Rise 、下降時: ChangeLevelTarget::Fall
* @param duty_cycle 単位時間当たりの最大変化デューティ比
* @return 設定したデューティ比の変化具合
*/
void change_level(ChangeLevelTarget target, float duty_cycle);
ChangeLevel get_change_level(ChangeLevelTarget target) const;

/**
* @brief デューティ比の変化具合を返す
* @brief 単位時間当たりの最大変化デューティ比を設定する
* @param target 上昇時: ChangeLevelTarget::Rise 、下降時: ChangeLevelTarget::Fall
* @return 設定したデューティ比の変化具合
* @param duty_cycle 単位時間当たりの最大変化デューティ比
* @attention この関数を実行すると、それより前に Motor::change_level(ChangeLevelTarget, ChangeLevel) で設定した値は無効になり、
* Motor::get_change_level(ChangeLevelTarget) は ChangeLevel::Manual を返す
*/
ChangeLevel get_change_level(ChangeLevelTarget target) const;
void maximum_change_duty_cycle(ChangeLevelTarget target, float duty_cycle);

/**
* @brief 単位時間当たりの最大変化デューティ比を返す
Expand Down
103 changes: 62 additions & 41 deletions src/spirit/src/Motor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ float Motor::get_duty_cycle() const
void Motor::speed(const float rps)
{
if (rps < 0.00F) {
_speed = rps;

Error::get_instance().warning(Error::Type::InvalidValue, 0, __FILE__, __func__, __LINE__,
"RPS (%g) is less than 0.00, so it will be 0.00", rps);
} else {
_speed = rps;
Error::get_instance().error(Error::Type::InvalidValue, 0, __FILE__, __func__, __LINE__,
"RPS (%g) is less than 0.00", rps);
return;
}

_speed = rps;
}

float Motor::get_speed() const
Expand All @@ -65,7 +64,23 @@ float Motor::get_speed() const

void Motor::pid_gain_factor(const float kp, const float ki, const float kd)
{
/// @todo kp, ki, kdの範囲チェック
if (kp < 0.00F) {
Error::get_instance().error(Error::Type::InvalidValue, 0, __FILE__, __func__, __LINE__,
"kp (%g) is less than 0.00", kp);
return;
}

if (ki < 0.00F) {
Error::get_instance().error(Error::Type::InvalidValue, 0, __FILE__, __func__, __LINE__,
"ki (%g) is less than 0.00", ki);
return;
}

if (kd < 0.00F) {
Error::get_instance().error(Error::Type::InvalidValue, 0, __FILE__, __func__, __LINE__,
"kd (%g) is less than 0.00", kd);
return;
}

_kp = kp;
_ki = ki;
Expand Down Expand Up @@ -134,13 +149,32 @@ void Motor::change_level(const ChangeLevelTarget target, const ChangeLevel level
}
}

void Motor::change_level(const ChangeLevelTarget target, const float duty_cycle)
Motor::ChangeLevel Motor::get_change_level(const ChangeLevelTarget target) const
{
switch (target) {
case ChangeLevelTarget::Rise:
return _rise_change_level;
case ChangeLevelTarget::Fall:
return _fall_change_level;

default:
Error::get_instance().error(Error::Type::UnknownValue, 0, __FILE__, __func__, __LINE__,
"Unknown motor change level target (%d)", static_cast<uint32_t>(target));
return ChangeLevel::OFF;
}
}

void Motor::maximum_change_duty_cycle(const ChangeLevelTarget target, float duty_cycle)
{
/// @todo duty_cycleは最大でも200%しか変化しないはずなので、範囲チェックをする
if (2.00F < duty_cycle) {
Error::get_instance().error(Error::Type::InvalidValue, 0, __FILE__, __func__, __LINE__,
"Change duty cycle (%g) is greater than 2.00", duty_cycle);
return;
}

if (duty_cycle < minimum_maximum_change_duty_cycle) {
Error::get_instance().error(Error::Type::InvalidValue, 0, __FILE__, __func__, __LINE__,
"Duty cycle (%g) is less than minimum duty cycle (%g)", duty_cycle,
"Change duty cycle (%g) is less than minimum duty cycle (%g)", duty_cycle,
minimum_maximum_change_duty_cycle);
return;
}
Expand All @@ -161,21 +195,6 @@ void Motor::change_level(const ChangeLevelTarget target, const float duty_cycle)
}
}

Motor::ChangeLevel Motor::get_change_level(const ChangeLevelTarget target) const
{
switch (target) {
case ChangeLevelTarget::Rise:
return _rise_change_level;
case ChangeLevelTarget::Fall:
return _fall_change_level;

default:
Error::get_instance().error(Error::Type::UnknownValue, 0, __FILE__, __func__, __LINE__,
"Unknown motor change level target (%d)", static_cast<uint32_t>(target));
return ChangeLevel::OFF;
}
}

float Motor::get_maximum_change_duty_cycle(ChangeLevelTarget target) const
{
ChangeLevel level = get_change_level(target);
Expand Down Expand Up @@ -225,22 +244,19 @@ float Motor::get_maximum_change_duty_cycle(ChangeLevelTarget target) const
void Motor::pulse_period(const float seconds)
{
if (max_pulse_period < seconds) {
_pulse_period = max_pulse_period;

Error::get_instance().warning(
Error::Type::InvalidValue, 0, __FILE__, __func__, __LINE__,
"Pulse period (%g) is greater than max pulse period (%g), so it will be max pulse period (%g)", seconds,
max_pulse_period, max_pulse_period);
} else if (seconds < min_pulse_period) {
_pulse_period = min_pulse_period;

Error::get_instance().warning(
Error::Type::InvalidValue, 0, __FILE__, __func__, __LINE__,
"Pulse period (%g) is less than min pulse period (%g), so it will be min pulse period (%g)", seconds,
min_pulse_period, min_pulse_period);
} else {
_pulse_period = seconds;
Error::get_instance().error(Error::Type::InvalidValue, 0, __FILE__, __func__, __LINE__,
"Pulse period (%g) is greater than max pulse period (%g)", seconds,
max_pulse_period);
return;
}

if (seconds < min_pulse_period) {
Error::get_instance().error(Error::Type::InvalidValue, 0, __FILE__, __func__, __LINE__,
"Pulse period (%g) is less than min pulse period (%g)", seconds, min_pulse_period);
return;
}

_pulse_period = seconds;
}

float Motor::get_pulse_period() const
Expand All @@ -250,7 +266,12 @@ float Motor::get_pulse_period() const

void Motor::release_time(const float seconds)
{
/// @todo マイナス秒はありえないので、エラーにする
if (seconds < 0.00F) {
Error::get_instance().error(Error::Type::InvalidValue, 0, __FILE__, __func__, __LINE__,
"Seconds (%g) is less than 0.00", seconds);
return;
}

_release_time = seconds;
}

Expand Down
Loading

0 comments on commit 48865a4

Please sign in to comment.