sonar_lut.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2015-2019, Dataspeed Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Dataspeed Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 #ifndef _DBW_MKZ_CAN_SONAR_LUT_H
36 #define _DBW_MKZ_CAN_SONAR_LUT_H
37 #include <sensor_msgs/PointCloud2.h>
38 #include <dbw_mkz_msgs/SurroundReport.h>
39 
40 namespace dbw_mkz_can
41 {
42 
43 static const struct {float x; float y; float z; float a;} SONAR_TABLE[] = {
44 // x, y, z, angle
45  { 4.000, 0.900, 0.100, 0.500 * M_PI}, // Front left side
46  { 4.000, 0.500, 0.100, 0.100 * M_PI}, // Front left corner
47  { 4.000, 0.200, 0.100, 0.000 * M_PI}, // Front left center
48  { 4.000, -0.200, 0.100, 0.000 * M_PI}, // Front right center
49  { 4.000, -0.500, 0.100, 1.900 * M_PI}, // Front right corner
50  { 4.000, -0.900, 0.100, 1.500 * M_PI}, // Front right side
51  {-1.000, 0.900, 0.100, 0.500 * M_PI}, // Rear left side
52  {-1.000, 0.500, 0.100, 0.900 * M_PI}, // Rear left corner
53  {-1.000, 0.200, 0.100, 1.000 * M_PI}, // Rear left center
54  {-1.000, -0.200, 0.100, 1.000 * M_PI}, // Rear right center
55  {-1.000, -0.500, 0.100, 1.100 * M_PI}, // Rear right corner
56  {-1.000, -0.900, 0.100, 1.500 * M_PI}, // Rear right side
57 };
58 static inline float sonarMetersFromBits(uint8_t bits) {
59  return bits ? ((float)bits * 0.15) + 0.15 : 0.0;
60 }
61 static inline uint32_t sonarColorFromRange(float range) {
62  if (range < 0.7) {
63  return 0xC0FF0000; // rgba = RED
64  } else if (range < 1.3) {
65  return 0xC0FFFF00; // rgba = YELLOW
66  } else {
67  return 0xC000FF00; // rgba = GREEN
68  }
69 }
70 static inline void sonarBuildPointCloud2(sensor_msgs::PointCloud2 &cloud, const dbw_mkz_msgs::SurroundReport &surround) {
71  // Populate message fields
72  const uint32_t POINT_STEP = 16;
73  cloud.header.frame_id = "base_link";
74  cloud.header.stamp = surround.header.stamp;
75  cloud.fields.resize(4);
76  cloud.fields[0].name = "x";
77  cloud.fields[0].offset = 0;
78  cloud.fields[0].datatype = sensor_msgs::PointField::FLOAT32;
79  cloud.fields[0].count = 1;
80  cloud.fields[1].name = "y";
81  cloud.fields[1].offset = 4;
82  cloud.fields[1].datatype = sensor_msgs::PointField::FLOAT32;
83  cloud.fields[1].count = 1;
84  cloud.fields[2].name = "z";
85  cloud.fields[2].offset = 8;
86  cloud.fields[2].datatype = sensor_msgs::PointField::FLOAT32;
87  cloud.fields[2].count = 1;
88  cloud.fields[3].name = "rgba";
89  cloud.fields[3].offset = 12;
90  cloud.fields[3].datatype = sensor_msgs::PointField::FLOAT32;
91  cloud.fields[3].count = 1;
92  cloud.data.resize(12 * POINT_STEP);
93 
94  uint8_t *ptr = cloud.data.data();
95  for (unsigned int i = 0; i < 12; i++) {
96  const float range = surround.sonar[i];
97  if (range > 0.0) {
98  *((float*)(ptr + 0)) = SONAR_TABLE[i].x + cosf(SONAR_TABLE[i].a) * range; // x
99  *((float*)(ptr + 4)) = SONAR_TABLE[i].y + sinf(SONAR_TABLE[i].a) * range; // y
100  *((float*)(ptr + 8)) = SONAR_TABLE[i].z; // z
101  *((uint32_t*)(ptr + 12)) = sonarColorFromRange(range); // rgba
102  ptr += POINT_STEP;
103  }
104  }
105  if (ptr == cloud.data.data()) {
106  // Prevent rviz from latching the last message
107  *((float*)(ptr + 0)) = NAN; // x
108  *((float*)(ptr + 4)) = NAN; // y
109  *((float*)(ptr + 8)) = NAN; // z
110  *((uint32_t*)(ptr + 12)) = 0x00000000; // rgba
111  ptr += POINT_STEP;
112  }
113 
114  // Populate message with number of valid points
115  cloud.point_step = POINT_STEP;
116  cloud.row_step = ptr - cloud.data.data();
117  cloud.height = 1;
118  cloud.width = cloud.row_step / POINT_STEP;
119  cloud.is_bigendian = false;
120  cloud.is_dense = true;
121  cloud.data.resize(cloud.row_step); // Shrink to actual size
122 }
123 
124 } // namespace dbw_mkz_can
125 
126 #endif // _DBW_MKZ_CAN_SONAR_LUT_H
127 
float x
Definition: sonar_lut.h:43
static float sonarMetersFromBits(uint8_t bits)
Definition: sonar_lut.h:58
static const struct dbw_mkz_can::@18 SONAR_TABLE[]
float y
Definition: sonar_lut.h:43
float a
Definition: sonar_lut.h:43
static void sonarBuildPointCloud2(sensor_msgs::PointCloud2 &cloud, const dbw_mkz_msgs::SurroundReport &surround)
Definition: sonar_lut.h:70
static uint32_t sonarColorFromRange(float range)
Definition: sonar_lut.h:61
float z
Definition: sonar_lut.h:43


dbw_mkz_can
Author(s): Kevin Hallenbeck
autogenerated on Fri May 14 2021 02:47:08