LEX_Threaded_Programming

Dependencies:   Heater_V2 MODSERIAL Nanopb FastPWM ADS8568_ADC

Revision:
6:ef2bcc5fe3af
Parent:
5:702b32ead94e
Child:
7:a4fc853feb30
--- a/main.cpp	Thu Aug 29 16:08:04 2019 +0000
+++ b/main.cpp	Fri Aug 30 15:00:29 2019 +0000
@@ -46,8 +46,8 @@
 
 
 //Threads
-Thread heater_control;
-Thread logging_thread;
+Thread heater_control(osPriorityHigh);
+Thread logging_thread(osPriorityAboveNormal);
 
 //Tickers
 Ticker heat_tick;
@@ -124,16 +124,19 @@
 
 //Ticking functions_________________________________________________________________
 
+
 void temp_trigger()
 {
     //This function triggers a temperature update.
     //N.B. update cannot be called directly from a ticker as tickers and
     //reading the ADC both rely on interrupts.
     flags.set(0x1);
-    led_0 = !led_0;
+      
 
 }
 
+
+
 void log_trigger()
 {
     flags.set(0x2);
@@ -160,11 +163,7 @@
 
 void temp_control() {
     while(1){
-        flags.wait_any(0x1);
-        led_1 = !led_1;
-
-        flags.clear(0x1);
-
+        flags.wait_any(0x1,osWaitForever,true);
         heater->read();
         heater->update();
         } 
@@ -173,9 +172,9 @@
 void log_state()
 {
     while(1){
-        flags.wait_any(0x2);
-        flags.clear(0x2);
-        heater->log();
+        flags.wait_any(0x2,osWaitForever,true);
+        //Output time, R_ref, R, error, error_integrated
+        pc.printf("%d,%f,%f,%f,%f",timer.read_ms(),heater->Get_R_ref(),heater->Get_R(),heater->Get_error(),heater->Get_error_integrated());
     }
 }
 
@@ -240,8 +239,8 @@
 
 
 
-    Heater * heater_main = new Heater(i_port_main, v_port_main, & drive_main, & guard_main, exp_config.thermal);
-    Heater * heater_lysis = new Heater(i_port_lysis, v_port_lysis, & drive_lysis, & guard_lysis, exp_config.thermal);
+    Heater * heater_main = new Heater(i_port_main, v_port_main, & drive_main, & guard_main, & adc, adc_busy, exp_config.thermal);
+    Heater * heater_lysis = new Heater(i_port_lysis, v_port_lysis, & drive_lysis, & guard_lysis, & adc, adc_busy, exp_config.thermal);
 
     //Select heater. Put control times in us for ticker functions
     if (exp_config.selected_heater == memspcr_ExperimentConfiguration_Heater_MAIN) {
@@ -256,21 +255,24 @@
     pc.printf("\nStarting pressure control\n");
     pressure_tick.attach(& pressure_control, 1);
     pc.printf("\nWaiting for start signal to begin temperature control (type in an s or press button 0)\n");
-
+    heater->Set_ref(0.0);
+    heater_control.start(& temp_control);
+    heat_tick.attach_us(& temp_trigger,exp_config.thermal.control_loop_interval_ms * 1000);
+    
     while (pc.getcNb()!='s' && !user_0);
 
     pc.printf("Start signal received");
-    heater_control.start(& temp_control);
 
-    heat_tick.attach_us(& temp_trigger,exp_config.thermal.control_loop_interval_ms * 500);
 
     logging_thread.start(& log_state);
-    log_tick.attach_us(& log_trigger,exp_config.logging_interval_ms * 500);
+    log_tick.attach_us(& log_trigger,exp_config.logging_interval_ms * 1000);
 
 
     pc.printf("\nStarting routine\n");
     timer.start();
     set_point_routine(profile);
+    
+    //Turn off
     heat_tick.detach();
     log_tick.detach();
     wait(1);