Example brushed dc motor example using an H-Bridge for motor direction/speed control and an opto interrupter for position/velocity feedback.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
lhiggs
Date:
Thu Oct 11 23:10:44 2012 +0000
Commit message:
First version of motor control example using an opto interrupter and H-Bridge.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 6f586d7c134c main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Oct 11 23:10:44 2012 +0000
@@ -0,0 +1,76 @@
+#include "mbed.h"
+
+PwmOut EN(p22);
+DigitalOut A1(p16);
+DigitalOut A2(p17);
+
+Serial pc(USBTX, USBRX);
+
+long float delta_time = 0;
+float rpm = 0;
+float inc_enc_constant = 0.03125;    // 32 Encoder Ticks per Revolution
+
+
+Timer t;
+// t.read_us() is 32bit interger, max 2^31 - 1 = 2147483647 us = 2147.483647 s = 35.7913941167 min
+
+class Counter
+{
+public:
+
+
+    Counter(PinName pin) : _interrupt(pin) {        // create the InterruptIn on the pin specified to Counter
+        _interrupt.fall(this, &Counter::increment); // attach increment function of this counter instance
+    }
+
+
+    void increment() {
+        delta_time = t.read_us();
+        t.reset();
+
+        rpm = ( inc_enc_constant  / (delta_time/1000000) ) *60 ; // rev / m
+
+        _count++;
+    }
+
+    int read() {
+        return _count;
+    }
+
+private:
+    InterruptIn _interrupt;
+    volatile int _count;
+};
+
+
+
+
+
+Counter counter(p30);   // opto interrupter counter
+
+int main()
+{
+    pc.baud(115200);
+    EN.period(0.020);
+
+    t.start();
+
+    float duty = 0.10;
+
+    while(1) {
+
+        wait(1);
+
+        EN.write(duty);
+        duty = duty + 0.01; // increment H-Bridge EN PWM duty cycle 1%
+        A1 = 1;         // set H-Bridge input A1 high
+        A2 = 0;         // set H-Bridge input A2 low
+
+        // pc.printf("Count so far: %d\n", counter.read());
+        // pc.printf("dt: %d\n", delta_time);
+
+        pc.printf("RPM: %f\n", rpm);
+        pc.printf("DUTY CYCLE: %f\n",duty);
+
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 6f586d7c134c mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Oct 11 23:10:44 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/cd19af002ccc
\ No newline at end of file