Simple motor controller library, using DIR, PWM, nSLP pin like pololu.
Diff: MotorControler.cpp
- Revision:
- 5:b46d1e179eb7
- Parent:
- 4:a60052db674c
- Child:
- 6:7955a025adee
diff -r a60052db674c -r b46d1e179eb7 MotorControler.cpp --- a/MotorControler.cpp Tue Feb 02 14:05:05 2021 +0000 +++ b/MotorControler.cpp Tue Feb 02 14:53:38 2021 +0000 @@ -4,9 +4,51 @@ { disableDriver(); reverse_direction_ = 0; + current_speed_ = 0; md_type_ = md_type; } +float MotorControler::operator = (float speed) { + setSpeed(speed); + return current_speed_; +} + +float MotorControler::operator + (float speed) { + return current_speed_ + speed; +} + +float MotorControler::operator += (float speed) { + setSpeed(current_speed_ + speed); + return current_speed_; +} + +float MotorControler::operator - (float speed) { + return current_speed_ - speed; +} + +float MotorControler::operator -= (float speed) { + setSpeed(current_speed_ - speed); + return current_speed_; +} + +float MotorControler::operator * (float val) { + return current_speed_ * val; +} + +float MotorControler::operator *= (float val) { + setSpeed(current_speed_ * val); + return current_speed_; +} + +float MotorControler::operator / (float val) { + return current_speed_ / val; +} + +float MotorControler::operator /= (float val) { + setSpeed(current_speed_ / val); + return current_speed_; +} + void MotorControler::enableDriver() { if(md_type_ == POLOLU) @@ -30,14 +72,17 @@ void MotorControler::setSpeed(float speed) { uint8_t reverse = 0; + + // 最大デューティ比で制限 + if(speed < -1) speed = -1; + else if(speed > 1) speed = 1; + current_speed_ = speed; + if (speed < 0) { speed = -speed; // Make speed a positive quantity reverse = 1; // Preserve the direction } - // 最大デューティ比で制限 - if (speed > 1) - speed = 1; PWM_ = speed; if (reverse ^ FLIP_MOTOR_DIR ^ reverse_direction_)