iCub-main
straincalibgui.cpp
Go to the documentation of this file.
1 #include "straincalibgui.h"
2 #include "ui_straincalibgui.h"
3 #include <QDebug>
4 #include <QtConcurrent/QtConcurrent>
5 #include <QFileDialog>
6 
7 
8 //#define NSAMPLES 1000
9 
10 StrainCalibGui::StrainCalibGui(QString device, int bus, int pid, FirmwareUpdaterCore *core, QWidget *parent) :
11  QDialog(parent), mutexacquisitionofdata(QMutex::Recursive), mutexdriver(QMutex::Recursive),
12  ui(new Ui::StrainCalibGui)
13 {
14  progressvalue = 0;
15  acquiringdata = false;
16  enableacquisitions = true;
17  ui->setupUi(this);
18  this->core = core;
19  fp = NULL;
20  trial=-1;
21  current_trial = -1;
22  connect(ui->btnAcquireData,SIGNAL(clicked(bool)),this,SLOT(onAcquireData(bool)));
23  buttons.append(ui->btn1);
24  buttons.append(ui->btn2);
25  buttons.append(ui->btn3);
26  buttons.append(ui->btn4);
27  buttons.append(ui->btn5);
28  buttons.append(ui->btn6);
29  buttons.append(ui->btn7);
30  buttons.append(ui->btn8);
31  buttons.append(ui->btn9);
32  buttons.append(ui->btn10);
33  buttons.append(ui->btn11);
34  buttons.append(ui->btn12);
35  buttons.append(ui->btn13);
36 
37  readValues.append(ui->cur1);
38  readValues.append(ui->cur2);
39  readValues.append(ui->cur3);
40  readValues.append(ui->cur4);
41  readValues.append(ui->cur5);
42  readValues.append(ui->cur6);
43 
44  expValues.append(ui->exp1);
45  expValues.append(ui->exp2);
46  expValues.append(ui->exp3);
47  expValues.append(ui->exp4);
48  expValues.append(ui->exp5);
49  expValues.append(ui->exp6);
50 
51  errValues.append(ui->err1);
52  errValues.append(ui->err2);
53  errValues.append(ui->err3);
54  errValues.append(ui->err4);
55  errValues.append(ui->err5);
56  errValues.append(ui->err6);
57 
58  ui->freeEdit1->setValidator(&validator);
59  ui->freeEdit2->setValidator(&validator);
60  ui->freeEdit3->setValidator(&validator);
61  ui->freeEdit4->setValidator(&validator);
62  ui->freeEdit5->setValidator(&validator);
63  ui->freeEdit6->setValidator(&validator);
64 
65 
66  int bi[13]={8020,1001,8010,8030,8040,8050,8060,8070,8080,3001,4001,5001,6001};
67 
68  for (int i=0; i<buttons.count();i++) {
69  buttons.at(i)->setProperty("btnIndex",bi[i]);
70  connect(buttons.at(i),SIGNAL(clicked(bool)),this,SLOT(onButtonClick(bool)));
71  }
72 
73  connect(ui->freeAcqModeGroup,SIGNAL(toggled(bool)),this,SLOT(onFreeAcqMode(bool)));
74  //connect(ui->buttonBox,SIGNAL(accepted()),this,SLOT(close()));
75  connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(onClose()));
76 
77 
78  std::string filenameOfexpectedValues = std::string("firmwareupdater.strain.expectedvalues.txt");
79  bool b = expected_values_handler.init(filenameOfexpectedValues.c_str());
80  if(!b){
81  yError() << "ERROR in opening file" << filenameOfexpectedValues << ".... using default values for expected value handler";
82  expected_values_handler.init();
83  }
84  else
85  {
86  yDebug() << "loaded file of expected values =" << filenameOfexpectedValues;
87  }
88 
89  int low = 0;
90  int high = 0;
91  expected_values_handler.get_thresholds(low, high);
92  yDebug() << "expected values for" << expected_values_handler.get_serialnumber() << "with thresholds = " << low << high;
93 
94 // QString tt = QString("Current Error [%1, %2]").arg(low, high);
95 // ui->label_16->setText(tt);
96 
97  connect(&watcher, SIGNAL(finished()), this, SLOT(onFutureFinished()));
98 
99 #ifdef ATI_SENS
100  ATIsens = new devMeasurementFns();
101 #endif
102 
103 
104  if((device == "ETH") || (device == "eth"))
105  {
107  yDebug() << "strainInterface::Network::ETH";
108  }
109  else if ((device == "SOCKETCAN") || (device == "socketcan"))
110  {
111  yDebug() << "strainInterface::Network::socketcan";
113  }
114  else if((device == "ECAN") || (device == "ecan"))
115  {
116  yDebug() << "strainInterface::Network::ecan";
118  }
119  else
120  {
122  yDebug() << "unknown strainInterface::Network =" << device.toStdString();
123  }
124 
125  config.canbus = (strainInterface::CanBus)bus;
127  config.txrate = 2;
128 // config.load_default();
129 // if(!sI.open(config)){
130 // qDebug() << "Error opening device";
131 // }
132 
133 
134  connect(&timer,SIGNAL(timeout()),this,SLOT(onTimerTimeout()));
135  timer.setInterval(500);
136  timer.setSingleShot(false);
137  timer.start();
138 
139  enabledebugprints = false;
140 
141 
142  if(false == checkDirectory())
143  {
144  yError() << "CANNOT find the data folder. Please create it!";
145  ui->labelInstructions->setText("You can select an acquisition from the menu ... but:"
146  "\nCANNOT find the data folder."
147  "\nIf you want your acquisition data saved, please create it!");
148  }
149 // else
150 // {
151 // ui->labelInstructions->setText("Select an acquisition from the menu");
152 // }
153 
154 }
155 
157 {
158  //sI.close();
159 
160 
161  stopacquisitions();
162 
163  delete ui;
164 }
165 
166 void StrainCalibGui::stopacquisitions(bool forcestop)
167 {
168  // wait all acquisitions to be over:
169  // - stop timer so that tick_acquisition() is not called anymore
170  // - wait pending acquisitions to be over by calling two lockdriver() true / false
171 
172  static bool alreadystopped = false;
173  if((false == alreadystopped) || (true == forcestop))
174  {
175  // we must be sure that no tick_acquisition() is executed: we stop the timer so that no future
176  // callback onTimerTimeout() is called. but it may be executing rigth now.
177  // hence we first set enableacquisitions = false for furthe security
178  enableacquisitions = false;
179  timer.stop();
180 
181  // we now wait until any acquisition is over and we force stop of strain tx. twice!
182  while(false == mutexdriver.tryLock(250))
183  {
184  ui->progress->setValue(progressvalue);
185  ui->labelInstructions->setText("Received the QUIT command: waiting for the acquisition to terminate");
186  }
187  //lockdriver(true);
188  // marco.accame: on windows, esdcan driver cannot manage streaming mode in reception.... sic
190  core->getDownloader()->strain_acquire_stop(config.get_canbus(), config.get_canaddress(), acqmode);
191  core->getDownloader()->strain_acquire_stop(config.get_canbus(), config.get_canaddress(), acqmode);
192  lockdriver(false);
193 
194  alreadystopped = true;
195  }
196 }
197 
198 void StrainCalibGui::onClose()
199 {
200  stopacquisitions(true);
201  stopacquisitions(true);
202 
203  close();
204 }
205 
206 void StrainCalibGui::onFreeAcqMode(bool b)
207 {
208  if(b){
209  ui->spinSamples->setEnabled(true);
210  ui->btnContainer1->setEnabled(false);
211  ui->btnContainer2->setEnabled(false);
212  ui->btnAcquireData->setEnabled(true);
213  ui->labelInstructions->setText("Free Acquisiton Mode");
214  } else {
215  ui->btnContainer1->setEnabled(true);
216  ui->btnContainer2->setEnabled(true);
217  ui->btnAcquireData->setEnabled(false);
218 
219  if(false == checkDirectory())
220  {
221  yError() << "CANNOT find the data folder. Please create it!";
222  ui->labelInstructions->setText("You can select an acquisition from the menu ... but:"
223  "\nCANNOT find the data folder."
224  "\nIf you want your acquisition data saved, please create it!");
225  }
226  else
227  {
228  ui->labelInstructions->setText("Select an acquisition from the menu");
229  }
230  }
231 }
232 
233 void StrainCalibGui::onFutureFinished()
234 {
235  ui->progress->setMaximum(100);
236 
237  if(!watcher.result()){
238  ui->labelInstructions->setText("Not enough data acquired. Repeat acquisition.");
239  close_files();
240  return;
241  }
242 
243 
244  if(ui->freeAcqModeGroup->isChecked())
245  {
246  close_files();
247  return;
248  }
249  else
250  {
251  int rtrial=remap_trials();
252 
253  if (rtrial==-1){
254  trial_bias=last_value;
255  }
256 
257  trial++;
258  qDebug("%d\n",trial); //debug only
259  showMenu();
260  }
261 
262 }
263 
264 void StrainCalibGui::onAcquireData(bool b)
265 {
266 
267  if(ui->freeAcqModeGroup->isChecked())
268  {
269  QString fileName = QFileDialog::getSaveFileName(NULL,"Choose a file");
270  if(fileName.isEmpty())
271  {
272  yWarning() << "choose another file";
273  return;
274  }
275  fp = fopen(fileName.toLatin1().data(),"w");
276  if(nullptr == fp)
277  {
278  yWarning() << "cannot open file" << fileName.toLatin1().data() << "hence i will not perform acquisition";
279  ui->labelInstructions->setText("Cannot open file. Try once more.");
280  return;
281  }
282  }
283 
284  QFuture<bool> future = QtConcurrent::run(this,&StrainCalibGui::acquire_samples,ui->spinSamples->value());
285  watcher.setFuture(future);
286  ui->progress->setMaximum(100);
287 
288 }
289 
290 
291 void StrainCalibGui::onButtonClick(bool b)
292 {
293  QPushButton *btn = (QPushButton*)sender();
294  int btnIndex = btn->property("btnIndex").toInt();
295  currentBtnIndex = buttons.indexOf(btn);
296  trial = btnIndex;
297  //ui->btn3->setStyleSheet("background-color: rgb(0, 175, 0);");
298  // 4/9/2018 Tolto con Marco Accame perchè non ritenuto comprensibile
299  turnOffButtons();
300  showMenu();
301 
302 }
303 
304 #ifdef ATI_SENS
305 void acquire_1000_samples()
306 {
307  char temp[255];
308  float data[6]={0,0,0,0,0,0};
309  printf("\n press return to acquire ***ATI sensor*** data or 'r' to reset\n");
310  cin >> temp;
311 
312  if (strcmp(temp,"r")==0)
313  {
314  ATIsens->BiasForceTorqueSensor();
315  printf("\n bias done. press return to acquire ***ATI sensor*** data\n");
316  cin >> temp;
317  }
318 
319  int i=0;
320  int j=0;
321  float data_mean[6] = {0,0,0,0,0,0};
322  for (i=0; i<1000; i++)
323  {
324  Sleep(5);
325  ATIsens->ReadFTsensorData();
326  ATIsens->GetFTData(data);
327  fprintf(fp,"%3d %+f %+f %+f %+f %+f %+f\n",i,data[0],data[1],data[2],data[3],data[4],data[5]);
328  printf("samples:%3d [0]:%+6.2f [1]:%+6.2f [2]:%+6.2f [3]:%+6.2f [4]:%+6.2f [5]:%+6.2f\r\n",i,data[0],data[1],data[2],data[3],data[4],data[5]);
329  for (j=0; j<6; j++)
330  {
331  data_mean[j]=data_mean[j]+data[j];
332  }
333  }
334  for (j=0; j<6; j++)
335  {
336  data_mean[j]/=1000;
337  }
338  //fprintf(fp2,"%3d %+f %+f %+f %+f %+f %+f\n",i,data_mean[0],data_mean[1],data_mean[2],data_mean[3],data_mean[4],data_mean[5]);
339  printf("means:%3d [0]:%+6.2f [1]:%+6.2f [2]:%+6.2f [3]:%+6.2f [4]:%+6.2f [5]:%+6.2f\r\n",i,data_mean[0],data_mean[1],data_mean[2],data_mean[3],data_mean[4],data_mean[5]);
340 
341 
342  trial++;
343 }
344 
345 #else
346 
347 void StrainCalibGui::progressbar(void *owner, float percent)
348 {
349  if(nullptr == owner)
350  {
351  return;
352  }
353 
354  int value = static_cast<int>(percent * 100.0);
355 // value /= 10;
356 // value *= 10;
357 
358  StrainCalibGui *thegui = reinterpret_cast<StrainCalibGui*>(owner);
359 
360  if(value != thegui->progressvalue)
361  {
362  thegui->progressvalue = value;
363  }
364 }
365 
366 bool StrainCalibGui::get(const unsigned int number, vector<cDownloader::strain_value_t> &values, bool debugprint, bool updateprogress)
367 {
368 
369  double t0 = yarp::os::SystemClock::nowSystem();
370 
371  if(debugprint)
372  {
373  yDebug() << "strainInterface::get(): is acquiring" << number << "from the 6 channels. Please wait ...";
374  }
375 
376  void * param = nullptr;
377  void (*updateProgressBar)(void*, float) = nullptr;
378 
379  if(true == updateprogress)
380  {
381  updateProgressBar = progressbar;
382  param = reinterpret_cast<void*>(this);
383  }
384 
385  const bool calibmode = false;
386  // marco.accame: on windows, esdcan driver cannot manage streaming mode in reception....sic
388  core->getDownloader()->strain_acquire_start(config.get_canbus(), config.get_canaddress(), config.get_txrate(), calibmode, acqmode);
389  int ret = core->getDownloader()->strain_acquire_get(config.get_canbus(), config.get_canaddress(), values, number, updateProgressBar, param, acqmode);
390  //yarp::os::SystemClock::delaySystem(0.100); i used it to test the flush operation of strain_acquire_stop()....
391  core->getDownloader()->strain_acquire_stop(config.get_canbus(), config.get_canaddress(), acqmode);
392  core->getDownloader()->strain_acquire_stop(config.get_canbus(), config.get_canaddress(), acqmode);
393 
394  double t1 = yarp::os::SystemClock::nowSystem();
395 
396  if(debugprint)
397  {
398  if (0 == ret)
399  {
400  yDebug() << "strainInterface::get() has succesfully acquired" << values.size() << "values of the 6 channels in" << (t1 - t0) << "seconds";
401  yDebug() << "the values are:";
402  for (int i = 0; i < values.size(); i++)
403  {
404  yDebug() << "#" << i + 1 << "=" << values[i].channel[0] << values[i].channel[1] << values[i].channel[2] << values[i].channel[3] <<
405  values[i].channel[4] << values[i].channel[5] << values[i].valid;
406  }
407  }
408  else
409  {
410  yDebug() << "strainInterface::get() has failed acquisitions";
411  }
412  }
413 
414  return (0 == ret) ? true : false;
415 }
416 
417 bool StrainCalibGui::print(const vector<cDownloader::strain_value_t> &values, FILE *fp, QList<float> ft)
418 {
419 
420  if(nullptr == fp)
421  {
422  yError("the file pointer is invalid: I cannot print my %lu values", values.size());
423  return false;
424  }
425 
426  if(!ft.isEmpty() && (ft.count() == 6)){
427  fprintf(fp,"- %f %f %f %f %f %f\n\n",ft.at(0),ft.at(1),ft.at(2),ft.at(3),ft.at(4),ft.at(5));
428  fflush(fp);
429  }
430 
431  for(size_t n=0; n<values.size(); n++)
432  {
433  unsigned short unsigned_gaugeData[6] = {0};
434  signed short signed_gaugeData[6] = {0};
435 
436  for(size_t c=0; c<6; c++)
437  {
438  unsigned_gaugeData[c] = values[values.size()-1-n].channel[c];
439  signed_gaugeData[c] = unsigned_gaugeData[c]-0x7fff;
440  }
441 
442  if(NULL != fp)
443  {
444  fprintf(fp,"%d %d %d %d %d %d %d\n",static_cast<int>(n),signed_gaugeData[0],signed_gaugeData[1],signed_gaugeData[2],
445  signed_gaugeData[3],signed_gaugeData[4],signed_gaugeData[5]);
446  fflush(fp);
447  }
448 
449  if(enabledebugprints)
450  {
451  yDebug("samples:%3d [0]:%+6d [1]:%+6d [2]:%+6d [3]:%+6d [4]:%+6d [5]:%+6d\r\n",static_cast<int>(n),signed_gaugeData[0],signed_gaugeData[1],signed_gaugeData[2],signed_gaugeData[3],signed_gaugeData[4],signed_gaugeData[5]);
452  }
453  }
454 
455  return true;
456 }
457 
458 bool StrainCalibGui::acquire_samples(int samples)
459 {
460  // marco.accame: now we acquire with the strainInterface class in one shot
461 
462  const bool enabledebugprints = false;
463 
464  if(false == enableacquisitions)
465  {
466  return false;
467  }
468 
469 
470  lockdriver(true);
471  setAcquisitionOfDataActive(true);
472 
473  vector<cDownloader::strain_value_t> values;
474  bool ret = get(samples, values, enabledebugprints, true);
475 
476  if (ret)
477  {
478 
479  if (samples != values.size())
480  {
481  yError() << "cannot acquire enough strain samples. Only:" << values.size() << "instead of" << samples;
482  }
483  else
484  {
485  if (enabledebugprints)
486  {
487  yDebug("Acquired %lu strain samples. Ready! \n", values.size());
488  }
489  }
490 
491  if (ui->freeAcqModeGroup->isChecked()) {
492  QList <float> v;
493  v.append(ui->freeEdit1->text().toFloat());
494  v.append(ui->freeEdit2->text().toFloat());
495  v.append(ui->freeEdit3->text().toFloat());
496  v.append(ui->freeEdit4->text().toFloat());
497  v.append(ui->freeEdit5->text().toFloat());
498  v.append(ui->freeEdit6->text().toFloat());
499  print(values, fp, v);
500  }
501  else {
502  print(values, fp);
503  }
504 
505 
506  // now i need to retrieve the most recent value from values but in a particular format and fill variable last_value
507 
508  cDownloader::strain_value_t lastvalue = values.at(values.size() - 1);
509 
510  lastvalue.extract(last_value.dat);
511 
512  }
513  else
514  {
515  fprintf(fp, "acquisition error. please check log on terminal.\n");
516  fflush(fp);
517  }
518 
519  setAcquisitionOfDataActive(false);
520  lockdriver(false);
521 
522  return true;
523 }
524 
525 #endif
526 
527 void StrainCalibGui::showMenu()
528 {
529  switch (trial) {
530 
531  case 0:
532  fp = fopen("./data/output0.dat","w");
533  ui->groupBox->setTitle("Beginning calibration ...");
534  ui->labelInstructions->setText("Remove all loads from the sensor\n"
535  "Orient the sensor with the flat surface facing upwards\n"
536  "Turn the sensor on (CAN message 205 2 7 0)");
537  terminate_section();
538  break;
539  case 1001:
540  fp = fopen("./data/output1.dat","w");
541  ui->groupBox->setTitle("(2) z+ pointing DOWNwards 5kg torques - file: output1.dat");
542  ui->labelInstructions->setText("1. assemble the assembly with a M10 nut\n"
543  "2. orient the assembly with the z+ axis pointing downwards\n\n"
544  "3. remove loads");
545  break;
546  case 1002:
547  ui->labelInstructions->setText("4. apply 5.00 kg on the y- axis\n");
548  break;
549  case 1003:
550  ui->labelInstructions->setText("5. apply 5.00 kg on the x+ axis\n");
551  break;
552  case 1004:
553  ui->labelInstructions->setText("6. apply 5.00 kg on the y+ axis\n");
554  break;
555  case 1005:
556  ui->labelInstructions->setText("7. apply 5.00 kg on the x- axis\n");
557  break;
558  case 1006:
559  terminate_section();
560  break;
561  case 3001:
562  fp = fopen("./data/output3.dat","w");
563  ui->groupBox->setTitle("(10) x+ axis pointing UPwards 5kg laterals - file: output3.dat");
564  ui->labelInstructions->setText("1. assemble the assembly with a M10 knob\n"
565  "2. orient the structure with the x+ axis pointing upwards\n"
566  "3. remove loads\n");
567  break;
568  case 3002:
569  ui->labelInstructions->setText("4. apply 5.00 kg on the y- axis\n");
570  break;
571  case 3003:
572  ui->labelInstructions->setText("5. apply 5.00 kg on the z+ axis\n");
573  break;
574  case 3004:
575  ui->labelInstructions->setText("6. apply 5.00 kg on the y+ axis\n");
576  break;
577  case 3005:
578  terminate_section();
579  break;
580  case 4001:
581  fp = fopen("./data/output4.dat","w");
582  ui->groupBox->setTitle("(11) y+ axis pointing UPwards 5kg laterals - file: output4.dat");
583  ui->labelInstructions->setText("1. assemble the assembly with a M10 knob\n"
584  "2. orient the structure with the y+ axis pointing upwards\n"
585  "3. remove loads\n");
586  break;
587  case 4002:
588  ui->labelInstructions->setText("4. apply 5.00 kg on the x+ axis\n");
589  break;
590  case 4003:
591  ui->labelInstructions->setText("5. apply 5.00 kg on the z+ axis\n");
592  break;
593  case 4004:
594  ui->labelInstructions->setText("6. apply 5.00 kg on the x- axis\n");
595  break;
596  case 4005:
597  terminate_section();
598  break;
599 
600  case 5001:
601  fp = fopen("./data/output5.dat","w");
602  ui->groupBox->setTitle("(12) x- axis pointing UPwards 5kg laterals - file: output5.dat");
603  ui->labelInstructions->setText("1. assemble the assembly with a M10 knob\n"
604  "2. orient the structure with the x- axis pointing upwards\n"
605  "3. remove loads\n");
606  break;
607  case 5002:
608  ui->labelInstructions->setText("4. apply 5.00 kg on the y+ axis\n");
609  break;
610  case 5003:
611  ui->labelInstructions->setText("5. apply 5.00 kg on the z+ axis\n");
612  break;
613  case 5004:
614  ui->labelInstructions->setText("6. apply 5.00 kg on the y- axis\n");
615  break;
616  case 5005:
617  terminate_section();
618  break;
619 
620 
621  case 6001:
622  fp = fopen("./data/output6.dat","w");
623  ui->groupBox->setTitle("(13) y- axis pointing UPwards 5kg laterals - file: output6.dat");
624  ui->labelInstructions->setText("1. assemble the assembly with a M10 knob\n"
625  "2. orient the structure with the y- axis pointing upwards\n"
626  "3. remove loads\n");
627  break;
628  case 6002:
629  ui->labelInstructions->setText("4. apply 5.00 kg on the x- axis\n");
630  break;
631  case 6003:
632  ui->labelInstructions->setText("5. apply 5.00 kg on the z+ axis\n");
633  break;
634  case 6004:
635  ui->labelInstructions->setText("6. apply 5.00 kg on the x+ axis\n");
636  break;
637  case 6005:
638  terminate_section();
639  break;
640 
641 
642  case 8010:
643  fp = fopen("./data/output81.dat","w");
644  ui->groupBox->setTitle("(3) z+ pointing UPwards 25kg compression - file: output81.dat");
645  ui->labelInstructions->setText("1. assemble the assembly with a M10 nut\n"
646  "2. orient the assembly with the z+ axis pointing upwards\n\n"
647  "3. remove loads\n");
648  break;
649 
650  case 8011:
651  ui->labelInstructions->setText("4. apply 25 kg in the z- direction (compression)\n");
652  break;
653  case 8012:
654  terminate_section();
655  break;
656 
657 
658  case 8020:
659  fp = fopen("./data/output82.dat","w");
660  ui->groupBox->setTitle("(1) z+ pointing DOWNwards 25kg traction - file: output82.dat");
661  ui->labelInstructions->setText("1. assemble the assembly with a M10 nut\n"
662  "2. screw the M10 ring on the top of the assembly\n"
663  "3. orient the assembly with the z+ axis pointing downwards\n\n"
664  "4. remove loads\n");
665  break;
666  case 8021:
667  ui->labelInstructions->setText("5. apply a 25 kg load in the z+ direction (traction)\n");
668  break;
669  case 8022:
670  terminate_section();
671  break;
672 
673 
674  case 8030:
675  fp = fopen("./data/output83.dat","w");
676  ui->groupBox->setTitle("(4) x+ pointing UPwards 25kg - file: output83.dat");
677  ui->labelInstructions->setText("1. assemble the assembly with a M10 knob\n"
678  "2. orient the plate with the x+ axis pointing upwards\n"
679  "3. remove loads\n");
680  break;
681  case 8031:
682  ui->labelInstructions->setText("4. apply a 25 kg load in the x- direction\n");
683  break;
684  case 8032:
685  terminate_section();
686  break;
687 
688 
689  case 8040:
690  fp = fopen("./data/output84.dat","w");
691  ui->groupBox->setTitle("(5) x- pointing UPwards 25kg - file: output84.dat");
692  ui->labelInstructions->setText("1. assemble the assembly with a M10 knob\n"
693  "2. orient the plate with the x- axis pointing upwards\n\n"
694  "3. remove loads\n");
695  break;
696  case 8041:
697  ui->labelInstructions->setText("4. apply a 25 kg load in the x+ direction\n");
698  break;
699  case 8042:
700  terminate_section();
701  break;
702 
703  case 8050:
704  fp = fopen("./data/output85.dat","w");
705  ui->groupBox->setTitle("(6) label 1 pointing DOWNwards 25kg - file: output85.dat");
706  ui->labelInstructions->setText("1. assemble the assembly with label 1 pointing DOWN\n\n"
707  "3. remove loads\n");
708  break;
709  case 8051:
710  ui->labelInstructions->setText("4. apply 25kg\n");
711  break;
712  case 8052:
713  terminate_section();
714  break;
715 
716 
717  case 8060:
718  fp = fopen("./data/output86.dat","w");
719  ui->groupBox->setTitle("(7) label 2 pointing DOWNwards 25kg - file: output86.dat");
720  ui->labelInstructions->setText("1. assemble the assembly with a M10 knob\n"
721  "2. orient the plate with label 2 pointing DOWN\n\n"
722  "3. remove loads\n");
723  break;
724  case 8061:
725  ui->labelInstructions->setText("4. apply 25kg\n");
726  break;
727  case 8062:
728  terminate_section();
729  break;
730  case 8070:
731  fp = fopen("./data/output87.dat","w");
732  ui->groupBox->setTitle("(8) label 3 pointing DOWNwards 25kg - file: output87.dat");
733  ui->labelInstructions->setText("1. assemble the assembly with a M10 knob\n"
734  "2. orient the plate with label 3 pointing DOWN\n\n"
735  "3. remove loads\n");
736  break;
737  case 8071:
738  ui->labelInstructions->setText("4. apply 25kg\n");
739  break;
740  case 8072:
741  terminate_section();
742  break;
743 
744  case 8080:
745  fp = fopen("./data/output88.dat","w");
746  ui->groupBox->setTitle("(9) label 4 pointing DOWNwards 25kg - file: output88.dat");
747  ui->labelInstructions->setText("1. assemble the assembly with a M10 knob\n"
748  "2. orient the plate with label 4 pointing DOWN\n\n"
749  "3. remove loads\n");
750  break;
751  case 8081:
752  ui->labelInstructions->setText("4. apply 25kg\n");
753  break;
754  case 8082:
755  terminate_section();
756  break;
757  case 9999:
758  bias_sensor();
759  trial=-1;
760  break;
761  default:
762  trial++;
763  break;
764  }
765 }
766 
767 void StrainCalibGui::close_files()
768 {
769  if (fp!=0){
770  fclose(fp);
771  fp=NULL;
772  }
773 }
774 
775 void StrainCalibGui::terminate_section()
776 {
777  trial=-1;
778 
779  close_files();
780  turnOnButtons();
781  if(!ui->freeAcqModeGroup->isChecked()){
782  buttons.at(currentBtnIndex)->setStyleSheet("background-color: rgb(0, 175, 0);");
783  }
784 
785 }
786 
787 
788 void StrainCalibGui::turnOffButtons()
789 {
790  if(!ui->freeAcqModeGroup->isChecked()){
791  foreach (QPushButton *btn, buttons) {
792  btn->setEnabled(false);
793  }
794 
795  ui->btnAcquireData->setEnabled(true);
796  ui->btnAcquireData->setText("Start Acquisition");
797  ui->freeAcqModeGroup->setEnabled(false);
798  }
799  ui->spinSamples->setEnabled(true);
800 
801 }
802 
803 void StrainCalibGui::turnOnButtons()
804 {
805  ui->groupBox->setTitle("Instructions");
806 
807  if(!ui->freeAcqModeGroup->isChecked()){
808  foreach (QPushButton *btn, buttons) {
809  btn->setEnabled(true);
810  }
811  ui->btnAcquireData->setEnabled(false);
812  ui->freeAcqModeGroup->setEnabled(true);
813  if(false == checkDirectory())
814  {
815  yError() << "CANNOT find the data folder. Please create it!";
816  ui->labelInstructions->setText("You can select an acquisition from the menu ... but:"
817  "\nCANNOT find the data folder."
818  "\nIf you want your acquisition data saved, please create it!");
819  }
820  else
821  {
822  ui->labelInstructions->setText("Select an acquisition from the menu");
823  }
824  }else{
825  ui->labelInstructions->setText("Free Acquisiton Mode");
826  }
827 
828  ui->spinSamples->setEnabled(false);
829 
830 }
831 
832 void StrainCalibGui::bias_sensor()
833 {
834  #ifdef ATI_SENS
835  ATIsens->BiasForceTorqueSensor();
836  #endif
837 }
838 
839 
840 int StrainCalibGui::remap_trials()
841 {
842  //{8020,1001,8010,8030,8040,8050,8060,8070,8080,3001,4001,5001,6001};
843 
844  switch (trial)
845  {
846  case 1001: return -1;
847  case 1002: return 0;
848  case 1003: return 1;
849  case 1004: return 2;
850  case 1005: return 3;
851 
852  case 3001: return -1;
853  case 3002: return 4;
854  case 3003: return 5;
855  case 3004: return 6;
856 
857  case 4001: return -1;
858  case 4002: return 7;
859  case 4003: return 8;
860  case 4004: return 9;
861 
862  case 5001: return -1;
863  case 5002: return 10;
864  case 5003: return 11;
865  case 5004: return 12;
866 
867  case 6001: return -1;
868  case 6002: return 13;
869  case 6003: return 14;
870  case 6004: return 15;
871 
872  case 8010: return -1;
873  case 8011: return 16;
874 
875  case 8020: return -1;
876  case 8021: return 17;
877 
878  case 8030: return -1;
879  case 8031: return 18;
880 
881  case 8040: return -1;
882  case 8041: return 19;
883 
884  case 8050: return -1;
885  case 8051: return 20;
886 
887  case 8060: return -1;
888  case 8061: return 21;
889 
890  case 8070: return -1;
891  case 8071: return 22;
892 
893  case 8080: return -1;
894  case 8081: return 23;
895 
896  case -1:
897  //printf("DEBUG: section completed, back to the menu\n");
898  return -1;
899 
900  default:
901  qDebug("DEBUG: **** ERROR, invalid trial (%d) selected! ****\n", trial);
902  return -1;
903  }
904 }
905 
906 
907 void StrainCalibGui::onTimerTimeout()
908 {
909  int i=0;
910 
911  current_trial=remap_trials();
912  signed_elem_class exp_vals;
913  signed_elem_class err_vals;
914  signed_elem_class in_bound;
915 
916  if (current_trial==-1){
917  for (i=0; i<6;i++){
918 
919  readValues.at(i)->setText(QString("%1").arg(last_value.dat[i]));
920  expValues.at(i)->setText("---");
921  errValues.at(i)->setText("---");
922  expValues.at(i)->setStyleSheet("");
923  errValues.at(i)->setStyleSheet("");
924 
925  }
926 
927  if(true == isAcquisitionOfDataActive())
928  {
929  ui->btnAcquireData->setEnabled(false);
930  ui->btnAcquireData->setText("Acquiring Data Now");
931  ui->spinSamples->setEnabled(false);
932  ui->progress->setValue(progressvalue);
933  }
934  else
935  {
936  if(ui->freeAcqModeGroup->isChecked())
937  {
938  ui->btnAcquireData->setEnabled(true);
939  ui->btnAcquireData->setText("Start Acquiring Data");
940  ui->spinSamples->setEnabled(true);
941  }
942  else
943  {
944  if(!ui->btnAcquireData->isEnabled())
945  {
946  ui->btnAcquireData->setText("Choose an Acquisition Mode");
947  }
948  }
949 
950  ui->progress->setValue(0);
951  }
952 
953  } else {
954 
955  if(true == isAcquisitionOfDataActive())
956  {
957  ui->btnAcquireData->setEnabled(false);
958  ui->btnAcquireData->setText("Acquiring Data Now");
959  ui->progress->setValue(progressvalue);
960  }
961  else
962  {
963 
964  ui->progress->setValue(0);
965 
966  tick_acquisition(1);
967 
968  expected_values_handler.get_current_expected_values(exp_vals, current_trial);
969  for (i=0; i<6;i++){
970  expValues.at(i)->setText(QString("%1").arg(exp_vals.dat[i]));
971  }
972 
973  signed_elem_class last;
974  last = last_value;
975  last.remove_bias(trial_bias);
976  for (i=0; i<6;i++){
977  readValues.at(i)->setText(QString("%1").arg(last.dat[i]));
978  }
979  bool in_boundary = expected_values_handler.check_vals(last,current_trial,err_vals,in_bound);
980  for (i=0; i<6; i++){
981  errValues.at(i)->setText(QString("%1").arg(err_vals.dat[i]));
982 
983  if (in_bound.dat[i]==0){
984  errValues.at(i)->setStyleSheet("color: rgb(255, 0, 0);");
985  } else if (in_bound.dat[i]==1) {
986  errValues.at(i)->setStyleSheet("color: rgb(0, 255, 0);");
987  } else if (in_bound.dat[i]==2) {
988  errValues.at(i)->setStyleSheet("color: orange;");
989  }
990  }
991 
992 
993  ui->btnAcquireData->setEnabled(true);
994  ui->spinSamples->setEnabled(false);
995 
996  if (in_boundary==false){
997  ui->btnAcquireData->setText("Strange values detected.\n Acquire anyway? ");
998  } else {
999  ui->btnAcquireData->setText("Regular Values: Acquire Data");
1000  }
1001  }
1002  }
1003 }
1004 
1005 
1006 bool StrainCalibGui::tick_acquisition(int samples)
1007 {
1008  // marco.accame: now we acquire with the strainInterface class in one shot
1009 
1010  if(false == enableacquisitions)
1011  {
1012  return false;
1013  }
1014 
1015  lockdriver(true);
1016 
1017  vector<cDownloader::strain_value_t> values;
1018  get(samples, values, enabledebugprints, false);
1019  // yDebug() << "simple acquisition of " << values.size() << "strain samples";
1020 
1021  // now i need to retrieve the most recent value from values but in a particular format and fill variable last_value
1022 
1023  if(values.size() > 0)
1024  {
1025  cDownloader::strain_value_t lastvalue = values.at(values.size()-1);
1026  lastvalue.extract(last_value.dat);
1027  }
1028 
1029  lockdriver(false);
1030 
1031  return true;
1032 
1033 }
1034 
1035 void StrainCalibGui::setAcquisitionOfDataActive(const bool active)
1036 {
1037  mutexacquisitionofdata.lock();
1038  acquiringdata = active;
1039  mutexacquisitionofdata.unlock();
1040 }
1041 
1042 bool StrainCalibGui::isAcquisitionOfDataActive()
1043 {
1044  bool active = false;
1045  mutexacquisitionofdata.lock();
1046  active = acquiringdata;
1047  mutexacquisitionofdata.unlock();
1048  return active;
1049 }
1050 
1051 void StrainCalibGui::lockdriver(const bool on)
1052 {
1053  if(on)
1054  {
1055  mutexdriver.lock();
1056  }
1057  else
1058  {
1059  mutexdriver.unlock();
1060  }
1061 }
1062 
1063 bool StrainCalibGui::checkDirectory()
1064 {
1065  QFileInfo dir = QFileInfo("./data");
1066  return dir.isDir();
1067 }
1068 
@ data
StrainCalibGui(QString device, int bus, int pid, FirmwareUpdaterCore *core, QWidget *parent=0)
int strain_acquire_start(int bus, int target_id, uint8_t txratemilli=20, bool calibmode=true, strain_acquisition_mode_t acqmode=strain_acquisition_mode_streaming, string *errorstring=NULL)
int strain_acquire_stop(int bus, int target_id, strain_acquisition_mode_t acqmode=strain_acquisition_mode_streaming, string *errorstring=NULL)
int strain_acquire_get(int bus, int target_id, vector< strain_value_t > &values, const unsigned int howmany=10, void(*updateProgressBar)(void *, float)=NULL, void *arg=NULL, strain_acquisition_mode_t acqmode=strain_acquisition_mode_streaming, const unsigned int maxerrors=1, string *errorstring=NULL)
strain_acquisition_mode_t
Definition: downloader.h:257
@ strain_acquisition_mode_polling
Definition: downloader.h:259
@ strain_acquisition_mode_streaming
Definition: downloader.h:258
bool get_thresholds(int &low, int &high)
bool init(const char *filename)
void get_current_expected_values(signed_elem_class &values, int trial)
bool check_vals(signed_elem_class values, int trial, signed_elem_class &diff, signed_elem_class &in_bound)
int n
static struct bpf_program fp
fprintf(fid,'\n')
grid on
Definition: show_eyes_axes.m:5
fclose(fileID)
void extract(signed short *ss6) const
Definition: downloader.h:250
signed short dat[6]
void remove_bias(signed_elem_class bias)