Initial commit
Dependencies: FastPWM Lamp_Intensity_Lock_withTempCo mbed
Fork of Lamp_Intensity_Lock_withTempCo by
Diff: main.cpp
- Revision:
- 2:1b142e2aa23e
- Parent:
- 1:2d9d931c8484
- Child:
- 3:a8ec3c6aeb08
--- a/main.cpp Mon Jan 09 14:35:41 2017 +0000 +++ b/main.cpp Fri Jan 13 14:41:31 2017 +0000 @@ -7,7 +7,9 @@ const uint8_t ALSDataRegister = 0x14; const uint8_t waitIntervals = 100; //number of 2.73 ms waits const int measPeriod_ms = 699; - +const float tempRef = 29.5; //C temperature at which all optical powers are calculated to +const float Ch0tempCo = 0.0025; // % per degree C +const float warmUp = 15; //number of measurements with a higher than usual pGain (to get to target faster); I2C i2c(I2C_SDA,I2C_SCL); Serial pc(USBTX,USBRX); //open serial port (optionally add baud rate after specifying TX and RX pins) @@ -109,26 +111,36 @@ writeRegister(TMD2772_Addr,(0x0F | 0x80),0x00); //ALS gain is 1x } +float tempCorrectTMDCh0(float counts, float tempC) +{ + float tDiff = tempC-tempRef; + float delta = Ch0tempCo*tDiff; //the % difference observed vs. reference temperature + return counts *(1-delta); //the count value equivalent if measured at reference temperature (less counts if temp is higher) + +} + + int main() { float ratio; char data[4]; uint16_t ch0Data; uint16_t ch1Data; - float setpoint = 2.1148; //ch0/ch1 color setpoint ~0.88 duty cycle - float step; //duty cycle change per sample - float dutyCycle=0.926; + float setpoint = 37250; //ch0/ch1 color setpoint ~0.88 duty cycle + static float step; //duty cycle change per sample + float dutyCycle=0.9275; float dutyCycleMin =0.8; - float dutyCycleMax =0.99; + float dutyCycleMax =0.98; float stepMax=0.0025; float stepMin=-0.0025; // float iGain = 0.05; //integral gain --adding this because when I blew on it, it couldn't recover float err; - float tol=1e-4; //tolerance within which to ignore changes in signal intensity - float pGain = 0.2; //proportional gain - float quadGain = 0; // 250 : 0.2 ratio relative to pGain + float tol=1; //tolerance within which to ignore changes in signal intensity + float pGain = 0.25; //proportional gain +// float quadGain = 0;*/ + static float temperature = 0; //float ch0Avg; //integral error // float filterLength = 15; - + float tempFilterLength = 8; //setup everything mypwm.period_us(400); mypwm.write(dutyCycle); @@ -138,6 +150,7 @@ regDump(TMD2772_Addr,(0x00 | 0x80),0x0F); pc.printf("Done initializing\r\n"); wait_ms(700); + static int loopCount =0; //get initial filter value // reg2write=ALSDataRegister | 0x80; @@ -152,6 +165,15 @@ while(1) { + loopCount++; + if (loopCount<warmUp) + { + pGain =0.25; + } + else + { + pGain =0.2; + } t.start(); readRegisters(TMD2772_Addr, (ALSDataRegister | 0x80), data ,4); //reg2write=ALSDataRegister | 0x80; @@ -159,15 +181,24 @@ // i2c.write(TMD2772_Addr,®2write,1,true); //1 byte of data, repeated start for read // i2c.read(TMD2772_Addr,data,4); ch0Data = LSB_MSB_2uint16(data); + if (temperature ==0) + { + temperature = getTemp(MAX31725_Addr); + } + else + { + temperature += (getTemp(MAX31725_Addr)-temperature)/tempFilterLength ; + } + ch0Data = tempCorrectTMDCh0(ch0Data,temperature); ch1Data = LSB_MSB_2uint16(data+2); ratio = (float)ch0Data/(float)ch1Data; - err = ratio - setpoint; - pc.printf( "%U,%U, %f, %f, %f\r\n",ch0Data,ch1Data,ratio,mypwm.read(),getTemp(MAX31725_Addr) ); + err = ch0Data - setpoint; + pc.printf( "%U,%U, %f, %f, %f, %f\r\n",ch0Data,ch1Data,ratio,mypwm.read(),temperature, step); if (abs(err)>tol) { - step = err * pGain + err/abs(err)*pow(err,2)*quadGain; + step = err/setpoint * pGain ; step = (step > stepMax) ? stepMax : step; step = (step < stepMin) ? stepMin : step; - dutyCycle -=step; + dutyCycle -= step; dutyCycle = (dutyCycle < dutyCycleMin) ? dutyCycleMin : dutyCycle; dutyCycle = (dutyCycle > dutyCycleMax) ? dutyCycleMax : dutyCycle; //update with new settings