Skip to content

Commit

Permalink
update & refactor: 全てのファイルのpwm値をintからdoubleに変更した.Motorはダミーファイルのため,公式が…
Browse files Browse the repository at this point in the history
…double型かは不安.

また,いくつかの変数,メソッド名を変更した.
  • Loading branch information
CHIHAYATAKU committed Jun 10, 2024
1 parent f36699d commit e16c348
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 77 deletions.
16 changes: 8 additions & 8 deletions module/API/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ double Controller::pwmOfLeftWheel = 0.0;
double Controller::pwmOfArm = 0.0;

// モータに設定するPWM値の制限
int Controller::limitPwmValue(const int value)
double Controller::limitPwmValue(const double inputPwm)
{
if(value > MOTOR_PWM_MAX) {
if(inputPwm > MOTOR_PWM_MAX) {
return MOTOR_PWM_MAX;
} else if(value < MOTOR_PWM_MIN) {
} else if(inputPwm < MOTOR_PWM_MIN) {
return MOTOR_PWM_MIN;
}
return value;
return inputPwm;
}

// 右モータにPWM値をセット
Expand Down Expand Up @@ -70,25 +70,25 @@ void Controller::setArmMotorPwm(const double pwm)
// アームのモータのPWM値をリセット
void Controller::resetArmMotorPwm()
{
pwmOfArm = 0;
pwmOfArm = 0.0;
armMotor.reset();
}

// アームのモータを停止する
void Controller::stopArmMotor()
{
pwmOfArm = 0;
pwmOfArm = 0.0;
armMotor.stop();
}

// 右タイヤのPWMを取得する
double Controller::getRightPwm()
double Controller::getRightMotorPwm()
{
return pwmOfRightWheel;
}

// 左タイヤのPWMを取得する
double Controller::getLeftPwm()
double Controller::getLeftMotorPwm()
{
return pwmOfLeftWheel;
}
12 changes: 6 additions & 6 deletions module/API/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
class Controller {
public:
/** PWM値の上限 */
static const int MOTOR_PWM_MAX = 100;
static constexpr double MOTOR_PWM_MAX = 100.0;

/** PWM値の下限 */
static const int MOTOR_PWM_MIN = -100;
static constexpr double MOTOR_PWM_MIN = -100.0;

/**
* コンストラクタ
Expand Down Expand Up @@ -60,13 +60,13 @@ class Controller {
* @brief 右タイヤのPWMを取得する
* @return 右タイヤのPWM
*/
static double getRightPwm();
static double getRightMotorPwm();

/**
* @brief 左タイヤのPWMを取得する
* @return 左タイヤのPWM
*/
static double getLeftPwm();
static double getLeftMotorPwm();

private:
ev3api::Motor rightWheel;
Expand All @@ -78,10 +78,10 @@ class Controller {

/**
* @brief モータに設定するPWM値の制限
* @param value 入力されたPWM値
* @param inputPwm 入力されたPWM値
* @return 制限されたPWM値
*/
int limitPwmValue(const int value);
double limitPwmValue(const double inputPwm);
};

#endif
20 changes: 10 additions & 10 deletions module/Calculator/SpeedCalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ SpeedCalculator::SpeedCalculator(double _targetSpeed)
rightPid(K_P, K_I, K_D, _targetSpeed),
leftPid(K_P, K_I, K_D, _targetSpeed)
{
rightPwm = Controller::getRightPwm();
leftPwm = Controller::getLeftPwm();
rightMotorPwm = Controller::getRightMotorPwm();
leftMotorPwm = Controller::getLeftMotorPwm();
int rightAngle = measurer.getRightCount();
int leftAngle = measurer.getLeftCount();
prevRightMileage = Mileage::calculateWheelMileage(rightAngle);
Expand All @@ -29,8 +29,8 @@ SpeedCalculator::SpeedCalculator(double _rightTargetSpeed, double _leftTargetSpe
rightPid(R_K_P, R_K_I, R_K_D, _rightTargetSpeed),
leftPid(R_K_P, R_K_I, R_K_D, _leftTargetSpeed)
{
rightPwm = Controller::getRightPwm();
leftPwm = Controller::getLeftPwm();
rightMotorPwm = Controller::getRightMotorPwm();
leftMotorPwm = Controller::getLeftMotorPwm();
int rightAngle = measurer.getRightCount();
int leftAngle = measurer.getLeftCount();
prevRightMileage = Mileage::calculateWheelMileage(rightAngle);
Expand All @@ -40,7 +40,7 @@ SpeedCalculator::SpeedCalculator(double _rightTargetSpeed, double _leftTargetSpe
prevLeftTime = currentTime;
}

double SpeedCalculator::calculateRightPwmFromTargetSpeed()
double SpeedCalculator::calculaterightMotorPwmFromTargetSpeed()
{
// 右タイヤの回転角度を取得
int rightAngle = measurer.getRightCount();
Expand All @@ -53,15 +53,15 @@ double SpeedCalculator::calculateRightPwmFromTargetSpeed()
// 右タイヤの走行速度を算出
double currentRightSpeed = calculateSpeed(diffRightMileage, diffRightTime);
// 走行速度に相当する右タイヤのPWM値を算出
rightPwm += rightPid.calculatePid(currentRightSpeed, diffRightTime);
rightMotorPwm += rightPid.calculatePid(currentRightSpeed, diffRightTime);
// メンバを更新
prevRightMileage = currentRightMileage;
prevRightTime = currentRightTime;

return rightPwm;
return rightMotorPwm;
}

double SpeedCalculator::calculateLeftPwmFromTargetSpeed()
double SpeedCalculator::calculateleftMotorPwmFromTargetSpeed()
{
// 左タイヤの回転角度を取得
int leftAngle = measurer.getLeftCount();
Expand All @@ -74,12 +74,12 @@ double SpeedCalculator::calculateLeftPwmFromTargetSpeed()
// 左タイヤの走行速度を算出
double currentLeftSpeed = calculateSpeed(diffLeftMileage, diffLeftTime);
// 走行速度に相当する左タイヤのPWM値を算出
leftPwm += leftPid.calculatePid(currentLeftSpeed, diffLeftTime);
leftMotorPwm += leftPid.calculatePid(currentLeftSpeed, diffLeftTime);
// メンバを更新
prevLeftMileage = currentLeftMileage;
prevLeftTime = currentLeftTime;

return leftPwm;
return leftMotorPwm;
}

double SpeedCalculator::calculateSpeed(double diffMileage, double diffTime)
Expand Down
8 changes: 4 additions & 4 deletions module/Calculator/SpeedCalculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class SpeedCalculator {
* @brief 目標とする走行速度に相当する右車輪のPWM値を算出する
* @return 走行速度に相当する右タイヤのPWM値
*/
double calculateRightPwmFromTargetSpeed();
double calculaterightMotorPwmFromTargetSpeed();

/**
* @brief 目標とする走行速度に相当する左車輪のPWM値を算出する
* @return 走行速度に相当する左タイヤのPWM値
*/
double calculateLeftPwmFromTargetSpeed();
double calculateleftMotorPwmFromTargetSpeed();

private:
const double rightTargetSpeed;
Expand All @@ -47,8 +47,8 @@ class SpeedCalculator {
Pid leftPid;
Timer timer;
Measurer measurer;
double rightPwm;
double leftPwm;
double rightMotorPwm;
double leftMotorPwm;
double prevRightMileage;
double prevLeftMileage;
int prevRightTime;
Expand Down
34 changes: 17 additions & 17 deletions test/ControllerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace etrobocon2024_test {
{
Measurer measurer;
Controller controller;
const int pwm = 50;
const double pwm = 50.0;
int initCount = measurer.getRightCount();
controller.setRightMotorPwm(pwm);
int currentCount = measurer.getRightCount();
Expand All @@ -29,7 +29,7 @@ namespace etrobocon2024_test {
{
Measurer measurer;
Controller controller;
const int pwm = -50;
const double pwm = -50.0;
int initCount = measurer.getRightCount();
controller.setRightMotorPwm(pwm);
int currentCount = measurer.getRightCount();
Expand All @@ -42,7 +42,7 @@ namespace etrobocon2024_test {
{
Measurer measurer;
Controller controller;
const int pwm = 50;
const double pwm = 50.0;
int initCount = measurer.getLeftCount();
controller.setLeftMotorPwm(pwm);
int currentCount = measurer.getLeftCount();
Expand All @@ -55,7 +55,7 @@ namespace etrobocon2024_test {
{
Measurer measurer;
Controller controller;
const int pwm = -50;
const double pwm = -50.0;
int initCount = measurer.getLeftCount();
controller.setLeftMotorPwm(pwm);
int currentCount = measurer.getLeftCount();
Expand All @@ -68,7 +68,7 @@ namespace etrobocon2024_test {
{
Measurer measurer;
Controller controller;
const int pwm = -150;
const double pwm = -150.0;
controller.setLeftMotorPwm(Controller::MOTOR_PWM_MIN);
int minCount = measurer.getLeftCount();
controller.resetLeftMotorPwm();
Expand All @@ -91,7 +91,7 @@ namespace etrobocon2024_test {
{
Measurer measurer;
Controller controller;
const int pwm = 50;
const double pwm = 50.0;
int initCount = measurer.getArmMotorCount();
controller.setArmMotorPwm(pwm);
int currentCount = measurer.getArmMotorCount();
Expand All @@ -108,31 +108,31 @@ namespace etrobocon2024_test {
}

// 右タイヤのPWM値を取得できるかのテスト
TEST(ControllerTest, getRightPwm)
TEST(ControllerTest, getRightMotorPwm)
{
Controller controller;
double expected = 0;
double actual = controller.getRightPwm();
double expected = 0.0;
double actual = controller.getRightMotorPwm();
EXPECT_DOUBLE_EQ(expected, actual);
int pwm = 80;
double pwm = 90.2;
controller.setRightMotorPwm(pwm);
expected = 80;
actual = controller.getRightPwm();
expected = 90.2;
actual = controller.getRightMotorPwm();
EXPECT_DOUBLE_EQ(expected, actual);
controller.resetRightMotorPwm();
}

// 左タイヤのPWM値を取得できるかのテスト
TEST(ControllerTest, getLeftPwm)
TEST(ControllerTest, getleftMotorPwm)
{
Controller controller;
double expected = 0;
double actual = controller.getLeftPwm();
double actual = controller.getLeftMotorPwm();
EXPECT_DOUBLE_EQ(expected, actual);
int pwm = 80;
double pwm = 80.1;
controller.setLeftMotorPwm(pwm);
expected = 80;
actual = controller.getLeftPwm();
expected = 80.1;
actual = controller.getLeftMotorPwm();
EXPECT_DOUBLE_EQ(expected, actual);
controller.resetLeftMotorPwm();
}
Expand Down
Loading

0 comments on commit e16c348

Please sign in to comment.