[chronojump] ForceSensor sync two devices WIP (connect and check versions done!)



commit 87218ed69158fbab243845faa2c8110507ff0cd5
Author: Xavier de Blas <xaviblas gmail com>
Date:   Tue Dec 15 18:18:18 2020 +0100

    ForceSensor sync two devices WIP (connect and check versions done!)

 glade/app1.glade            |  43 +++++++++++----
 src/gui/app1/chronojump.cs  |   7 +++
 src/gui/app1/forceSensor.cs | 130 ++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 162 insertions(+), 18 deletions(-)
---
diff --git a/glade/app1.glade b/glade/app1.glade
index 167e0fa6..7e059cc5 100644
--- a/glade/app1.glade
+++ b/glade/app1.glade
@@ -7028,6 +7028,19 @@ EncoderInertialCapture</property>
                                                             <property name="visible">True</property>
                                                             <property name="can_focus">False</property>
                                                             <child>
+                                                            <widget class="GtkButton" 
id="button_force_sensor_sync">
+                                                            <property name="label">2FSsync</property>
+                                                            <property name="can_focus">True</property>
+                                                            <property name="receives_default">True</property>
+                                                            <signal name="clicked" 
handler="on_button_force_sensor_sync_clicked" swapped="no"/>
+                                                            </widget>
+                                                            <packing>
+                                                            <property name="expand">True</property>
+                                                            <property name="fill">True</property>
+                                                            <property name="position">0</property>
+                                                            </packing>
+                                                            </child>
+                                                            <child>
                                                             <widget class="GtkAlignment" 
id="alignment_button_force_sensor_adjust">
                                                             <property name="visible">True</property>
                                                             <property name="can_focus">False</property>
@@ -23550,6 +23563,9 @@ Concentric</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             <packing>
                                                             <property name="expand">False</property>
@@ -28371,6 +28387,18 @@ Concentric</property>
                                                             <property name="can_focus">False</property>
                                                             <property name="spacing">6</property>
                                                             <child>
+                                                            <widget class="GtkLabel" 
id="label_video_encoder_tests_will_be_filmed">
+                                                            <property name="can_focus">False</property>
+                                                            <property name="label" translatable="yes">Tests 
will be filmed</property>
+                                                            </widget>
+                                                            <packing>
+                                                            <property name="expand">False</property>
+                                                            <property name="fill">False</property>
+                                                            <property name="pack_type">end</property>
+                                                            <property name="position">0</property>
+                                                            </packing>
+                                                            </child>
+                                                            <child>
                                                             <widget class="GtkHBox" 
id="hbox_video_encoder_capturing">
                                                             <property name="can_focus">False</property>
                                                             <property name="spacing">4</property>
@@ -28407,18 +28435,6 @@ Concentric</property>
                                                             </packing>
                                                             </child>
                                                             <child>
-                                                            <widget class="GtkLabel" 
id="label_video_encoder_tests_will_be_filmed">
-                                                            <property name="can_focus">False</property>
-                                                            <property name="label" translatable="yes">Tests 
will be filmed</property>
-                                                            </widget>
-                                                            <packing>
-                                                            <property name="expand">False</property>
-                                                            <property name="fill">False</property>
-                                                            <property name="pack_type">end</property>
-                                                            <property name="position">0</property>
-                                                            </packing>
-                                                            </child>
-                                                            <child>
                                                             <widget class="GtkHBox" 
id="hbox_video_encoder_no_capturing">
                                                             <property name="visible">True</property>
                                                             <property name="can_focus">False</property>
@@ -36641,6 +36657,9 @@ then click this button.</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             <packing>
                                                             <property name="expand">False</property>
diff --git a/src/gui/app1/chronojump.cs b/src/gui/app1/chronojump.cs
index 33ed7fa4..4065e9f2 100644
--- a/src/gui/app1/chronojump.cs
+++ b/src/gui/app1/chronojump.cs
@@ -318,6 +318,7 @@ public partial class ChronoJumpWindow
        [Widget] Gtk.Button button_activate_chronopics;
        [Widget] Gtk.Alignment alignment_button_threshold;
        [Widget] Gtk.Alignment alignment_button_force_sensor_adjust;
+       [Widget] Gtk.Button button_force_sensor_sync;
 
        //non standard icons    
        [Widget] Gtk.Image image_jump_reactive_bell;
@@ -2963,6 +2964,7 @@ public partial class ChronoJumpWindow
                notebook_analyze.CurrentPage = Convert.ToInt32(notebook_analyze_pages.STATISTICS);
                button_inspect_last_test_run_intervallic.Visible = false;
                alignment_button_force_sensor_adjust.Visible = false;
+               button_force_sensor_sync.Visible = false;
                vbox_contacts_load_recalculate.Visible = false;
                vbox_contacts_signal_comment.Visible = false;
                frame_jumps_automatic.Visible = false;
@@ -3213,6 +3215,7 @@ public partial class ChronoJumpWindow
                        hbox_contacts_sup_capture_analyze_two_buttons.Visible = true;
                        alignment_button_threshold.Visible = false;
                        alignment_button_force_sensor_adjust.Visible = true;
+                       button_force_sensor_sync.Visible = true;
                        //notebook_capture_analyze.GetNthPage(2).Hide(); //hide jumpsProfile on other tests
 
                        event_graph_label_graph_test.Visible = true;
@@ -7276,6 +7279,7 @@ LogB.Debug("mc finished 5");
                button_execute_test.Sensitive = false;
                button_auto_start.Sensitive = false;
                alignment_button_force_sensor_adjust.Sensitive = false;
+               button_force_sensor_sync.Sensitive = false;
 
                encoderButtonsSensitive(encoderSensEnum.NOSESSION);
                
@@ -7295,6 +7299,7 @@ LogB.Debug("mc finished 5");
                button_contacts_person_change.Sensitive = true;
                button_encoder_person_change.Sensitive = true;
                alignment_button_force_sensor_adjust.Sensitive = true;
+               button_force_sensor_sync.Sensitive = true;
                
                menuSessionSensitive(true);
                vbox_stats.Sensitive = true;
@@ -7417,6 +7422,7 @@ LogB.Debug("mc finished 5");
                button_activate_chronopics_encoder.Sensitive = false;
                alignment_button_threshold.Sensitive = false;
                alignment_button_force_sensor_adjust.Sensitive = false;
+               button_force_sensor_sync.Sensitive = false;
                button_auto_start.Sensitive = false;
                button_contacts_exercise.Sensitive = false;
                event_execute_button_update.Sensitive = false;
@@ -7479,6 +7485,7 @@ LogB.Debug("mc finished 5");
 
                alignment_button_threshold.Sensitive = true;
                alignment_button_force_sensor_adjust.Sensitive = true;
+               button_force_sensor_sync.Sensitive = true;
                button_auto_start.Sensitive = true;
                button_contacts_exercise.Sensitive = true;
                event_execute_button_update.Sensitive = true;
diff --git a/src/gui/app1/forceSensor.cs b/src/gui/app1/forceSensor.cs
index 7092c416..17fb05f6 100644
--- a/src/gui/app1/forceSensor.cs
+++ b/src/gui/app1/forceSensor.cs
@@ -114,6 +114,11 @@ public partial class ChronoJumpWindow
        bool forceSensorBinaryCapture;
        int forceSensorTopRectangleAtOperationStart; //operation can be capture, load
 
+       //for a second forceSensor
+       string forceSensorPortName_B = "/dev/ttyUSB1"; //TODO: hardcoded
+       SerialPort portFS_B; //Attention!! Don't reopen port because arduino makes reset and tare, 
calibration... is lost
+       bool portFSOpened_B;
+
        Gdk.GC pen_black_force_capture;
        Gdk.GC pen_red_force_capture;
        Gdk.GC pen_white_force_capture;
