robot

Dependencies:   FastPWM3 mbed

PwmIn/PwmIn.cpp

Committer:
bwang
Date:
2017-02-28
Revision:
79:d0b1bb3dcf68
Parent:
78:b8df106126a7
Child:
92:a9dac72d8cac

File content as of revision 79:d0b1bb3dcf68:

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

PwmIn::PwmIn(PinName pin, int usec_min, int usec_max, int usec_crazy)
{
    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;
    this->usec_crazy = usec_crazy;
    
    usecs = usec_min;
    blocked = false;
    enabled = false;
}


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

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

void PwmIn::handle_fall()
{
    int usecs_new = timer.read_us();
    timer.stop();
    timer.reset();
    timer.start();
    
    if (blocked) {
        blocked = false;
        return;
    }
    
    if (usecs_new <= usec_crazy) usecs = usecs_new;
}

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);
}