YARP
Yet Another Robot Platform
InputCallback.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "InputCallback.h"
20 #include "TextureBuffer.h"
21 #include "OVRHeadsetLogComponent.h"
22 
23 #include <yarp/os/BufferedPort.h>
24 #include <yarp/os/LogStream.h>
25 #include <yarp/os/Time.h>
26 #include <yarp/os/Value.h>
27 
29  yarp::os::BufferedPort<ImageType>(),
30  eyeRenderTexture(nullptr),
31  eye(eye),
32  expected(0),
33  droppedFrames(0),
34  lastImageWidth(0),
35  lastImageHeight(0),
36  rollOffset(0.0f),
37  pitchOffset(0.0f),
38  yawOffset(0.0f)
39 {
41 }
42 
43 
45 {
47 
48  if (eyeRenderTexture) {
49  delete eyeRenderTexture;
50  }
51  eyeRenderTexture = nullptr;
52 }
53 
54 
56 {
57  int delaycnt = 0;
58 
59  eyeRenderTexture->mutex.lock();
60  while (eyeRenderTexture->dataReady && delaycnt <= 3) {
61  eyeRenderTexture->mutex.unlock();
63  eyeRenderTexture->mutex.lock();
64  ++delaycnt;
65  }
66 
68  ++droppedFrames;
69  }
70 
71 // Debug dropped frames using alternating 30x30 red and green squares
72 // at the bottom right of the image, see gazebo_yarp_plugins camera
73 // plugin.
74 #define DEBUG_SQUARES 0
75 #if DEBUG_SQUARES
76  int found = -1;
77  for (int i = 0; i < 10; ++i) {
78  unsigned char* pix = img.getPixelAddress(img.width()-15-(i*30),img.height()-15);
79  if(i % 2) {
80  // Check if this is a RED pixel (255,0,0)
81  // lossy carriers will change the value, so we look for a
82  // pixel close enough to the one we are looking for
83  if (pix[0] <= 5 && pix[1] >= 250 && pix[2] <= 5) {
84  found = i;
85  break;
86  }
87  } else {
88  // Check if this is a GREEN pixel (0,255,0)
89  if (pix[0] >= 250 && pix[1] <= 5 && pix[2] <= 5) {
90  found = i;
91  break;
92  }
93  }
94  }
95  if (found != expected) {
96  yCWarning(OVRHEADSET) << "InputCallback" << (eye==0?"left ":"right") << " expected" << expected << "found" << found << "next" << (found + 1) % 10;
97  }
98  expected = (found + 1) % 10;
99 #endif // DEBUG_SQUARES
100 
101  if(eyeRenderTexture->ptr) {
102  size_t w = img.width();
103  size_t h = img.height();
104  size_t rs = img.getRowSize();
105  unsigned char *data = img.getRawImage();
106 
107  // update data directly on the mapped buffer
108  if (eyeRenderTexture->width == w && eyeRenderTexture->height == h) {
109  // Texture and image have the same size: no problems
111  } else if (eyeRenderTexture->width >= w && eyeRenderTexture->height >= h) {
112  // Texture is larger than image: image is centered in the texture
113  int x = (eyeRenderTexture->width - w)/2;
114  int y = (eyeRenderTexture->height - h)/2;
115  for (size_t i = 0; i < h; ++i) {
116  unsigned char* textureStart = eyeRenderTexture->ptr + (y+i)*eyeRenderTexture->rowSize + x*3;
117  unsigned char* dataStart = data + (i*rs);
118  memcpy(textureStart, dataStart, rs);
119  }
120  } else {
121  // Texture is smaller than image: image is cropped
122  int x = (w - eyeRenderTexture->width)/2;
123  int y = (h - eyeRenderTexture->height)/2;
124  for (size_t i = 0; i < eyeRenderTexture->width; ++i) {
125  unsigned char* textureStart = eyeRenderTexture->ptr + (y+i)*(i*eyeRenderTexture->rowSize) + x*3;
126  unsigned char* dataStart = data + (y+i)*rs + x*3;
127  memcpy(textureStart, dataStart, eyeRenderTexture->rowSize);
128  }
129  }
130 
131  float x = 0.0f;
132  float y = 0.0f;
133  float z = 0.0f;
134  float roll = rollOffset;
135  float pitch = pitchOffset;
136  float yaw = yawOffset;
137 
138  int seqNum;
139  double ts, r, p, yy;
140 
143  int ret = std::sscanf(b.toString().c_str(), "%d %lg %lg %lg %lg\n", &seqNum, &ts, &r, &p, &yy);
144  if (ret == 5) {
145  roll += OVR::DegreeToRad(static_cast<float>(r));
146  pitch += OVR::DegreeToRad(static_cast<float>(p));
147  yaw += OVR::DegreeToRad(static_cast<float>(yy));
148  }
149 
150  eyeRenderTexture->eyePose.Orientation.w = (float)(- cos(roll/2) * cos(pitch/2) * cos(yaw/2) - sin(roll/2) * sin(pitch/2) * sin(yaw/2));
151  eyeRenderTexture->eyePose.Orientation.x = (float)(- cos(roll/2) * sin(pitch/2) * cos(yaw/2) - sin(roll/2) * cos(pitch/2) * sin(yaw/2));
152  eyeRenderTexture->eyePose.Orientation.y = (float)(- cos(roll/2) * cos(pitch/2) * sin(yaw/2) + sin(roll/2) * sin(pitch/2) * cos(yaw/2));
153  eyeRenderTexture->eyePose.Orientation.z = (float)(- sin(roll/2) * cos(pitch/2) * cos(yaw/2) + cos(roll/2) * sin(pitch/2) * sin(yaw/2));
154 
155  eyeRenderTexture->eyePose.Position.x = x;
156  eyeRenderTexture->eyePose.Position.y = y;
157  eyeRenderTexture->eyePose.Position.z = z;
158 
159  eyeRenderTexture->imageWidth = img.width();
160  eyeRenderTexture->imageHeight = img.height();
161 
162  eyeRenderTexture->dataReady = true;
163  }
164  eyeRenderTexture->mutex.unlock();
165 }
LogStream.h
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
yarp::os::Bottle::toString
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:214
InputCallback::yawOffset
float yawOffset
Definition: InputCallback.h:48
TextureBuffer::imageWidth
size_t imageWidth
Definition: TextureBuffer.h:70
OVRHeadsetLogComponent.h
InputCallback::eyeRenderTexture
TextureBuffer * eyeRenderTexture
Definition: InputCallback.h:39
yCWarning
#define yCWarning(component,...)
Definition: LogComponent.h:146
yarp::math::eye
yarp::sig::Matrix eye(int r, int c)
Build an identity matrix (defined in Math.h).
Definition: math.cpp:562
OVRHEADSET
const yarp::os::LogComponent & OVRHEADSET()
Definition: OVRHeadsetLogComponent.cpp:11
InputCallback::eye
int eye
Definition: InputCallback.h:40
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
TextureBuffer::imageHeight
size_t imageHeight
Definition: TextureBuffer.h:71
InputCallback::onRead
virtual void onRead(ImageType &img)
Definition: InputCallback.cpp:55
yarp::sig::ImageOf< yarp::sig::PixelRgb >
TextureBuffer::mutex
std::mutex mutex
Definition: TextureBuffer.h:66
TextureBuffer::bufferSize
size_t bufferSize
Definition: TextureBuffer.h:57
yarp::os::SystemClock::delaySystem
static void delaySystem(double seconds)
Definition: SystemClock.cpp:32
TextureBuffer.h
TextureBuffer::dataReady
bool dataReady
Definition: TextureBuffer.h:65
InputCallback::droppedFrames
unsigned int droppedFrames
Definition: InputCallback.h:42
InputCallback::rollOffset
float rollOffset
Definition: InputCallback.h:46
InputCallback::InputCallback
InputCallback(int eye)
Definition: InputCallback.cpp:28
BufferedPort.h
TextureBuffer::height
size_t height
Definition: TextureBuffer.h:53
InputCallback::~InputCallback
~InputCallback()
Definition: InputCallback.cpp:44
InputCallback::expected
int expected
Definition: InputCallback.h:41
TextureBuffer::width
size_t width
Definition: TextureBuffer.h:52
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
TextureBuffer::ptr
GLubyte * ptr
Definition: TextureBuffer.h:64
InputCallback::pitchOffset
float pitchOffset
Definition: InputCallback.h:47
yarp::os::BufferedPort::getEnvelope
bool getEnvelope(PortReader &envelope) override
Get the envelope information (e.g., a timestamp) from the last message received on the port.
Definition: BufferedPort-inl.h:239
InputCallback.h
Time.h
yCTrace
#define yCTrace(component,...)
Definition: LogComponent.h:88
TextureBuffer::rowSize
size_t rowSize
Definition: TextureBuffer.h:56
TextureBuffer::eyePose
ovrPosef eyePose
Definition: TextureBuffer.h:60
Value.h