robot

Dependencies:   FastPWM3 mbed

PwmIn/PwmIn.cpp

Committer:
bwang
Date:
2016-11-02
Revision:
19:a6cf15f89f3d
Parent:
18:3863ca45cf26
Child:
75:591556ce033d

File content as of revision 19:a6cf15f89f3d:

#include "mbed.h"
#include "PwmIn.h"
#include "MathHelpers.h"

PwmIn::PwmIn(PinName pin, int usec_min, int usec_max)
{
    int_in = new InterruptIn(pin);
    dig_in = new DigitalIn(pin);
    int_in->rise(this, &PwmIn::handle_rise);
    int_in->fall(this, &PwmIn::handle_fall);
    this->usec_min = usec_min;
    this->usec_max = usec_max;
}


bool PwmIn::get_enabled()
{
    return enabled;
}

void PwmIn::handle_rise()
{
    enabled = true;
    timer.stop();
    timer.reset();
    timer.start();
    was_on = true;
}

void PwmIn::handle_fall()
{
    was_on = false;
    usecs = timer.read_us();
    timer.stop();
    timer.reset();
    timer.start();
}

float PwmIn::get_throttle()
{
    if(timer.read_us() > 40000) enabled = false;
    if(!enabled) return -1;
    return constrain(map((float)usecs, usec_min, usec_max, 0, 1), 0, 1);
}