[chronojump] Major change on ForceSensor threads



commit 42bb559722b580645a526731125d22cd1dbb0076
Author: Xavier de Blas <xaviblas gmail com>
Date:   Mon Oct 9 20:34:53 2017 +0200

    Major change on ForceSensor threads

 glade/app1.glade       |   48 ++++--------
 src/gui/chronojump.cs  |    4 +-
 src/gui/forceSensor.cs |  214 +++++++++++++++++++++++++++++++++++------------
 3 files changed, 177 insertions(+), 89 deletions(-)
---
diff --git a/glade/app1.glade b/glade/app1.glade
index 6bb4710..623d97c 100644
--- a/glade/app1.glade
+++ b/glade/app1.glade
@@ -1375,6 +1375,12 @@
                                                             <placeholder/>
                                                             </child>
                                                             <child>
+                                                            <placeholder/>
+                                                            </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
+                                                            <child>
                                                             <widget class="GtkLabel" 
id="label_start_selector_jumps">
                                                             <property name="visible">True</property>
                                                             <property name="can_focus">False</property>
@@ -4603,36 +4609,6 @@ EncoderInertialCapture</property>
                                                             <property name="position">1</property>
                                                             </packing>
                                                             </child>
-                                                            <child>
-                                                            <widget class="GtkButton" 
id="button_force_sensor_connect">
-                                                            <property name="label" 
translatable="yes">Connect</property>
-                                                            <property name="visible">True</property>
-                                                            <property name="can_focus">True</property>
-                                                            <property name="receives_default">True</property>
-                                                            <signal name="clicked" 
handler="on_button_force_sensor_connect_clicked" swapped="no"/>
-                                                            </widget>
-                                                            <packing>
-                                                            <property name="expand">False</property>
-                                                            <property name="fill">False</property>
-                                                            <property name="padding">20</property>
-                                                            <property name="position">2</property>
-                                                            </packing>
-                                                            </child>
-                                                            <child>
-                                                            <widget class="GtkButton" 
id="button_force_sensor_disconnect1">
-                                                            <property name="label" 
translatable="yes">Disconnect</property>
-                                                            <property name="visible">True</property>
-                                                            <property name="can_focus">True</property>
-                                                            <property name="receives_default">True</property>
-                                                            <signal name="clicked" 
handler="on_button_force_sensor_disconnect_clicked" swapped="no"/>
-                                                            </widget>
-                                                            <packing>
-                                                            <property name="expand">False</property>
-                                                            <property name="fill">False</property>
-                                                            <property name="padding">20</property>
-                                                            <property name="position">3</property>
-                                                            </packing>
-                                                            </child>
                                                             </widget>
                                                             </child>
                                                           </widget>
@@ -6300,7 +6276,7 @@ after time</property>
                                                             <property name="visible">True</property>
                                                             <property name="can_focus">True</property>
                                                             <property name="receives_default">True</property>
-                                                            <signal name="clicked" 
handler="on_button_force_sensor_tare_clicked" swapped="no"/>
+                                                            <signal name="clicked" 
handler="on_buttons_force_sensor_clicked" swapped="no"/>
                                                             </widget>
                                                             <packing>
                                                             <property name="expand">False</property>
@@ -6329,7 +6305,7 @@ after time</property>
                                                             <property 
name="secondary_icon_activatable">False</property>
                                                             <property 
name="primary_icon_sensitive">True</property>
                                                             <property 
name="secondary_icon_sensitive">True</property>
-                                                            <property name="adjustment">20 0 500 1 10 
0</property>
+                                                            <property name="adjustment">20 5 500 1 10 
0</property>
                                                             <property name="climb_rate">1</property>
                                                             <property name="digits">2</property>
                                                             <property name="numeric">True</property>
@@ -6366,7 +6342,7 @@ after time</property>
                                                             <property name="visible">True</property>
                                                             <property name="can_focus">True</property>
                                                             <property name="receives_default">True</property>
-                                                            <signal name="clicked" 
handler="on_button_force_sensor_calibrate_clicked" swapped="no"/>
+                                                            <signal name="clicked" 
handler="on_buttons_force_sensor_clicked" swapped="no"/>
                                                             </widget>
                                                             <packing>
                                                             <property name="expand">False</property>
@@ -21500,6 +21476,12 @@ then click this button.</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             <packing>
                                                             <property name="expand">False</property>
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index b9b48e7..9f2fbe6 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -2153,6 +2153,8 @@ public partial class ChronoJumpWindow
                        LogB.Information("cancelling force capture");
                        forceProcessCancel = true;
                }
+               if(forceOtherThread.IsAlive)
+                       forceOtherThread.Abort();
                if(portFSOpened)
                        portFS.Close();
 
