YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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"
22
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
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
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);
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;
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
161
163 }
164 eyeRenderTexture->mutex.unlock();
165}
bool ret
const yarp::os::LogComponent & OVRHEADSET()
void onRead(yarp::sig::ImageOf< yarp::sig::PixelBgra > &img) override
the function callback
Definition ImagePort.cpp:30
unsigned int droppedFrames
TextureBuffer * eyeRenderTexture
std::mutex mutex
ovrPosef eyePose
GLubyte * ptr
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition Bottle.cpp:211
bool getEnvelope(PortReader &envelope) override
Get the envelope information (e.g., a timestamp) from the last message received on the port.
static void delaySystem(double seconds)
size_t width() const
Gets width of image in pixels.
Definition Image.h:171
size_t getRowSize() const
Size of the underlying image buffer rows.
Definition Image.h:197
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition Image.cpp:479
size_t height() const
Gets height of image in pixels.
Definition Image.h:177
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
Definition Image.h:245
#define yCTrace(component,...)
#define yCWarning(component,...)
The main, catch-all namespace for YARP.
Definition dirs.h:16