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.
Dependents: backdrive backdrive_3
encoder.cpp@2:4bb7c8730d91, 2018-07-22 (annotated)
- Committer:
- takenowa
- Date:
- Sun Jul 22 00:15:08 2018 +0000
- Revision:
- 2:4bb7c8730d91
- Parent:
- 1:c4643cba43a9
- Child:
- 3:6dce9d79da13
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takenowa | 0:4b99060621fd | 1 | #include "encoder.h" |
takenowa | 0:4b99060621fd | 2 | |
takenowa | 2:4bb7c8730d91 | 3 | Encoder::Encoder(PinName a_phase , PinName b_phase ) : aPhase(a_phase) , bPhase(b_phase) {} |
takenowa | 0:4b99060621fd | 4 | |
takenowa | 0:4b99060621fd | 5 | void Encoder::calState(){ |
takenowa | 0:4b99060621fd | 6 | //パルスの差分を更新 |
takenowa | 1:c4643cba43a9 | 7 | count[DELTA][ONE_BEFORE] = count[NORMAL][ONE_BEFORE] - count[NORMAL][TWO_BEFORE]; |
takenowa | 2:4bb7c8730d91 | 8 | count[DELTA][CURRENT] = count[NORMAL][CURRENT] - count[NORMAL][ONE_BEFORE]; |
takenowa | 0:4b99060621fd | 9 | |
takenowa | 2:4bb7c8730d91 | 10 | distance = count[NORMAL][CURRENT] * circle / ppr; |
takenowa | 2:4bb7c8730d91 | 11 | rpm = (count[DELTA][CURRENT]/ppr) * 60 / dt; |
takenowa | 2:4bb7c8730d91 | 12 | velocity = (count[DELTA][CURRENT]/ppr)*circle/dt; |
takenowa | 2:4bb7c8730d91 | 13 | acceleration = (count[DELTA][CURRENT]-count[DELTA][ONE_BEFORE])/(dt_square*ppr) * circle; |
takenowa | 2:4bb7c8730d91 | 14 | omega = (2*PI*count[DELTA][CURRENT])/(ppr*dt); |
takenowa | 2:4bb7c8730d91 | 15 | delta_distance = (count[DELTA][CURRENT]/ppr)*circle; |
takenowa | 0:4b99060621fd | 16 | //1つ前・2つ前のデータを更新 |
takenowa | 0:4b99060621fd | 17 | count[NORMAL][TWO_BEFORE] = count[NORMAL][ONE_BEFORE]; |
takenowa | 2:4bb7c8730d91 | 18 | count[NORMAL][ONE_BEFORE] = count[NORMAL][CURRENT]; |
takenowa | 0:4b99060621fd | 19 | } |
takenowa | 0:4b99060621fd | 20 | |
takenowa | 0:4b99060621fd | 21 | float Encoder::getState(int ch){ |
takenowa | 0:4b99060621fd | 22 | float output; |
takenowa | 0:4b99060621fd | 23 | switch(ch){ |
takenowa | 0:4b99060621fd | 24 | case DISTANCE: |
takenowa | 0:4b99060621fd | 25 | output = distance; |
takenowa | 0:4b99060621fd | 26 | break; |
takenowa | 0:4b99060621fd | 27 | case RPM: |
takenowa | 0:4b99060621fd | 28 | distance = rpm; |
takenowa | 0:4b99060621fd | 29 | break; |
takenowa | 0:4b99060621fd | 30 | case VELOCITY: |
takenowa | 0:4b99060621fd | 31 | output = velocity; |
takenowa | 0:4b99060621fd | 32 | break; |
takenowa | 0:4b99060621fd | 33 | case ACCELERATION: |
takenowa | 0:4b99060621fd | 34 | output = acceleration; |
takenowa | 0:4b99060621fd | 35 | break; |
takenowa | 0:4b99060621fd | 36 | case OMEGA: |
takenowa | 0:4b99060621fd | 37 | output = omega; |
takenowa | 0:4b99060621fd | 38 | break; |
takenowa | 0:4b99060621fd | 39 | case DELTA_DISTANCE: |
takenowa | 0:4b99060621fd | 40 | output = delta_distance; |
takenowa | 0:4b99060621fd | 41 | break; |
takenowa | 0:4b99060621fd | 42 | } |
takenowa | 0:4b99060621fd | 43 | return output; |
takenowa | 0:4b99060621fd | 44 | } |
takenowa | 0:4b99060621fd | 45 | |
takenowa | 0:4b99060621fd | 46 | void Encoder::reset(){ |
takenowa | 2:4bb7c8730d91 | 47 | for(int i=CURRENT;i<BEFORE_NUMBER;i++){ |
takenowa | 0:4b99060621fd | 48 | count[NORMAL][i] = 0; |
takenowa | 0:4b99060621fd | 49 | count[DELTA][i] = 0; |
takenowa | 0:4b99060621fd | 50 | } |
takenowa | 0:4b99060621fd | 51 | } |
takenowa | 0:4b99060621fd | 52 | |
takenowa | 0:4b99060621fd | 53 | void Encoder::read(){ |
takenowa | 0:4b99060621fd | 54 | if(bPhase){ |
takenowa | 2:4bb7c8730d91 | 55 | count[NORMAL][CURRENT]++; |
takenowa | 0:4b99060621fd | 56 | } else { |
takenowa | 2:4bb7c8730d91 | 57 | count[NORMAL][CURRENT]--; |
takenowa | 0:4b99060621fd | 58 | } |
takenowa | 0:4b99060621fd | 59 | } |
takenowa | 0:4b99060621fd | 60 | |
takenowa | 0:4b99060621fd | 61 | void Encoder::init(float _ppr, float _radius,float _dt){ |
takenowa | 0:4b99060621fd | 62 | aPhase.rise(this,&Encoder::read); |
takenowa | 0:4b99060621fd | 63 | ppr = _ppr; |
takenowa | 0:4b99060621fd | 64 | dt = _dt; |
takenowa | 0:4b99060621fd | 65 | dt_square = dt*dt; |
takenowa | 0:4b99060621fd | 66 | circle = PI*2.0f*_radius; |
takenowa | 0:4b99060621fd | 67 | radius = _radius; |
takenowa | 0:4b99060621fd | 68 | } |