iCub-main
Loading...
Searching...
No Matches
SkinMeshThreadCan.h
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
10#ifndef __SKIN_MESH_THREAD_CAN_H__
11#define __SKIN_MESH_THREAD_CAN_H__
12
13#include <mutex>
14#include <string>
15
16#include <yarp/os/PeriodicThread.h>
17#include <yarp/dev/ControlBoardInterfaces.h>
18#include <yarp/dev/PolyDriver.h>
19#include <yarp/dev/CanBusInterface.h>
20
21#include "include/Quad16.h"
22#include "include/PalmLeft.h"
23#include "include/PalmRight.h"
24#include "include/fakePalm.h"
25#include "include/Triangle.h"
27#include "include/Fingertip.h"
35#include "include/CER_SH_PDL.h"
36
37using namespace yarp::os;
38using namespace yarp::dev;
39
40class SkinMeshThreadCan : public PeriodicThread
41{
42protected:
43 PolyDriver driver;
44 ICanBus *pCanBus;
45 ICanBufferFactory *pCanBufferFactory;
46 CanBuffer canBuffer;
47 std::string deviceName;
48 int netId;
49
51
52 std::mutex mtx;
53
54 int cardId;
57
58public:
59 SkinMeshThreadCan(Searchable& config,int period) : PeriodicThread((double)period/1000.0)
60 {
61 mbSimpleDraw=config.check("light");
62
63 sensorsNum=0;
64
65 for (int t=0; t<16; ++t)
66 {
67 sensor[t]=NULL;
68 }
69 netId=7;
70 deviceName="cfw2can";
71 cardId=0x300 | (config.find("cardid").asInt32() << 4);
72 netId=config.find("CanDeviceNum").asInt32();
73 deviceName=config.find("CanDeviceName").asString();
74 std::string part="/skinGui/";
75 part.append(config.find("robotPart").asString());
76 part.append(":i");
77 int width =config.find("width" ).asInt32();
78 int height=config.find("height").asInt32();
79 bool useCalibration = config.check("useCalibration");
80 if (useCalibration==true)
81 {
82 printf("Reading datadirectly from CAN! Calibration unsupported\n");
83 useCalibration = false;
84 }
85 else
86 {
87 printf("Using raw skin values (255-0)\n");
88 }
89
90 yarp::os::Bottle sensorSetConfig=config.findGroup("SENSORS").tail();
91
92 for (int t=0; t<sensorSetConfig.size(); ++t)
93 {
94 yarp::os::Bottle sensorConfig(sensorSetConfig.get(t).toString());
95
96 std::string type(sensorConfig.get(0).asString());
97 if (type == "triangle" ||
98 type == "fingertip" ||
99 type == "fingertip2L" ||
100 type == "cer_sh_pdl" ||
101 type == "fingertip2R" ||
102 type == "triangle_10pad" ||
103 type == "quad16" ||
104 type == "palmL" ||
105 type == "palmR" ||
106 type == "fingertip3R" ||
107 type == "fingertip3L" ||
108 type == "fingertip4R" ||
109 type == "fingertip4L" ||
110 type == "fingertipMID" ||
111 type == "fakePalm")
112 {
113 int id=sensorConfig.get(1).asInt32();
114 double xc=sensorConfig.get(2).asFloat64();
115 double yc=sensorConfig.get(3).asFloat64();
116 double th=sensorConfig.get(4).asFloat64();
117 double gain=sensorConfig.get(5).asFloat64();
118 int lrMirror=sensorConfig.get(6).asInt32();
119 int layoutNum=sensorConfig.get(7).asInt32();
120
121 printf("%d %f\n",id,gain);
122
123 if (id>=0 && id<16)
124 {
125 if (sensor[id])
126 {
127 printf("WARNING: triangle %d already exists.\n",id);
128 }
129 else
130 {
131 if (type=="triangle")
132 {
133 sensor[id]=new Triangle(xc,yc,th,gain,layoutNum,lrMirror);
134 }
135 else if (type=="triangle_10pad")
136 {
137 sensor[id]=new Triangle_10pad(xc,yc,th,gain,layoutNum,lrMirror);
138 }
139 else if (type=="fingertip")
140 {
141 sensor[id]=new Fingertip(xc,yc,th,gain,layoutNum,lrMirror);
142 }
143 else if (type=="fingertip2L")
144 {
145 sensor[id]=new Fingertip2L(xc,yc,th,gain,layoutNum,lrMirror);
146 }
147 else if (type=="fingertip3L")
148 {
149 sensor[id]=new Fingertip3L(xc,yc,th,gain,layoutNum,lrMirror);
150 }
151 else if(type == "fingertip4L")
152 {
153 sensor[id]=new Fingertip4L(xc,yc,th,gain,layoutNum,lrMirror);
154 }
155 else if(type == "cer_sh_pdl")
156 {
157 sensor[id] = new CER_SH_PDL(xc, yc, th, gain, layoutNum, lrMirror);
158 }
159 else if(type == "fingertip2R")
160 {
161 sensor[id]=new Fingertip2R(xc,yc,th,gain,layoutNum,lrMirror);
162 }
163 else if(type == "fingertip3R")
164 {
165 sensor[id]=new Fingertip3R(xc,yc,th,gain,layoutNum,lrMirror);
166 }
167 else if (type == "fingertip4R")
168 {
169 sensor[id]=new Fingertip4R(xc,yc,th,gain,layoutNum,lrMirror);
170 }
171 else if (type == "fingertipMID")
172 {
173 sensor[id]=new FingertipMID(xc,yc,th,gain,layoutNum,lrMirror);
174 }
175 else if (type == "quad16")
176 {
177 sensor[id]=new Quad16(xc,yc,th,gain,layoutNum,lrMirror);
178 }
179 else if(type == "palmR")
180 {
181 sensor[id]=new PalmR(xc,yc,th,gain,layoutNum,lrMirror);
182 }
183 else if(type == "palmL")
184 {
185 sensor[id]=new PalmL(xc,yc,th,gain,layoutNum,lrMirror);
186 }
187 ++sensorsNum;
188 }
189 }
190 else
191 {
192 printf("WARNING: %d is invalid triangle Id [0:15].\n",id);
193 }
194 }
195 else
196 {
197 printf("WARNING: sensor of type %s unknown, discarded.\n",type.c_str());
198 }
199 }
200
201 resize(width,height);
202 }
203
205 {
206 for (int t=0; t<16; ++t)
207 {
208 if (sensor[t]) delete sensor[t];
209 }
210 }
211
212 virtual bool threadInit();
213 virtual void threadRelease();
214 virtual void run();
215
216 void resize(int width,int height)
217 {
218 std::lock_guard<std::mutex> lck(mtx);
219 for (int t=0; t<16; ++t)
220 {
221 if (sensor[t]) sensor[t]->resize(width,height,40);
222 }
223 }
224
225 void eval(unsigned char *image)
226 {
227 std::lock_guard<std::mutex> lck(mtx);
228 for (int t=0; t<16; ++t)
229 {
230 if (sensor[t])
231 {
232 if (mbSimpleDraw)
233 {
234 sensor[t]->eval_light(image);
235 }
236 else
237 {
238 sensor[t]->eval(image);
239 }
240 }
241 }
242 }
243
244 void draw(unsigned char *image)
245 {
246 for (int t=0; t<16; ++t)
247 {
248 if (sensor[t]) sensor[t]->draw(image);
249 }
250 }
251};
252
253#endif
void draw(unsigned char *image)
void eval(unsigned char *image)
ICanBufferFactory * pCanBufferFactory
virtual void threadRelease()
SkinMeshThreadCan(Searchable &config, int period)
virtual bool threadInit()
TouchSensor * sensor[16]
void resize(int width, int height)
void eval(unsigned char *image)
void resize(int width, int height, int margin)
Definition TouchSensor.h:48
virtual void draw(unsigned char *image)
void eval_light(unsigned char *image)