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
SIMZEDCam.h
Go to the documentation of this file.
1
11#ifndef SIMZEDCAM_H
12#define SIMZEDCAM_H
13
14#include "../../../interfaces/AutonomyThread.hpp"
15#include "../../../interfaces/ZEDCamera.hpp"
16#include "WebRTC.h"
17
19#include <RoveComm/RoveComm.h>
20#include <RoveComm/RoveCommManifest.h>
21#include <opencv2/opencv.hpp>
22
24
25
35class SIMZEDCam : public ZEDCamera
36{
37 public:
39 // Declare public methods and member variables.
41
42 SIMZEDCam(const std::string szCameraPath,
43 const int nPropResolutionX,
44 const int nPropResolutionY,
45 const int nPropFramesPerSecond,
46 const double dPropHorizontalFOV,
47 const double dPropVerticalFOV,
48 const bool bEnableRecordingFlag,
49 const int nNumFrameRetrievalThreads = 10,
50 const unsigned int unCameraSerialNumber = 0);
51 ~SIMZEDCam();
52 std::future<bool> RequestFrameCopy(cv::Mat& cvFrame) override;
53 std::future<bool> RequestDepthCopy(cv::Mat& cvDepth, const bool bRetrieveMeasure = true);
54 std::future<bool> RequestPointCloudCopy(cv::Mat& cvPointCloud);
55 std::future<bool> RequestPositionalPoseCopy(Pose& stPose) override;
56 std::future<bool> RequestFusionGeoPoseCopy(sl::GeoPose& slGeoPose) override;
57 std::future<bool> RequestSensorsCopy(sl::SensorsData& slSensorsData) override;
58 sl::ERROR_CODE ResetPositionalTracking() override;
59 sl::ERROR_CODE RebootCamera() override;
60 sl::FUSION_ERROR_CODE SubscribeFusionToCameraUUID(sl::CameraIdentifier& slCameraUUID) override;
61 sl::CameraIdentifier PublishCameraToFusion() override;
62
64 // Setters for class member variables.
66
67 sl::ERROR_CODE EnablePositionalTracking(const float fExpectedCameraHeightFromFloorTolerance = constants::ZED_DEFAULT_FLOOR_PLANE_ERROR) override;
68 void DisablePositionalTracking() override;
69 void SetPositionalPose(const double dX, const double dY, const double dZ, const double dXO, const double dYO, const double dZO) override;
70
72 // Getters.
74
75 bool GetCameraIsOpen() override;
76 bool GetUsingGPUMem() const override;
77 std::string GetCameraModel() override;
78 bool GetPositionalTrackingEnabled() override;
79
80 private:
82 // Declare private methods.
84
85 void ThreadedContinuousCode() override;
86 void PooledLinearCode() override;
87 void SetCallbacks();
88 void EstimateDepthMeasure(const cv::Mat& cvDepthImage, cv::Mat& cvDepthMeasure);
89 void CalculatePointCloud(const cv::Mat& cvDepthMeasure, cv::Mat& cvPointCloud);
90
91
99 const std::function<void(const rovecomm::RoveCommPacket<double>&, const sockaddr_in&)> ProcessIMUData =
100 [this](const rovecomm::RoveCommPacket<double>& stPacket, const sockaddr_in& stdAddr)
101 {
102 // Not using this.
103 (void) stdAddr;
104
105 // Acquire a write lock on the sensors mutex.
106 std::unique_lock<std::shared_mutex> lkSensorsProcessLock(m_muSensorsCopyMutex);
107 // Update IMU data.
108 m_stIMUData.imu.linear_acceleration.x = static_cast<float>(stPacket.vData[0]);
109 m_stIMUData.imu.linear_acceleration.y = static_cast<float>(stPacket.vData[1]);
110 m_stIMUData.imu.linear_acceleration.z = static_cast<float>(stPacket.vData[2]);
111 m_stIMUData.imu.angular_velocity.x = static_cast<float>(stPacket.vData[3]);
112 m_stIMUData.imu.angular_velocity.y = static_cast<float>(stPacket.vData[4]);
113 m_stIMUData.imu.angular_velocity.z = static_cast<float>(stPacket.vData[5]);
114
115 // Manually calculate the Gyro pose using the Tait-Bryan angles (ZYX convention) and the quaternion representation.
116 // This is because the SIM does not provide orientation data from the IMU, only angular velocity.
117 double dQx = stPacket.vData[6];
118 double dQy = stPacket.vData[7];
119 double dQz = stPacket.vData[8];
120 double dQw = stPacket.vData[9];
121 double dRoll = std::atan2(2.0 * (dQw * dQx + dQy * dQz), 1.0 - 2.0 * (dQx * dQx + dQy * dQy));
122 double dPitch = std::asin(2.0 * (dQw * dQy - dQz * dQx));
123 double dYaw = std::atan2(2.0 * (dQw * dQz + dQx * dQy), 1.0 - 2.0 * (dQy * dQy + dQz * dQz));
124 // Pack the gyro values into a sl::Transform.
125 sl::float3 slEulerAngles(static_cast<float>(dRoll), static_cast<float>(dPitch), static_cast<float>(dYaw));
126 sl::Transform slIMUTransform;
127 slIMUTransform.setEulerAngles(slEulerAngles);
128 m_stIMUData.imu.pose = slIMUTransform;
129
130 // Unlock mutex.
131 lkSensorsProcessLock.unlock();
132
133 // Submit logger message.
134 LOG_DEBUG(logging::g_qSharedLogger,
135 "Incoming IMU data processed from RoveComm for SIM ZED Camera: (AccelX {}, AccelY {}, AccelZ {}, GyroX {}, GyroY {}, GyroZ {})",
136 stPacket.vData[0],
137 stPacket.vData[1],
138 stPacket.vData[2],
139 stPacket.vData[3],
140 stPacket.vData[4],
141 stPacket.vData[5]);
142 };
143
145 // Declare private member variables.
147
148 // ZED Camera specific.
149
150 std::string m_szCameraPath;
151 std::atomic<bool> m_bCameraPositionalTrackingEnabled;
152
153 // Simulated IMU Data from the SIM.
154 sl::SensorsData m_stIMUData;
155
156 // WebRTC connections for each camera stream from the RoveSoSimulator.
157
158 std::unique_ptr<WebRTC> m_pRGBStream;
159 std::unique_ptr<WebRTC> m_pDepthImageStream;
160
161 // Pose tracking offsets. (ZEDSDK is broken and can't handle large translations internally as it uses float32.)
162
163 double m_dPoseOffsetX;
164 double m_dPoseOffsetY;
165 double m_dPoseOffsetZ;
166 double m_dPoseOffsetXO;
167 double m_dPoseOffsetYO;
168 double m_dPoseOffsetZO;
169
170 // Data from NavBoard.
171
172 geoops::RoverPose m_stCurrentRoverPose;
173 std::shared_mutex m_muCurrentRoverPoseMutex;
174
175 // Mats for storing frames.
176
177 cv::Mat m_cvFrame;
178 cv::Mat m_cvDepthImageBuffer;
179 cv::Mat m_cvDepthImage;
180 cv::Mat m_cvDepthMeasure;
181 cv::Mat m_cvPointCloud;
182
183 std::queue<containers::DataFetchContainer<Pose>> m_qPoseCopySchedule;
184 std::queue<containers::DataFetchContainer<sl::GeoPose>> m_qGeoPoseCopySchedule;
185 std::queue<containers::DataFetchContainer<sl::SensorsData>> m_qSensorsCopySchedule;
186
187 // Mutexes for copying frames from the WebRTC connection to the OpenCV Mats.
188
189 std::shared_mutex m_muWebRTCRGBImageCopyMutex;
190 std::shared_mutex m_muWebRTCDepthImageCopyMutex;
191
192 // Mutexes for copying frames from the ZEDSDK to the OpenCV Mats in PoolLinearCode.
193 std::shared_mutex m_muPoseCopyMutex;
194 std::shared_mutex m_muGeoPoseCopyMutex;
195 std::shared_mutex m_muSensorsCopyMutex;
196
197 // Atomic flags for checking if data is queued.
198
199 std::atomic<bool> m_bPosesQueued;
200 std::atomic<bool> m_bGeoPosesQueued;
201 std::atomic<bool> m_bSensorsQueued;
202};
203#endif
This class implements and interfaces with the SIM cameras and data. It is designed in such a way that...
Definition SIMZEDCam.h:36
void CalculatePointCloud(const cv::Mat &cvDepthMeasure, cv::Mat &cvPointCloud)
This method calculates a point cloud from the decoded depth measure use some simple trig and the came...
Definition SIMZEDCam.cpp:222
void EstimateDepthMeasure(const cv::Mat &cvDepthImage, cv::Mat &cvDepthMeasure)
This method estimates the depth measure from the depth image.
Definition SIMZEDCam.cpp:175
std::future< bool > RequestDepthCopy(cv::Mat &cvDepth, const bool bRetrieveMeasure=true)
Requests a depth measure or image from the camera. Puts a frame pointer into a queue so a copy of a f...
Definition SIMZEDCam.cpp:564
~SIMZEDCam()
Destroy the SIM Cam:: SIM Cam object.
Definition SIMZEDCam.cpp:111
sl::FUSION_ERROR_CODE SubscribeFusionToCameraUUID(sl::CameraIdentifier &slCameraUUID) override
This method is used to subscribe the sl::Fusion object to the camera with the given UUID....
Definition SIMZEDCam.cpp:818
void ThreadedContinuousCode() override
The code inside this private method runs in a separate thread, but still has access to this*....
Definition SIMZEDCam.cpp:274
std::future< bool > RequestFusionGeoPoseCopy(sl::GeoPose &slGeoPose) override
Puts a sl::GeoPose pointer into a queue so a copy of a GeoPose from the camera can be written to it.
Definition SIMZEDCam.cpp:675
sl::ERROR_CODE RebootCamera() override
This method is used to reboot the camera. This method will stop the camera thread,...
Definition SIMZEDCam.cpp:784
sl::ERROR_CODE EnablePositionalTracking(const float fExpectedCameraHeightFromFloorTolerance=constants::ZED_DEFAULT_FLOOR_PLANE_ERROR) override
This method is used to enable positional tracking on the camera. Since this is a simulation camera,...
Definition SIMZEDCam.cpp:851
std::future< bool > RequestFrameCopy(cv::Mat &cvFrame) override
Puts a frame pointer into a queue so a copy of a frame from the camera can be written to it....
Definition SIMZEDCam.cpp:533
void SetCallbacks()
This method sets the callbacks for the WebRTC connections.
Definition SIMZEDCam.cpp:135
std::string GetCameraModel() override
Accessor for the name of this model of camera.
Definition SIMZEDCam.cpp:940
sl::CameraIdentifier PublishCameraToFusion() override
This method is used to publish the camera to the sl::Fusion object. Since this is a simulation camera...
Definition SIMZEDCam.cpp:835
std::future< bool > RequestPositionalPoseCopy(Pose &stPose) override
Puts a sl::GeoPose pointer into a queue so a copy of a GeoPose from the camera can be written to it.
Definition SIMZEDCam.cpp:625
std::future< bool > RequestPointCloudCopy(cv::Mat &cvPointCloud)
Requests a point cloud image from the camera. This image has the same resolution as a normal image bu...
Definition SIMZEDCam.cpp:600
sl::ERROR_CODE ResetPositionalTracking() override
This method is used to reset the positional tracking of the camera. Because this is a simulation came...
Definition SIMZEDCam.cpp:759
bool GetUsingGPUMem() const override
Returns if the camera is using GPU memory. This is a simulation camera, so this method will always re...
Definition SIMZEDCam.cpp:927
const std::function< void(const rovecomm::RoveCommPacket< double > &, const sockaddr_in &)> ProcessIMUData
Callback function to process incoming IMU data from RoveComm for the SIM ZED Camera....
Definition SIMZEDCam.h:99
bool GetPositionalTrackingEnabled() override
Accessor for the if the camera's positional tracking is enabled. Since this is a simulation camera,...
Definition SIMZEDCam.cpp:955
void SetPositionalPose(const double dX, const double dY, const double dZ, const double dXO, const double dYO, const double dZO) override
This method is used to set the positional pose of the camera. Since this is a simulation camera,...
Definition SIMZEDCam.cpp:889
bool GetCameraIsOpen() override
Accessor for the camera open status.
Definition SIMZEDCam.cpp:912
void DisablePositionalTracking() override
This method is used to disable positional tracking on the camera. Since this is a simulation camera,...
Definition SIMZEDCam.cpp:869
void PooledLinearCode() override
This method holds the code that is ran in the thread pool started by the ThreadedLinearCode() method....
Definition SIMZEDCam.cpp:367
std::future< bool > RequestSensorsCopy(sl::SensorsData &slSensorsData) override
Requests a copy of the sensors data from the camera.
Definition SIMZEDCam.cpp:726
This class serves as a middle inheritor between the Camera interface and the ZEDCam class....
Definition ZEDCamera.hpp:33
This struct is used within the ZEDCam class to store the camera pose with high precision....
Definition ZEDCamera.hpp:77
This struct is used by the WaypointHandler to provide an easy way to store all pose data about the ro...
Definition GeospatialOperations.hpp:708