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 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <yarp/os/Network.h>
36 #include <yarp/dev/ControlBoardInterfaces.h>
37 #include <yarp/dev/PolyDriver.h>
38 #include <yarp/dev/CanBusInterface.h>
39 #include <yarp/os/Time.h>
40 #include <yarp/sig/Vector.h>
41 
42 #include <yarp/os/PeriodicThread.h>
43 
44 #include <iostream>
45 #include <string>
46 
47 using namespace yarp::dev;
48 using namespace yarp::sig;
49 using namespace yarp::os;
50 using namespace yarp;
51 
52 const int SNIFFER_THREAD_RATE=50;
53 const int CAN_DRIVER_BUFFER_SIZE=2047;
54 const int localBufferSize=512;
55 
56 class SnifferThread: public PeriodicThread
57 {
58  PolyDriver driver;
59  ICanBus *iCanBus;
60  ICanBufferFactory *iBufferFactory;
61  CanBuffer readBuffer;
62  std::string devname;
63  int port;
64 public:
65  SnifferThread(std::string dname, int p, int r=SNIFFER_THREAD_RATE): PeriodicThread((double)r/1000.0)
66  {
67  port=p;
68  devname=dname;
69  }
70 
71  bool threadInit()
72  {
73  Property prop;
74  prop.put("device", devname.c_str());
75 
76  prop.put("CanTxTimeout", 500);
77  prop.put("CanRxTimeout", 500);
78  prop.put("CanDeviceNum", port);
79  prop.put("CanMyAddress", 0);
80 
81  prop.put("CanTxQueueSize", CAN_DRIVER_BUFFER_SIZE);
82  prop.put("CanRxQueueSize", CAN_DRIVER_BUFFER_SIZE);
83 
84  driver.open(prop);
85 
86  if (!driver.isValid())
87  {
88  fprintf(stderr, "Error opening PolyDriver check parameters\n");
89  return false;
90  }
91 
92  driver.view(iCanBus);
93  driver.view(iBufferFactory);
94 
95  iCanBus->canSetBaudRate(0); //default 1MB/s
96 
97  readBuffer=iBufferFactory->createBuffer(localBufferSize);
98  return true;
99  }
100 
101  void run()
102  {
103  unsigned int messages=localBufferSize;
104  unsigned int readMessages=0;
105  bool res=iCanBus->canRead(readBuffer, messages, &readMessages);
106  if (res)
107  fprintf(stderr, "Read %u messages\n", readMessages);
108  else
109  fprintf(stderr, "Failed (read %u messages)\n", readMessages);
110  }
111 
113  {
114  iBufferFactory->destroyBuffer(readBuffer);
115  driver.close();
116  }
117 };
118 
119 #ifdef USE_ICUB_MOD
120 #include "drivers.h"
121 #endif
122 
123 
124 int main(int argc, char *argv[])
125 {
126 
127 #ifdef USE_ICUB_MOD
128  yarp::dev::DriverCollection dev;
129 #endif
130 
131  if (argc!=3 && argc!=5)
132  {
133  std::cout<<"Usage: --device device_name {ecan|pcan|...}\n";
134  std::cout<<"Optional: --port {int} (default 0)\n";
135  return -1;
136  }
137 
138  std::string p1=std::string(argv[1]);
139  std::string p2=std::string(argv[2]);
140 
141  int port=0;
142  if (argc==5)
143  {
144  std::string tmp=std::string(argv[3]);
145  if (tmp=="--port")
146  {
147  port=atoi(argv[4]);
148  }
149  }
150 
151  if (p1!="--device")
152  {
153  std::cout<<"Usage: --device device_name {ecan|pcan}\n";
154  return -1;
155  }
156 
157  SnifferThread thread(p2, port);
158 
159  if (!thread.start())
160  {
161  std::cerr<<"Error thread did not start, quitting\n";
162  return -1;
163  }
164 
165  std::string input;
166  bool done=false;
167  while(!done)
168  {
169  std::cin>>input;
170  if (input=="quit")
171  done=true;
172  }
173 
174  thread.stop();
175  return 0;
176 }
void threadRelease()
Definition: main.cpp:112
void run()
Definition: main.cpp:101
SnifferThread(std::string dname, int p, int r=SNIFFER_THREAD_RATE)
Definition: main.cpp:65
bool threadInit()
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
const int CAN_DRIVER_BUFFER_SIZE
Definition: main.cpp:40
Copyright (C) 2008 RobotCub Consortium.
fprintf(fid,'\n')