Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed ros_lib_kinetic
Diff: library/RotaryInc.cpp
- Revision:
- 1:17051435cfc5
- Parent:
- 0:a8a56075e947
--- a/library/RotaryInc.cpp Sat May 18 12:09:43 2019 +0000 +++ b/library/RotaryInc.cpp Tue Jul 09 07:56:07 2019 +0000 @@ -1,127 +1,139 @@ #include "RotaryInc.hpp" -RotaryInc::RotaryInc(PinName pinA, PinName pinB,int mode):mode(mode){ - measur = false; +RotaryInc::RotaryInc(PinName pinA, PinName pinB,int mode):mode_(mode){ + measur_ = false; init(pinA,pinB); } -RotaryInc::RotaryInc(PinName pinA,PinName pinB,double circumference,int Resolution,int mode) - :mode(mode),Resolution(Resolution),circumference(circumference){ - measur = true; - zero(); +RotaryInc::RotaryInc(PinName pinA,PinName pinB,double circumference,int resolution,int mode) + :mode_(mode),resolution_(resolution),circumference_(circumference){ + measur_ = true; init(pinA,pinB); } void RotaryInc::init(PinName pinA,PinName pinB){ - A = new InterruptIn(pinA,PullUp); - B = new InterruptIn(pinB,PullUp); - A->rise(callback(this,&RotaryInc::riseA)); + reset(); + A_ = new InterruptIn(pinA,PullUp); + B_ = new InterruptIn(pinB,PullUp); + A_->rise(callback(this,&RotaryInc::riseA)); - if(mode == 2){ - A->fall(callback(this,&RotaryInc::fallA)); - }else if(mode == 4){ - A->fall(callback(this,&RotaryInc::fallA)); - B->rise(callback(this,&RotaryInc::riseB)); - B->fall(callback(this,&RotaryInc::fallB)); + if(mode_ == 2){ + A_->fall(callback(this,&RotaryInc::fallA)); + }else if(mode_ == 4){ + A_->fall(callback(this,&RotaryInc::fallA)); + B_->rise(callback(this,&RotaryInc::riseB)); + B_->fall(callback(this,&RotaryInc::fallB)); }else{ - mode = 1; + mode_ = 1; } } void RotaryInc::zero(){ - time.stop(); - time.reset(); - startflag = false; - flag = false; - last[0] = pulse; - speed = 0; - count = 0; - sum = 0; - now = 0; + timer_.stop(); + timer_.reset(); + start_frag_ = false; + flag_ = false; + last_[0] = pulse_; + speed_ = 0; + count_ = 0; + sum_ = 0; + now_ = 0; } void RotaryInc::calcu(){ - if(!startflag){ - time.start(); - startflag = true; - last[0] = pulse; - pre_t[0] = 0; - count = 1; - }else if(flag){ - now = time.read(); - time.reset(); - sum -= pre_t[count]; - pre_t[count] = now; - sum += now; - speed = (double)(pulse - last[count]) / sum; - last[count] = pulse; - if(count < 19){ - count++; + if(!start_frag_){ + timer_.start(); + start_frag_ = true; + last_[0] = pulse_; + pre_t_[0] = 0; + count_ = 1; + }else if(flag_){ + now_ = timer_.read(); + timer_.reset(); + sum_ -= pre_t_[count_]; + pre_t_[count_] = now_; + sum_ += now_; + speed_ = (double)(pulse_ - last_[count_]) / sum_; + last_[count_] = pulse_; + if(count_ < 19){ + count_++; }else{ - count = 0; + count_ = 0; } }else{ - now = time.read(); - time.reset(); - pre_t[count] = now; - sum += now; - speed = (double)(pulse - last[0]) / sum; - last[count] = pulse; - count++; - if(count > 19){ - count = 0; - flag = true; + now_ = timer_.read(); + timer_.reset(); + pre_t_[count_] = now_; + sum_ += now_; + speed_ = (double)(pulse_ - last_[0]) / sum_; + last_[count_] = pulse_; + count_++; + if(count_ > 19){ + count_ = 0; + flag_ = true; } } } void RotaryInc::riseA(){ - B->read() ? pulse-- : pulse++; - if(measur)calcu(); + B_->read() ? pulse_-- : pulse_++; + if(measur_){ + calcu(); + } } void RotaryInc::fallA(){ - B->read() ? pulse++ : pulse--; - if(measur)calcu(); + B_->read() ? pulse_++ : pulse_--; + if(measur_){ + calcu(); + } } void RotaryInc::riseB(){ - A->read() ? pulse++ : pulse--; - if(measur)calcu(); + A_->read() ? pulse_++ : pulse_--; + if(measur_){ + calcu(); + } } void RotaryInc::fallB(){ - A->read() ? pulse-- : pulse++; - if(measur)calcu(); + A_->read() ? pulse_-- : pulse_++; + if(measur_){ + calcu(); + } } long long RotaryInc::get(){ - return pulse; + return pulse_; } double RotaryInc::getSpeed(){ - if(!measur)return 0; - if(time.read_ms() > 150){ + if(!measur_){ + return 0; + } + if(timer_.read_ms() > 150){ zero(); } - return speed / Resolution / mode * circumference; + return speed_ / resolution_ / mode_ * circumference_; } int RotaryInc::diff(){ - int diff = pulse - prev; - prev = pulse; + int diff = pulse_ - prev_; + prev_ = pulse_; return diff; } void RotaryInc::reset(){ - pulse = 0; - prev = 0; - if(measur)zero(); + pulse_ = 0; + prev_ = 0; + if(measur_){ + zero(); + } } RotaryInc::~RotaryInc(){ - A->disable_irq(); - B->disable_irq(); - delete A; - delete B; + A_->disable_irq(); + B_->disable_irq(); + delete A_; + delete B_; } \ No newline at end of file