iCub-main
SkinMeshThreadCan.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) 2009 RobotCub Consortium
5  * Author: Alessandro Scalzo alessandro.scalzo@iit.it
6  * CopyPolicy: Released under the terms of the GNU GPL v2.0.
7  *
8  */
9 
11 
12 #include <yarp/os/Time.h>
13 
14 const int CAN_DRIVER_BUFFER_SIZE=2047;
15 
17 {
18  printf("Image Thread initialising...\n");
19 
20  Property prop;
21  prop.put("device", deviceName);
22  prop.put("CanTxTimeout", 500);
23  prop.put("CanRxTimeout", 500);
24  prop.put("CanDeviceNum", netId);
25  prop.put("CanMyAddress", 0);
26  prop.put("CanTxQueueSize", CAN_DRIVER_BUFFER_SIZE);
27  prop.put("CanRxQueueSize", CAN_DRIVER_BUFFER_SIZE);
28 
29  pCanBus=NULL;
30  pCanBufferFactory=NULL;
31 
32  driver.open(prop);
33  if (!driver.isValid())
34  {
35  fprintf(stderr, "Error opening PolyDriver check parameters\n");
36  return false;
37  }
38 
39  driver.view(pCanBus);
40 
41  if (!pCanBus)
42  {
43  fprintf(stderr, "Error opening /ecan device not available\n");
44  return false;
45  }
46 
48  pCanBus->canSetBaudRate(0); //default 1MB/s
49 
50  for (int id=0; id<16; ++id)
51  {
52  if (sensor[id])
53  {
54  pCanBus->canIdAdd(cardId+id);
55  }
56  }
57 
58  canBuffer=pCanBufferFactory->createBuffer(4*sensorsNum);
59 
60  printf("... done!\n");
61 
62  return true;
63 }
64 
66 {
67  std::lock_guard<std::mutex> lck(mtx);
68 
69  unsigned int canMessages=0;
70 
71  bool res=pCanBus->canRead(canBuffer,4*sensorsNum,&canMessages,true);
72 
73  for (unsigned int i=0; i<canMessages; i++)
74  {
75  CanMessage &msg=canBuffer[i];
76 
77  if ((msg.getId() & 0xFFFFFFF0) == cardId)
78  {
79  int sensorId=msg.getId() & 0x0F;
80 
81  if (sensor[sensorId])
82  {
83  if (msg.getData()[0] & 0x80)
84  {
85  sensor[sensorId]->setActivationLast5(msg.getData()); // last 5 bytes
86  }
87  else
88  {
89  sensor[sensorId]->setActivationFirst7(msg.getData()); // first 7 bytes
90  }
91  }
92  }
93  }
94 }
95 
97 {
98  printf("Skin Mesh Thread releasing...\n");
99  if (pCanBufferFactory)
100  {
101  pCanBufferFactory->destroyBuffer(canBuffer);
102  }
103  driver.close();
104  printf("... done.\n");
105 }
const int CAN_DRIVER_BUFFER_SIZE
ICanBufferFactory * pCanBufferFactory
virtual void threadRelease()
virtual bool threadInit()
std::string deviceName
TouchSensor * sensor[16]
void setActivationFirst7(unsigned char *data)
Definition: TouchSensor.h:237
void setActivationLast5(unsigned char *data)
Definition: TouchSensor.h:244
fprintf(fid,'\n')