LEX_Threaded_Programming

Dependencies:   Heater_V2 MODSERIAL Nanopb FastPWM ADS8568_ADC

Revision:
17:d86e749ae9be
Parent:
16:32b598af6f86
Child:
18:376350fb7ef9
Child:
21:1f233639f0ce
--- a/main.cpp	Fri Sep 20 08:04:50 2019 +0000
+++ b/main.cpp	Fri Sep 20 14:54:15 2019 +0000
@@ -11,33 +11,51 @@
 #include <iterator>
 
 #define BUFFER_SIZE 8192
-#define LED_PULSE_PERIOD 1000    // ticks, 84kHz when CPU clock is 84MHz
-#define LED_PULSE_WIDTH   100    // ticks, 10% duty cycle
+
+//UID lookup address and pointer
+#define UID_ADDR 0x1FFF7A10
+unsigned long *uid = (unsigned long *) UID_ADDR; 
+
+//UID and drive board calibration table
+#define UID_TABLE_LENGTH 4
+
+int drive_board_serial_number[UID_TABLE_LENGTH] =
+  {1,
+   2,
+   3,
+   4};
+
+unsigned long drive_board_uid[UID_TABLE_LENGTH][3] =
+ {{0x005B0060, 0x32375101, 0x32363531},
+  {0x0051003D, 0x32375114, 0x30333732},
+  {0x00520060, 0x32375101, 0x32363531},
+  {0x00570060, 0x32375101, 0x32363531}};
+
+float drive_board_cal[UID_TABLE_LENGTH][2][2] = 
+ {{{0.096724353, 10.1817431}, {0.056098807, 10.19962849}},
+  {{0.059473025, 10.14814327}, {0.03200058, 10.25073923}},
+  {{0.01887149,  10.39360225}, {0.03115874, 10.28199855}},
+  {{0.052545339, 10.06008621}, {0.094239471, 10.11983777}}}; 
+  
+float drive_cal[2] = {0.0, 1.0};
 
 Heater * heater;
 float r_gradient; //setpoint setting
 
-
 MODSERIAL pc(PA_9, PA_10, BUFFER_SIZE); //mcu TX, RX, BUFFER_SIZE byte TX and RX buffers
 ADS8568_ADC adc(PB_15, PB_14, PB_13, PB_12, PC_15, PC_0, PC_1, PC_2, PC_3);
 I2C i2c(PB_7, PB_8);            //SDA, SCL
 Timer timer;
 DigitalIn adc_busy(PA_8);                   //Busy interrupt sig#
 
-//Pressure Control
+//Fluidic Control
 DigitalOut pump(PA_2);
 AnalogIn pressure_1(PA_5);
-//set up pressure read averaging
 
-float pressure_fAvg = 0.1;    // rolling average
-
-float pressure_in = 0.0;
-float pressure_out = 0.0;
-
-float pressure_set = 0.46;
-float pressure_hys = 0.02;
-float pressure_set_low = pressure_set - pressure_hys/2;
-float pressure_set_high = pressure_set + pressure_hys/2;
+float pressure_in;
+float pressure_out;
+float pressure_set_low;
+float pressure_set_high;
 
 //Heater Control
 FastPWM drive_main(PC_9);
@@ -178,6 +196,7 @@
         flags.wait_any(0x1,osWaitForever,true);
         heater->read();
         heater->update();
+        wait_us(200);//Give other threads time to get selected
         } 
     }
 
@@ -187,27 +206,23 @@
         flags.wait_any(0x2,osWaitForever,true);
         //Output time, R_ref, R, error, error_integrated
         pc.printf("%10d,%10d,%10.6f,%10.6f,%10.6f,%10.6f\n", heater_ID, timer.read_ms(), heater->Get_R(), heater->Get_R_ref(), pressure_in, pressure_out);
+        wait_us(200);//Give other threads time to get selected
     }
 }
 
 void pressure_control() {
     while(1){
         flags.wait_any(0x3,osWaitForever,true);
-        pressure_in = pressure_in*(1.0-pressure_fAvg) + pressure_1.read()*pressure_fAvg;
-        //pressure_in = 0;
-        //for (int i = 0; i < pressure_nAvg; i++) pressure_in += pressure_1.read();  //read pressure
-        //pressure_in = pressure_in / pressure_nAvg;
-//        pc.printf("%10.6f %10.6f %10.6f\n", pressure_set_low, pressure_set_high, pressure_in);
+        pressure_in = pressure_1.read();
         if (pressure_in < pressure_set_low) {
             led_1 = 1;
             pump = 1;
-//            pc.printf(" pump on\n");
         }
         else if (pressure_in > pressure_set_high) {
             led_1 = 0;
             pump = 0;
-//            pc.printf(" pump off\n");
         }
+        wait_us(200);//Give other threads time to get selected
     }
 }
 
@@ -252,6 +267,20 @@
 {
     pc.baud(115200);
     adc.init();
+    
+    pc.printf("\r\nUnique ID: %08X %08X %08X \r\n", uid[0], uid[1], uid[2]);   
+    int i_board = -1;  
+    for (int i = 0; i < UID_TABLE_LENGTH; i++)
+    {
+      if (uid[0]==drive_board_uid[i][0] && uid[1]==drive_board_uid[i][1] && uid[2]==drive_board_uid[i][2])
+        {
+            i_board = i;
+            i = UID_TABLE_LENGTH;
+        }
+    }
+    
+    if (i_board != -1) pc.printf("Drive board: Board %d\n",drive_board_serial_number[i_board]);
+    else pc.printf("Drive board UID match not found\n");
 
     buffer_length = sizeof(buffer)/sizeof(uint8_t);
     pc.printf("# Input configuration file\n");
@@ -283,6 +312,16 @@
         return 1;
     }
 
+    //Set drive ADC->Resistance calibration coefficients
+    if (i_board != -1)
+        for (int i=0; i<2; i++) drive_cal[i] = drive_board_cal[i_board][heater_ID-1][i];
+    else {
+        drive_cal[0] = 0.0;
+        drive_cal[1] = 1.0;
+    }
+        
+    pc.printf("Heater: %d drive_cal[0]: %10.6f drive_cal[1]: %10.6f\n", heater_ID, drive_cal[0], drive_cal[1]);
+
     pc.printf("# Starting pressure control\n");    
     pressure_thread.start(& pressure_control);
     pressure_tick.attach_us(& pressure_trigger, 500000);