iCub-main
calibrate_window.cpp
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 /*
4  * Copyright (C) 2008 RobotCub Consortium
5  * Author: Marco Maggiali, Marco Randazzo, Lorenzo Natale
6  * CopyPolicy: Released under the terms of the GNU GPL v2.0.
7  *
8  */
9 
10 #include "downloader.h"
11 #include "calibrate_window.h"
12 #include <canProtocolLib/iCubCanProtocol.h>
13 #include <canProtocolLib/iCubCanProto_types.h>
14 #include <fstream>
15 #include <yarp/os/Log.h>
16 
17 using namespace std;
18 
19 extern cDownloader downloader;
20 extern GtkWidget *window;
21 int selected = 0;
22 unsigned int calibration_value=32767;
23 int ch[6]={0,1,2,3,4,5};
24 unsigned int offset[6];
25 unsigned int amp_gain1[6];
26 unsigned int amp_gain2[6];
27 unsigned int adc[6]={0,0,0,0,0,0};
28 unsigned int maxadc[6]={0,0,0,0,0,0};
29 int calib_bias[6]={0,0,0,0,0,0};
30 int curr_bias[6]={0,0,0,0,0,0};
31 unsigned int minadc[6]={65535,65535,65535,65535,65535,65535};
32 unsigned int matrix[6][6];
33 unsigned int calib_matrix[6][6];
34 guint timer_refresh = 0;
35 GtkWidget* curr_measure[6];
36 GtkWidget* max_measure[6];
37 GtkWidget* min_measure[6];
38 GtkWidget* diff_measure[6];
39 GtkWidget* newton_measure[6];
40 GtkWidget* edit_matrix[6][6];
41 GtkWidget* edit_matrix_gain;
42 GtkWidget* label_matrix_gain;
43 GtkWidget* slider_gain[6];
44 GtkWidget* slider_zero;
45 GtkWidget* info_dlg;
46 GtkWidget* picker_calib;
47 GtkWidget *save_button;
49 GtkWidget *check_raw_vals;
54 GtkWidget *curr_bias_label[6];
55 GtkWidget *calib_bias_label[6];
56 GtkWidget *full_scale_label[6];
57 GtkWidget *edit_serial_number;
58 gboolean timer_func (gpointer data);
59 
60 unsigned int calib_const=0;
61 unsigned int full_scale_const[6]={0,0,0,0,0,0};
62 char serial_no[8]={'U','N','D','E','F',0,0,0};
67 bool first_time[6]={1,1,1,1,1,1};
68 
69 #define START_TIMER timer_refresh = g_timeout_add (500, timer_func, NULL);
70 #define STOP_TIMER {if (timer_refresh>0) g_source_remove(timer_refresh); timer_refresh=0;}
71 #define HEX_VALC 0x8000
72 
73 
74 //*********************************************************************************
75 void save_click (GtkButton *button, gpointer user_data)
76 {
78  drv_sleep (1000);
79  downloader.strain_save_to_eeprom(downloader.board_list[selected].bus, downloader.board_list[selected].pid);
80  drv_sleep (1000);
81  something_changed=false;
83 }
84 
85 //*********************************************************************************
86 void auto_click (GtkButton *button, gpointer user_data)
87 {
89  downloader.strain_calibrate_offset(downloader.board_list[selected].bus, downloader.board_list[selected].pid, icubCanProto_boardType__strain, calibration_value);
91 }
92 /*
93 #include <sstream>
94 #include <string>
95 #include <string.h>
96 
97 
98 // Problems using itoa in Linux (not ansi C). Using this code instead.
99 
100 char *myitoa(int a, char *buff, int d)
101 {
102  std::string tempbuf;
103  std::stringstream lStream;
104  // lStream.fill('0');
105  // lStream.width(3);
106  lStream << adc[0];
107  tempbuf+=lStream.str();
108 
109  strncpy(buff, tempbuf.c_str(), d);
110  return buff;
111 }
112 */
113 
114 //*********************************************************************************
115 gboolean timer_func (gpointer data)
116 {
117  int ret=0;
118  ret =downloader.strain_get_eeprom_saved(downloader.board_list[selected].bus, downloader.board_list[selected].pid, &eeprom_saved_status);
119  if (ret!=0) printf("debug: message 'strain_get_eeprom_saved' lost.\n");
120 
121  /*
122  ret =downloader.sg6_get_amp_gain (downloader.board_list[selected].pid, 0, amp_gain1[0], amp_gain2[0]);
123  ret|=downloader.sg6_get_amp_gain (downloader.board_list[selected].pid, 1, amp_gain1[1], amp_gain2[1]);
124  ret|=downloader.sg6_get_amp_gain (downloader.board_list[selected].pid, 2, amp_gain1[2], amp_gain2[2]);
125  ret|=downloader.sg6_get_amp_gain (downloader.board_list[selected].pid, 3, amp_gain1[3], amp_gain2[3]);
126  ret|=downloader.sg6_get_amp_gain (downloader.board_list[selected].pid, 4, amp_gain1[4], amp_gain2[4]);
127  ret|=downloader.sg6_get_amp_gain (downloader.board_list[selected].pid, 5, amp_gain1[5], amp_gain2[5]);
128  if (ret!=0) printf("debug: message 'sg6_get_amp_gain' lost.\n");
129  */
130 
131  ret =downloader.strain_get_offset (downloader.board_list[selected].bus, downloader.board_list[selected].pid, 0, offset[0]);
132  ret|=downloader.strain_get_offset (downloader.board_list[selected].bus, downloader.board_list[selected].pid, 1, offset[1]);
133  ret|=downloader.strain_get_offset (downloader.board_list[selected].bus, downloader.board_list[selected].pid, 2, offset[2]);
134  ret|=downloader.strain_get_offset (downloader.board_list[selected].bus, downloader.board_list[selected].pid, 3, offset[3]);
135  ret|=downloader.strain_get_offset (downloader.board_list[selected].bus, downloader.board_list[selected].pid, 4, offset[4]);
136  ret|=downloader.strain_get_offset (downloader.board_list[selected].bus, downloader.board_list[selected].pid, 5, offset[5]);
137  if (ret!=0) printf("debug: message 'strain_get_offset' lost.\n");
138 
139  int bool_raw= gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check_raw_vals));
140  ret =downloader.strain_get_adc (downloader.board_list[selected].bus, downloader.board_list[selected].pid, 0, adc[0], bool_raw);
141  ret|=downloader.strain_get_adc (downloader.board_list[selected].bus, downloader.board_list[selected].pid, 1, adc[1], bool_raw);
142  ret|=downloader.strain_get_adc (downloader.board_list[selected].bus, downloader.board_list[selected].pid, 2, adc[2], bool_raw);
143  ret|=downloader.strain_get_adc (downloader.board_list[selected].bus, downloader.board_list[selected].pid, 3, adc[3], bool_raw);
144  ret|=downloader.strain_get_adc (downloader.board_list[selected].bus, downloader.board_list[selected].pid, 4, adc[4], bool_raw);
145  ret|=downloader.strain_get_adc (downloader.board_list[selected].bus, downloader.board_list[selected].pid, 5, adc[5], bool_raw);
146  if (ret!=0) printf("debug: message 'strain_get_adc' lost.\n");
147 
148  int ri,ci=0;
149  char tempbuf [250];
150  GdkColor r_color,y_color;
151  r_color.red=65535;
152  r_color.green=39000;
153  r_color.blue=39000;
154  y_color.red=65535;
155  y_color.green=65535;
156  y_color.blue=38000;
157 
158  if (eeprom_saved_status==false)
159  {
160  gtk_widget_modify_bg (save_button, GTK_STATE_NORMAL, &r_color);
161  gtk_widget_modify_bg (save_button, GTK_STATE_ACTIVE, &r_color);
162  gtk_widget_modify_bg (save_button, GTK_STATE_PRELIGHT, &r_color);
163  gtk_widget_modify_bg (save_button, GTK_STATE_SELECTED, &r_color);
164  gtk_widget_modify_bg (save_button, GTK_STATE_INSENSITIVE, &r_color);
165  gtk_button_set_label (GTK_BUTTON(save_button), "Not saved.\nSave to eeprom?"); }
166  else
167  {
168  gtk_button_set_label (GTK_BUTTON(save_button), "Save to eeprom");
169  }
170 /* if (something_changed==true)
171  {
172  gtk_widget_modify_base (save_button, GTK_STATE_NORMAL, &r_color);
173  gtk_widget_modify_bg (save_button, GTK_STATE_NORMAL, &r_color);
174  gtk_widget_modify_bg (save_button, GTK_STATE_ACTIVE, &r_color);
175  gtk_widget_modify_bg (save_button, GTK_STATE_PRELIGHT, &r_color);
176  gtk_widget_modify_bg (save_button, GTK_STATE_SELECTED, &r_color);
177  gtk_widget_modify_bg (save_button, GTK_STATE_INSENSITIVE, &r_color);
178  }*/
179 
180  if (serial_number_changed==false)
181  {
182  gtk_widget_modify_base (edit_serial_number,GTK_STATE_NORMAL, NULL );
183  }
184  else
185  {
186  gtk_widget_modify_base (edit_serial_number,GTK_STATE_NORMAL, &r_color );
187  }
188 
189  if (matrix_changed==false)
190  {
191  for (ri=0;ri<6;ri++)
192  for (ci=0;ci<6;ci++)
193  {
194  downloader.strain_get_matrix_rc(downloader.board_list[selected].bus, downloader.board_list[selected].pid, ri, ci, matrix[ri][ci]);
195  sprintf(tempbuf,"%x",matrix[ri][ci]);
196  gtk_entry_set_text (GTK_ENTRY (edit_matrix[ri][ci]), tempbuf);
197  gtk_widget_modify_base (edit_matrix[ri][ci],GTK_STATE_NORMAL, NULL );
198  }
199  downloader.strain_get_matrix_gain(downloader.board_list[selected].bus, downloader.board_list[selected].pid, calib_const);
200  sprintf(tempbuf,"%d",calib_const);
201  gtk_label_set_text (GTK_LABEL(edit_matrix_gain), tempbuf);
202 
203  for (ri=0;ri<6;ri++)
204  {
205  downloader.strain_get_full_scale(downloader.board_list[selected].bus, downloader.board_list[selected].pid, ri, full_scale_const[ri]);
206  sprintf(tempbuf,"%d",full_scale_const[ri]);
207  gtk_label_set_text (GTK_LABEL(full_scale_label[ri]), tempbuf);
208  }
209  }
210  else
211  {
212  for (ri=0;ri<6;ri++)
213  for (ci=0;ci<6;ci++)
214  {
215  gtk_widget_modify_base (edit_matrix[ri][ci],GTK_STATE_NORMAL, &r_color );
216  }
217  }
218  for (int i=0;i<6;i++)
219  {
220  if (!bool_raw)
221  {
222  if (adc[i]>maxadc[i]) maxadc[i]=adc[i];
223  if (adc[i]<minadc[i]) minadc[i]=adc[i];
224  }
225  else
226  {
227  maxadc[i]=0;
228  minadc[i]=65535;
229  }
230  }
231 
232  for (int i=0;i<6;i++)
233  {
234  downloader.strain_get_calib_bias(downloader.board_list[selected].bus, downloader.board_list[selected].pid, i, calib_bias[i]);
235  sprintf(tempbuf,"%d",calib_bias[i]);
236  gtk_label_set_text (GTK_LABEL(calib_bias_label[i]), tempbuf);
237 
238  downloader.strain_get_curr_bias(downloader.board_list[selected].bus, downloader.board_list[selected].pid, i, curr_bias[i]);
239  sprintf(tempbuf,"%d",curr_bias[i]);
240  gtk_label_set_text (GTK_LABEL(curr_bias_label[i]), tempbuf);
241  }
242 
243  gtk_range_set_value (GTK_RANGE(slider_gain[0]),(offset[0]));
244  gtk_range_set_value (GTK_RANGE(slider_gain[1]),(offset[1]));
245  gtk_range_set_value (GTK_RANGE(slider_gain[2]),(offset[2]));
246  gtk_range_set_value (GTK_RANGE(slider_gain[3]),(offset[3]));
247  gtk_range_set_value (GTK_RANGE(slider_gain[4]),(offset[4]));
248  gtk_range_set_value (GTK_RANGE(slider_gain[5]),(offset[5]));
249 
250  sprintf(tempbuf,"%d",adc[0]-HEX_VALC); //@@@@ changed here
251  gtk_label_set_text(GTK_LABEL(curr_measure[0]),tempbuf);
252  sprintf(tempbuf,"%d",adc[1]-HEX_VALC);
253  gtk_label_set_text(GTK_LABEL(curr_measure[1]),tempbuf);
254  sprintf(tempbuf,"%d",adc[2]-HEX_VALC);
255  gtk_label_set_text(GTK_LABEL(curr_measure[2]),tempbuf);
256  sprintf(tempbuf,"%d",adc[3]-HEX_VALC);
257  gtk_label_set_text(GTK_LABEL(curr_measure[3]),tempbuf);
258  sprintf(tempbuf,"%d",adc[4]-HEX_VALC);
259  gtk_label_set_text(GTK_LABEL(curr_measure[4]),tempbuf);
260  sprintf(tempbuf,"%d",adc[5]-HEX_VALC);
261  gtk_label_set_text(GTK_LABEL(curr_measure[5]),tempbuf);
262 
263  if (bool_raw)
264  {
265  sprintf(tempbuf,"---");
266  gtk_label_set_text(GTK_LABEL(max_measure[0]),tempbuf);
267  sprintf(tempbuf,"---");
268  gtk_label_set_text(GTK_LABEL(max_measure[1]),tempbuf);
269  sprintf(tempbuf,"---");
270  gtk_label_set_text(GTK_LABEL(max_measure[2]),tempbuf);
271  sprintf(tempbuf,"---");
272  gtk_label_set_text(GTK_LABEL(max_measure[3]),tempbuf);
273  sprintf(tempbuf,"---");
274  gtk_label_set_text(GTK_LABEL(max_measure[4]),tempbuf);
275  sprintf(tempbuf,"---");
276  gtk_label_set_text(GTK_LABEL(max_measure[5]),tempbuf);
277 
278  sprintf(tempbuf,"---");
279  gtk_label_set_text(GTK_LABEL(min_measure[0]),tempbuf);
280  sprintf(tempbuf,"---");
281  gtk_label_set_text(GTK_LABEL(min_measure[1]),tempbuf);
282  sprintf(tempbuf,"---");
283  gtk_label_set_text(GTK_LABEL(min_measure[2]),tempbuf);
284  sprintf(tempbuf,"---");
285  gtk_label_set_text(GTK_LABEL(min_measure[3]),tempbuf);
286  sprintf(tempbuf,"---");
287  gtk_label_set_text(GTK_LABEL(min_measure[4]),tempbuf);
288  sprintf(tempbuf,"---");
289  gtk_label_set_text(GTK_LABEL(min_measure[5]),tempbuf);
290 
291  sprintf(tempbuf,"---");
292  gtk_label_set_text(GTK_LABEL(diff_measure[0]),tempbuf);
293  sprintf(tempbuf,"---");
294  gtk_label_set_text(GTK_LABEL(diff_measure[1]),tempbuf);
295  sprintf(tempbuf,"---");
296  gtk_label_set_text(GTK_LABEL(diff_measure[2]),tempbuf);
297  sprintf(tempbuf,"---");
298  gtk_label_set_text(GTK_LABEL(diff_measure[3]),tempbuf);
299  sprintf(tempbuf,"---");
300  gtk_label_set_text(GTK_LABEL(diff_measure[4]),tempbuf);
301  sprintf(tempbuf,"---");
302  gtk_label_set_text(GTK_LABEL(diff_measure[5]),tempbuf);
303 
304  /*
305  //previous version
306  sprintf(tempbuf,"%+.3f N",(int(adc[0])-HEX_VALC)/float(calib_const));
307  gtk_label_set_text(GTK_LABEL(newton_measure[0]),tempbuf);
308  sprintf(tempbuf,"%+.3f N",(int(adc[1])-HEX_VALC)/float(calib_const));
309  gtk_label_set_text(GTK_LABEL(newton_measure[1]),tempbuf);
310  sprintf(tempbuf,"%+.3f N",(int(adc[2])-HEX_VALC)/float(calib_const));
311  gtk_label_set_text(GTK_LABEL(newton_measure[2]),tempbuf);
312  sprintf(tempbuf,"%+.3f N/m",(int(adc[3])-HEX_VALC)/float(calib_const));
313  gtk_label_set_text(GTK_LABEL(newton_measure[3]),tempbuf);
314  sprintf(tempbuf,"%+.3f N/m",(int(adc[4])-HEX_VALC)/float(calib_const));
315  gtk_label_set_text(GTK_LABEL(newton_measure[4]),tempbuf);
316  sprintf(tempbuf,"%+.3f N/m",(int(adc[5])-HEX_VALC)/float(calib_const));
317  gtk_label_set_text(GTK_LABEL(newton_measure[5]),tempbuf);*/
318 
319  bool skip_display_calib=false;
320  for (int i=0; i<6; i++)
321  {
322  if (full_scale_const[i]==0)
323  {
324  printf("Error getting the full scale %d from the sensor\n",i);
325  skip_display_calib=true;
326  }
327  }
328 
329  if (skip_display_calib==false)
330  {
331  sprintf(tempbuf,"%+.3f N",(int(adc[0])-HEX_VALC)/float(HEX_VALC)*full_scale_const[0]);
332  gtk_label_set_text(GTK_LABEL(newton_measure[0]),tempbuf);
333  sprintf(tempbuf,"%+.3f N",(int(adc[1])-HEX_VALC)/float(HEX_VALC)*full_scale_const[1]);
334  gtk_label_set_text(GTK_LABEL(newton_measure[1]),tempbuf);
335  sprintf(tempbuf,"%+.3f N",(int(adc[2])-HEX_VALC)/float(HEX_VALC)*full_scale_const[2]);
336  gtk_label_set_text(GTK_LABEL(newton_measure[2]),tempbuf);
337  sprintf(tempbuf,"%+.3f N/m",(int(adc[3])-HEX_VALC)/float(HEX_VALC)*full_scale_const[3]);
338  gtk_label_set_text(GTK_LABEL(newton_measure[3]),tempbuf);
339  sprintf(tempbuf,"%+.3f N/m",(int(adc[4])-HEX_VALC)/float(HEX_VALC)*full_scale_const[4]);
340  gtk_label_set_text(GTK_LABEL(newton_measure[4]),tempbuf);
341  sprintf(tempbuf,"%+.3f N/m",(int(adc[5])-HEX_VALC)/float(HEX_VALC)*full_scale_const[5]);
342  gtk_label_set_text(GTK_LABEL(newton_measure[5]),tempbuf);
343  }
344  else
345  {
346  gtk_label_set_text(GTK_LABEL(newton_measure[0]),"ERROR");
347  gtk_label_set_text(GTK_LABEL(newton_measure[1]),"ERROR");
348  gtk_label_set_text(GTK_LABEL(newton_measure[2]),"ERROR");
349  gtk_label_set_text(GTK_LABEL(newton_measure[3]),"ERROR");
350  gtk_label_set_text(GTK_LABEL(newton_measure[4]),"ERROR");
351  gtk_label_set_text(GTK_LABEL(newton_measure[5]),"ERROR");
352  }
353  }
354  else
355  {
356  sprintf(tempbuf,"%d",maxadc[0]);
357  gtk_label_set_text(GTK_LABEL(max_measure[0]),tempbuf);
358  sprintf(tempbuf,"%d",maxadc[1]);
359  gtk_label_set_text(GTK_LABEL(max_measure[1]),tempbuf);
360  sprintf(tempbuf,"%d",maxadc[2]);
361  gtk_label_set_text(GTK_LABEL(max_measure[2]),tempbuf);
362  sprintf(tempbuf,"%d",maxadc[3]);
363  gtk_label_set_text(GTK_LABEL(max_measure[3]),tempbuf);
364  sprintf(tempbuf,"%d",maxadc[4]);
365  gtk_label_set_text(GTK_LABEL(max_measure[4]),tempbuf);
366  sprintf(tempbuf,"%d",maxadc[5]);
367  gtk_label_set_text(GTK_LABEL(max_measure[5]),tempbuf);
368 
369  sprintf(tempbuf,"%d",minadc[0]);
370  gtk_label_set_text(GTK_LABEL(min_measure[0]),tempbuf);
371  sprintf(tempbuf,"%d",minadc[1]);
372  gtk_label_set_text(GTK_LABEL(min_measure[1]),tempbuf);
373  sprintf(tempbuf,"%d",minadc[2]);
374  gtk_label_set_text(GTK_LABEL(min_measure[2]),tempbuf);
375  sprintf(tempbuf,"%d",minadc[3]);
376  gtk_label_set_text(GTK_LABEL(min_measure[3]),tempbuf);
377  sprintf(tempbuf,"%d",minadc[4]);
378  gtk_label_set_text(GTK_LABEL(min_measure[4]),tempbuf);
379  sprintf(tempbuf,"%d",minadc[5]);
380  gtk_label_set_text(GTK_LABEL(min_measure[5]),tempbuf);
381 
382  sprintf(tempbuf,"%d",maxadc[0]-minadc[0]);
383  gtk_label_set_text(GTK_LABEL(diff_measure[0]),tempbuf);
384  sprintf(tempbuf,"%d",maxadc[1]-minadc[1]);
385  gtk_label_set_text(GTK_LABEL(diff_measure[1]),tempbuf);
386  sprintf(tempbuf,"%d",maxadc[2]-minadc[2]);
387  gtk_label_set_text(GTK_LABEL(diff_measure[2]),tempbuf);
388  sprintf(tempbuf,"%d",maxadc[3]-minadc[3]);
389  gtk_label_set_text(GTK_LABEL(diff_measure[3]),tempbuf);
390  sprintf(tempbuf,"%d",maxadc[4]-minadc[4]);
391  gtk_label_set_text(GTK_LABEL(diff_measure[4]),tempbuf);
392  sprintf(tempbuf,"%d",maxadc[5]-minadc[5]);
393  gtk_label_set_text(GTK_LABEL(diff_measure[5]),tempbuf);
394 
395  sprintf(tempbuf,"--- N");
396  gtk_label_set_text(GTK_LABEL(newton_measure[0]),tempbuf);
397  sprintf(tempbuf,"--- N");
398  gtk_label_set_text(GTK_LABEL(newton_measure[1]),tempbuf);
399  sprintf(tempbuf,"--- N");
400  gtk_label_set_text(GTK_LABEL(newton_measure[2]),tempbuf);
401  sprintf(tempbuf,"--- N/m");
402  gtk_label_set_text(GTK_LABEL(newton_measure[3]),tempbuf);
403  sprintf(tempbuf,"--- N/m");
404  gtk_label_set_text(GTK_LABEL(newton_measure[4]),tempbuf);
405  sprintf(tempbuf,"--- N/m");
406  gtk_label_set_text(GTK_LABEL(newton_measure[5]),tempbuf);
407  }
408  return true;
409 }
410 
411 //*********************************************************************************
412 void close_window (GtkDialog *window, gpointer user_data)
413 {
414  STOP_TIMER
415  gtk_widget_destroy (GTK_WIDGET(window));
416 }
417 
418 //*********************************************************************************
419 void slider_changed (GtkButton *button, gpointer ch_p)
420 {
421  int chan = *(int*)ch_p;
422  //printf("debug: moved slider chan:%d\n",chan);
423  offset[chan] = (unsigned int) (gtk_range_get_value (GTK_RANGE(slider_gain[chan])));
424 // downloader.strain_get_offset (downloader.board_list[selected].bus, downloader.board_list[selected].pid, chan, curr_offset);
425 
426 // if (offset[chan]!=curr_offset)
427 /* {
428  something_changed=true;
429  downloader.strain_set_offset (downloader.board_list[selected].bus, downloader.board_list[selected].pid, chan, offset[chan]);
430  }*/
431 
432  if (first_time[chan]!=true)
433  {
434  something_changed=true;
435  downloader.strain_set_offset (downloader.board_list[selected].bus, downloader.board_list[selected].pid, chan, offset[chan]);
436  }
437  first_time[chan]=false;
438 }
439 
440 //*********************************************************************************
441 void file_save_click (GtkButton *button, gpointer ch_p)
442 {
443  std::string filename = "calibrationData";
444  filename += serial_no;
445  filename += ".dat";
446  fstream filestr;
447  filestr.open (filename.c_str(), fstream::out);
448  int i=0;
449  char buffer[256];
450 
451  //file version
452  filestr<<"File version:"<<endl;
453  filestr<<"2"<<endl;
454 
455  //serial number
456  filestr<<"Serial number:"<<endl;
457  sprintf (buffer,"%s",serial_no);
458  filestr<<buffer<<endl;
459 
460  //offsets
461  filestr<<"Offsets:"<<endl;
462  for (i=0;i<6; i++)
463  {
464  sprintf (buffer,"%d",offset[i]);
465  filestr<<buffer<<endl;
466  }
467 
468  //calibration matrix
469  filestr<<"Calibration matrix:"<<endl;
470  for (i=0;i<36; i++)
471  {
472  sprintf (buffer,"%x",matrix[i/6][i%6]);
473  filestr<<buffer<<endl;
474  }
475 
476  //matrix gain
477  filestr<<"Matrix gain:"<<endl;
478  sprintf (buffer,"%d",calib_const);
479  filestr<<buffer<<endl;
480 
481  //tare
482  filestr<<"Tare:"<<endl;
483  for (i=0;i<6; i++)
484  {
485  sprintf (buffer,"%d",calib_bias[i]);
486  filestr<<buffer<<endl;
487  }
488 
489  //full scale values
490  filestr<<"Full scale values:"<<endl;
491  for (i=0;i<6; i++)
492  {
493  sprintf (buffer,"%d",full_scale_const[i]);
494  filestr<<buffer<<endl;
495  }
496 
497  printf ("Calibration file saved!\n");
498  filestr.close();
499 }
500 
501 //*********************************************************************************
502 void reset_matrix_click (GtkButton *button, gpointer ch_p)
503 {
504  int ri=0;
505  int ci=0;
506  for (ri=0; ri<6; ri++)
507  {
508  for (ci=0; ci<6; ci++)
509  {
510  if (ri==ci)
511  gtk_entry_set_text (GTK_ENTRY (edit_matrix[ri][ci]),"7fff");
512  else
513  gtk_entry_set_text (GTK_ENTRY (edit_matrix[ri][ci]),"0");
514  }
515  }
516  calib_const=1;
517  char tempbuf[255];
518  sprintf(tempbuf,"%d",calib_const);
519  gtk_entry_set_text (GTK_ENTRY (edit_matrix_gain), tempbuf);
520 }
521 
522 //*********************************************************************************
523 void set_curr_bias_click (GtkButton *button, gpointer ch_p)
524 {
525  downloader.strain_set_curr_bias(downloader.board_list[selected].bus, downloader.board_list[selected].pid);
526 }
527 //*********************************************************************************
528 void reset_curr_bias_click (GtkButton *button, gpointer ch_p)
529 {
530  downloader.strain_reset_curr_bias(downloader.board_list[selected].bus, downloader.board_list[selected].pid);
531 }
532 //*********************************************************************************
533 void set_calib_bias_click (GtkButton *button, gpointer ch_p)
534 {
535  something_changed=true;
536  downloader.strain_set_calib_bias(downloader.board_list[selected].bus, downloader.board_list[selected].pid);
537 }
538 //*********************************************************************************
539 void reset_calib_bias_click (GtkButton *button, gpointer ch_p)
540 {
541  something_changed=true;
542  downloader.strain_reset_calib_bias(downloader.board_list[selected].bus, downloader.board_list[selected].pid);
543 }
544 //*********************************************************************************
545 bool calibration_load_v2 (char* filename, int selected_bus, int selected_id)
546 {
547  if (filename==NULL)
548  {
549  yError("File not found!\n");
550  return false;
551  }
552  if (selected_id <1 || selected_id >= 15)
553  {
554  yError("Invalid board address!\n");
555  return false;
556  }
557 
558  int file_version=0;
559  fstream filestr;
560  filestr.open (filename, fstream::in);
561  if (!filestr.is_open())
562  {
563  yError("Error opening calibration file!\n");
564  return false;
565  }
566 
567  int i=0;
568  char buffer[256];
569 
570  //file version
571  filestr.getline (buffer,256);
572  filestr.getline (buffer,256);
573  sscanf (buffer,"%d",&file_version);
574  if (file_version!=2)
575  {
576  yError("Wrong file. Calibration version != 2\n");
577  return false;
578  }
579 
580  //serial number
581  filestr.getline (buffer,256);
582  filestr.getline (buffer,256);
583  sprintf(serial_no,"%s", buffer);
584  downloader.strain_set_serial_number(selected_bus, selected_id, serial_no);
585 
586  //offsets
587  filestr.getline (buffer,256);
588  for (i=0;i<6; i++)
589  {
590  filestr.getline (buffer,256);
591  sscanf (buffer,"%d",&offset[i]);
592  // downloader.strain_set_offset (downloader.board_list[selected].bus, downloader.board_list[selected].pid, i, offset[i]);
593  downloader.strain_set_offset (selected_bus, selected_id, i, offset[i]);
594  drv_sleep(200);
595  }
596 
597  //calibration matrix
598  filestr.getline (buffer,256);
599  for (i=0;i<36; i++)
600  {
601  int ri=i/6;
602  int ci=i%6;
603  filestr.getline (buffer,256);
604  sscanf (buffer,"%x",&calib_matrix[ri][ci]);
605  printf("%d %x\n", calib_matrix[ri][ci],calib_matrix[ri][ci]);
606  downloader.strain_set_matrix_rc(selected_bus, selected_id, ri, ci, calib_matrix[ri][ci]);
607  }
608 
609  //matrix gain
610  filestr.getline (buffer,256);
611  filestr.getline (buffer,256);
612  int cc=0;
613  sscanf (buffer,"%d",&cc);
614  downloader.strain_set_matrix_gain(selected_bus, selected_id, cc);
615 
616  //tare
617  filestr.getline (buffer,256);
618  for (i=0;i<6; i++)
619  {
620  filestr.getline (buffer,256);
621  sscanf (buffer,"%d",&calib_bias[i]);
622  downloader.strain_set_calib_bias(selected_bus, selected_id, i, calib_bias[i]);
623  }
624 
625  //full scale values
626  filestr.getline (buffer,256);
627  for (i=0;i<6; i++)
628  {
629  filestr.getline (buffer,256);
630  sscanf (buffer,"%d",&full_scale_const[i]);
631  downloader.strain_set_full_scale(selected_bus, selected_id, i, full_scale_const[i]);
632  }
633 
634  filestr.close();
635  filestr.clear();
636 
637  matrix_changed=true;
638  something_changed=true;
639  printf ("Calibration file loaded!\n");
640 
641  return true;
642 }
643 
644 //*********************************************************************************
645 void file_load_click (GtkButton *button, gpointer ch_p)
646 {
647  int selected_bus = downloader.board_list[selected].bus;
648  int selected_id = downloader.board_list[selected].pid;
649 
650  char* buff;
651  buff = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(picker_calib));
652  if (buff==NULL)
653  {
654  yError("File not found!\n");
655  return;
656  }
657 
658  //load data file
659  calibration_load_v2 (buff, selected_bus, selected_id);
660 
661  //update windows graphics
662  int i=0;
663  int ri=0;
664  int ci=0;
665  char buffer[256];
666 
667  drv_sleep (500);
668  downloader.strain_get_serial_number(selected_bus, selected_id, buffer);
669  gtk_entry_set_text (GTK_ENTRY (edit_serial_number), buffer);
670  serial_number_changed=false;
671 
672  drv_sleep (500);
673  for (ri=0;ri<6;ri++)
674  for (ci=0;ci<6;ci++)
675  {
676  downloader.strain_get_matrix_rc(selected_bus, selected_id, ri, ci, matrix[ri][ci]);
677  sprintf(buffer,"%x",matrix[ri][ci]);
678  gtk_entry_set_text (GTK_ENTRY (edit_matrix[ri][ci]), buffer);
679  gtk_widget_modify_base (edit_matrix[ri][ci],GTK_STATE_NORMAL, NULL );
680  }
681  drv_sleep (500);
682  int count_ok=0;
683  for (i=0;i<36; i++)
684  {
685  ri=i/6;
686  ci=i%6;
687  if (calib_matrix[ri][ci]==matrix[ri][ci])
688  {
689  count_ok++;
690  }
691  else
692  {
693  printf ("Found 1 error on element %d,%d !!\n",ri, ci);
694  }
695  }
696  if (count_ok==36)
697  {
698  printf ("Calibration file applied with no errors\n");
699  matrix_changed=false;
700  }
701  else
702  {
703  printf ("Found %d errors applying the calibration file!!\n",36-count_ok);
704  }
705 }
706 
707 //*********************************************************************************
708 void file_import_click (GtkButton *button, gpointer ch_p)
709 {
710  std::string filename = "C:\\Software\\iCub\\bin\\debug\\ciao.dat";
711 
712  char* buff;
713 
714  buff = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(picker_calib));
715  if (buff==NULL)
716  {
717  yError("File not found!\n");
718  return;
719  }
720 
721  fstream filestr;
722  filestr.open (buff, fstream::in);
723  if (!filestr.is_open())
724  {
725  yError("Error opening calibration file!\n");
726  return;
727  }
728 
729  int i=0;
730  char buffer[256];
731  for (i=0;i<36; i++)
732  {
733  int ri=i/6;
734  int ci=i%6;
735  filestr.getline (buffer,256);
736  sscanf (buffer,"%x",&calib_matrix[ri][ci]);
737  printf("%d %x\n", calib_matrix[ri][ci],calib_matrix[ri][ci]);
738  downloader.strain_set_matrix_rc(downloader.board_list[selected].bus, downloader.board_list[selected].pid,ri,ci,calib_matrix[ri][ci]);
739  }
740  filestr.getline (buffer,256);
741  int cc=0;
742  sscanf (buffer,"%d",&cc);
743  downloader.strain_set_matrix_gain(downloader.board_list[selected].bus, downloader.board_list[selected].pid,cc);
744  for (i=0;i<6; i++)
745  {
746  filestr.getline (buffer,256);
747  sscanf (buffer,"%d",&cc);
748  downloader.strain_set_full_scale(downloader.board_list[selected].bus, downloader.board_list[selected].pid,i,cc);
749  }
750  filestr.close();
751 
752  something_changed=true;
753  printf ("Calibration file loaded!\n");
754 
755  int ri=0;
756  int ci=0;
757 
758  for (i=0; i<2; i++)
759  {
760  drv_sleep (1000);
761  for (ri=0;ri<6;ri++)
762  for (ci=0;ci<6;ci++)
763  {
764  downloader.strain_get_matrix_rc(downloader.board_list[selected].bus, downloader.board_list[selected].pid,ri,ci,matrix[ri][ci]);
765  sprintf(buffer,"%x",matrix[ri][ci]);
766  gtk_entry_set_text (GTK_ENTRY (edit_matrix[ri][ci]), buffer);
767  gtk_widget_modify_base (edit_matrix[ri][ci],GTK_STATE_NORMAL, NULL );
768  }
769  }
770  int count_ok=0;
771  for (i=0;i<36; i++)
772  {
773  ri=i/6;
774  ci=i%6;
775  if (calib_matrix[ri][ci]==matrix[ri][ci])
776  {
777  count_ok++;
778  }
779  else
780  {
781  printf ("Found 1 error on element %d,%d !!\n",ri, ci);
782  }
783  }
784  if (count_ok==36)
785  {
786  printf ("Calibration file %s applied with no errors\n", buff);
787  matrix_changed=false;
788  }
789  else
790  {
791  printf ("Found %d errors applying the calibration file!!\n",36-count_ok);
792  }
793 }
794 
795 //*********************************************************************************
796 void zero_changed (GtkButton *button, gpointer ch_p)
797 {
798  calibration_value = (unsigned int) (gtk_range_get_value (GTK_RANGE(slider_zero)));
799 }
800 
801 //*********************************************************************************
802 void matrix_change (GtkEntry *entry, gpointer index)
803 {
804  matrix_changed=true;
805  something_changed=true;
806  //printf("Calibration matrix changed\n");
807 }
808 
809 //*********************************************************************************
810 void serial_number_change (GtkEntry *entry, gpointer index)
811 {
813  something_changed=true;
814  //printf("Calibration matrix changed\n");
815 }
816 
817 //*********************************************************************************
818 void serial_number_send (GtkEntry *entry, gpointer index)
819 {
820  serial_number_changed=false;
821  const gchar* temp2 = gtk_entry_get_text (GTK_ENTRY (edit_serial_number));
822  sprintf(serial_no,"%s", temp2);
823  downloader.strain_set_serial_number(downloader.board_list[selected].bus, downloader.board_list[selected].pid,temp2);
824 }
825 //*********************************************************************************
826 void matrix_send (GtkEntry *entry, gpointer index)
827 {
828  int ri=0;
829  int ci=0;
830 
831  for (ri=0; ri<6; ri++)
832  for (ci=0; ci<6; ci++)
833  {
834  const gchar* temp2 = gtk_entry_get_text (GTK_ENTRY (edit_matrix[ri][ci]));
835  sscanf (temp2,"%x",&matrix[ri][ci]);
836  downloader.strain_set_matrix_rc(downloader.board_list[selected].bus, downloader.board_list[selected].pid,ri,ci,matrix[ri][ci]);
837  }
838 
839  downloader.strain_set_matrix_gain(downloader.board_list[selected].bus, downloader.board_list[selected].pid,calib_const);
840  printf("Calibration matrix updated\n");
841  matrix_changed=false;
842 
843  for (ri=0; ri<6; ri++)
844  for (ci=0; ci<6; ci++)
845  gtk_widget_modify_base (edit_matrix[ri][ci],GTK_STATE_NORMAL, NULL );
846 }
847 
848 //*********************************************************************************
849 void calibrate_click (GtkButton *button, gpointer user_data)
850 {
851  matrix_changed=false;
852  something_changed=false;
853  eeprom_saved_status=false;
854 
855  //which strain board is selected)
856  int i = 0;
857  int count = 0;
858 
859  for (i=0; i<6;i++)
860  {
861  first_time[i]=true;
862  amp_gain1[i]=0;
863  amp_gain2[i]=0;
864  }
865 
866  for (i=0; i<downloader.board_list_size; i++)
867  {
868  if (downloader.board_list[i].status==BOARD_RUNNING &&
869  (downloader.board_list[i].type==icubCanProto_boardType__strain || downloader.board_list[i].type==icubCanProto_boardType__6sg) &&
870  downloader.board_list[i].selected==true)
871  {
872  selected = i;
873  count++;
874  }
875  }
876  //only one board can be calibrated!!
877  if (count!=1) return;
878 
879  GtkWidget *calib_window;
880  GtkWidget *fixed;
881  GtkWidget *auto_button;
882  GtkWidget *file_load_button;
883  GtkWidget *file_import_button;
884  GtkWidget *file_save_button;
885  GtkWidget* label_gain[6];
886  GtkWidget* label_meas[6];
887 /*
888  calib_window = gtk_dialog_new_with_buttons ("Calibration Dialog",
889  GTK_WINDOW (window),
890  GTK_DIALOG_MODAL,
891  GTK_STOCK_OK,
892  GTK_RESPONSE_OK,
893  NULL);
894 */
895  calib_window = gtk_dialog_new ();
896  gtk_window_resize (GTK_WINDOW(calib_window),300,480);
897  char buff[255];
898  sprintf(buff, "Calibration of strain board CAN%d:%d", downloader.board_list[selected].bus, downloader.board_list[selected].pid);
899  gtk_window_set_title (GTK_WINDOW(calib_window),buff);
900  gtk_dialog_set_has_separator (GTK_DIALOG(calib_window),false);
901 
902  for (i=0;i<6;i++)
903  {
904  adc[i]=0;
905  maxadc[i]=0;
906  minadc[i]=65535;
907  }
908 
909  fixed = gtk_fixed_new();
910  picker_calib = gtk_file_chooser_button_new ("Pick a File", GTK_FILE_CHOOSER_ACTION_OPEN);
911  auto_button = gtk_button_new_with_mnemonic ("Automatic \nOffset Adj");
912  save_button = gtk_button_new_with_label ("Save to eeprom");
913  file_load_button = gtk_button_new_with_mnemonic ("Load Calibration File");
914  file_import_button = gtk_button_new_with_mnemonic ("Import Calib Matrix");
915  file_save_button = gtk_button_new_with_mnemonic ("Save Calibration File");
916  matrix_reset_button = gtk_button_new_with_mnemonic ("Reset Calibration");
917  set_calib_bias_button = gtk_button_new_with_mnemonic ("Set Calibration Bias"); ;
918  reset_calib_bias_button = gtk_button_new_with_mnemonic ("Reset Calibration Bias"); ;
919  set_curr_bias_button = gtk_button_new_with_mnemonic ("Set Current Bias"); ;
920  reset_curr_bias_button = gtk_button_new_with_mnemonic ("Reset Current Bias"); ;
921 
922  curr_measure[0] = gtk_label_new_with_mnemonic ("32000");
923  curr_measure[1] = gtk_label_new_with_mnemonic ("32000");
924  curr_measure[2] = gtk_label_new_with_mnemonic ("32000");
925  curr_measure[3] = gtk_label_new_with_mnemonic ("32000");
926  curr_measure[4] = gtk_label_new_with_mnemonic ("32000");
927  curr_measure[5] = gtk_label_new_with_mnemonic ("32000");
928  label_meas[0] = gtk_label_new_with_mnemonic ("Channel 0:");
929  label_meas[1] = gtk_label_new_with_mnemonic ("Channel 1:");
930  label_meas[2] = gtk_label_new_with_mnemonic ("Channel 2:");
931  label_meas[3] = gtk_label_new_with_mnemonic ("Channel 3:");
932  label_meas[4] = gtk_label_new_with_mnemonic ("Channel 4:");
933  label_meas[5] = gtk_label_new_with_mnemonic ("Channel 5:");
934  label_gain[0] = gtk_label_new_with_mnemonic ("Offset 0:");
935  label_gain[1] = gtk_label_new_with_mnemonic ("Offset 1:");
936  label_gain[2] = gtk_label_new_with_mnemonic ("Offset 2:");
937  label_gain[3] = gtk_label_new_with_mnemonic ("Offset 3:");
938  label_gain[4] = gtk_label_new_with_mnemonic ("Offset 4:");
939  label_gain[5] = gtk_label_new_with_mnemonic ("Offset 5:");
940 
941  max_measure[0] = gtk_label_new_with_mnemonic ("32000");
942  max_measure[1] = gtk_label_new_with_mnemonic ("32000");
943  max_measure[2] = gtk_label_new_with_mnemonic ("32000");
944  max_measure[3] = gtk_label_new_with_mnemonic ("32000");
945  max_measure[4] = gtk_label_new_with_mnemonic ("32000");
946  max_measure[5] = gtk_label_new_with_mnemonic ("32000");
947  min_measure[0] = gtk_label_new_with_mnemonic ("32000");
948  min_measure[1] = gtk_label_new_with_mnemonic ("32000");
949  min_measure[2] = gtk_label_new_with_mnemonic ("32000");
950  min_measure[3] = gtk_label_new_with_mnemonic ("32000");
951  min_measure[4] = gtk_label_new_with_mnemonic ("32000");
952  min_measure[5] = gtk_label_new_with_mnemonic ("32000");
953  diff_measure[0] = gtk_label_new_with_mnemonic ("32000");
954  diff_measure[1] = gtk_label_new_with_mnemonic ("32000");
955  diff_measure[2] = gtk_label_new_with_mnemonic ("32000");
956  diff_measure[3] = gtk_label_new_with_mnemonic ("32000");
957  diff_measure[4] = gtk_label_new_with_mnemonic ("32000");
958  diff_measure[5] = gtk_label_new_with_mnemonic ("32000");
959  newton_measure[0] = gtk_label_new_with_mnemonic ("0");
960  newton_measure[1] = gtk_label_new_with_mnemonic ("0");
961  newton_measure[2] = gtk_label_new_with_mnemonic ("0");
962  newton_measure[3] = gtk_label_new_with_mnemonic ("0");
963  newton_measure[4] = gtk_label_new_with_mnemonic ("0");
964  newton_measure[5] = gtk_label_new_with_mnemonic ("0");
965  calib_bias_label[0] = gtk_label_new_with_mnemonic ("0");
966  calib_bias_label[1] = gtk_label_new_with_mnemonic ("0");
967  calib_bias_label[2] = gtk_label_new_with_mnemonic ("0");
968  calib_bias_label[3] = gtk_label_new_with_mnemonic ("0");
969  calib_bias_label[4] = gtk_label_new_with_mnemonic ("0");
970  calib_bias_label[5] = gtk_label_new_with_mnemonic ("0");
971  curr_bias_label[0] = gtk_label_new_with_mnemonic ("0");
972  curr_bias_label[1] = gtk_label_new_with_mnemonic ("0");
973  curr_bias_label[2] = gtk_label_new_with_mnemonic ("0");
974  curr_bias_label[3] = gtk_label_new_with_mnemonic ("0");
975  curr_bias_label[4] = gtk_label_new_with_mnemonic ("0");
976  curr_bias_label[5] = gtk_label_new_with_mnemonic ("0");
977  full_scale_label[0] = gtk_label_new_with_mnemonic ("0");
978  full_scale_label[1] = gtk_label_new_with_mnemonic ("0");
979  full_scale_label[2] = gtk_label_new_with_mnemonic ("0");
980  full_scale_label[3] = gtk_label_new_with_mnemonic ("0");
981  full_scale_label[4] = gtk_label_new_with_mnemonic ("0");
982  full_scale_label[5] = gtk_label_new_with_mnemonic ("0");
983  label_matrix_gain = gtk_label_new_with_mnemonic ("matrix gain:");
984  edit_matrix_gain = gtk_label_new_with_mnemonic ("null");
985 
986  check_raw_vals = gtk_check_button_new_with_label ("use calib matrix");
987 
988  slider_gain[0] = gtk_hscale_new_with_range (0,0x3FF,1);
989  slider_gain[1] = gtk_hscale_new_with_range (0,0x3FF,1);
990  slider_gain[2] = gtk_hscale_new_with_range (0,0x3FF,1);
991  slider_gain[3] = gtk_hscale_new_with_range (0,0x3FF,1);
992  slider_gain[4] = gtk_hscale_new_with_range (0,0x3FF,1);
993  slider_gain[5] = gtk_hscale_new_with_range (0,0x3FF,1);
994  slider_zero = gtk_hscale_new_with_range (0,65535,1);
995 
996  int ri,ci=0;
997  for (ri=0;ri<6;ri++)
998  for (ci=0;ci<6;ci++)
999  edit_matrix[ri][ci] = gtk_entry_new ();
1000 
1001  edit_serial_number = gtk_entry_new ();
1002 
1003  gtk_container_add (GTK_CONTAINER(GTK_BOX (GTK_DIALOG (calib_window)->vbox)),fixed);
1004 
1005  int r[8]={0+10,60+10,60*2+10,60*3+10,60*4+10,60*5+10,60*6+10,60*7+10};
1006  int c[12]={0+10,50+10,150+10,230+10,350+10,400+10,450+10,500+10,550+10,650+10,700+10,750+10};
1007 
1008  gtk_fixed_put(GTK_FIXED(fixed),label_gain[0],c[0],r[0]);
1009  gtk_fixed_put(GTK_FIXED(fixed),label_gain[1],c[0],r[1]);
1010  gtk_fixed_put(GTK_FIXED(fixed),label_gain[2],c[0],r[2]);
1011  gtk_fixed_put(GTK_FIXED(fixed),label_gain[3],c[0],r[3]);
1012  gtk_fixed_put(GTK_FIXED(fixed),label_gain[4],c[0],r[4]);
1013  gtk_fixed_put(GTK_FIXED(fixed),label_gain[5],c[0],r[5]);
1014 
1015  gtk_fixed_put(GTK_FIXED(fixed),slider_gain[0],c[1],r[0]-10);
1016  gtk_fixed_put(GTK_FIXED(fixed),slider_gain[1],c[1],r[1]-10);
1017  gtk_fixed_put(GTK_FIXED(fixed),slider_gain[2],c[1],r[2]-10);
1018  gtk_fixed_put(GTK_FIXED(fixed),slider_gain[3],c[1],r[3]-10);
1019  gtk_fixed_put(GTK_FIXED(fixed),slider_gain[4],c[1],r[4]-10);
1020  gtk_fixed_put(GTK_FIXED(fixed),slider_gain[5],c[1],r[5]-10);
1021 
1022  gtk_fixed_put(GTK_FIXED(fixed),set_calib_bias_button,c[11],r[2]-10);
1023  gtk_fixed_put(GTK_FIXED(fixed),reset_calib_bias_button,c[11],r[3]-10);
1024  gtk_fixed_put(GTK_FIXED(fixed),set_curr_bias_button,c[11],r[4]-10);
1025  gtk_fixed_put(GTK_FIXED(fixed),reset_curr_bias_button,c[11],r[5]-10);
1026 
1027  for (ri=0;ri<6;ri++)
1028  for (ci=0;ci<6;ci++)
1029  {
1030  gtk_fixed_put(GTK_FIXED(fixed),edit_matrix[ci][ri],c[5]+ri*42,r[5]+40+ci*32);
1031  gtk_widget_set_size_request(edit_matrix[ri][ci],40,20);
1032  }
1033 
1034  gtk_fixed_put(GTK_FIXED(fixed),edit_serial_number,c[1]-20,r[5]+150);
1035  gtk_widget_set_size_request(edit_serial_number,100,20);
1036 
1037  gtk_widget_set_size_request(slider_gain[0],80,30);
1038  gtk_widget_set_size_request(slider_gain[1],80,30);
1039  gtk_widget_set_size_request(slider_gain[2],80,30);
1040  gtk_widget_set_size_request(slider_gain[3],80,30);
1041  gtk_widget_set_size_request(slider_gain[4],80,30);
1042  gtk_widget_set_size_request(slider_gain[5],80,30);
1043 
1044  gtk_widget_set_size_request(slider_zero,100,30);
1045 
1046  gtk_fixed_put(GTK_FIXED(fixed),label_meas[0],c[2],r[0]);
1047  gtk_fixed_put(GTK_FIXED(fixed),label_meas[1],c[2],r[1]);
1048  gtk_fixed_put(GTK_FIXED(fixed),label_meas[2],c[2],r[2]);
1049  gtk_fixed_put(GTK_FIXED(fixed),label_meas[3],c[2],r[3]);
1050  gtk_fixed_put(GTK_FIXED(fixed),label_meas[4],c[2],r[4]);
1051  gtk_fixed_put(GTK_FIXED(fixed),label_meas[5],c[2],r[5]);
1052 
1053  gtk_fixed_put(GTK_FIXED(fixed),curr_measure[0],c[3],r[0]);
1054  gtk_fixed_put(GTK_FIXED(fixed),curr_measure[1],c[3],r[1]);
1055  gtk_fixed_put(GTK_FIXED(fixed),curr_measure[2],c[3],r[2]);
1056  gtk_fixed_put(GTK_FIXED(fixed),curr_measure[3],c[3],r[3]);
1057  gtk_fixed_put(GTK_FIXED(fixed),curr_measure[4],c[3],r[4]);
1058  gtk_fixed_put(GTK_FIXED(fixed),curr_measure[5],c[3],r[5]);
1059 
1060  gtk_fixed_put(GTK_FIXED(fixed),max_measure[0],c[4],r[0]);
1061  gtk_fixed_put(GTK_FIXED(fixed),max_measure[1],c[4],r[1]);
1062  gtk_fixed_put(GTK_FIXED(fixed),max_measure[2],c[4],r[2]);
1063  gtk_fixed_put(GTK_FIXED(fixed),max_measure[3],c[4],r[3]);
1064  gtk_fixed_put(GTK_FIXED(fixed),max_measure[4],c[4],r[4]);
1065  gtk_fixed_put(GTK_FIXED(fixed),max_measure[5],c[4],r[5]);
1066 
1067  gtk_fixed_put(GTK_FIXED(fixed),min_measure[0],c[5],r[0]);
1068  gtk_fixed_put(GTK_FIXED(fixed),min_measure[1],c[5],r[1]);
1069  gtk_fixed_put(GTK_FIXED(fixed),min_measure[2],c[5],r[2]);
1070  gtk_fixed_put(GTK_FIXED(fixed),min_measure[3],c[5],r[3]);
1071  gtk_fixed_put(GTK_FIXED(fixed),min_measure[4],c[5],r[4]);
1072  gtk_fixed_put(GTK_FIXED(fixed),min_measure[5],c[5],r[5]);
1073 
1074  gtk_fixed_put(GTK_FIXED(fixed),diff_measure[0],c[6],r[0]);
1075  gtk_fixed_put(GTK_FIXED(fixed),diff_measure[1],c[6],r[1]);
1076  gtk_fixed_put(GTK_FIXED(fixed),diff_measure[2],c[6],r[2]);
1077  gtk_fixed_put(GTK_FIXED(fixed),diff_measure[3],c[6],r[3]);
1078  gtk_fixed_put(GTK_FIXED(fixed),diff_measure[4],c[6],r[4]);
1079  gtk_fixed_put(GTK_FIXED(fixed),diff_measure[5],c[6],r[5]);
1080 
1081  gtk_fixed_put(GTK_FIXED(fixed),newton_measure[0],c[8],r[0]);
1082  gtk_fixed_put(GTK_FIXED(fixed),newton_measure[1],c[8],r[1]);
1083  gtk_fixed_put(GTK_FIXED(fixed),newton_measure[2],c[8],r[2]);
1084  gtk_fixed_put(GTK_FIXED(fixed),newton_measure[3],c[8],r[3]);
1085  gtk_fixed_put(GTK_FIXED(fixed),newton_measure[4],c[8],r[4]);
1086  gtk_fixed_put(GTK_FIXED(fixed),newton_measure[5],c[8],r[5]);
1087 
1088  gtk_fixed_put(GTK_FIXED(fixed),calib_bias_label[0],c[9],r[0]);
1089  gtk_fixed_put(GTK_FIXED(fixed),calib_bias_label[1],c[9],r[1]);
1090  gtk_fixed_put(GTK_FIXED(fixed),calib_bias_label[2],c[9],r[2]);
1091  gtk_fixed_put(GTK_FIXED(fixed),calib_bias_label[3],c[9],r[3]);
1092  gtk_fixed_put(GTK_FIXED(fixed),calib_bias_label[4],c[9],r[4]);
1093  gtk_fixed_put(GTK_FIXED(fixed),calib_bias_label[5],c[9],r[5]);
1094 
1095  gtk_fixed_put(GTK_FIXED(fixed),curr_bias_label[0],c[10],r[0]);
1096  gtk_fixed_put(GTK_FIXED(fixed),curr_bias_label[1],c[10],r[1]);
1097  gtk_fixed_put(GTK_FIXED(fixed),curr_bias_label[2],c[10],r[2]);
1098  gtk_fixed_put(GTK_FIXED(fixed),curr_bias_label[3],c[10],r[3]);
1099  gtk_fixed_put(GTK_FIXED(fixed),curr_bias_label[4],c[10],r[4]);
1100  gtk_fixed_put(GTK_FIXED(fixed),curr_bias_label[5],c[10],r[5]);
1101 
1102  gtk_fixed_put(GTK_FIXED(fixed),full_scale_label[0],c[1]-20,r[5]+180);
1103  gtk_fixed_put(GTK_FIXED(fixed),full_scale_label[1],c[1]-20,r[5]+195);
1104  gtk_fixed_put(GTK_FIXED(fixed),full_scale_label[2],c[1]-20,r[5]+210);
1105  gtk_fixed_put(GTK_FIXED(fixed),full_scale_label[3],c[1]-20,r[5]+225);
1106  gtk_fixed_put(GTK_FIXED(fixed),full_scale_label[4],c[1]-20,r[5]+240);
1107  gtk_fixed_put(GTK_FIXED(fixed),full_scale_label[5],c[1]-20,r[5]+255);
1108 
1109  gtk_fixed_put(GTK_FIXED(fixed),label_matrix_gain,c[5],r[5]+40+6*32);
1110  gtk_fixed_put(GTK_FIXED(fixed),edit_matrix_gain,c[5]+100,r[5]+40+6*32);
1111  gtk_fixed_put(GTK_FIXED(fixed),matrix_reset_button,c[5]+140,r[5]+40+6*32);
1112 
1113  gtk_widget_set_size_request(auto_button,100,40);
1114  gtk_widget_set_size_request(save_button,140,40);
1115  gtk_widget_set_size_request(file_load_button,140,40);
1116  gtk_widget_set_size_request(file_import_button,140,40);
1117  gtk_widget_set_size_request(file_save_button,140,40);
1118  gtk_widget_set_size_request(picker_calib,140,40);
1119  gtk_widget_set_size_request(matrix_reset_button,140,30);
1120  gtk_widget_set_size_request(set_calib_bias_button,140,40);
1121  gtk_widget_set_size_request(reset_calib_bias_button,140,40);
1122  gtk_widget_set_size_request(set_curr_bias_button,140,40);
1123  gtk_widget_set_size_request(reset_curr_bias_button,140,40);
1124 
1125  g_signal_connect (file_import_button, "clicked", G_CALLBACK (file_import_click),NULL);
1126  g_signal_connect (file_load_button, "clicked", G_CALLBACK (file_load_click),NULL);
1127  g_signal_connect (file_save_button, "clicked", G_CALLBACK (file_save_click),NULL);
1128  g_signal_connect (matrix_reset_button, "clicked", G_CALLBACK (reset_matrix_click),NULL);
1129  g_signal_connect (auto_button, "clicked", G_CALLBACK (auto_click),NULL);
1130  g_signal_connect (save_button, "clicked", G_CALLBACK (save_click),NULL);
1131  g_signal_connect (calib_window, "response", G_CALLBACK (close_window),NULL);
1132  g_signal_connect (set_calib_bias_button, "clicked", G_CALLBACK (set_calib_bias_click),NULL);
1133  g_signal_connect (reset_calib_bias_button, "clicked", G_CALLBACK (reset_calib_bias_click),NULL);
1134  g_signal_connect (set_curr_bias_button, "clicked", G_CALLBACK (set_curr_bias_click),NULL);
1135  g_signal_connect (reset_curr_bias_button, "clicked", G_CALLBACK (reset_curr_bias_click),NULL);
1136 
1137 
1138  g_signal_connect (slider_gain[0], "value-changed", G_CALLBACK (slider_changed),&ch[0]);
1139  g_signal_connect (slider_gain[1], "value-changed", G_CALLBACK (slider_changed),&ch[1]);
1140  g_signal_connect (slider_gain[2], "value-changed", G_CALLBACK (slider_changed),&ch[2]);
1141  g_signal_connect (slider_gain[3], "value-changed", G_CALLBACK (slider_changed),&ch[3]);
1142  g_signal_connect (slider_gain[4], "value-changed", G_CALLBACK (slider_changed),&ch[4]);
1143  g_signal_connect (slider_gain[5], "value-changed", G_CALLBACK (slider_changed),&ch[5]);
1144  g_signal_connect (slider_zero, "value-changed", G_CALLBACK (zero_changed),NULL);
1145 
1146  gtk_fixed_put(GTK_FIXED(fixed),check_raw_vals,c[5],r[5]+60+6*32);
1147  gtk_fixed_put(GTK_FIXED(fixed),auto_button,c[1]-20,r[5]+40);
1148  gtk_fixed_put(GTK_FIXED(fixed),slider_zero,c[1]-20,r[5]+100);
1149  gtk_fixed_put(GTK_FIXED(fixed),save_button,c[2]+10,r[5]+40);
1150  gtk_fixed_put(GTK_FIXED(fixed),file_load_button,c[2]+10,r[5]+90);
1151  gtk_fixed_put(GTK_FIXED(fixed),file_save_button,c[2]+10,r[5]+140);
1152  gtk_fixed_put(GTK_FIXED(fixed),file_import_button,c[2]+10,r[5]+190);
1153 
1154  gtk_fixed_put(GTK_FIXED(fixed),picker_calib,c[2]+10,r[5]+240);
1155 
1156  gtk_range_set_value (GTK_RANGE(slider_zero),calibration_value);
1157 
1158  downloader.strain_get_serial_number(downloader.board_list[selected].bus, downloader.board_list[selected].pid, serial_no);
1159  gtk_entry_set_text (GTK_ENTRY (edit_serial_number), serial_no);
1160 
1161  downloader.strain_start_sampling(downloader.board_list[selected].bus, downloader.board_list[selected].pid);
1162 
1163  STOP_TIMER
1164  START_TIMER
1165  timer_func (NULL);
1166 
1167  int index[36];
1168  for (ri=0;ri<6;ri++)
1169  for (ci=0;ci<6;ci++)
1170  {
1171  index[ri*6+ci]=ri*100+ci;
1172  g_signal_connect(edit_matrix[ri][ci], "changed", G_CALLBACK (matrix_change),&index[ri*6+ci]);
1173  g_signal_connect(edit_matrix[ri][ci], "activate", G_CALLBACK (matrix_send),&index[ri*6+ci]);
1174  }
1175 
1176  g_signal_connect(edit_serial_number, "changed", G_CALLBACK (serial_number_change),NULL);
1177  g_signal_connect(edit_serial_number, "activate", G_CALLBACK (serial_number_send),NULL);
1178 
1179  gtk_widget_show_all (fixed);
1180  //gtk_window_set_resizable(GTK_WINDOW(calib_window),false);
1181  gtk_dialog_run (GTK_DIALOG (calib_window));
1182 }
GtkWidget * edit_matrix[6][6]
#define BOARD_RUNNING
Definition: downloader.h:45
int strain_reset_curr_bias(int bus, int target_id, string *errorstring=NULL)
Definition: downloader.cpp:624
void calibrate_click(GtkButton *button, gpointer user_data)
void reset_curr_bias_click(GtkButton *button, gpointer ch_p)
GtkWidget * matrix_reset_button
GtkWidget * picker_calib
int strain_set_serial_number(int bus, int target_id, const char *serial_number, string *errorstring=NULL)
Definition: downloader.cpp:646
#define STOP_TIMER
GtkWidget * reset_calib_bias_button
int selected
out
Definition: sine.m:8
int ch[6]
int strain_set_matrix_gain(int bus, int target_id, unsigned int gain, int regset=strain_regset_inuse, string *errorstring=NULL)
Definition: downloader.cpp:893
bool selected
Definition: downloader.h:38
GtkWidget * max_measure[6]
char serial_no[8]
int board_list_size
Definition: downloader.h:150
GtkWidget * curr_bias_label[6]
GtkWidget * label_matrix_gain
int strain_reset_calib_bias(int bus, int target_id, string *errorstring=NULL)
Definition: downloader.cpp:526
STL namespace.
int strain_get_matrix_rc(int bus, int target_id, char r, char c, unsigned int &elem, int regset=strain_regset_inuse, string *errorstring=NULL)
void file_load_click(GtkButton *button, gpointer ch_p)
void file_save_click(GtkButton *button, gpointer ch_p)
unsigned int amp_gain2[6]
unsigned int calib_const
GtkWidget * set_calib_bias_button
unsigned int amp_gain1[6]
GtkWidget * check_raw_vals
int strain_get_serial_number(int bus, int target_id, char *serial_number, string *errorstring=NULL)
Definition: downloader.cpp:674
int strain_get_full_scale(int bus, int target_id, unsigned char channel, unsigned int &full_scale, int regset=strain_regset_inuse, string *errorstring=NULL)
sBoard * board_list
Definition: downloader.h:149
void reset_calib_bias_click(GtkButton *button, gpointer ch_p)
void matrix_send(GtkEntry *entry, gpointer index)
unsigned int calibration_value
int strain_set_curr_bias(int bus, int target_id, string *errorstring=NULL)
Definition: downloader.cpp:577
#define HEX_VALC
gboolean timer_func(gpointer data)
bool serial_number_changed
void close_window(GtkDialog *window, gpointer user_data)
void auto_click(GtkButton *button, gpointer user_data)
int calib_bias[6]
int strain_get_curr_bias(int bus, int target_id, char channel, signed int &bias, string *errorstring=NULL)
Definition: downloader.cpp:547
int strain_start_sampling(int bus, int target_id, string *errorstring=NULL)
bool eeprom_saved_status
int strain_set_matrix_rc(int bus, int target_id, char r, char c, unsigned int elem, int regset=strain_regset_inuse, string *errorstring=NULL)
unsigned int offset[6]
#define START_TIMER
void serial_number_change(GtkEntry *entry, gpointer index)
bool calibration_load_v2(char *filename, int selected_bus, int selected_id)
int strain_get_matrix_gain(int bus, int target_id, unsigned int &gain, int regset=strain_regset_inuse, string *errorstring=NULL)
Definition: downloader.cpp:748
void slider_changed(GtkButton *button, gpointer ch_p)
int strain_get_offset(int bus, int target_id, char channel, unsigned int &offset, int regset=strain_regset_inuse, string *errorstring=NULL)
Definition: downloader.cpp:401
GtkWidget * set_curr_bias_button
int strain_save_to_eeprom(int bus, int target_id, string *errorstring=NULL)
Definition: downloader.cpp:216
unsigned int maxadc[6]
GtkWidget * diff_measure[6]
void set_calib_bias_click(GtkButton *button, gpointer ch_p)
GtkWidget * edit_serial_number
GtkWidget * edit_matrix_gain
void drv_sleep(double time)
Definition: downloader.cpp:28
GtkWidget * calib_bias_label[6]
int bus
Definition: downloader.h:25
void file_import_click(GtkButton *button, gpointer ch_p)
int curr_bias[6]
GtkWidget * slider_zero
int strain_set_calib_bias(int bus, int target_id, string *errorstring=NULL)
Definition: downloader.cpp:478
GtkWidget * full_scale_label[6]
unsigned int adc[6]
int status
Definition: downloader.h:37
int strain_get_calib_bias(int bus, int target_id, char channel, signed int &bias, int regset=strain_regset_inuse, string *errorstring=NULL)
Definition: downloader.cpp:446
void matrix_change(GtkEntry *entry, gpointer index)
int strain_set_offset(int bus, int target_id, char channel, unsigned int offset, int regset=strain_regset_inuse, string *errorstring=NULL)
GtkWidget * save_button
GtkWidget * window
Definition: main.cpp:152
void reset_matrix_click(GtkButton *button, gpointer ch_p)
GtkWidget * slider_gain[6]
bool something_changed
void zero_changed(GtkButton *button, gpointer ch_p)
guint timer_refresh
GtkWidget * info_dlg
GtkWidget * min_measure[6]
void set_curr_bias_click(GtkButton *button, gpointer ch_p)
unsigned int minadc[6]
void save_click(GtkButton *button, gpointer user_data)
int strain_set_full_scale(int bus, int target_id, unsigned char channel, unsigned int full_scale, int regset=strain_regset_inuse, string *errorstring=NULL)
unsigned int calib_matrix[6][6]
GtkWidget * newton_measure[6]
bool first_time[6]
GtkWidget * curr_measure[6]
cDownloader downloader
Definition: main.cpp:188
unsigned int matrix[6][6]
int strain_get_eeprom_saved(int bus, int target_id, bool *status, string *errorstring=NULL)
Definition: downloader.cpp:715
void serial_number_send(GtkEntry *entry, gpointer index)
int strain_get_adc(int bus, int target_id, char channel, unsigned int &adc, int type, string *errorstring=NULL)
Definition: downloader.cpp:350
int strain_calibrate_offset(int bus, int target_id, icubCanProto_boardType_t boardtype, unsigned int middle_val, string *errorstring=NULL)
bool matrix_changed
int pid
Definition: downloader.h:26
GtkWidget * reset_curr_bias_button
int type
Definition: downloader.h:27
unsigned int full_scale_const[6]