インクリメント方式エンコーダ用ライブラリ

Dependents:   RobotControl_Sample2022 sotsuken_mecha

初期化 IncEncoder name(pinA,pinB,分解能); 分解能 : x2_resolution x4_resolution

リセット name.reset();

Files at this revision

API Documentation at this revision

Comitter:
koki_konishi
Date:
Fri Jul 01 06:56:51 2022 +0000
Commit message:
encoder

Changed in this revision

IncEncoder.cpp Show annotated file Show diff for this revision Revisions of this file
IncEncoder.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r c40c29a063c0 IncEncoder.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IncEncoder.cpp	Fri Jul 01 06:56:51 2022 +0000
@@ -0,0 +1,42 @@
+#include "IncEncoder.h"
+#include "mbed.h"
+
+IncEncoder::IncEncoder(PinName ENC_A,PinName ENC_B,Encoding encoding) : ENC_A_(ENC_A),ENC_B_(ENC_B){
+    pulses = 0;
+    
+    ENC_A_.rise(this, &IncEncoder::A_RISE);
+    ENC_A_.fall(this, &IncEncoder::A_FALL);
+    
+    if(encoding == x4_Resolution){
+        ENC_B_.rise(this, &IncEncoder::B_RISE);
+        ENC_B_.fall(this, &IncEncoder::B_FALL);
+    }
+}
+
+int IncEncoder::GetIncPulses(){
+    return pulses;
+}
+
+void IncEncoder::reset(){
+    pulses = 0;
+}
+
+void IncEncoder::A_RISE(){
+    if(ENC_B_) pulses++;
+    else       pulses--;
+}
+
+void IncEncoder::A_FALL(){
+    if(ENC_B_) pulses--;
+    else       pulses++;
+}
+
+void IncEncoder::B_RISE(){
+    if(ENC_A_) pulses--;
+    else       pulses++;
+}
+
+void IncEncoder::B_FALL(){
+    if(ENC_A_) pulses++;
+    else       pulses--;
+}
\ No newline at end of file
diff -r 000000000000 -r c40c29a063c0 IncEncoder.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IncEncoder.h	Fri Jul 01 06:56:51 2022 +0000
@@ -0,0 +1,34 @@
+#ifndef MBED_INCENCODER_H
+#define MBED_INCENCODER_H
+
+#include "mbed.h"
+
+class IncEncoder{
+public: 
+    typedef enum Encoding {
+        x2_Resolution,
+        x4_Resolution
+    } Encoding;
+    
+    IncEncoder(PinName ENC_A,PinName ENC_B,Encoding encoding);
+    
+    int IncEncoder::GetIncPulses();
+    
+    void IncEncoder::reset();
+    
+    void IncEncoder::A_RISE();
+    
+    void IncEncoder::A_FALL();
+    
+    void IncEncoder::B_RISE();
+    
+    void IncEncoder::B_FALL();
+
+private:
+    int pulses;
+    
+    InterruptIn ENC_A_;
+    InterruptIn ENC_B_;
+};
+
+#endif
\ No newline at end of file