Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
OpenZEDCam.hpp File Reference

Example file that demonstrates opening and using multiple different features of the ZED camera. More...

Include dependency graph for OpenZEDCam.hpp:

Go to the source code of this file.

Functions

std::string PrintMatrix3f (const sl::Matrix3f &slMatrix)
 Constructs a printable string out of a given Matrix3f data structure from the stereolabs library.
void RunExample ()
 This example demonstrates the proper way to interact with the CameraHandler. A pointer to a ZEDCam is retrieved and then a couple of local cv::Mat are created for storing frames. Then, the frames are passed to the RequestFrameCopy function of the camera and a future is IMMEDIATELY returned. The method call doesn't wait for frame to be retrieved/copied before returning. This allows you to request multiple frames/data from the camera non-sequentially.

Variables

const bool ENABLE_SPATIAL_MAPPING = false

Detailed Description

Example file that demonstrates opening and using multiple different features of the ZED camera.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-09-16

Function Documentation

◆ PrintMatrix3f()

std::string PrintMatrix3f ( const sl::Matrix3f & slMatrix)

Constructs a printable string out of a given Matrix3f data structure from the stereolabs library.

Parameters
slMatrix- The matrix to print.
Author
MissouriMRDT (mrdt..nosp@m.auto.nosp@m.nomy@.nosp@m.gmai.nosp@m.l.com)
Date
2025-11-17
36{
37 std::ostringstream stdOSS;
38 stdOSS << std::fixed << std::setprecision(4);
39
40 for (int nIter = 0; nIter < 3; ++nIter)
41 {
42 stdOSS << "[ ";
43 for (int nJter = 0; nJter < 3; ++nJter)
44 {
45 // Access element at row i, column j
46 stdOSS << slMatrix.r[nIter * 3 + nJter];
47 if (nJter < 2)
48 {
49 stdOSS << ", ";
50 }
51 }
52 stdOSS << " ]" << std::endl;
53 }
54
55 return stdOSS.str();
56}
Here is the caller graph for this function:

◆ RunExample()

void RunExample ( )

This example demonstrates the proper way to interact with the CameraHandler. A pointer to a ZEDCam is retrieved and then a couple of local cv::Mat are created for storing frames. Then, the frames are passed to the RequestFrameCopy function of the camera and a future is IMMEDIATELY returned. The method call doesn't wait for frame to be retrieved/copied before returning. This allows you to request multiple frames/data from the camera non-sequentially.

Inside the camera thread, the cv::Mat pointer that points to the cv::Mat within THIS class is written to and an std::promise is set to TRUE. The future that was return now contains this TRUE value. When the get() method is called on the returned future, the code will block until the promise is fulfilled (set to TRUE). Once the get() method returns, the cv::Mat within this class now contains a complete frame and can be display or used in other computer vision things.

