Sets ticks directly for heater test

Committer:
seoirsem
Date:
Mon Aug 12 10:41:37 2019 +0000
Revision:
23:65a9f9d85a3a
Parent:
22:2d34a03ae57e
V2 heater set PWM ticks directly;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
omatthews 0:4e33cc8171f4 1 /*------------------------------------------------------------------------------
omatthews 0:4e33cc8171f4 2 Library code file for interface to Heater
omatthews 0:4e33cc8171f4 3 Date: 16/07/2018
omatthews 0:4e33cc8171f4 4
omatthews 0:4e33cc8171f4 5
omatthews 0:4e33cc8171f4 6 ------------------------------------------------------------------------------*/
omatthews 1:4435d407d827 7 #include "mbed.h"
omatthews 1:4435d407d827 8 #include "MODSERIAL.h"
omatthews 0:4e33cc8171f4 9 #include "Heater.h"
omatthews 0:4e33cc8171f4 10 #include "ADS8568_ADC.h"
omatthews 0:4e33cc8171f4 11
omatthews 0:4e33cc8171f4 12 extern ADS8568_ADC adc;
omatthews 0:4e33cc8171f4 13 extern Timer timer;
omatthews 1:4435d407d827 14 extern DigitalIn adc_busy;
omatthews 1:4435d407d827 15 extern MODSERIAL pc;
omatthews 11:785a0329f802 16 extern int log_count;
omatthews 11:785a0329f802 17 extern float R_avg;
omatthews 22:2d34a03ae57e 18 extern DigitalOut led_0;
omatthews 0:4e33cc8171f4 19
omatthews 0:4e33cc8171f4 20
seoirsem 23:65a9f9d85a3a 21 Heater::Heater(const int i_port, const int v_port, FastPWM * drive, FastPWM * guard, const float intercept, const float slope, float R_ref)
seoirsem 23:65a9f9d85a3a 22 :i_port(i_port),v_port(v_port),drive(drive),guard(guard),intercept(intercept),slope(slope) {}
omatthews 18:f5d26d3d532f 23
omatthews 18:f5d26d3d532f 24 void Heater::output()const
omatthews 11:785a0329f802 25 {
omatthews 18:f5d26d3d532f 26 //Prints the current state to the terminal
omatthews 17:0bfed0e96927 27 pc.printf("%d,%f,%f,%f,%f,%f\n",timer.read_ms(),R_ref,R,error,error_integrated,drive->read());
omatthews 11:785a0329f802 28 }
omatthews 0:4e33cc8171f4 29
omatthews 2:7f15386fcc90 30 void Heater::read()
omatthews 0:4e33cc8171f4 31 {
omatthews 1:4435d407d827 32 //Reads R and then resets the drive back to its previous value
omatthews 17:0bfed0e96927 33
omatthews 1:4435d407d827 34 int i = 0;
omatthews 18:f5d26d3d532f 35 //float error_prev = error;
omatthews 17:0bfed0e96927 36
omatthews 17:0bfed0e96927 37 double drive_prev = drive->read(); //Store previous value of drive
omatthews 22:2d34a03ae57e 38 //drive->period_ticks(15); //Set period to 1us for the measurement
omatthews 22:2d34a03ae57e 39 //guard->period_ticks(15);
omatthews 18:f5d26d3d532f 40 *drive = 1.0f; //Turn the driver on for the measurement
omatthews 22:2d34a03ae57e 41 //led_0 = 1;
omatthews 7:59ece353eea2 42 wait_us(MEAS_DELAY); //Wait for ADC to settle
omatthews 22:2d34a03ae57e 43 //led_0 = 0;
omatthews 1:4435d407d827 44 adc.start_conversion(ALL_CH);
omatthews 18:f5d26d3d532f 45 //Incremental back off until ADC is free
omatthews 1:4435d407d827 46 while(adc_busy == 1)
omatthews 1:4435d407d827 47 {
omatthews 1:4435d407d827 48 wait_us(1);
omatthews 1:4435d407d827 49 i++;
omatthews 18:f5d26d3d532f 50 }
omatthews 22:2d34a03ae57e 51 drive->write(0); //Reset the duty cycle back to what it was
omatthews 22:2d34a03ae57e 52 //drive->period_ticks(1000); //Reset the period to what it was
omatthews 22:2d34a03ae57e 53 //guard->period_ticks(1000);
omatthews 17:0bfed0e96927 54
omatthews 18:f5d26d3d532f 55
omatthews 18:f5d26d3d532f 56 //Get voltage, current and R values from the ADC conversion
omatthews 0:4e33cc8171f4 57 adc.read_channels();
omatthews 7:59ece353eea2 58 curr = adc.read_channel_result(i_port);
omatthews 7:59ece353eea2 59 v = adc.read_channel_result(v_port);
omatthews 18:f5d26d3d532f 60
omatthews 17:0bfed0e96927 61 if (curr > 0) {R = (float)v/curr;} //Avoid dividing by 0
omatthews 12:8a048f111140 62
omatthews 18:f5d26d3d532f 63 //Get error values
omatthews 18:f5d26d3d532f 64
omatthews 17:0bfed0e96927 65 error = R_ref - R;
omatthews 18:f5d26d3d532f 66 //error_diff = (error - error_prev)/WAIT_DELAY;
omatthews 14:f266bf960b8d 67
omatthews 19:fccdd7127f94 68 //Avoid integral windup by limiting error past actuation saturation (actuator does saturate for any negative error, but to ensure integrated error can decrease, the limit has been set to the negative of the positive limit
omatthews 19:fccdd7127f94 69 //if (error*Kp > WIND_UP_LIMIT) {error_integrated += WIND_UP_LIMIT/Kp;}
omatthews 19:fccdd7127f94 70 //else if (error*Kp < -WIND_UP_LIMIT) {error_integrated -= WIND_UP_LIMIT/Kp;}
omatthews 22:2d34a03ae57e 71 if (abs(error) < WIND_UP_LIMIT) {error_integrated += error;}
omatthews 22:2d34a03ae57e 72 if (error_integrated < -0.0) {error_integrated = -0.0;}
omatthews 22:2d34a03ae57e 73
omatthews 0:4e33cc8171f4 74 }
omatthews 0:4e33cc8171f4 75
omatthews 2:7f15386fcc90 76
omatthews 18:f5d26d3d532f 77 void Heater::hold(const int hold_time)
omatthews 0:4e33cc8171f4 78 {
omatthews 7:59ece353eea2 79 //Holds the heater at R_ref for the given hold time
omatthews 7:59ece353eea2 80 // in: int hold_time - is the time in ms to hold the reference
omatthews 7:59ece353eea2 81
omatthews 0:4e33cc8171f4 82 int end_time = timer.read_ms() + hold_time;
omatthews 1:4435d407d827 83 while (timer.read_ms() < end_time)
omatthews 0:4e33cc8171f4 84 {
omatthews 2:7f15386fcc90 85 read();
seoirsem 23:65a9f9d85a3a 86 drive->period_ticks(floor((double) (Kp * (error + error_integrated/Ti))));
seoirsem 23:65a9f9d85a3a 87 guard->period_ticks((double) (Kp * GUARD_PWM_RATIO * (error + error_integrated/Ti)));
omatthews 22:2d34a03ae57e 88 //Output the error every LOG_LIM reads
omatthews 22:2d34a03ae57e 89
omatthews 22:2d34a03ae57e 90 log_count++;
omatthews 22:2d34a03ae57e 91 if (log_count >= LOG_LIM)
omatthews 22:2d34a03ae57e 92 {
omatthews 22:2d34a03ae57e 93 log_count = 0;
omatthews 22:2d34a03ae57e 94 output();
omatthews 22:2d34a03ae57e 95 }
omatthews 18:f5d26d3d532f 96 wait_ms(WAIT_DELAY); //Wait before reading again
omatthews 0:4e33cc8171f4 97 }
omatthews 1:4435d407d827 98 }
omatthews 1:4435d407d827 99
omatthews 0:4e33cc8171f4 100
omatthews 18:f5d26d3d532f 101 void Heater::ramp_R(const int ramp_time, const float R_final, const float R_start)
omatthews 7:59ece353eea2 102 {
omatthews 18:f5d26d3d532f 103 //Ramps the heater from R_start to R_final for the given hold time
omatthews 18:f5d26d3d532f 104 // in: int hold_time - is the time in ms to hold the reference
omatthews 18:f5d26d3d532f 105 // float R_final - is the final R_ref value
omatthews 18:f5d26d3d532f 106 // float R_start - is the initial R_ref value
omatthews 18:f5d26d3d532f 107
omatthews 8:5da71ae16115 108 int time = timer.read_ms();
omatthews 8:5da71ae16115 109 int start_time = time;
omatthews 7:59ece353eea2 110 int end_time = start_time + ramp_time;
omatthews 7:59ece353eea2 111 float ramp_rate = (R_final - R_start)/ramp_time;
omatthews 7:59ece353eea2 112
omatthews 8:5da71ae16115 113 while (time < end_time)
omatthews 7:59ece353eea2 114 {
omatthews 8:5da71ae16115 115 Set_R_ref(R_start + ramp_rate * (time - start_time));
omatthews 7:59ece353eea2 116 hold(1);
omatthews 8:5da71ae16115 117 time = timer.read_ms();
omatthews 7:59ece353eea2 118 }
omatthews 8:5da71ae16115 119
omatthews 7:59ece353eea2 120 }
omatthews 18:f5d26d3d532f 121
omatthews 18:f5d26d3d532f 122
seoirsem 23:65a9f9d85a3a 123 void Heater::Set_R_ref(float R)
omatthews 19:fccdd7127f94 124 {
omatthews 19:fccdd7127f94 125 R_ref = R;
omatthews 19:fccdd7127f94 126 error_integrated = 0;
omatthews 19:fccdd7127f94 127 }
seoirsem 23:65a9f9d85a3a 128
omatthews 18:f5d26d3d532f 129 float Heater::Get_R() const {return R;}
omatthews 0:4e33cc8171f4 130
omatthews 1:4435d407d827 131
omatthews 19:fccdd7127f94 132 void Heater::turn_off ()
omatthews 19:fccdd7127f94 133 {
omatthews 19:fccdd7127f94 134 *drive = 0;
omatthews 19:fccdd7127f94 135 *guard = 0;
omatthews 19:fccdd7127f94 136 }