OITRP_2022 / Mbed OS encoder_motor

Dependencies:   QEI

Revision:
0:ca2a77886231
Child:
1:f0e6a1eb94a8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jun 23 08:34:39 2021 +0000
@@ -0,0 +1,59 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2019 ARM Limited
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include "mbed.h"
+#include "platform/mbed_thread.h"
+#include "QEI.h"
+Serial pc(USBTX, USBRX, 115200);
+Serial md(PC_10, PC_11, 38400);
+PwmOut motor1(PF_8);
+PwmOut motor2(PF_7);
+// Blinking rate in milliseconds
+#define BLINKING_RATE_MS                                                    500
+#define ROTATE_PER_REVOLUTIONS 64    //For QEI's pulse data
+int r;
+bool kai=0;
+DigitalIn btn(USER_BUTTON);
+DigitalOut led(LED2);
+void moter(int x){
+    r = x+128;
+    md.putc(r);
+     pc.printf("%d\n",r);
+}
+int main()
+{   
+    md.format(8,Serial::None,1);
+    moter(0);
+    while(!btn){
+        led=!led;
+        wait(0.05);
+    }
+    led=1;
+    while(btn){}
+    // Initialise the digital pin LED1 as an output
+    DigitalOut led(LED1);
+ QEI wheel(PC_8, PC_9, NC, ROTATE_PER_REVOLUTIONS, QEI::X4_ENCODING);
+    while(1){
+        pc.printf("c");
+        pc.printf("%d",kai);
+        pc.printf("Pulses: %07d", wheel.getPulses());
+        pc.printf("Rotate: %d\n", (int)wheel.getPulses()/(ROTATE_PER_REVOLUTIONS*4));
+        if((int)wheel.getPulses()/(ROTATE_PER_REVOLUTIONS*4)<100&&kai==0){
+            moter(64);
+            pc.printf("a");
+            if((int)wheel.getPulses()/(ROTATE_PER_REVOLUTIONS*4)==99){
+            kai=1;
+            }
+        }
+        if((int)wheel.getPulses()/(ROTATE_PER_REVOLUTIONS*4)>0&&kai==1){
+            moter(-64);
+            pc.printf("b");
+            if((int)wheel.getPulses()/(ROTATE_PER_REVOLUTIONS*4)==1){
+            kai=0;
+            }
+        }
+    //    moter(0);
+    }
+}
\ No newline at end of file