Heater for threaded program
Dependents: LEX_Threaded_Programming_V3
Heater.cpp
- Committer:
- omatthews
- Date:
- 2019-08-19
- Revision:
- 25:09a315a59956
- Parent:
- 23:947850bbf325
- Child:
- 26:f6c98b05ee85
File content as of revision 25:09a315a59956:
/*------------------------------------------------------------------------------ Library code file for interface to Heater Date: 16/07/2018 ------------------------------------------------------------------------------*/ #include "mbed.h" #include "MODSERIAL.h" #include "Heater.h" #include "ADS8568_ADC.h" extern ADS8568_ADC adc; extern Timer timer; extern DigitalIn adc_busy; extern MODSERIAL pc; extern DigitalOut led_0; Heater::Heater(const int i_port, const int v_port, FastPWM * drive, FastPWM * guard, const float corr_grad, const float corr_int, float R_ref) :R_ref(R_ref),i_port(i_port),v_port(v_port),drive(drive),guard(guard),corr_grad(corr_grad),corr_int(corr_int) {} void Heater::output()const { //Prints the current state to the terminal pc.printf("%d,%f,%f,%f,%f,%f\n",timer.read_ms(),R_ref,R,error,error_integrated,drive->read()); } void Heater::read() { //Reads R and then resets the drive back to its previous value int i = 0; //float error_prev = error; double drive_prev = drive->read(); //Store previous value of drive *drive = 1.0f; //Turn the driver on for the measurement wait_us(MEAS_DELAY); //Wait for ADC to settle adc.start_conversion(15); //Incremental back off until ADC is free while(adc_busy == 1) { wait_us(1); i++; } drive->write(0); //Reset the duty cycle back to what it was //Get voltage, current and R values from the ADC conversion adc.read_channels(); curr = adc.read_channel_result(i_port); v = adc.read_channel_result(v_port); if (curr > 0) {R = (float)v/curr;} //Avoid dividing by 0 //Get error values error = R_ref - R; //Only allow positive integrated errors and limit change in integrated error //to help avoid integral windup if (abs(error) < WIND_UP_LIMIT) {error_integrated += error;} if (error_integrated < 0.0) {error_integrated = 0.0;} } void Heater::update() { //Update PWM from setpoint and resistance read(); drive->write((double) (Kp * (error + error_integrated/Ti))); guard->write((double) (Kp * GUARD_PWM_RATIO * (error + error_integrated/Ti))); log_count++; if (log_count >= LOG_LIM) { log_count = 0; output(); } } void Heater::Set_ref(float R) { R_ref = R; } void Heater::Set_D(float D) { drive->write(D); guard->write(D*GUARD_PWM_RATIO); } int Heater::Get_i() const {return curr;} int Heater::Get_v() const {return v;} float Heater::Get_R() const {return R;} void Heater::turn_on () { *drive = 1; *guard = GUARD_PWM_RATIO; } void Heater::turn_off () { *drive = 0; *guard = 0; }