@@ -3705,7 +3707,7 @@ public partial class ChronoJumpWindow
                        if(numForceSensor == 0)
                                new DialogMessage(Constants.MessageTypes.WARNING, "Sensor not found.");
                        else
-                               forceSensorCapture();
+                               on_buttons_force_sensor_clicked(button_execute_test, new EventArgs ());
 
                        return;
                }
diff --git a/src/gui/forceSensor.cs b/src/gui/forceSensor.cs
index 056feb1..267bcc0 100644
--- a/src/gui/forceSensor.cs
+++ b/src/gui/forceSensor.cs
@@ -29,6 +29,8 @@ using System.Collections.Generic; //List<T>
 
 public partial class ChronoJumpWindow 
 {
+       [Widget] Gtk.Button button_force_sensor_tare;
+       [Widget] Gtk.Button button_force_sensor_calibrate;
        [Widget] Gtk.Label label_force_sensor_value_max;
        [Widget] Gtk.Label label_force_sensor_value;
        [Widget] Gtk.Label label_force_sensor_value_min;
@@ -37,10 +39,15 @@ public partial class ChronoJumpWindow
        [Widget] Gtk.Image image_force_sensor_graph;
        [Widget] Gtk.SpinButton spin_force_sensor_calibration_kg_value;
        
-       Thread forceThread;
+       Thread forceCaptureThread;
        static bool forceProcessFinish;
        static bool forceProcessCancel;
 
+       Thread forceOtherThread; //for messages on: capture, tare, calibrate
+       static string forceSensorOtherMessage = "";
+       static bool forceSensorOtherMessageShowSeconds;
+       static DateTime forceSensorTimeStart;
+
        /*
         * forceStatus:
         * STOP is when is not used
@@ -58,15 +65,15 @@ public partial class ChronoJumpWindow
        SerialPort portFS;
        bool portFSOpened;
 
-       //TODO: don't reopen port because arduino makes reset and tare, calibration... is lost
+       //Don't reopen port because arduino makes reset and tare, calibration... is lost
 
-       private void on_button_force_sensor_connect_clicked(object o, EventArgs args)
+       private bool forceSensorConnect()
        {
                LogB.Information(" FS connect 0 ");
                if(chronopicRegister.ConnectedOfType(ChronopicRegisterPort.Types.ARDUINO_FORCE) == null)
                {
                        new DialogMessage(Constants.MessageTypes.WARNING, "Force sensor is not connected!");
-                       return;
+                       return false;
                }
 
                LogB.Information(" FS connect 1 ");
@@ -75,35 +82,138 @@ public partial class ChronoJumpWindow
                if(forceSensorPortName == null || forceSensorPortName == "")
                {
                        new DialogMessage(Constants.MessageTypes.WARNING, "Please, select port");
-                       return;
+                       return false;
                }
                LogB.Information(" FS connect 3 ");
+               forceSensorOtherMessage = "Connecting ...";
 
                portFS = new SerialPort(forceSensorPortName, 115200); //forceSensor
                LogB.Information(" FS connect 4 ");
-               portFS.Open();
+
+               try {
+                       portFS.Open();
+               }
+               catch (System.IO.IOException)
+               {
+                       return false;
+               }
+
                portFSOpened = true;
                Thread.Sleep(2500); //sleep to let arduino start reading
-               event_execute_label_message.Text = "Connected!";
+               forceSensorOtherMessage = "Connected!";
                LogB.Information(" FS connect 5 ");
+               return true;
        }
-       private void on_button_force_sensor_disconnect_clicked(object o, EventArgs args)
+       private void forceSensorDisconnect()
        {
                portFS.Close();
                portFSOpened = false;
                event_execute_label_message.Text = "Disconnected!";
        }
 
-       private void on_button_force_sensor_tare_clicked(object o, EventArgs args)
+       //Attention: no GTK here!!
+       private bool forceSensorSendCommand(string command, string displayMessage, string errorMessage)
        {
-               if(! portFSOpened) //TODO: better check is connected!
+               forceSensorOtherMessage = displayMessage;
+
+               try {
+                       portFS.WriteLine(command);
+                       forceSensorTimeStart = DateTime.Now;
+               }
+               catch (Exception ex)
                {
-                       new DialogMessage(Constants.MessageTypes.WARNING, "Please, connect!");
-                       return;
+                       if(ex is System.IO.IOException || ex is System.TimeoutException)
+                       {
+                               LogB.Information(errorMessage);
+                               portFSOpened = false;
+                               return false;
+                       }
+                       //throw;
+               }
+               return true;
+       }
+
+       enum forceSensorOtherModeEnum { TARE, CALIBRATE, CAPTURE_PRE }
+       static forceSensorOtherModeEnum forceSensorOtherMode;
+
+       private void on_buttons_force_sensor_clicked(object o, EventArgs args)
+       {
+               capturingForce = forceStatus.STOP;
+               forceSensorButtonsSensitive(false);
+               forceSensorTimeStart = DateTime.Now;
+               forceSensorOtherMessageShowSeconds = true;
+
+               if(o == (object) button_force_sensor_tare)
+               {
+                       forceSensorOtherMode = forceSensorOtherModeEnum.TARE;
+                       forceOtherThread = new Thread(new ThreadStart(forceSensorTare));
+               }
+               else if(o == (object) button_force_sensor_calibrate)
+               {
+                       forceSensorOtherMode = forceSensorOtherModeEnum.CALIBRATE;
+                       forceOtherThread = new Thread(new ThreadStart(forceSensorCalibrate));
                }
+               else { //if (o == (object) button_execute_test)
+                       forceSensorOtherMode = forceSensorOtherModeEnum.CAPTURE_PRE;
+                       forceOtherThread = new Thread(new ThreadStart(forceSensorCapturePre));
+               }
+
+               GLib.Idle.Add (new GLib.IdleHandler (pulseGTKForceSensorOther));
 
-               event_execute_label_message.Text = "Taring ...";
-               portFS.WriteLine("tare:");
+               LogB.ThreadStart();
+               forceOtherThread.Start();
+       }
+
+       void forceSensorButtonsSensitive(bool sensitive)
+       {
+               button_force_sensor_tare.Sensitive = sensitive;
+               button_force_sensor_calibrate.Sensitive = sensitive;
+               button_execute_test.Sensitive = sensitive;
+       }
+
+       private bool pulseGTKForceSensorOther ()
+       {
+               if(forceSensorOtherMessage != "")
+               {
+                       string secondsStr = "";
+                       if(forceSensorOtherMessageShowSeconds)
+                       {
+                               TimeSpan ts = DateTime.Now.Subtract(forceSensorTimeStart);
+                               double seconds = ts.TotalSeconds;
+                               secondsStr = " (" + Util.TrimDecimals(seconds, 0) + " s)";
+
+                       }
+                       event_execute_label_message.Text = forceSensorOtherMessage + secondsStr;
+               }
+
+               if(! forceOtherThread.IsAlive)
+               {
+                       LogB.ThreadEnding();
+
+                       if(forceSensorOtherMode == forceSensorOtherModeEnum.TARE || forceSensorOtherMode == 
forceSensorOtherModeEnum.CALIBRATE)
+                               forceSensorButtonsSensitive(true);
+                       else //if(forceSensorOtherMode == forceSensorOtherModeEnum.CAPTURE_PRE)
+                               forceSensorCapturePre2();
+
+                       return false;
+               }
+
+               //LogB.Information(" ForceSensor:"+ forceOtherThread.ThreadState.ToString());
+               Thread.Sleep (25);
+               return true;
+       }
+
+       //Attention: no GTK here!!
+       private void forceSensorTare()
+       {
+               if(! portFSOpened)
+               {
+                       if(! forceSensorConnect())
+                               return;
+               }
+
+               if(! forceSensorSendCommand("tare:", "Taring ...", "Catched force taring"))
+                       return;
 
                string str = "";
                do {
@@ -113,20 +223,22 @@ public partial class ChronoJumpWindow
                }
                while(! str.Contains("Taring OK"));
 
-               event_execute_label_message.Text = "Tared!";
+               forceSensorOtherMessageShowSeconds = false;
+               forceSensorOtherMessage = "Tared!";
        }
 
-       private void on_button_force_sensor_calibrate_clicked(object o, EventArgs args)
+       //Attention: no GTK here!!
+       private void forceSensorCalibrate()
        {
-               if(! portFSOpened) //TODO: better check is connected!
+               if(! portFSOpened)
                {
-                       new DialogMessage(Constants.MessageTypes.WARNING, "Please, connect!");
-                       return;
+                       if(! forceSensorConnect())
+                               return;
                }
 
-               event_execute_label_message.Text = "Calibrating ...";
-               LogB.Information("calibrate:" + spin_force_sensor_calibration_kg_value.Value.ToString() + 
";");
-               portFS.WriteLine("calibrate:" + spin_force_sensor_calibration_kg_value.Value.ToString() + 
";");
+               if(! forceSensorSendCommand("calibrate:" + 
spin_force_sensor_calibration_kg_value.Value.ToString() + ";",
+                               "Calibrating ...", "Catched force calibrating"))
+                       return;
 
                string str = "";
                do {
@@ -136,40 +248,34 @@ public partial class ChronoJumpWindow
                }
                while(! str.Contains("Calibrating OK"));
 
-               event_execute_label_message.Text = "Calibrated!";
+               forceSensorOtherMessageShowSeconds = false;
+               forceSensorOtherMessage = "Calibrated!";
        }
 
-
-       private void forceSensorCapture()
+       //Attention: no GTK here!!
+       private void forceSensorCapturePre()
        {
-               //TODO: check is connected!
-               //
-               /*
-               forceSensorPortName = 
chronopicRegister.ConnectedOfType(ChronopicRegisterPort.Types.ARDUINO_FORCE).Port;
-               if(forceSensorPortName == null || forceSensorPortName == "")
-               {
-                       new DialogMessage(Constants.MessageTypes.WARNING, "Please, select port");
-                       return;
-               }
-               */
-               if(! portFSOpened) //TODO: better check is connected!
+               if(! portFSOpened)
                {
-                       new DialogMessage(Constants.MessageTypes.WARNING, "Please, connect!");
-                       return;
+                       if(! forceSensorConnect())
+                               return;
                }
 
+               forceSensorOtherMessage = "Please, wait ...";
+               capturingForce = forceStatus.STARTING;
+       }
 
