Pete Isley / i_moto

i_moto.cpp

Committer:
Fenwiz
Date:
2011-10-13
Revision:
0:4c893274b335

File content as of revision 0:4c893274b335:

#include "mbed.h"
#include "i_moto.h"

//-------------------------------------------------------------------------

i_moto::i_moto( PinName _outpin, int _cycleTime_ms ): mOut( _outpin )
{
    if (_cycleTime_ms < 1) _cycleTime_ms = 1;           //clamp the cycle time to between 1 and 200 miliseconds
    if (_cycleTime_ms > 200) _cycleTime_ms = 200;
    
    setTargetPower( 0 );                                //zero the power variables
    currentPower = 0;
    setCycle_ms( _cycleTime_ms );
    setRampUp_s( 10 );                                  //default ramp up of 10 seconds
    setRampDown_s( 0 );                                 //default ramp down of 0 seconds
    ticker.attach_us( this, &i_moto::updatePower, (_cycleTime_ms * 200) );      //setup the ticker handler to update the mark space ratio of the PWM
}

//-------------------------------------------------------------------------

i_moto::i_moto( PinName _outpin, int _cycleTime_ms, int _rampUp, int _rampDown ): mOut( _outpin )
{
    if (_cycleTime_ms < 1) _cycleTime_ms = 1;             //clamp the cycle time to between 1 and 200 miliseconds
    if (_cycleTime_ms > 200) _cycleTime_ms = 200;
    
    setTargetPower( 0 );                                //zero the power variables
    currentPower = 0;
    setCycle_ms( _cycleTime_ms );
    setRampUp_s( _rampUp );                             //default ramp up of 10 seconds
    setRampDown_s( _rampDown );                         //default ramp down of 0 seconds
    ticker.attach_us( this, &i_moto::updatePower, (_cycleTime_ms * 200) );      //setup the ticker handler to update the mark space ratio of the PWM
}

//-------------------------------------------------------------------------

void i_moto::setCycle_ms( int cycleTime_ms )
{
    mOut.period_ms( cycleTime_ms );
    cycleTime = cycleTime_ms;
}

//-------------------------------------------------------------------------

void i_moto::setRampUp_s( int ramp_s )
{
    if (ramp_s < 0) ramp_s = 0;                 //clamp the ramp time between 0 and 120 seconds
    if (ramp_s > 120) ramp_s = 120;
    
    if (ramp_s == 0)
    {
        temp = 0.0001;                          //prevent divide by zero error
    }
    else
    {
        temp = ramp_s;
    }
    upRamp = cycleTime / (temp * 5000);         //calculate the ramp delta as a function of Cycle Time
}

//-------------------------------------------------------------------------

int i_moto::getRampUp_s( void )
{
    temp = cycleTime / (upRamp * 5000);         //ramp up from 0 to 100% in temp seconds
    return temp;
}

//-------------------------------------------------------------------------

void i_moto::setRampDown_s( int ramp_s )
{
    if (ramp_s < 0) ramp_s = 0;                 //clamp the ramp time between 0 and 120 seconds
    if (ramp_s > 120) ramp_s = 120;
    
    if (ramp_s == 0)
    {
        temp = 0.0001;                          //prevent divide by zero error
    }
    else
    {
        temp = ramp_s;
    }
    downRamp = cycleTime / (temp * 5000);       //calculate the ramp delta as a function of Cycle Time
}

//-------------------------------------------------------------------------

int i_moto::getRampDown_s( void )
{
    temp = cycleTime / (downRamp * 5000);       //ramp down from 100% to 0 in temp seconds
    return temp;
}

//-------------------------------------------------------------------------

void i_moto::setTargetPower( int percent )
{
    if (percent > 100) percent = 100;           //clamp target power to between 0 and 100 percent
    if (percent < 0) percent = 0;
    temp = percent;
    targetPower = temp / 100;
}

//-------------------------------------------------------------------------

int i_moto::getTargetPower( void )
{
    return targetPower * 100;                   //return target power as a percentage
}

//-------------------------------------------------------------------------

int i_moto::getCurrentPower( void )
{
    return currentPower * 100;                  //return current power as a percentage
}

//-------------------------------------------------------------------------

void i_moto::updatePower( void )
//Ticker interupt routine for updating the PWM output
{
    if (currentPower < targetPower)             //current power needs to increace
    {
        currentPower += upRamp;
        if (currentPower > targetPower)         //check for overshoot
        {
            currentPower = targetPower;         //clamp current power to target power
        }
    }
    else if (currentPower > targetPower)        //current power needs to decreace
    {
        currentPower -= downRamp;
        if (currentPower < targetPower)         //check for overshoot
        {
            currentPower = targetPower;         //clamp current power to target power
        }
    }
    mOut = currentPower;                        //update the PWM output
}

//-------------------------------------------------------------------------