Joseph Bradshaw / MotCon

Dependents:   mbed_ES410_simpleSpeedMeasurement mbed_ES20X_V21_Tester

Revision:
8:67f2711fdeed
Parent:
5:3e07f69d8abd
Child:
9:98c700c7dc30
--- a/MotCon.cpp	Tue Oct 18 11:32:36 2016 +0000
+++ b/MotCon.cpp	Mon Oct 31 17:54:50 2016 +0000
@@ -7,6 +7,7 @@
     _pwm_pin.period_us(50);
     _pwm_pin = 0.0;
     _dir1_pin = 0;
+    this->duty_cycle=0.0;
 }
 MotCon::MotCon(PinName pwm_pin, PinName dir1_pin, PinName dir2_pin) : _pwm_pin(pwm_pin), _dir1_pin(dir1_pin), _dir2_pin(dir2_pin) {
     _dir2 = true;
@@ -15,25 +16,27 @@
     _dir1_pin = 0;
     _dir2_pin = 0;
     
-    mc_mode = 0;    //mode pin determines braking (1 = dynamic braking, 0 = free-wheeling)    
+    this->duty_cycle=0.0;
+    this->mc_mode = 0;    //mode pin determines braking (1 = dynamic braking, 0 = free-wheeling)    
 }
 // dc is signed duty cycle (+/-1.0)
-void MotCon::mot_control(float dc){    
+void MotCon::write(float dc){
     if(dc>1.0)
         dc=1.0;
     if(dc<-1.0)
         dc=-1.0;
             
+    this->duty_cycle = dc;
     if(_dir2){            
-        if(dc > 0.001){
+        if(this->duty_cycle > 0.001){
             _dir1_pin = 0;
             _dir2_pin = 1;
-            _pwm_pin = dc;
+            _pwm_pin = this->duty_cycle;
         }
-        else if(dc < -0.001){
+        else if(this->duty_cycle < -0.001){
             _dir2_pin = 0;
             _dir1_pin = 1;        
-            _pwm_pin = abs(dc);
+            _pwm_pin = abs(this->duty_cycle);
         }
         else{
             if(mc_mode){
@@ -49,13 +52,13 @@
         }         
     }
     else{            
-        if(dc > 0.001){
+        if(this->duty_cycle > 0.001){
             _dir1_pin = 0;
-            _pwm_pin = dc;
+            _pwm_pin = this->duty_cycle;
         }
-        else if(dc < -0.001){
+        else if(this->duty_cycle < -0.001){
             _dir1_pin = 1;
-            _pwm_pin = abs(dc);
+            _pwm_pin = abs(this->duty_cycle);
         }
         else{
             _dir1_pin = 0;
@@ -65,29 +68,31 @@
 }
 
 // dc is signed duty cycle (+/-1.0)
-void MotCon::mot_control(float dc, int invert){
+void MotCon::write(float dc, int invert){
     if(dc>1.0)
         dc=1.0;
     if(dc<-1.0)
         dc=-1.0;
                 
+    duty_cycle = dc;
     if(_dir2){
         if(invert==0){
-            if(dc > 0.001){
+            if(this->duty_cycle > 0.001){
                 _dir1_pin = 0;
                 _dir2_pin = 1;
-                _pwm_pin = dc;
+                _pwm_pin = this->duty_cycle;
             }
-            else if(dc < -0.001){
+            else if(duty_cycle < -0.001){
                 _dir2_pin = 0;
                 _dir1_pin = 1;
-                _pwm_pin = abs(dc);
+                _pwm_pin = abs(this->duty_cycle);
             }
             else{
                 if(mc_mode){
                     _dir1_pin = 0;
                     _dir2_pin = 0;
-                    _pwm_pin = 1.0;                
+                    _pwm_pin = 1.0;
+                    this->duty_cycle=1.0;
                 }
                 else{
                     _dir1_pin = 0;
@@ -97,21 +102,22 @@
             }
         }
         else{
-            if(dc > 0.001){
+            if(this->duty_cycle > 0.001){
                 _dir2_pin = 0;
                 _dir1_pin = 1;
-                _pwm_pin = dc;
+                _pwm_pin = this->duty_cycle;
             }
-            else if(dc < -0.001){
+            else if(this->duty_cycle < -0.001){
                 _dir1_pin = 0;
                 _dir2_pin = 1;
-                _pwm_pin = abs(dc);
+                _pwm_pin = abs(this->duty_cycle);
             }
             else{
                 if(mc_mode){
                     _dir1_pin = 0;
                     _dir2_pin = 0;
-                    _pwm_pin = 1.0;                
+                    _pwm_pin = 1.0;
+                    this->duty_cycle=1.0;
                 }
                 else{
                     _dir1_pin = 0;
@@ -123,13 +129,13 @@
     }
     else{        
         if(invert==0){
-            if(dc > 0.001){
+            if(this->duty_cycle > 0.001){
                 _dir1_pin = 0;
-                _pwm_pin = dc;
+                _pwm_pin = this->duty_cycle;
             }
-            else if(dc < -0.001){
+            else if(this->duty_cycle < -0.001){
                 _dir1_pin = 1;
-                _pwm_pin = abs(dc);
+                _pwm_pin = abs(this->duty_cycle);
             }
             else{
                 _dir1_pin = 0;
@@ -137,13 +143,13 @@
             }
         }
         else{
-            if(dc > 0.001){
+            if(this->duty_cycle > 0.001){
                 _dir1_pin = 1;
-                _pwm_pin = dc;
+                _pwm_pin = this->duty_cycle;
             }
-            else if(dc < -0.001){
+            else if(this->duty_cycle < -0.001){
                 _dir1_pin = 0;
-                _pwm_pin = abs(dc);
+                _pwm_pin = abs(this->duty_cycle);
             }
             else{
                 _dir1_pin = 0;
@@ -159,4 +165,17 @@
 
 bool MotCon::getMode(void){
     return mc_mode;
+}
+
+float MotCon::read(){
+    return this->duty_cycle;
+}
+
+MotCon& MotCon::operator= (float dc) { 
+    write(dc);
+    return *this;
+}
+ 
+MotCon::operator float() {
+    return read();
 }
\ No newline at end of file