Skip to content

Commit

Permalink
feat: adjust_duty_cycle() の state の範囲チェックを追加 #186
Browse files Browse the repository at this point in the history
  • Loading branch information
grssbl authored Aug 15, 2023
1 parent 2026330 commit d5a734d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/spirit/include/adjust_duty_cycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,31 @@ namespace spirit {
* @note
* Motor::State::Coast, Motor::State::Brake は必ずデューティー比を0として扱う
*/
void adjust_duty_cycle(spirit::Motor::State target_state, float target_duty_cycle, float max_rise_delta,
float max_fall_delta, spirit::Motor::State current_state, float current_duty_cycle,
spirit::Motor::State& next_state, float& next_duty_cycle)
void adjust_duty_cycle(Motor::State target_state, float target_duty_cycle, float max_rise_delta, float max_fall_delta,
Motor::State current_state, float current_duty_cycle, Motor::State& next_state,
float& next_duty_cycle)
{
/**
* @brief stateの範囲チェック
* @param type チェックしたい回転方向
*/
auto within_state = [](Motor::State type) {
switch (type) {
case Motor::State::Coast:
case Motor::State::CW:
case Motor::State::CCW:
case Motor::State::Brake:
break;
default:
Error::get_instance().error(Error::Type::UnknownValue, 0, __FILE__, __func__, __LINE__,
"Unknown motor state (%d)", static_cast<uint32_t>(type));
return;
}
};

within_state(target_state);
within_state(current_state);

// 回転方向がCoast or Brakeの場合は現在のデューティー比を0として扱う
if ((target_state == Motor::State::Coast) || (target_state == Motor::State::Brake)) {
target_duty_cycle = 0.00F;
Expand Down
34 changes: 34 additions & 0 deletions tests/test_adjust_duty_cycle.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <gtest/gtest.h>

#include "Error.h"
#include "Motor.h"
#include "adjust_duty_cycle.h"

Expand Down Expand Up @@ -374,5 +375,38 @@ TEST(AdjustDutyCycle, Normal)
}
}
}
TEST(adjust_duty_cycle, AnomalyTest)
{
Motor::State next_state;
float next_duty_cycle;

// Error時に標準エラー出力に文字列が出力される
// 本当のエラー時にエラー出力させたいので、異常系のテスト中は標準エラー出力をキャプチャする
testing::internal::CaptureStderr();

Error& error = Error::get_instance();
EXPECT_EQ(error.get_status(), Error::Status::Normal);

/// @test target_stateが範囲外の時にエラーになることのテスト
error.reset();
EXPECT_EQ(error.get_status(), Error::Status::Normal);
adjust_duty_cycle(static_cast<Motor::State>(4), 0.00F, 0.50F, 0.50F, Motor::State::Coast, 0.00F, next_state,
next_duty_cycle);
EXPECT_EQ(error.get_status(), Error::Status::Error);

/// @test current_stateが範囲外の時にエラーになることのテスト
error.reset();
EXPECT_EQ(error.get_status(), Error::Status::Normal);
adjust_duty_cycle(Motor::State::Coast, 0.00F, 0.50F, 0.50F, static_cast<Motor::State>(4), 0.00F, next_state,
next_duty_cycle);
EXPECT_EQ(error.get_status(), Error::Status::Error);

/// @test target_stateとcurrent_stateが範囲外の時にエラーになることのテスト
error.reset();
EXPECT_EQ(error.get_status(), Error::Status::Normal);
adjust_duty_cycle(static_cast<Motor::State>(4), 0.00F, 0.50F, 0.50F, static_cast<Motor::State>(4), 0.00F,
next_state, next_duty_cycle);
EXPECT_EQ(error.get_status(), Error::Status::Error);
}

} // namespace

0 comments on commit d5a734d

Please sign in to comment.