Fix for pwLimit overflow

This commit is contained in:
Josh Stewart 2015-08-23 18:04:53 +10:00
parent 7dac91ddac
commit 7f67ac3b16
2 changed files with 6 additions and 5 deletions

6
math.h
View File

@ -61,8 +61,8 @@ int divs100(int n) {
} }
//Unsigned divide by 100 //Unsigned divide by 100
unsigned int divu100(unsigned n) { unsigned long divu100(unsigned long n) {
unsigned q, r; unsigned long q, r;
q = (n >> 1) + (n >> 3) + (n >> 6) - (n >> 10) + q = (n >> 1) + (n >> 3) + (n >> 6) - (n >> 10) +
(n >> 12) + (n >> 13) - (n >> 16); (n >> 12) + (n >> 13) - (n >> 16);
q = q + (q >> 20); q = q + (q >> 20);
@ -74,7 +74,7 @@ unsigned int divu100(unsigned n) {
//Return x percent of y //Return x percent of y
//This is a relatively fast approximation of a percentage value. //This is a relatively fast approximation of a percentage value.
unsigned int percentage(byte x, unsigned int y) unsigned long percentage(byte x, unsigned long y)
{ {
return divu100(y) * x; return divu100(y) * x;
} }

View File

@ -564,7 +564,7 @@ void loop()
//Check that the duty cycle of the chosen pulsewidth isn't too high. This is disabled at cranking //Check that the duty cycle of the chosen pulsewidth isn't too high. This is disabled at cranking
if( !BIT_CHECK(currentStatus.engine, BIT_ENGINE_CRANK) ) if( !BIT_CHECK(currentStatus.engine, BIT_ENGINE_CRANK) )
{ {
unsigned int pwLimit = percentage(configPage1.dutyLim, revolutionTime); //The pulsewidth limit is determined to be the duty cycle limit (Eg 85%) by the total time it takes to perform 1 revolution unsigned long pwLimit = percentage(configPage1.dutyLim, revolutionTime); //The pulsewidth limit is determined to be the duty cycle limit (Eg 85%) by the total time it takes to perform 1 revolution
if (currentStatus.PW > pwLimit) { currentStatus.PW = pwLimit; } if (currentStatus.PW > pwLimit) { currentStatus.PW = pwLimit; }
} }
@ -777,7 +777,8 @@ void loop()
//Check for hard cut rev limit (If we're above the hardcut limit, we simply don't set a spark schedule) //Check for hard cut rev limit (If we're above the hardcut limit, we simply don't set a spark schedule)
if(ignitionOn && (currentStatus.RPM < ((unsigned int)(configPage2.HardRevLim) * 100) )) if(ignitionOn && (currentStatus.RPM < ((unsigned int)(configPage2.HardRevLim) * 100) ))
{ {
if ( (ignition1StartAngle > crankAngle) ) //if ( (ignition1StartAngle > crankAngle) && ign1LastRev != startRevolutions)
if ( (ignition1StartAngle > crankAngle) && ign1LastRev != startRevolutions)
{ {
setIgnitionSchedule1(beginCoil1Charge, setIgnitionSchedule1(beginCoil1Charge,
((unsigned long)(ignition1StartAngle - crankAngle) * (unsigned long)timePerDegree), ((unsigned long)(ignition1StartAngle - crankAngle) * (unsigned long)timePerDegree),