The same exact process happens for the positional tracking pose that is retrieved from the camera. Multiple other methods of the ZEDCam class work this way as it allows this thread and other threads to get multiple pieces of from the camera without slowing it down to an unusable speed.

Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-07-22
82{
83 // Initialize and start handlers.
84 globals::g_pCameraHandler = new CameraHandler();
85
86 // Get pointer to camera.
87 std::shared_ptr<ZEDCamera> ExampleZEDCam1 = globals::g_pCameraHandler->GetZED(CameraHandler::ZEDCamName::eHeadMainCam);
88 // Start ZED cam.
89 ExampleZEDCam1->Start();
90
91 // Turn on ZED features.
92 ExampleZEDCam1->EnablePositionalTracking();
93 // Check if we should turn on spatial mapping.
94 if (ENABLE_SPATIAL_MAPPING)
95 {
96 // Enable spatial mapping.
97 ExampleZEDCam1->EnableSpatialMapping();
98 }
99
100 // Declare mats to store images in.
101 cv::Mat cvNormalFrame1;
102 cv::Mat cvDepthFrame1;
103 cv::Mat cvPointCloud1;
104 cv::Mat cvPointCloudColor1;
105 cv::cuda::GpuMat cvGPUNormalFrame1;
106 cv::cuda::GpuMat cvGPUDepthFrame1;
107 cv::cuda::GpuMat cvGPUPointCloud1;
108 // Declare other data types to store data in.
109 ZEDCam::Pose stPose;
110 sl::SensorsData slSensors;
111
112 // Declare FPS counter.
113 IPS FPS = IPS();
114
115 // Loop forever, or until user hits ESC.
116 while (true)
117 {
118 // Create instance variables.
119 std::future<bool> fuFrameCopyStatus;
120 std::future<bool> fuDepthCopyStatus;
121 std::future<bool> fuPointCloudCopyStatus;
122
123 // Check if the camera is setup to use CPU or GPU mats.
124 if (ExampleZEDCam1->GetUsingGPUMem())
125 {
126 // Grab frames from camera.
127 fuFrameCopyStatus = ExampleZEDCam1->RequestFrameCopy(cvGPUNormalFrame1);
128 fuDepthCopyStatus = ExampleZEDCam1->RequestDepthCopy(cvGPUDepthFrame1, false);
129 fuPointCloudCopyStatus = ExampleZEDCam1->RequestPointCloudCopy(cvGPUPointCloud1);
130 }
131 else
132 {
133 // Grab frames from camera.
134 fuFrameCopyStatus = ExampleZEDCam1->RequestFrameCopy(cvNormalFrame1);
135 fuDepthCopyStatus = ExampleZEDCam1->RequestDepthCopy(cvDepthFrame1, false);
136 fuPointCloudCopyStatus = ExampleZEDCam1->RequestPointCloudCopy(cvPointCloud1);
137 }
138 // Grab other info from camera.
139 std::future<bool> fuPoseCopyStatus = ExampleZEDCam1->RequestPositionalPoseCopy(stPose);
140 std::future<bool> fuSensorsCopyStatus = ExampleZEDCam1->RequestSensorsCopy(slSensors);
141
142 // Wait for the frames to be copied.
143 if (fuFrameCopyStatus.get() && fuDepthCopyStatus.get() && fuPointCloudCopyStatus.get())
144 {
145 // Check if the camera is setup to use CPU or GPU mats.
146 if (ExampleZEDCam1->GetUsingGPUMem())
147 {
148 // Download memory from gpu mats if necessary.
149 cvGPUNormalFrame1.download(cvNormalFrame1);
150 cvGPUDepthFrame1.download(cvDepthFrame1);
151 cvGPUPointCloud1.download(cvPointCloud1);
152 }
153
154 // Put FPS on normal frame.
155 cv::putText(cvNormalFrame1,
156 std::to_string(ExampleZEDCam1->GetIPS().GetExactIPS()),
157 cv::Point(50, 50),
159 1,
160 cv::Scalar(255, 255, 255));
161
162 // Put FPS on depth frame.
163 cv::putText(cvDepthFrame1, std::to_string(ExampleZEDCam1->GetIPS().GetExactIPS()), cv::Point(50, 50), cv::FONT_HERSHEY_COMPLEX, 1, cv::Scalar(255, 255, 255));
164
165 // Split color from point cloud.
166 imgops::SplitPointCloudColors(cvPointCloud1, cvPointCloudColor1);
167
168 // Wait for the other info to be copied.
169 if (fuPoseCopyStatus.get())
170 {
171 LOG_INFO(logging::g_qConsoleLogger,
172 "Positional Tracking: X: {} | Y: {} | Z: {}",
173 stPose.stTranslation.dX,
174 stPose.stTranslation.dY,
175 stPose.stTranslation.dZ);
176 LOG_INFO(logging::g_qConsoleLogger,
177 "Positional Orientation: Roll: {} | Pitch: {} | Yaw:{}",
178 stPose.stEulerAngles.dXO,
179 stPose.stEulerAngles.dYO,
180 stPose.stEulerAngles.dZO);
181 }
182
183 // Wait for sensors data to be copied.
184 if (fuSensorsCopyStatus.get())
185 {
186 // Unpack sensors data.
187 double dRelativeAltitude = slSensors.barometer.relative_altitude;
188 float fTemperature = 0.0;
189 slSensors.temperature.get(sl::SensorsData::TemperatureData::SENSOR_LOCATION::IMU, fTemperature);
190 float fMagHeading = slSensors.magnetometer.magnetic_heading;
191 float fIMUVelocityX = slSensors.imu.angular_velocity.x;
192 float fIMUVelocityY = slSensors.imu.angular_velocity.y;
193 float fIMUVelocityZ = slSensors.imu.angular_velocity.z;
194 sl::Matrix3f slIMUVelocityCov = slSensors.imu.angular_velocity_covariance.r;
195 float fAccelX = slSensors.imu.linear_acceleration.x;
196 float fAccelY = slSensors.imu.linear_acceleration.y;
197 float fAccelZ = slSensors.imu.linear_acceleration.z;
198 sl::Matrix3f slAccelCov = slSensors.imu.angular_velocity_covariance.r;
199
200 LOG_INFO(logging::g_qConsoleLogger,
201 "Sensors Data: Altitude: {} | Temperature: {} | Mag Heading: {}\nIMU VelX: {}\nIMU VelY: {}\nIMU VelZ: {}\nIMU AccelX: {}\nIMU AccelY: {}\n"
202 "IMU AccelZ: {}\nIMU VelCov: {}\nIMU AccelCov: {}",
203 dRelativeAltitude,
204 fTemperature,
205 fMagHeading,
206 fIMUVelocityX,
207 fIMUVelocityY,
208 fIMUVelocityZ,
209 fAccelX,
210 fAccelY,
211 fAccelZ,
212 PrintMatrix3f(slIMUVelocityCov),
213 PrintMatrix3f(slAccelCov));
214 }
215
216 // Print info.
217 LOG_INFO(logging::g_qConsoleLogger, "ZED Getter FPS: {} | 1% Low: {}", ExampleZEDCam1->GetIPS().GetAverageIPS(), ExampleZEDCam1->GetIPS().Get1PercentLow());
218 // Check if spatial mapping is enabled.
219 if (ENABLE_SPATIAL_MAPPING)
220 {
221 LOG_INFO(logging::g_qConsoleLogger, "Spatial Mapping State: {}", sl::toString(ExampleZEDCam1->GetSpatialMappingState()).get());
222 }
223
224 // Display frames.
225 cv::imshow("FRAME1", cvNormalFrame1);
226 cv::imshow("DEPTH1", cvDepthFrame1);
227 cv::imshow("POINT CLOUD COLOR 1", cvPointCloudColor1);
228 }
229
230 // Tick FPS counter.
231 FPS.Tick();
232 // Print FPS of main loop.
233 LOG_INFO(logging::g_qConsoleLogger, "Main FPS: {}", FPS.GetAverageIPS());
234
235 char chKey = cv::waitKey(1);
236 if (chKey == 27) // Press 'Esc' key to exit
237 break;
238 }
239
240 // Close all OpenCV windows.
242
243 // Check if spatial mapping is enabled.
244 if (ENABLE_SPATIAL_MAPPING)
245 {
246 // Extract spatial map.
247 std::future<sl::Mesh> fuSpatialMap;
248 ExampleZEDCam1->ExtractSpatialMapAsync(fuSpatialMap);
249 sl::Mesh slSpatialMap = fuSpatialMap.get();
250 slSpatialMap.save("test.obj", sl::MESH_FILE_FORMAT::PLY);
251 }
252
254 // Cleanup.
256 // Stop RoveComm quill logging or quill will segfault if trying to output logs to RoveComm.
257 network::g_bRoveCommUDPStatus = false;
258 network::g_bRoveCommTCPStatus = false;
259
260 // Stop camera threads.
261 globals::g_pCameraHandler->StopAllCameras();
262}
std::string PrintMatrix3f(const sl::Matrix3f &slMatrix)
Constructs a printable string out of a given Matrix3f data structure from the stereolabs library.
Definition OpenZEDCam.hpp:35
The CameraHandler class is responsible for managing all of the camera feeds that Autonomy_Software us...
Definition CameraHandler.h:34
This util class provides an easy way to keep track of iterations per second for any body of code.
Definition IPS.hpp:30
double GetAverageIPS() const
Calculates the average iterations per second.
Definition IPS.hpp:180
void Tick()
This method is used to update the iterations per second counter and recalculate all of the IPS metric...
Definition IPS.hpp:138
void download(OutputArray dst) const
Point2i Point
Scalar_< double > Scalar
void imshow(const String &winname, InputArray mat)
int waitKey(int delay=0)
void destroyAllWindows()
void putText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false)
FONT_HERSHEY_COMPLEX
void SplitPointCloudColors(cv::Mat &cvInputPointCloud, cv::Mat &cvOutputColors)
Given a cv::Mat containing X, Y, Z, and BGRA values for each pixel in the third dimension,...
Definition ImageOperations.hpp:181
This struct is used within the ZEDCam class to store the camera pose with high precision....
Definition ZEDCamera.hpp:77
Here is the call graph for this function: