iCub-main
main.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 RobotCub Consortium, European Commission FP6 Project IST-004370
3  * Authors: Andrea Del Prete
4  * email: andrea.delprete@iit.it
5  * website: www.robotcub.org
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * http://www.robotcub.org/icub/license/gpl.txt
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17  */
18 
19 #include <mutex>
20 #include <string>
21 #include <sstream>
22 #include <iomanip> // io manipulator (setw, setfill)
23 #include <cstdarg>
24 //#include <cstdlib>
25 #include <math.h>
26 #include <vector>
27 
28 #include <gtk/gtk.h>
29 #include <glib.h>
30 
31 #include <yarp/os/Time.h>
32 #include <yarp/os/Port.h>
33 #include <yarp/os/BufferedPort.h>
34 #include <yarp/os/Bottle.h>
35 #include <yarp/sig/Vector.h>
36 #include <yarp/os/Network.h>
37 #include <yarp/os/RFModule.h>
38 
40 
41 using namespace std;
42 using namespace yarp::os;
43 using namespace yarp::sig;
44 using namespace iCub::skinManager;
45 
46 // main window
47 GtkWindow *window;
48 GtkStatusbar *statusBar;
49 GtkStatusbar *statusBarFreq;
50 // first tab
51 GtkProgressBar *progBarCalib;
52 GtkButton *btnCalibration;
53 GtkToggleButton *btnSmooth;
54 GtkToggleButton *btnBinarization;
55 GtkScale *scaleSmooth;
56 GtkTextView *tvLog;
57 GtkTextBuffer *tbLog;
58 GtkSpinButton *spinThreshold;
59 GtkSpinButton *spinGain;
60 GtkSpinButton *spinContGain;
61 GtkSpinButton *spinMaxNeighDist;
62 // second tab
63 GtkTreeView *treeBaselines;
64 GtkTreeStore *treeStoreComp;
65 // third tab
66 GtkCurve *curveComp;
67 GtkLabel *lblMaxY;
68 GtkLabel *lblMinY;
69 GtkLabel *lblMaxX;
70 GtkLabel *lblMinX;
71 GtkComboBox *comboPort;
72 GtkComboBox *comboTriangle;
73 GtkComboBox *comboTaxel;
74 GtkListStore *listPort;
75 GtkListStore *listTriangle;
76 GtkListStore *listTaxel;
77 // fourth tab
78 GtkLabel *lblInfo;
79 
80 // ports for communicating with the module
81 Port guiRpcPort; // to send rpc command to the module
82 BufferedPort<Vector> driftCompMonitorPort; // for reading streaming data (frequency, drift)
83 BufferedPort<Bottle> driftCompInfoPort; // for reading sporadic msgs (errors, warnings)
84 
86 BufferedPort<Bottle> portSkinDiagnosticsErrorsIn;
87 
88 //Port wholeBodyRpcPort; // port connected to wholeBodyDynamics rpc port
89 
90 // global data
91 guint timeoutId; // id of the timeout callback function
92 vector<string> portNames; // names of the skin input ports
93 vector<unsigned int> portDim; // number of taxels of the input ports
94 double currentSmoothFactor; // current smooth factor value
95 bool initDone; // true if the gui has been initialized
96 unsigned int currentThreshold; // current safety threshold
97 double currentCompGain; // current compensation gain
98 double currentContCompGain; // current contact compensation gain
99 double currentMaxNeighDist; // current max neighbor distance
101 int currentSampleNum; // size of the dataPlot array
102 
103 // plot data
104 mutex plotSem;
105 gint port2plot; // index of the skin port to plot
106 gint tr2plot; // triangle to plot
107 gint tax2plot; // taxel to plot
108 vector<gfloat> dataPlot; // data to plot
109 
110 bool initGuiStatus();
111 
112 static void printLog(string text){
113  text = text + "\n";
114  GtkTextIter tbIter;
115  gtk_text_buffer_get_end_iter(tbLog, &tbIter);
116  gtk_text_buffer_insert(tbLog, &tbIter, text.c_str(), text.length());
117  gtk_text_view_scroll_to_iter(tvLog, &tbIter, 0.0, false, 0.0, 0.0);
118 }
119 
120 static double round(double value, int decimalDigit){
121  double q = pow(10.0, decimalDigit);
122  return double(int((value*q)+0.5))/q;
123 }
124 
125 static void openDialog(const char* msg, GtkMessageType type){
126  GtkWidget* dialog = gtk_message_dialog_new (window,
127  GTK_DIALOG_DESTROY_WITH_PARENT,
128  type,
129  GTK_BUTTONS_CLOSE,
130  "%s", msg);
131  gtk_dialog_run (GTK_DIALOG (dialog));
132  gtk_widget_destroy (dialog);
133 }
134 
135 static void setStatusBarText(string text){
136  guint contextId = gtk_statusbar_get_context_id(statusBar, text.c_str());
137  gtk_statusbar_push(statusBar, contextId, text.c_str());
138 }
139 
140 static void setStatusBarFreq(bool freqUpdated, double freq){
141  stringstream text;
142  if(freqUpdated){
143  freq = round(freq, 2);
144  text<< "SkinDriftCompensation frequency: "<< freq;
145  }else{
146  text<< "Cannot read the frequency. Probably the skinDriftCompensation module has stopped working.";
147  }
148  guint contextId = gtk_statusbar_get_context_id(statusBarFreq, text.str().c_str());
149  gtk_statusbar_push(statusBarFreq, contextId, text.str().c_str());
150 }
151 
152 //static Bottle sendRpcCommand(bool responseExpected, int commandWordCount, const char* command, ...){
153 static Bottle sendRpcCommand(bool responseExpected, SkinManagerCommand cmd){
154  Bottle resp;
155  // check whether the port is connected
156  if(guiRpcPort.getOutputCount()==0){
157  openDialog((string("Connection to the rpc port of the skinDriftCompensation")
158  + "module not available. Connect and try again.").c_str(), GTK_MESSAGE_ERROR);
159  return resp;
160  }
161 
162  // create the bottle
163  Bottle b;
164  b.addInt(cmd);
165  //g_print("Going to send rpc msg: %s\n", b.toString().c_str());
166  if(responseExpected){
167  guiRpcPort.write(b, resp);
168  }else{
169  guiRpcPort.write(b);
170  }
171  return resp;
172 }
173 
174 
175 static void resetPlotData(){
176  for(int i=0; i<currentSampleNum; i++)
177  dataPlot[i] = 0;
178 }
GtkListStore * listTaxel
Definition: main.h:76
GtkTextBuffer * tbLog
Definition: main.h:57
GtkWindow * window
Definition: main.h:47
static double round(double value, int decimalDigit)
Definition: main.h:120
GtkTreeView * treeBaselines
Definition: main.h:63
static void openDialog(const char *msg, GtkMessageType type)
Definition: main.h:125
static Bottle sendRpcCommand(bool responseExpected, SkinManagerCommand cmd)
Definition: main.h:153
vector< unsigned int > portDim
Definition: main.h:93
double currentContCompGain
Definition: main.h:98
vector< string > portNames
Definition: main.h:92
GtkToggleButton * btnSmooth
Definition: main.h:53
BufferedPort< Vector > driftCompMonitorPort
Definition: main.h:82
STL namespace.
vector< gfloat > dataPlot
Definition: main.h:108
GtkLabel * lblInfo
Definition: main.h:78
static void setStatusBarText(string text)
Definition: main.h:135
GtkSpinButton * spinMaxNeighDist
Definition: main.h:61
GtkButton * btnCalibration
Definition: main.h:52
GtkSpinButton * spinThreshold
Definition: main.h:58
GtkComboBox * comboTriangle
Definition: main.h:72
GtkToggleButton * btnBinarization
Definition: main.h:54
GtkComboBox * comboTaxel
Definition: main.h:73
double currentSmoothFactor
Definition: main.h:94
cmd
Definition: dataTypes.h:30
mutex plotSem
Definition: main.h:104
GtkLabel * lblMinX
Definition: main.h:70
bool initGuiStatus()
Definition: main.cpp:88
GtkLabel * lblMinY
Definition: main.h:68
BufferedPort< Bottle > driftCompInfoPort
Definition: main.h:83
GtkSpinButton * spinContGain
Definition: main.h:60
GtkLabel * lblMaxX
Definition: main.h:69
gint tr2plot
Definition: main.h:106
GtkScale * scaleSmooth
Definition: main.h:55
BufferedPort< Bottle > portSkinDiagnosticsErrorsIn
Input port for skin diagnostics erro messages.
Definition: main.h:86
bool initDone
Definition: main.h:95
double currentCompGain
Definition: main.h:97
static void resetPlotData()
Definition: main.h:175
GtkListStore * listTriangle
Definition: main.h:75
GtkStatusbar * statusBarFreq
Definition: main.h:49
GtkSpinButton * spinGain
Definition: main.h:59
unsigned int currentThreshold
Definition: main.h:96
int currentSampleNum
Definition: main.h:101
double currentMaxNeighDist
Definition: main.h:99
static void printLog(string text)
Definition: main.h:112
Port guiRpcPort
Definition: main.h:81
guint timeoutId
Definition: main.h:91
GtkListStore * listPort
Definition: main.h:74
GtkTextView * tvLog
Definition: main.h:56
GtkTreeStore * treeStoreComp
Definition: main.h:64
GtkStatusbar * statusBar
Definition: main.h:48
GtkComboBox * comboPort
Definition: main.h:71
gint port2plot
Definition: main.h:105
gint tax2plot
Definition: main.h:107
GtkProgressBar * progBarCalib
Definition: main.h:51
GtkCurve * curveComp
Definition: main.h:66
GtkLabel * lblMaxY
Definition: main.h:67
static void setStatusBarFreq(bool freqUpdated, double freq)
Definition: main.h:140
int currentSampleFreq
Definition: main.h:100