iCub-main
dumperThread.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) 2010 RobotCub Consortium, European Commission FP6 Project IST-004370
5  * Author: Francesco Nori
6  * email: francesco.nori@iit.it
7  * website: www.robotcub.org
8  * Permission is granted to copy, distribute, and/or modify this program
9  * under the terms of the GNU General Public License, version 2 or any
10  * later version published by the Free Software Foundation.
11  *
12  * A copy of the license can be found at
13  * http://www.robotcub.org/icub/license/gpl.txt
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
18  * Public License for more details
19 */
20 #include "dumperThread.h"
21 #include <cstring>
22 #include <string>
23 
24 /*
25  * Copyright (C) 2006 Francesco Nori
26  * CopyPolicy: Released under the terms of the GNU GPL v2.0.
27  *
28  */
29 
30 
31 void boardDumperThread::setDevice(PolyDriver *board_d, PolyDriver *debug_d, int rate, std::string portPrefix, std::string dataToDump, bool logOnDisk)
32 {
33  // open ports
34  board_dd=board_d;
35  debug_dd=debug_d;
36 
37  //getter = NULL;
38 
39  bool ok=true;
40  ok &= board_d->view(pos);
41  ok &= board_d->view(vel);
42  ok &= board_d->view(enc);
43  ok &= board_d->view(pid);
44  ok &= board_d->view(amp);
45  ok &= board_d->view(lim);
46  ok &= board_d->view(trq);
47  ok &= board_d->view(cmod);
48  ok &= board_d->view(imod);
49  ok &= board_d->view(imot);
50  ok &= board_d->view(imotenc);
51 
52  if (!ok)
53  printf("Problems acquiring interfaces\n");
54  else
55  printf("Control board was accessed succesfully!\n");
56 
57  pos->getAxes(&numberOfJoints);
58  fprintf(stderr, "Number of axes is: %d \n", numberOfJoints);
59 
60  //initialize used variables
61  data = new double [numberOfJoints];
62 
63  port = new Port;
64 
65  portName = portPrefix + dataToDump;
66  port->open(portName);
67 
68  logToFile = logOnDisk;
69  this->setPeriod((double)rate/1000.0);
70 }
71 
73 {
74 }
75 
77 {
78  fprintf(stderr, "Setting the map dimension %d \n", n);
79  numberOfJointsRead = n;
80  dataRead = new double [numberOfJointsRead];
81  dataMap = new int [numberOfJointsRead];
82  for (int i = 0; i < numberOfJointsRead; i++)
83  {
84  //fprintf(stderr, "map is %d \n", map[i]);
85  dataMap[i] = map[i];
86  dataRead[i] = 1.0;
87  }
88 }
89 
91 {
92  getter = g;
93 }
94 
96 {
97  char buff [255];
98  strcpy(buff, this->portName.c_str());
99  for (size_t i=0; i<strlen(buff); i++)
100  if (buff[i]=='/') buff[i]='_';
101  strcat(buff,".log");
102 
103  if (logToFile)
104  {
105  logFile = fopen(buff,"w");
106  if (logFile == 0)
107  {
108  printf ("error opening logfile: %s\n",this->portName.c_str());
109  }
110  else
111  {
112  printf ("logfile opened: %s\n",this->portName.c_str());
113  }
114  }
115  return 1;
116 }
117 
119 {
120  getter = 0;
121  board_dd = 0;
122  debug_dd = 0;
123  port = 0;
124  pos = 0;
125  vel = 0;
126  enc = 0;
127  imotenc = 0;
128  pid = 0;
129  amp = 0;
130  lim = 0;
131  trq = 0;
132  cmod = 0;
133  imod = 0;
134  logFile = 0;
135  imot = 0;
136  logToFile = false;
137 }
138 
140 {
141  fprintf(stderr, "Closing thread \n");
142  //Arm_dd->close();
143  Time::delay(.1);
144  fprintf(stderr, "Closing ports \n");
145  if(port)
146  port->close();
147 
148  if (logFile)
149  {
150  fprintf(stderr, "Closing logFile \n");
151  fclose (logFile);
152  }
153 }
154 
156 {
157  //printf("Entering the main thread\n");
158  //enc->getEncoders(data);
159  //Bottle bData;
160  //for (int i = 0; i < numberOfJointsRead; i++)
161  // {
162  // dataRead[i] = data[dataMap[i]];
163  // bData.addFloat64(dataRead[i]);
164  // }
165  //port->write(bData);
166 
167  if (getter)
168  {
169  //printf("Getter is getting something\n");
170  getter -> getData(data);
171 
172  //fprintf(stderr, "Time is %lf \n", stmp.getTime());
173 
174  Bottle bData;
175  for (int i = 0; i < numberOfJointsRead; i++)
176  {
177  //printf("%.2f \n", data[dataMap[i]]);
178  dataRead[i] = data[dataMap[i]];
179  bData.addFloat64(dataRead[i]);
180  }
181 
182  if (getter->getStamp(stmp))
183  {
184  if (stmp.isValid())
185  {
186  port->setEnvelope(stmp);
187  }
188  else
189  {
190  //stmp.update();
191  stmp=Stamp(-1,0.0);
192  port->setEnvelope(stmp);
193  }
194  }
195  else
196  {
197  fprintf(stderr, "boardDumperThread::warning. Trying to get a stamp without a proper IPreciselyTimed defined. \n");
198  }
199 
200  if (logFile)
201  {
202  char buff [20];
203  sprintf(buff,"%d ",stmp.getCount());
204  fputs (buff,logFile);
205  sprintf(buff,"%f ",stmp.getTime());
206  fputs (buff,logFile);
207  fputs (bData.toString().c_str(),logFile);
208  fputs ("\n",logFile);
209  }
210 
211  port->write(bData);
212  }
213 }
bool getStamp(Stamp &)
void setThetaMap(int *, int)
void setGetter(GetData *)
void setDevice(PolyDriver *board_d, PolyDriver *debug_d, int rate, std::string portPrefix, std::string dataToDump, bool logOnDisk)
int n
yarp::sig::Vector & map(yarp::sig::Vector &v, double(op)(double))
Performs a unary operator inplace on each element of a vector.
Definition: Math.cpp:305
fprintf(fid,'\n')
fclose(fileID)