gusimplewhiteboard
wb_pixel_to_robot_relative_coord.c
Go to the documentation of this file.
1/*
2 * file wb_pixel_to_robot_relative_coord.c
3 *
4 * This file was generated by classgenerator from pixel_to_robot_relative_coord.gen.
5 * DO NOT CHANGE MANUALLY!
6 *
7 * Copyright © 2021 Carl Lusty. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials
19 * provided with the distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgement:
23 *
24 * This product includes software developed by Carl Lusty.
25 *
26 * 4. Neither the name of the author nor the names of contributors
27 * may be used to endorse or promote products derived from this
28 * software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
34 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
37 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * -----------------------------------------------------------------------
43 * This program is free software; you can redistribute it and/or
44 * modify it under the above terms or under the terms of the GNU
45 * General Public License as published by the Free Software Foundation;
46 * either version 2 of the License, or (at your option) any later version.
47 *
48 * This program is distributed in the hope that it will be useful,
49 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51 * GNU General Public License for more details.
52 *
53 * You should have received a copy of the GNU General Public License
54 * along with this program; if not, see http://www.gnu.org/licenses/
55 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
56 * Fifth Floor, Boston, MA 02110-1301, USA.
57 *
58 */
59
60#ifndef WHITEBOARD_POSTER_STRING_CONVERSION
61#define WHITEBOARD_POSTER_STRING_CONVERSION
62#endif // WHITEBOARD_POSTER_STRING_CONVERSION
63
65#include <stdio.h>
66#include <string.h>
67#include <stdlib.h>
68#include <ctype.h>
69#include <limits.h>
70
71/* Network byte order functions */
72#pragma clang diagnostic push
73#pragma clang diagnostic ignored "-Wunused-macros"
74#if defined(__linux)
75# include <endian.h>
76# include <byteswap.h>
77#elif defined(__APPLE__)
78# include <machine/endian.h> //Needed for __BYTE_ORDER
79# include <architecture/byte_order.h> //Needed for byte swap functions
80# define bswap_16(x) NXSwapShort(x)
81# define bswap_32(x) NXSwapInt(x)
82# define bswap_64(x) NXSwapLongLong(x)
83#elif defined(ESP8266)
84# define bswap_16(x) __builtin_bswap16(x)
85# define bswap_32(x) __builtin_bswap32(x)
86# define bswap_64(x) __builtin_bswap64(x)
87#else
88 //Manually define swap macros?
89#endif
90
91#if (!defined(__BYTE_ORDER) && !defined(__LITTLE_ENDIAN)) || (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN)
92# if !defined(htonll) && !defined(ntohll)
93# define htonll(x) bswap_64(x)
94# define ntohll(x) bswap_64(x)
95# endif
96# if !defined(htonl) && !defined(ntohl)
97# define htonl(x) bswap_32(x)
98# define ntohl(x) bswap_32(x)
99# endif
100# if !defined(htons) && !defined(ntohs)
101# define htons(x) bswap_16(x)
102# define ntohs(x) bswap_16(x)
103# endif
104#else
105# if !defined(htonll) && !defined(ntohll)
106# define htonll(x) (x)
107# define ntohll(x) (x)
108# endif
109# if !defined(htonl) && !defined(ntohl)
110# define htonl(x) (x)
111# define ntohl(x) (x)
112# endif
113# if !defined(htons) && !defined(ntohs)
114# define htons(x) (x)
115# define ntohs(x) (x)
116# endif
117#endif
118#pragma clang diagnostic pop
119
120#pragma clang diagnostic push
121#pragma clang diagnostic ignored "-Wunused-function"
122#pragma clang diagnostic ignored "-Wc++-compat"
123#pragma clang diagnostic ignored "-Wdouble-promotion"
124#pragma clang diagnostic ignored "-Wunused-parameter"
125
126
127void pixel_to_rr_coord(int32_t sx, int32_t sy, enum VisionCamera camera, struct wb_vision_control_status *vs, struct wb_sensors_torsojointsensors *joints, double *distance, double *angle) {
128 pixel_to_rr_coord_kneeling(sx, sy, camera, vs, joints, distance, angle, false);
129}
130
131void pixel_to_rr_coord_legs(int32_t sx, int32_t sy, enum VisionCamera camera, struct wb_vision_control_status *vs, struct wb_sensors_torsojointsensors *joints, struct wb_sensors_legjointsensors *leg_sensors, double *distance, double *angle) {
132 pixel_to_rr_coord_kneeling(sx, sy, camera, vs, joints, distance, angle, leg_sensors->LKneePitch > 2.0f && leg_sensors->RKneePitch > 2.0f);
133}
134
135void pixel_to_rr_coord_legs_no_conv(int32_t sx, int32_t sy, enum VisionCamera camera, struct wb_vision_control_status *vs, struct wb_sensors_torsojointsensors *joints, struct wb_sensors_legjointsensors *leg_sensors, double *distance, double *angle) {
136 pixel_to_rr_coord_kneeling_conv(sx, sy, camera, vs, joints, distance, angle, leg_sensors->LKneePitch > 2.0f && leg_sensors->RKneePitch > 2.0f, false);
137}
138
139void pixel_to_rr_coord_no_conv(int32_t sx, int32_t sy, enum VisionCamera camera, struct wb_vision_control_status *vs, struct wb_sensors_torsojointsensors *joints, double *distance, double *angle) {
140 pixel_to_rr_coord_kneeling_conv(sx, sy, camera, vs, joints, distance, angle, false, false);
141}
142
143void pixel_to_rr_coord_kneeling(int32_t sx, int32_t sy, enum VisionCamera camera, struct wb_vision_control_status *vs, struct wb_sensors_torsojointsensors *joints, double *distance, double *angle, bool kneeling) {
144 pixel_to_rr_coord_kneeling_conv(sx, sy, camera, vs, joints, distance, angle, kneeling, true);
145}
146
147void pixel_to_rr_coord_kneeling_conv(int32_t sx, int32_t sy, enum VisionCamera camera, struct wb_vision_control_status *vs, struct wb_sensors_torsojointsensors *joints, double *distance, double *angle, bool kneeling, bool conv) {
148
149 //TODO Eugene needs to write a C version of the resolution enum to actual values
150 //This should query 'vs' for the resolution!
151 int image_width = 640;
152 int image_height = 480;
153
154 int32_t x = sx;
155 int32_t y = sy;
156
157 if(conv) {
158 x = CONV_TO_IMG(sx, image_width);
159 y = CONV_TO_IMG(-sy, image_height);
160 }
161
162 double camHeight = get_camera_height(camera, joints, kneeling);
163
164 //the lowest angle we can see at for the camera
165 double cameraBottomVFOV = M_PI_2 + (-joints->HeadPitch - CAMERA_VFOV/2 + (camera?BOTTOM_CAMERA_ANGLE:TOP_CAMERA_ANGLE));
166
167 double imageAngleY = ((double)(image_height-y)/image_height)*CAMERA_VFOV;
168
169 double angleY = cameraBottomVFOV + imageAngleY;
170
171 double frontDistance = camHeight*tan(angleY);
172
173 int im_width_2 = (image_width+1)/2;
174 double imageAngleX = ((double)((x-im_width_2))/im_width_2)*CAMERA_HFOV/2;
175
176 *distance = frontDistance/cos(imageAngleX);
177 if (angleY > M_PI_2) {
178 *distance = INFINITY;
179 }
180
181 *angle = -(imageAngleX-joints->HeadYaw);
182}
183
184void rr_to_pixel_coord(double angle, double distance, enum VisionCamera camera, struct wb_vision_control_status *vs, struct wb_sensors_torsojointsensors *joints, int32_t *sx, int32_t *sy, bool kneeling) {
185 int image_width = 640;
186 int image_height = 480;
187
188 double camHeight = get_camera_height(camera, joints, kneeling);
189
190 //the lowest angle we can see at for the camera
191 double cameraBottomVFOV = M_PI_2 + (-joints->HeadPitch - CAMERA_VFOV/2 + (camera?BOTTOM_CAMERA_ANGLE:TOP_CAMERA_ANGLE));
192
193 int im_width_2 = (image_width+1)/2;
194 double imageAngleX = angle +joints->HeadYaw;
195
196 *sx = (int)((imageAngleX/(CAMERA_HFOV/2))*im_width_2)+im_width_2;
197
198 double frontDistance = distance*cos(imageAngleX);
199
200 double angleY = atan(frontDistance/camHeight);
201
202 double imageAngleY = angleY - cameraBottomVFOV;
203
204 *sy = (int)(image_height-(imageAngleY*image_height/CAMERA_VFOV));
205}
206
207double get_camera_height(enum VisionCamera camera, struct wb_sensors_torsojointsensors *joints, bool kneeling) {
208 double dx = (camera?BOTTOM_CAMERA_OFFSET_X:TOP_CAMERA_OFFSET_X);
209 double dz = (camera?BOTTOM_CAMERA_OFFSET_Z:TOP_CAMERA_OFFSET_Z);
210
211 double height = dz*cos(-joints->HeadPitch) + dx*sin(-joints->HeadPitch);
212 if (kneeling) {
213 return height + HEAD_BASE_HEIGHT_KNEELING;
214 }
215 return height + HEAD_BASE_HEIGHT;
216}
217
218#pragma clang diagnostic pop
219
220
221
225const char* wb_pixel_to_robot_relative_coord_description(const struct wb_pixel_to_robot_relative_coord* self, char* descString, size_t bufferSize)
226{
227#pragma clang diagnostic push
228#pragma clang diagnostic ignored "-Wunused-variable"
229 size_t len = 0;
230 if (len >= bufferSize) {
231 return descString;
232 }
233 len += snprintf(descString + len, bufferSize - len, "i=%d", self->i);
234 return descString;
235#pragma clang diagnostic pop
236}
237
241const char* wb_pixel_to_robot_relative_coord_to_string(const struct wb_pixel_to_robot_relative_coord* self, char* toString, size_t bufferSize)
242{
243#pragma clang diagnostic push
244#pragma clang diagnostic ignored "-Wunused-variable"
245 size_t len = 0;
246 if (len >= bufferSize) {
247 return toString;
248 }
249 len += snprintf(toString + len, bufferSize - len, "%d", self->i);
250 return toString;
251#pragma clang diagnostic pop
252}
253
258{
259 size_t temp_length = strlen(str);
260 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
261 if (length < 1 || length > PIXEL_TO_ROBOT_RELATIVE_COORD_DESC_BUFFER_SIZE) {
262 return self;
263 }
264 char var_str_buffer[PIXEL_TO_ROBOT_RELATIVE_COORD_DESC_BUFFER_SIZE + 1];
265 char* var_str = &var_str_buffer[0];
266 char key_buffer[2];
267 char* key = &key_buffer[0];
268 int bracecount = 0;
269 int startVar = 0;
270 int index = 0;
271 int startKey = 0;
272 int endKey = -1;
273 int varIndex = 0;
274 if (index == 0 && str[0] == '{') {
275 index = 1;
276 }
277 startVar = index;
278 startKey = startVar;
279 do {
280 for (int i = index; i < length; i++) {
281 index = i + 1;
282 if (bracecount == 0 && str[i] == '=') {
283 endKey = i - 1;
284 startVar = index;
285 continue;
286 }
287 if (bracecount == 0 && isspace(str[i])) {
288 startVar = index;
289 if (endKey == -1) {
290 startKey = index;
291 }
292 continue;
293 }
294 if (bracecount == 0 && str[i] == ',') {
295 index = i - 1;
296 break;
297 }
298 if (str[i] == '{') {
299 bracecount++;
300 continue;
301 }
302 if (str[i] == '}') {
303 bracecount--;
304 if (bracecount < 0) {
305 index = i - 1;
306 break;
307 }
308 }
309 if (i == length - 1) {
310 index = i;
311 }
312 }
313 if (endKey >= startKey && endKey - startKey < length) {
314 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
315 key[(endKey - startKey) + 1] = 0;
316 } else {
317 key[0] = 0;
318 }
319 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
320 var_str[(index - startVar) + 1] = 0;
321 bracecount = 0;
322 index += 2;
323 startVar = index;
324 startKey = startVar;
325 endKey = -1;
326 if (strlen(key) > 0) {
327 if (0 == strcmp("i", key)) {
328 varIndex = 0;
329 } else {
330 varIndex = -1;
331 }
332 }
333 switch (varIndex) {
334 case -1: { break; }
335 case 0:
336 {
337 self->i = ((int)atoi(var_str));
338 break;
339 }
340 }
341 if (varIndex >= 0) {
342 varIndex++;
343 }
344 } while(index < length);
345 return self;
346}
347
348/*#ifdef WHITEBOARD_SERIALISATION*/
349
354{
355 uint16_t bit_offset = 0;
356 int i_nbo = htonl(self->i);
357 do {
358 int8_t b;
359 for (b = (32 - 1); b >= 0; b--) {
360 do {
361 uint16_t byte = bit_offset / 8;
362 uint16_t bit = 7 - (bit_offset % 8);
363 unsigned long newbit = !!((i_nbo >> b) & 1U);
364 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
365 bit_offset = bit_offset + 1;
366 } while(false);
367 }
368 } while(false);
369 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
370 (void)self;
371 (void)dst;
372 return bit_offset;
373}
374
379{
380 uint16_t bit_offset = 0;
381 do {
382 int8_t b;
383 for (b = (32 - 1); b >= 0; b--) {
384 do {
385 uint16_t byte = bit_offset / 8;
386 uint16_t bit = 7 - (bit_offset % 8);
387 char dataByte = src[byte];
388 unsigned char bitValue = (dataByte >> bit) & 1U;
389 dst->i ^= (-bitValue ^ dst->i) & (1UL << b);
390 bit_offset = bit_offset + 1;
391 } while(false);
392 }
393 } while(false);
394 dst->i = ntohl(dst->i);
395 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
396 (void)src;
397 (void)dst;
398 return bit_offset;
399}
400
401/*#endif // WHITEBOARD_SERIALISATION*/
WHITEBOARD_POSTER_STRING_CONVERSION.
WHITEBOARD_POSTER_STRING_CONVERSION.
float RKneePitch
pitch of the knee joint (right)
float LKneePitch
pitch of the knee joint (left)
WHITEBOARD_POSTER_STRING_CONVERSION.
WHITEBOARD_POSTER_STRING_CONVERSION.
const char * wb_pixel_to_robot_relative_coord_to_string(const struct wb_pixel_to_robot_relative_coord *self, char *toString, size_t bufferSize)
Convert to a string.
void pixel_to_rr_coord_no_conv(int32_t sx, int32_t sy, enum VisionCamera camera, struct wb_vision_control_status *vs, struct wb_sensors_torsojointsensors *joints, double *distance, double *angle)
size_t wb_pixel_to_robot_relative_coord_from_network_serialised(const char *src, struct wb_pixel_to_robot_relative_coord *dst)
Convert from a compressed, serialised, network byte order byte stream.
const char * wb_pixel_to_robot_relative_coord_description(const struct wb_pixel_to_robot_relative_coord *self, char *descString, size_t bufferSize)
Convert to a description string.
void rr_to_pixel_coord(double angle, double distance, enum VisionCamera camera, struct wb_vision_control_status *vs, struct wb_sensors_torsojointsensors *joints, int32_t *sx, int32_t *sy, bool kneeling)
void pixel_to_rr_coord_kneeling(int32_t sx, int32_t sy, enum VisionCamera camera, struct wb_vision_control_status *vs, struct wb_sensors_torsojointsensors *joints, double *distance, double *angle, bool kneeling)
double get_camera_height(enum VisionCamera camera, struct wb_sensors_torsojointsensors *joints, bool kneeling)
void pixel_to_rr_coord(int32_t sx, int32_t sy, enum VisionCamera camera, struct wb_vision_control_status *vs, struct wb_sensors_torsojointsensors *joints, double *distance, double *angle)
void pixel_to_rr_coord_kneeling_conv(int32_t sx, int32_t sy, enum VisionCamera camera, struct wb_vision_control_status *vs, struct wb_sensors_torsojointsensors *joints, double *distance, double *angle, bool kneeling, bool conv)
#define ntohl(x)
void pixel_to_rr_coord_legs_no_conv(int32_t sx, int32_t sy, enum VisionCamera camera, struct wb_vision_control_status *vs, struct wb_sensors_torsojointsensors *joints, struct wb_sensors_legjointsensors *leg_sensors, double *distance, double *angle)
struct wb_pixel_to_robot_relative_coord * wb_pixel_to_robot_relative_coord_from_string(struct wb_pixel_to_robot_relative_coord *self, const char *str)
Convert from a string.
void pixel_to_rr_coord_legs(int32_t sx, int32_t sy, enum VisionCamera camera, struct wb_vision_control_status *vs, struct wb_sensors_torsojointsensors *joints, struct wb_sensors_legjointsensors *leg_sensors, double *distance, double *angle)
#define htonl(x)
size_t wb_pixel_to_robot_relative_coord_to_network_serialised(const struct wb_pixel_to_robot_relative_coord *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
#define CONV_TO_IMG(x, r)
#define BOTTOM_CAMERA_ANGLE
#define TOP_CAMERA_ANGLE
#define BOTTOM_CAMERA_OFFSET_Z
#define TOP_CAMERA_OFFSET_X
#define BOTTOM_CAMERA_OFFSET_X
#define HEAD_BASE_HEIGHT
#define HEAD_BASE_HEIGHT_KNEELING
#define TOP_CAMERA_OFFSET_Z
#define PIXEL_TO_ROBOT_RELATIVE_COORD_DESC_BUFFER_SIZE
VisionCamera
Enum of available camera's that can be used by vision.