iCub-main
Loading...
Searching...
No Matches
TouchSensor.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#include <gsl/gsl_math.h>
11#include <memory.h>
12
13#include <stdio.h>
14
15#ifndef __ALE_TOUCHSENSOR_H__
16#define __ALE_TOUCHSENSOR_H__
17
18#define MAX_TAXELS 192
19
21{
22protected:
23
25
27 {
28 for (int n = 0; n < MAX_TAXELS; ++n)
29 {
30 connected[n] = true;
31 activation[n] = 0.0;
33 }
34 }
35
36public:
37
38 void setColor(unsigned char r, unsigned char g, unsigned char b)
39 {
40 R_MAX = r;
41 G_MAX = g;
42 B_MAX = b;
43 }
44
45 void setCalibrationFlag (bool use_calibrated_skin)
46 {
47 calibrated_skin=use_calibrated_skin;
48 }
49
51 {
52 for (int n = 0; n < MAX_TAXELS; ++n)
53 {
54 activation[n] = 0.0;
56 }
57 }
58
59 void resize(int width,int height,int margin)
60 {
61 if (3*margin>=width || 3*margin>=height) margin=0;
62
63 double scaleX=double(width -2*margin)/(dXmax-dXmin);
64 double scaleY=double(height-2*margin)/(dYmax-dYmin);
65 double scale=scaleX<scaleY?scaleX:scaleY;
66
68
70
71 double dXmid=0.5*(dXmin+dXmax);
72 double dYmid=0.5*(dYmin+dYmax);
73
74 int w2=width /2;
75 int h2=height/2;
76
77 for (int i=0; i<nTaxels; ++i)
78 {
79 x[i]=w2+int(scale*(dX[i]-dXmid));
80 y[i]=h2+int(scale*(dY[i]-dYmid));
81 }
82
83 for (int i=0; i<nVerts; ++i)
84 {
85 xv[i]=w2+int(scale*(dXv[i]-dXmid));
86 yv[i]=h2+int(scale*(dYv[i]-dYmid));
87 }
88
89 double sigma=0.5*5.55*scale;
90 int maxRange=int(2.5*sigma);
91
92 if (maxRange!=m_maxRange)
93 {
94 m_maxRange=maxRange;
95
96 delete [] Exponential;
97 Exponential=new double[maxRange];
98
99 double k=-0.5/(sigma*sigma);
100 for (int x=0; x<maxRange; ++x)
101 {
102 Exponential[x]=exp(k*double(x*x));
103 }
104 }
105
106 xMin=w2+int(scale*(dXc-dXmid-15.0))-maxRange;
107 xMax=w2+int(scale*(dXc-dXmid+15.0))+maxRange;
108 yMin=h2+int(scale*(dYc-dYmid-15.0))-maxRange;
109 yMax=h2+int(scale*(dYc-dYmid+15.0))+maxRange;
110
111 if (xMin<0) xMin=0;
112 if (xMax>width) xMax=width;
113 if (yMin<0) yMin=0;
114 if (yMax>height) yMax=height;
115
116 m_Width=width;
117 m_Height=height;
118 }
119
120 virtual ~TouchSensor()
121 {
122 if (Exponential)
123 {
124 delete [] Exponential;
125 Exponential=0;
126 }
127 }
128
129 int Abs(int x)
130 {
131 return x>=0?x:-x;
132 }
133
135 {
136 return nTaxels;
137 }
138
139 void eval_light(unsigned char *image)
140 {
141 int act;
142 int dx,dy;
143 int Y0,Y1;
144 int dya,dyb,dxa,dxb;
145
146 switch (ilayoutNum)
147 {
148 case 0:
149 for (int i=0; i<nTaxels; ++i) remapped_activation[i]=activation[i];
150 break;
151 case 1:
152 for (int i=0; i<nTaxels; ++i) remapped_activation[nTaxels-1-i]=activation[i];
153 break;
154 default:
155 for (int i=0; i<nTaxels; ++i) remapped_activation[i]=activation[i];
156 printf("WARN: unkwnown layout number.\n");
157 break;
158 }
159
160 int maxRange2=m_maxRangeLight*m_maxRangeLight;
161
162 for (int i=0; i<nTaxels; ++i) if (connected[i] && remapped_activation[i]>0.0)
163 {
164 act=int(dGain*remapped_activation[i]);
165 Y0=(m_Height-y[i]-1)*m_Width+x[i];
166
167 dya=(y[i]>=m_maxRangeLight)?-m_maxRangeLight:-y[i];
169
170 dxa=(x[i]>=m_maxRangeLight)?-m_maxRangeLight:-x[i];
172
173 for (dy=dya; dy<=dyb; ++dy)
174 {
175 Y1=Y0-dy*m_Width;
176
177 for (dx=dxa; dx<=dxb; ++dx)
178 {
179 if (dx*dx+dy*dy<=maxRange2)
180 {
181 image[(dx+Y1)*3]=act<255?act:255;
182 }
183 }
184 }
185 }
186 }
187
188 void eval(unsigned char *image)
189 {
190 int act;
191 int dx,dy;
192 int Y0,Y1;
193 int index;
194 double k0,k1;
195 int dya,dyb,dxa,dxb;
196
197 switch (ilayoutNum)
198 {
199 case 0:
200 for (int i=0; i<nTaxels; ++i) remapped_activation[i]=activation[i];
201 break;
202 case 1:
203 for (int i=0; i<nTaxels; ++i) remapped_activation[nTaxels-1-i]=activation[i];
204 break;
205 default:
206 for (int i=0; i<nTaxels; ++i) remapped_activation[i]=activation[i];
207 printf("WARN: unkwnown layout number.\n");
208 break;
209 }
210
211 for (int i=0; i<nTaxels; ++i) if (connected[i] && remapped_activation[i]>0.0)
212 {
214 Y0=(m_Height-y[i]-1)*m_Width+x[i];
215
216 dya=(y[i]>=m_maxRange)?-m_maxRange:-y[i];
218
219 dxa=(x[i]>=m_maxRange)?-m_maxRange:-x[i];
220 dxb=(x[i]+m_maxRange<m_Width)?m_maxRange:m_Width-x[i]-1;
221
222 for (dy=dya; dy<=dyb; ++dy)
223 {
224 k1=k0*Exponential[Abs(dy)];
225 Y1=Y0-dy*m_Width;
226
227 for (dx=dxa; dx<=dxb; ++dx)
228 {
229 index=(dx+Y1)*3;
230
231 if (image[index]<R_MAX || image[index+1]<G_MAX || image[index+2]<B_MAX)
232 {
233 act=int(k1*Exponential[Abs(dx)]);
234
235 int actR=image[index ]+(act*R_MAX)/255;
236 int actG=image[index+1]+(act*G_MAX)/255;
237 int actB=image[index+2]+(act*B_MAX)/255;
238
239 image[index ]=actR<R_MAX?actR:R_MAX;
240 image[index+1]=actG<G_MAX?actG:G_MAX;
241 image[index+2]=actB<B_MAX?actB:B_MAX;
242 }
243 }
244 }
245 }
246 }
247
248 void setActivationFirst7(unsigned char* data)
249 {
250 for (int i=0; i<7; ++i)
251 {
252 activation[i]=data[i+1]<=244?double(244-data[i+1]):0.0;
253 }
254 }
255 void setActivationLast5(unsigned char* data)
256 {
257 for (int i=1; i<=5; ++i)
258 {
259 activation[i+6]=data[i]<=244?double(244-data[i]):0.0;
260 }
261 }
262
263 void setActivationFromPortData(double val, int id)
264 {
265 if (calibrated_skin)
266 {
267 if (val>244.0)
268 {
269 activation[id]=244.0;
270 }
271 else if (val<0.0)
272 {
273 activation[id]=0.0;
274 }
275 else
276 {
277 activation[id]=val;
278 }
279 }
280 else
281 {
282 activation[id]=val<=244?double(244-val):0.0;
283 }
284 }
285
286 virtual void draw(unsigned char *image)
287 {
288 for (int i=0; i<nVerts; ++i)
289 {
290 drawLine(image,xv[i],yv[i],xv[(i+1)%nVerts],yv[(i+1)%nVerts]);
291 }
292
293 for (int i=0; i<nTaxels; ++i)
294 {
295 drawCircle(image,x[i],y[i],m_Radius);
296 }
297 }
298
299protected:
300 void dither(int x,int y,unsigned char *image)
301 {
302 static const unsigned char R1=0x80,G1=0x50,B1=0x00;
303 static const unsigned char R2=3*R1/4,G2=3*G1/4,B2=3*B1/4;
304 static const unsigned char R4=3*R2/4,G4=3*G2/4,B4=3*B2/4;
305
306 //y=m_Width-y-1;
307 //int bytePos=(x+(y-1)*m_Width)*3;
308 int bytePos=(x+(m_Height-y-2)*m_Width)*3;
309
310 if (image[bytePos-3]<R4) image[bytePos-3]=R4;
311 if (image[bytePos-2]<G4) image[bytePos-2]=G4;
312 //if (image[bytePos-1]<B4) image[bytePos-1]=B4;
313
314 if (image[bytePos ]<R2) image[bytePos ]=R2;
315 if (image[bytePos+1]<G2) image[bytePos+1]=G2;
316 //if (image[bytePos+2]<B2) image[bytePos+2]=B2;
317
318 if (image[bytePos+3]<R4) image[bytePos+3]=R4;
319 if (image[bytePos+4]<G4) image[bytePos+4]=G4;
320 //if (image[bytePos+5]<B4) image[bytePos+5]=B4;
321
322 bytePos+=m_Width*3;
323
324 if (image[bytePos-3]<R2) image[bytePos-3]=R2;
325 if (image[bytePos-2]<G2) image[bytePos-2]=G2;
326 //if (image[bytePos-1]<B2) image[bytePos-1]=B2;
327
328 if (image[bytePos ]<R1) image[bytePos ]=R1;
329 if (image[bytePos+1]<G1) image[bytePos+1]=G1;
330 //if (image[bytePos+2]<B1) image[bytePos+2]=B1;
331
332 if (image[bytePos+3]<R2) image[bytePos+3]=R2;
333 if (image[bytePos+4]<G2) image[bytePos+4]=G2;
334 //if (image[bytePos+5]<B2) image[bytePos+5]=B2;
335
336 bytePos+=m_Width*3;
337
338 if (image[bytePos-3]<R4) image[bytePos-3]=R4;
339 if (image[bytePos-2]<G4) image[bytePos-2]=G4;
340 //if (image[bytePos-1]<B4) image[bytePos-1]=B4;
341
342 if (image[bytePos ]<R2) image[bytePos ]=R2;
343 if (image[bytePos+1]<G2) image[bytePos+1]=G2;
344 //if (image[bytePos+2]<B2) image[bytePos+2]=B2;
345
346 if (image[bytePos+3]<R4) image[bytePos+3]=R4;
347 if (image[bytePos+4]<G4) image[bytePos+4]=G4;
348 //if (image[bytePos+5]<B4) image[bytePos+5]=B4;
349 }
350
351 void drawLine(unsigned char *image,int x0,int y0,int x1,int y1)
352 {
353 if (x1==x0 && y1==y0) return;
354 double Vx=double(x1-x0);
355 double Vy=double(y1-y0);
356 double dt=1.0/sqrt(Vx*Vx+Vy*Vy);
357
358 for (double t=0.0; t<=1.0; t+=dt)
359 {
360 dither(x0+int(t*Vx),y0+int(t*Vy),image);
361 }
362 }
363
364 void drawCircle(unsigned char *image,int cx,int cy,double radius)
365 {
366 double dt=1.0/(2*M_PI*radius);
367
368 int dx,dy;
369
370 double cs,sn;
371
372 for (double t=0.0; t<=M_PI_4; t+=dt)
373 {
374 cs=cos(t);
375 sn=sin(t);
376
377 dx=int(radius*cs);
378 dy=int(radius*sn);
379
380 dither(cx+dx,cy+dy,image);
381 dither(cx+dx,cy-dy,image);
382 dither(cx-dx,cy-dy,image);
383 dither(cx-dx,cy+dy,image);
384
385 dither(cx+dy,cy+dx,image);
386 dither(cx+dy,cy-dx,image);
387 dither(cx-dy,cy-dx,image);
388 dither(cx-dy,cy+dx,image);
389 }
390 }
391
392 // original
394 static double dXmin,dXmax,dYmin,dYmax;
395 double dXv[8],dYv[8];
396 double dXc,dYc;
397 double dGain;
400
405
406 unsigned char R_MAX, G_MAX, B_MAX;
407
409 static int m_maxRange;
410 static double *Exponential;
411
412 // scaled
414 int xv[8],yv[8];
415
418
420
422
423 public:
426};
427
428#endif
@ data
#define MAX_TAXELS
Definition TouchSensor.h:18
#define M_PI
Definition XSensMTx.cpp:24
double dX[MAX_TAXELS]
int Abs(int x)
bool connected[MAX_TAXELS]
unsigned char R_MAX
bool calibrated_skin
Definition TouchSensor.h:24
double m_Radius
int x[MAX_TAXELS]
virtual ~TouchSensor()
double activation[MAX_TAXELS]
double m_RadiusOrig
static double dXmin
double remapped_activation[MAX_TAXELS]
void setActivationFromPortData(double val, int id)
void eval(unsigned char *image)
void resize(int width, int height, int margin)
Definition TouchSensor.h:59
void setCalibrationFlag(bool use_calibrated_skin)
Definition TouchSensor.h:45
static double * Exponential
static double dYmax
double dXv[8]
int get_nTaxels()
void setActivationFirst7(unsigned char *data)
void clearActivation()
Definition TouchSensor.h:50
double dY[MAX_TAXELS]
void drawLine(unsigned char *image, int x0, int y0, int x1, int y1)
void setActivationLast5(unsigned char *data)
virtual void draw(unsigned char *image)
static double dXmax
void dither(int x, int y, unsigned char *image)
double dYv[8]
int m_maxRangeLight
int y[MAX_TAXELS]
void drawCircle(unsigned char *image, int cx, int cy, double radius)
static int m_maxRange
unsigned char G_MAX
static double dYmin
unsigned char B_MAX
void setColor(unsigned char r, unsigned char g, unsigned char b)
Definition TouchSensor.h:38
void eval_light(unsigned char *image)
exp(-x3 *T)]
int n