robot

Dependencies:   FastPWM3 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PwmIn.cpp Source File

PwmIn.cpp

00001 #include "mbed.h"
00002 #include "PwmIn.h"
00003 #include "MathHelpers.h"
00004 
00005 PwmIn::PwmIn(PinName pin, int usec_min, int usec_max)
00006 {
00007     int_in = new InterruptIn(pin);
00008     dig_in = new DigitalIn(pin);
00009     int_in->rise(this, &PwmIn::handle_rise);
00010     int_in->fall(this, &PwmIn::handle_fall);
00011     this->usec_min = usec_min;
00012     this->usec_max = usec_max;
00013     
00014     usecs = usec_min;
00015     blocked = false;
00016     enabled = false;
00017     risen = false;
00018 }
00019 
00020 
00021 bool PwmIn::get_enabled()
00022 {
00023     return enabled;
00024 }
00025 
00026 void PwmIn::handle_rise()
00027 {
00028     if (!enabled) {
00029         enabled = true;
00030         usecs = usec_min;
00031     }
00032     risen = true;
00033     
00034     timer.stop();
00035     timer.reset();
00036     timer.start();
00037 }
00038 
00039 void PwmIn::handle_fall()
00040 {
00041     if (!risen) return;
00042     risen = false;
00043     
00044     if (blocked) {
00045         blocked = false;;
00046         return;
00047     }
00048     
00049     usecs = timer.read_us();
00050     timer.stop();
00051     timer.reset();
00052     timer.start();
00053 }
00054 
00055 float PwmIn::get_throttle()
00056 {
00057     if(timer.read_us() > 40000) {
00058         enabled = false;
00059         usecs = usec_min;
00060     }
00061     if(!enabled) return 0;
00062     return constrain(map((float)usecs, usec_min, usec_max, 0, 1), 0, 1);
00063 }
00064