Dependents:   nhk_2018_undercarry_test04 nhk_2018_undercarry_test08 nhk_2018_undercarry_test09 nhk_2018_undercarry_test10 ... more

Files at this revision

API Documentation at this revision

Comitter:
kenken0721
Date:
Thu Mar 22 05:55:17 2018 +0000
Commit message:

Changed in this revision

encoder.cpp Show annotated file Show diff for this revision Revisions of this file
encoder.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 0d6f91c4a794 encoder.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/encoder.cpp	Thu Mar 22 05:55:17 2018 +0000
@@ -0,0 +1,36 @@
+#include "encoder.h"
+#include "mbed.h"
+
+Encoder::Encoder(PinName APin, PinName BPin, PinName ZPin) : A(APin) , B(BPin) , Z(ZPin){
+    A.rise(this,&Encoder::flag);
+    init();
+}
+
+void Encoder::init(void){
+    count = 0;
+    zcount = 0;
+}
+
+void Encoder::flag(void){
+  if(B == true){
+    count++;
+    if(Z == true){
+        zcount++;
+    }
+  }else{
+    count--;
+    if(Z == true){
+        zcount--;
+    }
+  }
+}
+
+float Encoder::read_rotate(){
+    return (float)(count / 300.0);
+}
+
+int Encoder::read_z(){
+    return zcount;
+}
+
+
diff -r 000000000000 -r 0d6f91c4a794 encoder.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/encoder.h	Thu Mar 22 05:55:17 2018 +0000
@@ -0,0 +1,22 @@
+#ifndef ENCODER_H
+#define ENCODER_H
+
+#include "mbed.h"
+
+class Encoder{
+public  :
+    Encoder(PinName APin, PinName BPin, PinName ZPin);
+    float read_rotate();
+    int read_z();
+    void init();
+private :
+    InterruptIn A;
+    DigitalIn B;
+    DigitalIn Z;
+    int count;
+    int zcount;
+    void flag();
+};
+
+#endif
+