Pete Isley / i_moto
Committer:
Fenwiz
Date:
Thu Oct 13 09:32:19 2011 +0000
Revision:
0:4c893274b335
Revision 1.02 (stable release)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Fenwiz 0:4c893274b335 1 #include "mbed.h"
Fenwiz 0:4c893274b335 2 #include "i_moto.h"
Fenwiz 0:4c893274b335 3
Fenwiz 0:4c893274b335 4 //-------------------------------------------------------------------------
Fenwiz 0:4c893274b335 5
Fenwiz 0:4c893274b335 6 i_moto::i_moto( PinName _outpin, int _cycleTime_ms ): mOut( _outpin )
Fenwiz 0:4c893274b335 7 {
Fenwiz 0:4c893274b335 8 if (_cycleTime_ms < 1) _cycleTime_ms = 1; //clamp the cycle time to between 1 and 200 miliseconds
Fenwiz 0:4c893274b335 9 if (_cycleTime_ms > 200) _cycleTime_ms = 200;
Fenwiz 0:4c893274b335 10
Fenwiz 0:4c893274b335 11 setTargetPower( 0 ); //zero the power variables
Fenwiz 0:4c893274b335 12 currentPower = 0;
Fenwiz 0:4c893274b335 13 setCycle_ms( _cycleTime_ms );
Fenwiz 0:4c893274b335 14 setRampUp_s( 10 ); //default ramp up of 10 seconds
Fenwiz 0:4c893274b335 15 setRampDown_s( 0 ); //default ramp down of 0 seconds
Fenwiz 0:4c893274b335 16 ticker.attach_us( this, &i_moto::updatePower, (_cycleTime_ms * 200) ); //setup the ticker handler to update the mark space ratio of the PWM
Fenwiz 0:4c893274b335 17 }
Fenwiz 0:4c893274b335 18
Fenwiz 0:4c893274b335 19 //-------------------------------------------------------------------------
Fenwiz 0:4c893274b335 20
Fenwiz 0:4c893274b335 21 i_moto::i_moto( PinName _outpin, int _cycleTime_ms, int _rampUp, int _rampDown ): mOut( _outpin )
Fenwiz 0:4c893274b335 22 {
Fenwiz 0:4c893274b335 23 if (_cycleTime_ms < 1) _cycleTime_ms = 1; //clamp the cycle time to between 1 and 200 miliseconds
Fenwiz 0:4c893274b335 24 if (_cycleTime_ms > 200) _cycleTime_ms = 200;
Fenwiz 0:4c893274b335 25
Fenwiz 0:4c893274b335 26 setTargetPower( 0 ); //zero the power variables
Fenwiz 0:4c893274b335 27 currentPower = 0;
Fenwiz 0:4c893274b335 28 setCycle_ms( _cycleTime_ms );
Fenwiz 0:4c893274b335 29 setRampUp_s( _rampUp ); //default ramp up of 10 seconds
Fenwiz 0:4c893274b335 30 setRampDown_s( _rampDown ); //default ramp down of 0 seconds
Fenwiz 0:4c893274b335 31 ticker.attach_us( this, &i_moto::updatePower, (_cycleTime_ms * 200) ); //setup the ticker handler to update the mark space ratio of the PWM
Fenwiz 0:4c893274b335 32 }
Fenwiz 0:4c893274b335 33
Fenwiz 0:4c893274b335 34 //-------------------------------------------------------------------------
Fenwiz 0:4c893274b335 35
Fenwiz 0:4c893274b335 36 void i_moto::setCycle_ms( int cycleTime_ms )
Fenwiz 0:4c893274b335 37 {
Fenwiz 0:4c893274b335 38 mOut.period_ms( cycleTime_ms );
Fenwiz 0:4c893274b335 39 cycleTime = cycleTime_ms;
Fenwiz 0:4c893274b335 40 }
Fenwiz 0:4c893274b335 41
Fenwiz 0:4c893274b335 42 //-------------------------------------------------------------------------
Fenwiz 0:4c893274b335 43
Fenwiz 0:4c893274b335 44 void i_moto::setRampUp_s( int ramp_s )
Fenwiz 0:4c893274b335 45 {
Fenwiz 0:4c893274b335 46 if (ramp_s < 0) ramp_s = 0; //clamp the ramp time between 0 and 120 seconds
Fenwiz 0:4c893274b335 47 if (ramp_s > 120) ramp_s = 120;
Fenwiz 0:4c893274b335 48
Fenwiz 0:4c893274b335 49 if (ramp_s == 0)
Fenwiz 0:4c893274b335 50 {
Fenwiz 0:4c893274b335 51 temp = 0.0001; //prevent divide by zero error
Fenwiz 0:4c893274b335 52 }
Fenwiz 0:4c893274b335 53 else
Fenwiz 0:4c893274b335 54 {
Fenwiz 0:4c893274b335 55 temp = ramp_s;
Fenwiz 0:4c893274b335 56 }
Fenwiz 0:4c893274b335 57 upRamp = cycleTime / (temp * 5000); //calculate the ramp delta as a function of Cycle Time
Fenwiz 0:4c893274b335 58 }
Fenwiz 0:4c893274b335 59
Fenwiz 0:4c893274b335 60 //-------------------------------------------------------------------------
Fenwiz 0:4c893274b335 61
Fenwiz 0:4c893274b335 62 int i_moto::getRampUp_s( void )
Fenwiz 0:4c893274b335 63 {
Fenwiz 0:4c893274b335 64 temp = cycleTime / (upRamp * 5000); //ramp up from 0 to 100% in temp seconds
Fenwiz 0:4c893274b335 65 return temp;
Fenwiz 0:4c893274b335 66 }
Fenwiz 0:4c893274b335 67
Fenwiz 0:4c893274b335 68 //-------------------------------------------------------------------------
Fenwiz 0:4c893274b335 69
Fenwiz 0:4c893274b335 70 void i_moto::setRampDown_s( int ramp_s )
Fenwiz 0:4c893274b335 71 {
Fenwiz 0:4c893274b335 72 if (ramp_s < 0) ramp_s = 0; //clamp the ramp time between 0 and 120 seconds
Fenwiz 0:4c893274b335 73 if (ramp_s > 120) ramp_s = 120;
Fenwiz 0:4c893274b335 74
Fenwiz 0:4c893274b335 75 if (ramp_s == 0)
Fenwiz 0:4c893274b335 76 {
Fenwiz 0:4c893274b335 77 temp = 0.0001; //prevent divide by zero error
Fenwiz 0:4c893274b335 78 }
Fenwiz 0:4c893274b335 79 else
Fenwiz 0:4c893274b335 80 {
Fenwiz 0:4c893274b335 81 temp = ramp_s;
Fenwiz 0:4c893274b335 82 }
Fenwiz 0:4c893274b335 83 downRamp = cycleTime / (temp * 5000); //calculate the ramp delta as a function of Cycle Time
Fenwiz 0:4c893274b335 84 }
Fenwiz 0:4c893274b335 85
Fenwiz 0:4c893274b335 86 //-------------------------------------------------------------------------
Fenwiz 0:4c893274b335 87
Fenwiz 0:4c893274b335 88 int i_moto::getRampDown_s( void )
Fenwiz 0:4c893274b335 89 {
Fenwiz 0:4c893274b335 90 temp = cycleTime / (downRamp * 5000); //ramp down from 100% to 0 in temp seconds
Fenwiz 0:4c893274b335 91 return temp;
Fenwiz 0:4c893274b335 92 }
Fenwiz 0:4c893274b335 93
Fenwiz 0:4c893274b335 94 //-------------------------------------------------------------------------
Fenwiz 0:4c893274b335 95
Fenwiz 0:4c893274b335 96 void i_moto::setTargetPower( int percent )
Fenwiz 0:4c893274b335 97 {
Fenwiz 0:4c893274b335 98 if (percent > 100) percent = 100; //clamp target power to between 0 and 100 percent
Fenwiz 0:4c893274b335 99 if (percent < 0) percent = 0;
Fenwiz 0:4c893274b335 100 temp = percent;
Fenwiz 0:4c893274b335 101 targetPower = temp / 100;
Fenwiz 0:4c893274b335 102 }
Fenwiz 0:4c893274b335 103
Fenwiz 0:4c893274b335 104 //-------------------------------------------------------------------------
Fenwiz 0:4c893274b335 105
Fenwiz 0:4c893274b335 106 int i_moto::getTargetPower( void )
Fenwiz 0:4c893274b335 107 {
Fenwiz 0:4c893274b335 108 return targetPower * 100; //return target power as a percentage
Fenwiz 0:4c893274b335 109 }
Fenwiz 0:4c893274b335 110
Fenwiz 0:4c893274b335 111 //-------------------------------------------------------------------------
Fenwiz 0:4c893274b335 112
Fenwiz 0:4c893274b335 113 int i_moto::getCurrentPower( void )
Fenwiz 0:4c893274b335 114 {
Fenwiz 0:4c893274b335 115 return currentPower * 100; //return current power as a percentage
Fenwiz 0:4c893274b335 116 }
Fenwiz 0:4c893274b335 117
Fenwiz 0:4c893274b335 118 //-------------------------------------------------------------------------
Fenwiz 0:4c893274b335 119
Fenwiz 0:4c893274b335 120 void i_moto::updatePower( void )
Fenwiz 0:4c893274b335 121 //Ticker interupt routine for updating the PWM output
Fenwiz 0:4c893274b335 122 {
Fenwiz 0:4c893274b335 123 if (currentPower < targetPower) //current power needs to increace
Fenwiz 0:4c893274b335 124 {
Fenwiz 0:4c893274b335 125 currentPower += upRamp;
Fenwiz 0:4c893274b335 126 if (currentPower > targetPower) //check for overshoot
Fenwiz 0:4c893274b335 127 {
Fenwiz 0:4c893274b335 128 currentPower = targetPower; //clamp current power to target power
Fenwiz 0:4c893274b335 129 }
Fenwiz 0:4c893274b335 130 }
Fenwiz 0:4c893274b335 131 else if (currentPower > targetPower) //current power needs to decreace
Fenwiz 0:4c893274b335 132 {
Fenwiz 0:4c893274b335 133 currentPower -= downRamp;
Fenwiz 0:4c893274b335 134 if (currentPower < targetPower) //check for overshoot
Fenwiz 0:4c893274b335 135 {
Fenwiz 0:4c893274b335 136 currentPower = targetPower; //clamp current power to target power
Fenwiz 0:4c893274b335 137 }
Fenwiz 0:4c893274b335 138 }
Fenwiz 0:4c893274b335 139 mOut = currentPower; //update the PWM output
Fenwiz 0:4c893274b335 140 }
Fenwiz 0:4c893274b335 141
Fenwiz 0:4c893274b335 142 //-------------------------------------------------------------------------