3 #include <astra/capi/astra.h>
9 #include <unordered_map>
12 #include <eigen3/Eigen/Dense>
19 struct hash<astra_status_t>
21 std::size_t operator() (astra_status_t status)
const
23 return static_cast<std::size_t
>(status);
30 __attribute__((destructor))
31 static void terminate()
39 Encoding(
const std::string &type,
const std::uint32_t byte_length)
41 , byte_length(byte_length)
46 std::uint32_t byte_length;
49 const static std::unordered_map<astra_pixel_format_t, Encoding> ENCODING_MAPPING {
56 const static std::unordered_map<astra_status_t, std::string> STATUS_STRINGS {
57 { ASTRA_STATUS_SUCCESS,
"Success" },
58 { ASTRA_STATUS_INVALID_PARAMETER,
"Invalid Parameter" },
59 { ASTRA_STATUS_DEVICE_ERROR,
"Device Error" },
60 { ASTRA_STATUS_TIMEOUT,
"Timeout" },
61 { ASTRA_STATUS_INVALID_PARAMETER_TOKEN,
"Invalid Parameter Token" },
62 { ASTRA_STATUS_INVALID_OPERATION,
"Invalid Operation" },
63 { ASTRA_STATUS_INTERNAL_ERROR,
"Internal Error" },
64 { ASTRA_STATUS_UNINITIALIZED,
"Uninitialized" }
70 const auto it = STATUS_STRINGS.find(status);
71 if (it == STATUS_STRINGS.cend())
throw std::runtime_error(
"Unknown astra_status_t value");
75 sensor_msgs::Image
astra_ros::toRos(
const std::uint8_t *
const data,
const std::size_t length,
const astra_image_metadata_t &metadata)
77 sensor_msgs::Image ret;
78 ret.height = metadata.height;
79 ret.width = metadata.width;
80 const auto it = ENCODING_MAPPING.find(metadata.pixelFormat);
81 if (it == ENCODING_MAPPING.cend())
throw std::runtime_error(
"Can't convert pixel format to sensor_msgs::Image");
82 ret.encoding = it->second.type;
83 ret.is_bigendian =
false;
84 ret.step = ret.width * it->second.byte_length;
85 ret.data.insert(ret.data.end(), data, data +
length);
89 sensor_msgs::Image
astra_ros::toRos(
const std::int16_t *
const data,
const std::size_t length,
const astra_image_metadata_t &metadata)
91 sensor_msgs::Image ret;
92 ret.height = metadata.height;
93 ret.width = metadata.width;
95 ret.is_bigendian =
false;
96 ret.step = ret.width * 2;
99 ret.data.insert(ret.data.end(),
reinterpret_cast<const std::uint8_t *
>(data),
reinterpret_cast<const std::uint8_t *
>(data) +
length);
103 sensor_msgs::Image
astra_ros::toRos(
const astra_rgba_pixel_t *
const data,
const std::size_t length,
const astra_image_metadata_t &metadata)
105 sensor_msgs::Image ret;
106 ret.height = metadata.height;
107 ret.width = metadata.width;
110 ret.is_bigendian =
false;
111 ret.step = ret.width * 4;
114 ret.data.reserve(
length * 4);
115 for (std::size_t i = 0; i <
length; ++i)
117 ret.data.push_back(data[i].r);
118 ret.data.push_back(data[i].g);
119 ret.data.push_back(data[i].b);
120 ret.data.push_back(data[i].alpha);
128 sensor_msgs::Image ret;
129 ret.height = floor_mask.height;
130 ret.width = floor_mask.width;
132 ret.is_bigendian =
false;
133 ret.step = ret.width;
135 const std::size_t size = floor_mask.height * floor_mask.width;
136 ret.data.reserve(size);
137 for (std::size_t i = 0; i < size; ++i)
139 ret.data.push_back(floor_mask.data[i] ? 0xFF : 0x00);
150 ret.status =
static_cast<std::uint8_t
>(body.status);
151 ret.center_of_mass.x = body.centerOfMass.x / 1000.0;
152 ret.center_of_mass.y = -body.centerOfMass.y / 1000.0;
153 ret.center_of_mass.z = body.centerOfMass.z / 1000.0;
154 ret.is_tracking_joints = body.features & ASTRA_BODY_TRACKING_JOINTS;
155 ret.is_tracking_hand_poses = body.features & ASTRA_BODY_TRACKING_HAND_POSES;
157 for (std::size_t i = 0; i < ASTRA_MAX_JOINTS; ++i)
159 if (body.joints[i].type == ASTRA_JOINT_UNKNOWN)
continue;
162 joint.type =
static_cast<std::uint8_t
>(body.joints[i].type);
163 joint.status =
static_cast<std::uint8_t
>(body.joints[i].status);
164 joint.depth_position.x = body.joints[i].depthPosition.x;
165 joint.depth_position.y = body.joints[i].depthPosition.y;
166 joint.pose.position.x = body.joints[i].worldPosition.x / 1000.0;
167 joint.pose.position.y = -body.joints[i].worldPosition.y / 1000.0;
168 joint.pose.position.z = body.joints[i].worldPosition.z / 1000.0;
170 const auto &orientation = body.joints[i].orientation;
173 mat << orientation.m00, orientation.m01, orientation.m02,
174 orientation.m10, orientation.m11, orientation.m12,
175 orientation.m20, orientation.m21, orientation.m22;
184 Eigen::Quaternionf q(mat * P);
186 joint.pose.orientation.x = q.x();
187 joint.pose.orientation.y = q.y();
188 joint.pose.orientation.z = q.z();
189 joint.pose.orientation.w = q.w();
192 ret.joints.push_back(joint);
195 ret.left_hand_pose =
static_cast<std::uint8_t
>(body.handPoses.leftHand);
196 ret.right_hand_pose =
static_cast<std::uint8_t
>(body.handPoses.rightHand);
201 std::vector<astra_ros::Body>
astra_ros::toRos(
const astra_body_list_t &body_list,
const std_msgs::Header &header)
203 std::vector<Body> ret;
204 ret.reserve(body_list.count);
205 for (std::int32_t i = 0; i < body_list.count; ++i)
227 ret.width = image_stream_mode.
width.get_value_or(0);
228 ret.height = image_stream_mode.
height.get_value_or(0);
229 ret.fps = image_stream_mode.
fps.get_value_or(0);
230 ret.pixel_format =
static_cast<std::uint32_t
>(image_stream_mode.
pixel_format.get_value_or(ASTRA_PIXEL_FORMAT_UNKNOWN));
236 Device::ImageStreamMode ret;
237 if (image_stream_mode.width != 0) ret.
width = image_stream_mode.width;
238 if (image_stream_mode.height != 0) ret.height = image_stream_mode.height;
239 if (image_stream_mode.fps != 0) ret.fps = image_stream_mode.fps;
240 if (image_stream_mode.pixel_format != ImageStreamMode::PIXEL_FORMAT_UNKNOWN) ret.pixel_format =
static_cast<astra_pixel_format_t
>(image_stream_mode.pixel_format);