MakoTr 315 / Mbed 2 deprecated Motor_PWM_Eidan05

Dependencies:   mbed

Revision:
0:2cedfd753855
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Aug 02 13:34:15 2020 +0000
@@ -0,0 +1,55 @@
+#include "mbed.h"
+
+AnalogIn IN(A0);
+DigitalOut MotorP(D2);
+DigitalOut MotorM(D3);
+
+int main() {
+  double InVal;
+  double AbsVal;
+  int PwmPeriod=420;//423
+    while(1) {
+        InVal=IN.read();
+        InVal=((InVal-0.5)*2.0);
+        AbsVal=fabs(InVal);
+
+
+        PwmPeriod=3325;         //
+        if(AbsVal>0.9){                         //
+            PwmPeriod=3325;
+        }else if(AbsVal>0.8){
+            PwmPeriod=1640;
+        }else if(AbsVal>0.16){
+            PwmPeriod=1050;
+        }else if(AbsVal>0.11){                   //
+            PwmPeriod=1640;
+        }else if(AbsVal>0.06){
+            PwmPeriod=3325;
+        }else if(AbsVal>0.01){
+            PwmPeriod=6663;
+        }
+        //printf("%f   ",InVal);
+        
+        
+        if(InVal>0){
+            MotorP=1;
+            MotorM=0;
+            wait_us(PwmPeriod*InVal);
+            MotorP=0;
+            MotorM=0;
+            wait_us(PwmPeriod-PwmPeriod*InVal);
+        }else if(InVal<0){
+            MotorP=0;
+            MotorM=1;
+            wait_us(PwmPeriod*-InVal);
+            MotorP=0;
+            MotorM=0;
+            wait_us(PwmPeriod-PwmPeriod*-InVal);
+        }else{
+            MotorP=0;
+            MotorM=0;
+            wait_us(PwmPeriod);
+        }
+        //printf("%d\n\r",(int)(PwmPeriod*InVal));
+    }
+}
\ No newline at end of file