lhiggs CSUM / Mbed 2 deprecated OPTO_INTERRUPTER

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 PwmOut EN(p22);
00004 DigitalOut A1(p16);
00005 DigitalOut A2(p17);
00006 
00007 Serial pc(USBTX, USBRX);
00008 
00009 long float delta_time = 0;
00010 float rpm = 0;
00011 float inc_enc_constant = 0.03125;    // 32 Encoder Ticks per Revolution
00012 
00013 
00014 Timer t;
00015 // t.read_us() is 32bit interger, max 2^31 - 1 = 2147483647 us = 2147.483647 s = 35.7913941167 min
00016 
00017 class Counter
00018 {
00019 public:
00020 
00021 
00022     Counter(PinName pin) : _interrupt(pin) {        // create the InterruptIn on the pin specified to Counter
00023         _interrupt.fall(this, &Counter::increment); // attach increment function of this counter instance
00024     }
00025 
00026 
00027     void increment() {
00028         delta_time = t.read_us();
00029         t.reset();
00030 
00031         rpm = ( inc_enc_constant  / (delta_time/1000000) ) *60 ; // rev / m
00032 
00033         _count++;
00034     }
00035 
00036     int read() {
00037         return _count;
00038     }
00039 
00040 private:
00041     InterruptIn _interrupt;
00042     volatile int _count;
00043 };
00044 
00045 
00046 
00047 
00048 
00049 Counter counter(p30);   // opto interrupter counter
00050 
00051 int main()
00052 {
00053     pc.baud(115200);
00054     EN.period(0.020);
00055 
00056     t.start();
00057 
00058     float duty = 0.10;
00059 
00060     while(1) {
00061 
00062         wait(1);
00063 
00064         EN.write(duty);
00065         duty = duty + 0.01; // increment H-Bridge EN PWM duty cycle 1%
00066         A1 = 1;         // set H-Bridge input A1 high
00067         A2 = 0;         // set H-Bridge input A2 low
00068 
00069         // pc.printf("Count so far: %d\n", counter.read());
00070         // pc.printf("dt: %d\n", delta_time);
00071 
00072         pc.printf("RPM: %f\n", rpm);
00073         pc.printf("DUTY CYCLE: %f\n",duty);
00074 
00075     }
00076 }