@@ -257,7 +262,7 @@ public partial class ChronoJumpWindow
                                return false;
 
                        //read confirmation data
-                       if(! forceSensorReceiveFeedback("Tare set"))
+                       if(forceSensorReceiveFeedback("Tare set") == "")
                                return false;
 
 
@@ -267,7 +272,7 @@ public partial class ChronoJumpWindow
                                return false;
 
                        //read confirmation data
-                       if(! forceSensorReceiveFeedback("Calibration factor set"))
+                       if(forceSensorReceiveFeedback("Calibration factor set") == "")
                                return false;
                }
 
@@ -283,6 +288,23 @@ public partial class ChronoJumpWindow
                LogB.Information(" FS connect 7: connected and adjusted!");
                return true;
        }
+       private bool forceSensorConnect_Port_B()
+       {
+               portFS_B = new SerialPort(forceSensorPortName_B, 115200); //forceSensor
+
+               try {
+                       portFS_B.Open();
+               }
+               catch (System.IO.IOException)
+               {
+                       return false;
+               }
+
+               Thread.Sleep(3000); //sleep to let arduino start reading serial event
+
+               portFSOpened_B = true;
+               return true;
+       }
 
        //this is called on change mode
        private void forceSensorDisconnect()
@@ -317,22 +339,53 @@ public partial class ChronoJumpWindow
                return true;
        }
 
+       //Attention: no GTK here!!
+       private bool forceSensorSendCommand_B(string command, string displayMessage, string errorMessage)
+       {
+               try {
+                       LogB.Information("Force sensor command |" + command + "|");
+                       portFS_B.WriteLine(command);
+               }
+               catch (Exception ex)
+               {
+                       if(ex is System.IO.IOException || ex is System.TimeoutException)
+                       {
+                               LogB.Information(errorMessage);
+//portFS_B.Close(); //TODO: seguir investigant
+                               portFSOpened_B = false;
+                               return false;
+                       }
+                       //throw;
+               }
+               return true;
+       }
+
+       //PORT_B is used when there are two devices (and have to be in sync)
+       private enum forceSensorPortEnum { PORT_A, PORT_B };
+
+       private string forceSensorReceiveFeedback(string expected)
+       {
+               return forceSensorReceiveFeedback(expected, forceSensorPortEnum.PORT_A);
+       }
        //use this method for other feedback, but beware some of the commands do a Trim on ReadLine
-       private bool forceSensorReceiveFeedback(string expected)
+       private string forceSensorReceiveFeedback(string expected, forceSensorPortEnum myPort)
        {
                string str = "";
                do {
                        Thread.Sleep(100); //sleep to let arduino start reading
                        try {
-                               str = portFS.ReadLine();
+                               if(myPort == forceSensorPortEnum.PORT_A)
+                                       str = portFS.ReadLine();
+                               else
+                                       str = portFS_B.ReadLine();
                        } catch {
                                forceSensorOtherMessage = "Disconnected";
-                               return false;
+                               return "";
                        }
                        LogB.Information("init string: " + str);
                }
                while(! str.Contains(expected));
-               return true;
+               return str;
        }
 
        enum forceSensorOtherModeEnum { TARE, CALIBRATE, CAPTURE_PRE, TARE_AND_CAPTURE_PRE, STIFFNESS_DETECT, 
CHECK_VERSION }
@@ -723,6 +776,7 @@ public partial class ChronoJumpWindow
        }
 
        //Attention: no GTK here!!
