v1

Dependencies:   PinDetect TextLCD mbed

Fork of SunflowerMach1 by Milan Draganic

Files at this revision

API Documentation at this revision

Comitter:
mdraganic
Date:
Sat Nov 09 06:49:43 2013 +0000
Parent:
0:7447b8021b33
Child:
2:0bf41ad96558
Commit message:
added pwm motor driver

Changed in this revision

Motor.cpp Show diff for this revision Revisions of this file
Motor.h Show diff for this revision Revisions of this file
MotorDrivers/Motor.cpp Show annotated file Show diff for this revision Revisions of this file
MotorDrivers/Motor.h Show annotated file Show diff for this revision Revisions of this file
MotorDrivers/MotorPwm.cpp Show annotated file Show diff for this revision Revisions of this file
MotorDrivers/MotorPwm.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Motor.cpp	Fri Nov 08 19:09:47 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,43 +0,0 @@
-#include "Motor.h"
-
-Motor::Motor(PinName positivePin, PinName negativePin): positiveOut(positivePin), negativeOut(negativePin) {
-
-}
-
-void Motor::movePositive() {
-
-    direction = 1;
-    move();
-}
-
-void Motor::moveNegative() {
-
-    direction = -1;
-    move();
-}
-
-void Motor::move() {
-
-    positiveOut = 0;
-    negativeOut = 0;
-        
-    switch(direction) {    
-    case 0:
-        return;
-    case 1:
-        positiveOut = 1;
-        break;
-    case -1:
-        negativeOut = 1;
-        break;
-    }
-    
-    wait_ms(motorDriveTime);
-}
-
-void Motor::stop() {
-
-    positiveOut = 0;
-    negativeOut = 0;
-    direction = 0;
-}
--- a/Motor.h	Fri Nov 08 19:09:47 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-#ifndef MOTOR_H
-#define MOTOR_H
-
-#include "mbed.h"
-
-#define motorDriveTime 1000 // vrijeme koje se motor kreće, u milisekundama.
-
-class Motor {
-
-private:
-    DigitalOut positiveOut, negativeOut;
-    void move();
-    short direction;
-
-public:
-    Motor(PinName, PinName);
-    void movePositive();
-    void moveNegative();
-    void stop();
-
-
-};
-
-#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MotorDrivers/Motor.cpp	Sat Nov 09 06:49:43 2013 +0000
@@ -0,0 +1,47 @@
+#include "Motor.h"
+
+Motor::Motor() : positiveOut(NC), negativeOut(NC) {
+}
+
+Motor::Motor(PinName positivePin, PinName negativePin): 
+        positiveOut(positivePin), negativeOut(negativePin) {
+}
+
+void Motor::movePositive() {
+
+    direction = 1;
+    move();
+}
+
+void Motor::moveNegative() {
+
+    direction = -1;
+    move();
+}
+
+void Motor::move() {
+
+    positiveOut = 0;
+    negativeOut = 0;
+        
+    switch(direction) {    
+    case 0:
+        return;
+    case 1:
+        positiveOut = 1;
+        break;
+    case -1:
+        negativeOut = 1;
+        break;
+    }
+    
+    direction = 0;
+    wait_ms(motorDriveTime);
+}
+
+void Motor::stop() {
+
+    positiveOut = 0;
+    negativeOut = 0;
+    direction = 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MotorDrivers/Motor.h	Sat Nov 09 06:49:43 2013 +0000
@@ -0,0 +1,26 @@
+#ifndef MOTOR_H
+#define MOTOR_H
+
+#include "mbed.h"
+
+#define motorDriveTime 1000 // vrijeme koje se motor kreće, u milisekundama.
+
+class Motor {
+
+private:
+    DigitalOut positiveOut, negativeOut;
+    
+protected:
+    short direction;    
+    Motor();
+    void move();
+
+public:
+    Motor(PinName, PinName);
+    void movePositive();
+    void moveNegative();
+    void stop();    
+    
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MotorDrivers/MotorPwm.cpp	Sat Nov 09 06:49:43 2013 +0000
@@ -0,0 +1,47 @@
+#include "MotorPwm.h"
+
+MotorPwm::MotorPwm(PinName positivePin, PinName negativePin): 
+        positiveOutPwm(positivePin), negativeOutPwm(negativePin) {
+        
+    positiveOutPwm.period(motorPwmPeriod);
+    positiveOutPwm = motorPwmInitDutyCycle;
+    negativeOutPwm.period(motorPwmPeriod);
+    negativeOutPwm = motorPwmInitDutyCycle;
+}
+
+void MotorPwm::move() {
+
+    positiveOutPwm = 0;
+    negativeOutPwm = 0;
+    
+    switch(direction) {    
+    case 0:
+        return;
+    case 1:
+        for (int i = 0; i < 1; i += 0.1) {
+            positiveOutPwm = i;
+            wait(motorPwmWaitTime);
+        }    
+        break;
+    case -1:
+        for (int i = 0; i < 1; i += 0.1) {
+            negativeOutPwm = i;
+            wait(motorPwmWaitTime);
+        }    
+        break;
+    }
+    
+    direction = 0;
+    wait_ms(motorDriveTime);
+}
+
+void MotorPwm::stop() {
+
+    for (int i = 1; i > 0; i -= 0.1) {
+        positiveOutPwm = i;
+        negativeOutPwm = i;
+        wait(motorPwmWaitTime);
+    }
+    direction = 0;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MotorDrivers/MotorPwm.h	Sat Nov 09 06:49:43 2013 +0000
@@ -0,0 +1,29 @@
+#ifndef MOTORPWM_H
+#define MOTORPWM_H
+
+#include "mbed.h"
+#include "Motor.h"
+
+#define motorPwmPeriod 0.010 // PWM period to 10 ms
+#define motorPwmInitDutyCycle 0.5 // PWM initial duty cycle, 50%
+#define motorPwmWaitTime 0.05 // PWM wait time in sec.
+
+class MotorPwm : public Motor {
+
+private:
+    PwmOut positiveOutPwm, negativeOutPwm;
+    
+protected:
+    void move();
+
+public:
+    MotorPwm(PinName, PinName);
+    void stop();    
+    
+};
+
+
+
+
+
+#endif
--- a/main.cpp	Fri Nov 08 19:09:47 2013 +0000
+++ b/main.cpp	Sat Nov 09 06:49:43 2013 +0000
@@ -1,5 +1,6 @@
 #include "mbed.h"
 #include "Motor.h"
+#include "MotorPwm.h"
 
 AnalogIn ainSensA(p17);
 AnalogIn ainSensB(p18);
@@ -11,8 +12,8 @@
 
 int main() {
 
-    Motor *motorEl = new Motor(p25, p26);
-    Motor *motorAz = new Motor(p23, p24);
+    Motor *motorEl = new MotorPwm(p25, p26);
+    Motor *motorAz = new MotorPwm(p23, p24);
 
     while(1) {