Microduino的cube小车。

Dependencies:   mbed-rtos mbed

Revision:
4:0670023d3f36
Parent:
3:e4ac7c1a14de
--- a/Microduino_Motor.cpp	Fri May 27 01:44:31 2016 +0000
+++ b/Microduino_Motor.cpp	Sat May 28 05:09:18 2016 +0000
@@ -13,9 +13,10 @@
 #if 0
             pinMode( _motor_pinA, OUTPUT) ;
 #else
-            _period_us = 2000; // 500Hz
+            _period_us = 255; // 500Hz
             pwmout_init(&_pwmA, _motor_pinA);
             pwmout_period_us(&_pwmA, _period_us); // 500Hz
+            pwmout_pulsewidth_us(&_pwmA, 1);
 #endif
             motors[this->motorIndex].Pin.nbr_A = _motor_pinA;
 
@@ -24,6 +25,7 @@
 #else
             pwmout_init(&_pwmB, _motor_pinB);
             pwmout_period_us(&_pwmB, _period_us); // 500Hz
+            pwmout_pulsewidth_us(&_pwmB, 1);
 #endif
             motors[this->motorIndex].Pin.nbr_B = _motor_pinB;
 
@@ -39,21 +41,24 @@
     this->fix=_fix;
 }
 
-int16_t Motor::GetData(int16_t _throttle, int16_t _steering, bool _dir)
+int16_t Motor::GetData(int16_t _throttle, int16_t _steering, uint8_t _dir)
 {
     this->_motor_vol = _throttle;
 
-    if(_dir)
+    if(_dir == 1)
+    {
         this->_motor_vol -= _steering / 2;
+    }
     else
+    {
         this->_motor_vol += _steering / 2;
-
+    }
     if (this->_motor_vol > 255)
         this->_motor_vol = 255;
     else if (this->_motor_vol < -255)
         this->_motor_vol = -255;
 
-    this->_motor_vol *= fix;
+    //this->_motor_vol *= fix;
 
     return this->_motor_vol;
 }
@@ -77,21 +82,52 @@
 #else
 void Motor::Driver(int16_t _motor_driver)
 {
-    int16_t pulseWidth = 0;
-    PinName channel_A = motors[this->motorIndex].Pin.nbr_A;
-    PinName channel_B = motors[this->motorIndex].Pin.nbr_B;
+    //static bool flag = true;
+    uint32_t pulseWidth = 0;
+    //PinName channel_A = motors[this->motorIndex].Pin.nbr_A;
+    //PinName channel_B = motors[this->motorIndex].Pin.nbr_B;
+    #if 0
+    pwmout_pulsewidth_us(&_pwmA, _period_us/2);
+    pwmout_pulsewidth_us(&_pwmB, 0);
+    return;
+    #endif
+    #if 0
+    pwmout_pulsewidth_us(&_pwmA, 0);
+    pwmout_pulsewidth_us(&_pwmB, _period_us/2);
+    return;
+    #endif
     if (_motor_driver == 0) {
         pwmout_pulsewidth_us(&_pwmA, 0);
         pwmout_pulsewidth_us(&_pwmB, 0);
     } else if (_motor_driver > 0) {
-        pulseWidth = _period_us * _motor_driver / 255;
+        #if 1
+        pulseWidth = _period_us / 255 * _motor_driver;
         pwmout_pulsewidth_us(&_pwmA, pulseWidth);
+        pwmout_pulsewidth_us(&_pwmB, 2);
+        #else
+        pwmout_pulsewidth_us(&_pwmA, _period_us/2);
         pwmout_pulsewidth_us(&_pwmB, 0);
+        #endif
     } else {
+        #if 0
         _motor_driver = 255 + _motor_driver;
-        pulseWidth = _period_us * _motor_driver / 255;
-        pwmout_pulsewidth_us(&_pwmA, pulseWidth);
-        pwmout_pulsewidth_us(&_pwmB, _period_us);
+        pulseWidth = _period_us / 255 * _motor_driver;
+        pwmout_pulsewidth_us(&_pwmA, 0);
+        pwmout_pulsewidth_us(&_pwmB, pulseWidth);
+        #elif 1
+        _motor_driver = abs(_motor_driver);
+        pulseWidth = _period_us / 255 * _motor_driver;
+        pwmout_pulsewidth_us(&_pwmA, 2);
+        pwmout_pulsewidth_us(&_pwmB, pulseWidth);
+        #elif 0
+        _motor_driver = 255 + _motor_driver;
+        pulseWidth = _period_us / 255 * _motor_driver;
+        pwmout_pulsewidth_us(&_pwmA, _period_us);
+        pwmout_pulsewidth_us(&_pwmB, pulseWidth);
+        #else
+        pwmout_pulsewidth_us(&_pwmA, 0);
+        pwmout_pulsewidth_us(&_pwmB, 241);
+        #endif
     }
 }
 #endif