iCub-main
main.cpp
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
17 #include <stdio.h>
18 #include <yarp/dev/ControlBoardInterfaces.h>
19 #include <yarp/dev/PolyDriver.h>
20 #include <yarp/dev/CanBusInterface.h>
21 #include <yarp/os/Time.h>
22 #include <yarp/sig/Vector.h>
23 #include <yarp/os/PeriodicThread.h>
24 #include <iostream>
25 #include <iomanip>
26 #include <string>
27 #include <bitset>
28 
29 using namespace yarp::dev;
30 using namespace yarp::sig;
31 using namespace yarp::os;
32 using namespace yarp;
33 
34 using namespace std;
35 
36 const int SNIFFER_THREAD_RATE=50;
37 const int CAN_DRIVER_BUFFER_SIZE=2047;
38 const int localBufferSize=2048;
39 bool done=false;
40 
41 
42 bool log_start=true;
43 class SnifferThread: public PeriodicThread
44 {
45  PolyDriver driver;
46  ICanBus *iCanBus;
47  ICanBufferFactory *iBufferFactory;
48  CanBuffer messageBuffer;
49  unsigned long int cnt;
50  FILE *fp;
51 
52  /* dimension of local buffer, number of recieved messages */
53  unsigned int messages, readMessages;
54 
55  /* variables to be sniffed from the Can Bus */
56  signed int position[2];
57  signed short speed[2];
58  signed short pwm[2];
59  signed short pid[2];
60  signed short sin_frequency[2];
61  signed short sin_amplitude[2];
62  signed short dutyCycle[2];
63  signed short torque[2];
64  signed short commut[2];
65 
66  unsigned short unsigned_gaugeData[6];
67  signed short signed_gaugeData[6];
68  signed short dataBuffer[6];
69 
70 public:
71  SnifferThread(int r=SNIFFER_THREAD_RATE): PeriodicThread((double)r/1000.0)
72  {
73  messages = localBufferSize;
74 
75  for(int i=0; i<6; i++)
76  {
77  unsigned_gaugeData[i] = 0;
78  signed_gaugeData[i] = 0;
79  dataBuffer[i] = 0;
80  }
81 
82  for(int i=0; i<2; i++)
83  {
84  position[i] = 0;
85  speed[i] = 0;
86  pwm[i] = 0;
87  pid[i] = 0;
88  dutyCycle[i] = 0;
89  torque[i] = 0;
90  commut[i] = 0;
91  sin_frequency[i] = 0;
92  sin_amplitude[i] = 0;
93  }
94 
95  }
96 
97  bool threadInit()
98  {
99  // load configuration parameters into the options property collector
100  Property prop;
101 
102  // set driver properties
103  prop.put("device", "ecan");
104 
105  prop.put("CanTxTimeout", 500);
106  prop.put("CanRxTimeout", 500);
107  prop.put("CanDeviceNum", 0);
108 
109  prop.put("CanTxQueue", CAN_DRIVER_BUFFER_SIZE);
110  prop.put("CanRxQueue", CAN_DRIVER_BUFFER_SIZE);
111 
112  // open driver
113  driver.open(prop);
114 
115  if (!driver.isValid())
116  {
117  fprintf(stderr, "Error opening PolyDriver check parameters\n");
118  return false;
119  }
120 
121  driver.view(iCanBus);
122  driver.view(iBufferFactory);
123 
124  // set the baud rate (0 is defaul for 1Mb/s)
125  if (!iCanBus->canSetBaudRate(0))
126  fprintf(stderr, "Error setting baud rate\n");
127 
128  // add the id of can messages to be read
129  iCanBus->canIdAdd(0x12A);
130  iCanBus->canIdAdd(0x12B);
131  messageBuffer=iBufferFactory->createBuffer(localBufferSize);
132 
133  cnt = 0;
134  fp = fopen("output.dat","w");
135  }
136 
137  void run()
138  {
139  readMessages = 0;
140  // read from the Can Bus messages with the id that has been specified
141  bool res=iCanBus->canRead(messageBuffer, messages, &readMessages);
142 
143  // parse the messages
144  for(int i=0; i<readMessages; i++)
145  {
146  /*
147  if (messageBuffer[i].getId() == 0x35a)
148  {
149  unsigned_gaugeData[0] = (messageBuffer[i].getData()[1]<<8)|messageBuffer[i].getData()[0];
150  unsigned_gaugeData[1] = (messageBuffer[i].getData()[3]<<8)|messageBuffer[i].getData()[2];
151  unsigned_gaugeData[2] = (messageBuffer[i].getData()[5]<<8)|messageBuffer[i].getData()[4];
152  signed_gaugeData[0] = unsigned_gaugeData[0]-0x7fff;
153  signed_gaugeData[1] = unsigned_gaugeData[1]-0x7fff;
154  signed_gaugeData[2] = unsigned_gaugeData[2]-0x7fff;
155  }
156  if (messageBuffer[i].getId() == 0x35b)
157  {
158  unsigned_gaugeData[3] = (messageBuffer[i].getData()[1]<<8)|messageBuffer[i].getData()[0];
159  unsigned_gaugeData[4] = (messageBuffer[i].getData()[3]<<8)|messageBuffer[i].getData()[2];
160  unsigned_gaugeData[5] = (messageBuffer[i].getData()[5]<<8)|messageBuffer[i].getData()[4];
161  signed_gaugeData[3] = unsigned_gaugeData[3]-0x7fff;
162  signed_gaugeData[4] = unsigned_gaugeData[4]-0x7fff;
163  signed_gaugeData[5] = unsigned_gaugeData[5]-0x7fff;
164  }
165  */
166 
167  if (messageBuffer[i].getId() == 0x12B)
168  {
169  sin_frequency[0] = (messageBuffer[i].getData()[1]<<8) | messageBuffer[i].getData()[0];
170  sin_amplitude[0] = (messageBuffer[i].getData()[3]<<8) | messageBuffer[i].getData()[2];
171  dutyCycle[0] = (messageBuffer[i].getData()[5]<<8) | messageBuffer[i].getData()[4];
172  }
173 
174  if (messageBuffer[i].getId() == 0x12A)
175  {
176  position[0] = (messageBuffer[i].getData()[1]<<8) | messageBuffer[i].getData()[0];
177  speed[0] = (messageBuffer[i].getData()[3]<<8) | messageBuffer[i].getData()[2];
178  pid[0] = (messageBuffer[i].getData()[5]<<8) | messageBuffer[i].getData()[4];
179  torque[0] = (messageBuffer[i].getData()[7]<<8) | messageBuffer[i].getData()[6];
180 
181  /* if (sin_frequency[0]==1000)
182  {
183  this->stop();
184  fclose(fp);
185  exit(0);
186  };*/
187 
188  if(log_start) fprintf(fp,"%d %d %d %d %d %d %d %d\n",cnt,sin_frequency[0],sin_amplitude[0],position[0],speed[0],pid[0],torque[0],dutyCycle[0]);
189  cnt++;
190  if ((cnt % 500) == 0)
191  {
192  fprintf(stdout,"%d %d %d %d %d %d %d %d\n",cnt,sin_frequency[0],sin_amplitude[0],position[0],speed[0],pid[0],torque[0],dutyCycle[0]);
193  }
194  }
195  }
196 
197 /*
198  if (cnt==50000)
199  {
200  this->stop();
201  done =true;
202  }
203 */
204 
205  /*
206  cout<<setiosflags(ios::fixed)
207  <<setw(10)<<"commut:"
208  <<setw(8)<<setprecision(3)<<commut[0]
209  <<" duty cycle:"
210  <<setw(8)<<setprecision(3)<<dutyCycle[0]
211  <<" gauge 5:"
212  <<setw(8)<<setprecision(3)<<signed_gaugeData[5]
213  <<" dsp torque:"
214  <<setw(8)<<setprecision(3)<<torque[0]
215  <<" pid:"
216  <<setw(8)<<setprecision(3)<<pid[0]
217  <<" kp:"
218  <<setw(8)<<setprecision(3)<<kp[0]
219  <<"\r";
220  */
221  /*
222  cout<<setiosflags(ios::fixed)
223  <<" "
224  <<setw(8)<<setprecision(3)<<cnt
225  <<" "
226  <<setw(8)<<setprecision(3)<<gaugeData[0]
227  <<" "
228  <<setw(8)<<setprecision(3)<<gaugeData[1]
229  <<" "
230  <<setw(8)<<setprecision(3)<<gaugeData[2]
231  <<" "
232  <<setw(8)<<setprecision(3)<<gaugeData[3]
233  <<" "
234  <<setw(8)<<setprecision(3)<<gaugeData[4]
235  <<" "
236  <<setw(8)<<setprecision(3)<<gaugeData[5]<<"\r";
237  */
238  }
239 
241  {
242  iBufferFactory->destroyBuffer(messageBuffer);
243  driver.close();
244  fclose(fp);
245  }
246 };
247 
248 int main(int argc, char *argv[])
249 {
250  SnifferThread thread;
251  thread.start();
252 
253  std::string input;
254  while(!done)
255  {
256 /* std::cin>>input;
257  if (input=="quit")
258  done=true;*/
259  }
260 
261  thread.stop();
262 }
void threadRelease()
Definition: main.cpp:240
void run()
Definition: main.cpp:137
bool threadInit()
Definition: main.cpp:97
SnifferThread(int r=SNIFFER_THREAD_RATE)
Definition: main.cpp:71
int main(int argc, char *argv[])
Definition: main.cpp:31
bool done
Definition: main.cpp:42
const int SNIFFER_THREAD_RATE
Definition: main.cpp:39
const int localBufferSize
Definition: main.cpp:41
bool log_start
Definition: main.cpp:120
const int CAN_DRIVER_BUFFER_SIZE
Definition: main.cpp:40
Copyright (C) 2008 RobotCub Consortium.
static struct bpf_program fp
fprintf(fid,'\n')
fclose(fileID)