+       //we do not pass the port because there are problems passing ports
        private string forceSensorCheckVersionDo()
        {
                if (portFS.BytesToRead > 0)
@@ -747,6 +801,26 @@ public partial class ChronoJumpWindow
                forceSensorOtherMessageShowSeconds = secondsEnum.NO;
                forceSensorOtherMessage = str;
 
+               return forceSensorCheckVersionMatch(str);
+       }
+
+       private string forceSensorCheckVersionDo_Port_B()
+       {
+               if (portFS_B.BytesToRead > 0)
+                       LogB.Information("Port_B check_version read possible bytes: " + 
portFS_B.ReadExisting());
+
+               if(! forceSensorSendCommand_B("get_version:", "Checking version ...", "Catched checking 
version"))
+                       return "";
+
+               string str = forceSensorReceiveFeedback("Force_Sensor-", forceSensorPortEnum.PORT_B);
+               if(str == "")
+                       return str;
+
+               return forceSensorCheckVersionMatch(str.Trim());
+       }
+
+       private string forceSensorCheckVersionMatch(string str)
+       {
                /*
                 * //return the version without "Force_Sensor-"
                 * return(str.Remove(0,13));
@@ -2549,6 +2623,7 @@ LogB.Information(" fs R ");
        private void on_button_force_sensor_adjust_clicked (object o, EventArgs args)
        {
                alignment_button_force_sensor_adjust.Sensitive = false; //to not be called again
+               button_force_sensor_sync.Sensitive = false;
 
                //hbox_force_capture_buttons.Sensitive = false;
                notebook_contacts_execute_or_instructions.Sensitive = false;
@@ -2563,6 +2638,7 @@ LogB.Information(" fs R ");
        private void on_button_force_sensor_adjust_close_clicked (object o, EventArgs args)
        {
                alignment_button_force_sensor_adjust.Sensitive = true;
+               button_force_sensor_sync.Sensitive = true;
 
                //hbox_force_capture_buttons.Sensitive = true;
                notebook_contacts_execute_or_instructions.Sensitive = true;
@@ -2597,6 +2673,48 @@ LogB.Information(" fs R ");
                                preferences.GetForceSensorAdjustString());
        }
 
+       private void on_button_force_sensor_sync_clicked (object o, EventArgs args)
+       {
+               LogB.Information("on_button_force_sensor_sync_clicked 0");
+               //port A check connection
+               if(! portFSOpened)
+                       if(! forceSensorConnect())
+                       {
+                               new DialogMessage(Constants.MessageTypes.WARNING, "Cannot connect port A");
+                               return;
+                       }
+
+               LogB.Information("on_button_force_sensor_sync_clicked 1");
+               string versionStr = forceSensorCheckVersionDo();
+                double versionDouble = Convert.ToDouble(Util.ChangeDecimalSeparator(versionStr));
+               if(versionDouble < Convert.ToDouble(Util.ChangeDecimalSeparator("0.6")))
+               {
+                       new DialogMessage(Constants.MessageTypes.WARNING, "Force version A == " + versionStr);
+                       return;
+               }
+
+               LogB.Information("on_button_force_sensor_sync_clicked 2");
+               //port B check connection
+               if(! portFSOpened_B)
+                       if(! forceSensorConnect_Port_B())
+                       {
+                               new DialogMessage(Constants.MessageTypes.WARNING, "Cannot connect port B");
+                               return;
+                       }
+
+               LogB.Information("on_button_force_sensor_sync_clicked 3");
+               versionStr = forceSensorCheckVersionDo_Port_B();
+                versionDouble = Convert.ToDouble(Util.ChangeDecimalSeparator(versionStr));
+               if(versionDouble < Convert.ToDouble(Util.ChangeDecimalSeparator("0.6")))
+               {
+                       new DialogMessage(Constants.MessageTypes.WARNING, "Force version B == " + versionStr);
+                       return;
+               }
+
+               LogB.Information("on_button_force_sensor_sync_clicked 4");
+               new DialogMessage(Constants.MessageTypes.WARNING, "Success! Both dispositives with >= 0.6");
+       }
+
        private void createComboForceSensorCaptureOptions()
        {
                UtilGtk.ComboUpdate(combo_force_sensor_capture_options, ForceSensor.CaptureOptionsList());


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