1
0
forked from me/IronOS

Work in progress

Working, but has temp offset issue slightly.

Could have slightly wrong gain values
This commit is contained in:
Ben V. Brown
2019-10-07 21:11:31 +11:00
parent 3e1abc451c
commit 6a39e4bcc8
9 changed files with 67 additions and 83 deletions

View File

@@ -12,33 +12,30 @@
const uint16_t powerPWM = 255;
const uint16_t totalPWM = 255 + 17; //htim2.Init.Period, the full PWM cycle
history<uint32_t, oscillationPeriod> milliWattHistory = { { 0 }, 0, 0 };
history<uint32_t, oscillationPeriod> x10WattHistory = { { 0 }, 0, 0 };
int32_t tempToMilliWatts(int32_t rawTemp) {
int32_t tempToX10Watts(int32_t rawTemp) {
// mass is in milliJ/*C, rawC is raw per degree C
// returns milliWatts needed to raise/lower a mass by rawTemp
// degrees in one cycle.
int32_t milliJoules = tipMass*10 * rawTemp;
int32_t milliJoules = tipMass * rawTemp;
return milliJoules;
}
void setTipMilliWatts(int32_t mw) {
//Enforce Max Watts Limiter # TODO
int32_t output = milliWattsToPWM(mw, systemSettings.voltageDiv , 1);
void setTipX10Watts(int32_t mw) {
int32_t output = X10WattsToPWM(mw, 1);
setTipPWM(output);
uint32_t actualMilliWatts = PWMToMilliWatts(output,
systemSettings.voltageDiv , 0);
uint32_t actualMilliWatts = PWMToX10Watts(output, 0);
milliWattHistory.update(actualMilliWatts);
x10WattHistory.update(actualMilliWatts);
}
int32_t availableW10(uint8_t divisor, uint8_t sample) {
uint32_t availableW10(uint8_t sample) {
//P = V^2 / R, v*v = v^2 * 100
// R = R*10
// P therefore is in V^2*100/R*10 = W*10.
int32_t v = getInputVoltageX10(divisor, sample); // 100 = 10v
int32_t availableWattsX10 = (v * v) / tipResistance;
uint32_t v = getInputVoltageX10(systemSettings.voltageDiv, sample); // 100 = 10v
uint32_t availableWattsX10 = (v * v) / tipResistance;
//However, 100% duty cycle is not possible as there is a dead time while the ADC takes a reading
//Therefore need to scale available milliwats by this
@@ -50,27 +47,23 @@ int32_t availableW10(uint8_t divisor, uint8_t sample) {
return availableWattsX10;
}
uint8_t milliWattsToPWM(int32_t milliWatts, uint8_t divisor, uint8_t sample) {
// Scale input milliWatts to the pwm rate
if (milliWatts < 10) // no pint driving tip
uint8_t X10WattsToPWM(int32_t milliWatts, uint8_t sample) {
// Scale input milliWatts to the pwm range available
if (milliWatts < 1)
return 0;
// if (milliWatts > (int(systemSettings.pidPowerLimit) * 10))
// milliWatts = (int(systemSettings.pidPowerLimit) * 10);
//Calculate desired milliwatts as a percentage of availableW10
int32_t pwm = (powerPWM * milliWatts) / availableW10(divisor, sample);
uint32_t pwm = (powerPWM * milliWatts) / availableW10(sample);
if (pwm > powerPWM) {
pwm = powerPWM; //constrain to max PWM counter, shouldnt be possible, but small cost for safety to avoid wraps
} else if (pwm < 0) { //cannot go negative
pwm = 0;
}
return pwm;
}
int32_t PWMToMilliWatts(uint8_t pwm, uint8_t divisor, uint8_t sample) {
int32_t maxMW = availableW10(divisor, sample); //Get the milliwatts for the max pwm period
int32_t PWMToX10Watts(uint8_t pwm, uint8_t sample) {
uint32_t maxMW = availableW10(sample); //Get the milliwatts for the max pwm period
//Then convert pwm into percentage of powerPWM to get the percentage of the max mw
int32_t res = (pwm * maxMW) / powerPWM;
if (res < 0)
res = 0;
return res;
return (((uint32_t) pwm) * maxMW) / powerPWM;
}