+       private void forceSensorCapturePre2()
+       {
                button_execute_test.Sensitive = false;
                event_execute_button_finish.Sensitive = true;
                event_execute_button_cancel.Sensitive = true;
-               event_execute_label_message.Text = "Please, wait ...";
                forceCaptureStartMark = false;
                vscale_force_sensor.Value = 0;
                label_force_sensor_value_max.Text = "0";
                label_force_sensor_value.Text = "0";
                label_force_sensor_value_min.Text = "0";
 
-               capturingForce = forceStatus.STARTING;
                forceProcessFinish = false;
                forceProcessCancel = false;
                forceSensorLast = 0;
@@ -180,25 +286,24 @@ public partial class ChronoJumpWindow
                event_execute_ButtonCancel.Clicked -= new EventHandler(on_cancel_clicked);
                event_execute_ButtonCancel.Clicked += new EventHandler(on_cancel_clicked);
 
-               forceThread = new Thread(new ThreadStart(forceSensorCaptureDo));
-               GLib.Idle.Add (new GLib.IdleHandler (pulseGTKForceSensor));
+               forceCaptureThread = new Thread(new ThreadStart(forceSensorCaptureDo));
+               GLib.Idle.Add (new GLib.IdleHandler (pulseGTKForceSensorCapture));
 
                LogB.ThreadStart();
-               forceThread.Start();
+               forceCaptureThread.Start();
        }
 
        //non GTK on this method
        private void forceSensorCaptureDo()
        {
                lastChangedTime = 0;
-               /*
-               SerialPort port = new SerialPort(forceSensorPortName, 115200); //forceSensor
-               port.Open();
-               Thread.Sleep(2500); //sleep to let arduino start reading
-               */
 
-               //port.WriteLine("Start:-920.80:"); //Imp: note decimal is point
-               portFS.WriteLine("start_capture:"); //Imp: note decimal is point
+               if(! forceSensorSendCommand("start_capture:", "", "Catched force capturing"))
+               {
+                       forceProcessCancel = true;
+                       return;
+               }
+
                string str = "";
                do {
                        Thread.Sleep(100); //sleep to let arduino start reading
@@ -274,10 +379,10 @@ public partial class ChronoJumpWindow
                }
        }
        
-       private bool pulseGTKForceSensor ()
+       private bool pulseGTKForceSensorCapture ()
        {
                //LogB.Information(capturingForce.ToString())
-               if(! forceThread.IsAlive || forceProcessFinish || forceProcessCancel)
+               if(! forceCaptureThread.IsAlive || forceProcessFinish || forceProcessCancel)
                {
                        LogB.ThreadEnding();
 
@@ -301,7 +406,7 @@ public partial class ChronoJumpWindow
 
                        LogB.ThreadEnded(); 
 
-                       button_execute_test.Sensitive = true;
+                       forceSensorButtonsSensitive(true);
 
                        return false;
                }
@@ -342,7 +447,7 @@ public partial class ChronoJumpWindow
                }
 
                Thread.Sleep (25);
-//             LogB.Information(" ForceSensor:"+ forceThread.ThreadState.ToString());
+//             LogB.Information(" ForceSensor:"+ forceCaptureThread.ThreadState.ToString());
                return true;
        }
 
@@ -444,4 +549,3 @@ public partial class ChronoJumpWindow
        }
 
 }
-


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]