FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
receiver.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-only
2//
3// Copyright (c) 2006-2008 Marc Brooker and Michael Inggs
4// Copyright (c) 2008-present FERS Contributors (see AUTHORS.md).
5//
6// See the GNU GPLv2 LICENSE file in the FERS project root for more information.
7
8/**
9 * @file receiver.h
10 * @brief Radar Receiver class for managing signal reception and response handling.
11 */
12
13#pragma once
14
15#include <condition_variable>
16#include <mutex>
17#include <queue>
18#include <random>
19
20#include "core/rendering_job.h"
21#include "radar_obj.h"
22#include "serial/response.h"
23
24namespace pool
25{
26 class ThreadPool;
27}
28
29namespace radar
30{
31 /**
32 * @class Receiver
33 * @brief Manages radar signal reception and response processing.
34 */
35 class Receiver final : public Radar
36 {
37 public:
38 /**
39 * @enum RecvFlag
40 * @brief Enumeration for receiver configuration flags.
41 */
42 enum class RecvFlag
43 {
44 FLAG_NODIRECT = 1,
46 };
47
48 /**
49 * @brief Constructs a Receiver object.
50 *
51 * @param platform The platform associated with this receiver.
52 * @param name The name of the receiver.
53 * @param seed The seed for the receiver's internal random number generator.
54 * @param mode The operational mode (PULSED_MODE or CW_MODE).
55 */
56 explicit Receiver(Platform* platform, std::string name, unsigned seed, OperationMode mode) noexcept;
57
58 ~Receiver() override = default;
59
60 Receiver(const Receiver&) = delete;
61
62 Receiver(Receiver&&) = delete;
63
64 Receiver& operator=(const Receiver&) = delete;
65
67
68 /**
69 * @brief Adds a response to the receiver's pulsed-mode inbox.
70 * @param response A unique pointer to the response object.
71 */
72 void addResponseToInbox(std::unique_ptr<serial::Response> response) noexcept;
73
74 /**
75 * @brief Adds a pulsed interference response to the receiver's CW-mode log.
76 * @param response A unique pointer to the response object.
77 */
78 void addInterferenceToLog(std::unique_ptr<serial::Response> response) noexcept;
79
80 /**
81 * @brief Checks if a specific flag is set.
82 *
83 * @param flag The flag to check.
84 * @return True if the flag is set, false otherwise.
85 */
86 [[nodiscard]] bool checkFlag(RecvFlag flag) const noexcept { return _flags & static_cast<int>(flag); }
87
88 /**
89 * @brief Retrieves the noise temperature of the receiver.
90 *
91 * @return The noise temperature.
92 */
93 [[nodiscard]] RealType getNoiseTemperature() const noexcept { return _noise_temperature; }
94
95 /**
96 * @brief Retrieves the radar window length.
97 *
98 * @return The radar window length.
99 */
100 [[nodiscard]] RealType getWindowLength() const noexcept { return _window_length; }
101
102 /**
103 * @brief Retrieves the pulse repetition frequency (PRF) of the radar window.
104 *
105 * @return The PRF of the radar window.
106 */
107 [[nodiscard]] RealType getWindowPrf() const noexcept { return _window_prf; }
108
109 /**
110 * @brief Retrieves the window skip time.
111 *
112 * @return The window skip time.
113 */
114 [[nodiscard]] RealType getWindowSkip() const noexcept { return _window_skip; }
115
116 /**
117 * @brief Gets the noise temperature for a specific angle.
118 *
119 * @param angle The angle in spherical coordinates (SVec3).
120 * @return The noise temperature at the given angle.
121 */
122 [[nodiscard]] RealType getNoiseTemperature(const math::SVec3& angle) const noexcept override;
123
124 /**
125 * @brief Retrieves the start time of a specific radar window.
126 *
127 * @param window The index of the window.
128 * @return The start time of the specified window.
129 * @throws std::logic_error If the receiver is not associated with a timing source.
130 */
131 [[nodiscard]] RealType getWindowStart(unsigned window) const;
132
133 /**
134 * @brief Gets the number of radar windows.
135 *
136 * @return The total number of radar windows.
137 */
138 [[nodiscard]] unsigned getWindowCount() const noexcept;
139
140 /**
141 * @brief Gets the receiver's internal random number generator engine.
142 * @return A mutable reference to the RNG engine.
143 */
144 [[nodiscard]] std::mt19937& getRngEngine() noexcept { return _rng; }
145
146 /**
147 * @brief Gets the operational mode of the receiver.
148 *
149 * @return The operational mode (PULSED_MODE or CW_MODE).
150 */
151 [[nodiscard]] OperationMode getMode() const noexcept { return _mode; }
152
153 /**
154 * @brief Checks if the receiver is currently active (listening).
155 * @return True if active, false otherwise.
156 */
157 [[nodiscard]] bool isActive() const noexcept { return _is_active; }
158
159 /**
160 * @brief Sets the active state of the receiver.
161 * @param active The new active state.
162 */
163 void setActive(const bool active) noexcept { _is_active = active; }
164
165 /**
166 * @brief Moves all responses from the inbox into a RenderingJob.
167 * @return A vector of unique pointers to the responses.
168 */
169 std::vector<std::unique_ptr<serial::Response>> drainInbox() noexcept;
170
171 /**
172 * @brief Adds a completed RenderingJob to the finalizer queue.
173 * @param job The RenderingJob to enqueue.
174 */
175 void enqueueFinalizerJob(core::RenderingJob&& job);
176
177 /**
178 * @brief Waits for and dequeues a RenderingJob from the finalizer queue.
179 *
180 * This is a blocking call, intended for use by the dedicated finalizer thread.
181 *
182 * @param job A reference to a RenderingJob to be filled.
183 * @return `false` if a shutdown signal is received, `true` otherwise.
184 */
185 bool waitAndDequeueFinalizerJob(core::RenderingJob& job);
186
187 /**
188 * @brief Sets the properties for radar windows.
189 *
190 * @param length The length of the radar window.
191 * @param prf The pulse repetition frequency.
192 * @param skip The skip time between windows.
193 */
194 void setWindowProperties(RealType length, RealType prf, RealType skip) noexcept;
195
196 /**
197 * @brief Sets a receiver flag.
198 *
199 * @param flag The flag to set.
200 */
201 void setFlag(RecvFlag flag) noexcept { _flags |= static_cast<int>(flag); }
202
203 /**
204 * @brief Sets the noise temperature of the receiver.
205 *
206 * @param temp The new noise temperature.
207 * @throws std::runtime_error If the noise temperature is negative.
208 */
209 void setNoiseTemperature(RealType temp);
210
211 /**
212 * @brief Prepares the internal storage for CW IQ data.
213 * @param numSamples The total number of samples to allocate memory for.
214 */
215 void prepareCwData(size_t numSamples);
216
217 /**
218 * @brief Sets a single IQ sample at a specific index for CW simulation.
219 * @param index The index at which to store the sample.
220 * @param sample The complex IQ sample.
221 */
222 void setCwSample(size_t index, ComplexType sample);
223
224 /**
225 * @brief Retrieves the collected CW IQ data.
226 * @return A constant reference to the vector of complex IQ samples.
227 */
228 [[nodiscard]] const std::vector<ComplexType>& getCwData() const { return _cw_iq_data; }
229
230 /**
231 * @brief Retrieves the collected CW IQ data for modification.
232 * @return A mutable reference to the vector of complex IQ samples.
233 */
234 [[nodiscard]] std::vector<ComplexType>& getMutableCwData() { return _cw_iq_data; }
235
236 /**
237 * @brief Retrieves the log of pulsed interferences for CW mode.
238 * @return A const reference to the vector of interference responses.
239 */
240 [[nodiscard]] const std::vector<std::unique_ptr<serial::Response>>& getPulsedInterferenceLog() const
241 {
242 return _pulsed_interference_log;
243 }
244
245 /**
246 * @brief Sets the active schedule for the receiver.
247 * @param schedule A vector of active periods.
248 */
249 void setSchedule(std::vector<SchedulePeriod> schedule);
250
251 /**
252 * @brief Retrieves the list of active reception periods.
253 * @return A const reference to the schedule vector.
254 */
255 [[nodiscard]] const std::vector<SchedulePeriod>& getSchedule() const noexcept { return _schedule; }
256
257 /**
258 * @brief Determines the next valid window start time at or after the given time.
259 *
260 * @param time The proposed window start time.
261 * @return The actual start time, or nullopt if no valid time exists in the schedule.
262 */
263 [[nodiscard]] std::optional<RealType> getNextWindowTime(RealType time) const;
264
265 private:
266 // --- Common Members ---
267 bool _is_active = false;
268 RealType _noise_temperature = 0; ///< The noise temperature of the receiver.
269 int _flags = 0; ///< Flags for receiver configuration.
270 OperationMode _mode; ///< The operational mode of the receiver.
271 std::mt19937 _rng; ///< Per-object random number generator for statistical independence.
272 std::vector<SchedulePeriod> _schedule; ///< The schedule of active periods.
273
274 // --- Pulsed Mode Members ---
275 RealType _window_length = 0; ///< The length of the radar window.
276 RealType _window_prf = 0; ///< The pulse repetition frequency (PRF) of the radar window.
277 RealType _window_skip = 0; ///< The skip time between radar windows.
278 std::vector<std::unique_ptr<serial::Response>>
279 _inbox; /// Mailbox for incoming Response objects during a receive window.
280 std::mutex _inbox_mutex;
281 std::queue<core::RenderingJob> _finalizer_queue; /// Queue of completed windows waiting for final processing.
282 std::mutex _finalizer_queue_mutex;
283 std::condition_variable _finalizer_queue_cv;
284
285 // --- CW Mode Members ---
286 std::vector<std::unique_ptr<serial::Response>>
287 _pulsed_interference_log; /// Log of pulsed signals that interfere with CW reception.
288 std::mutex _interference_log_mutex;
289 std::vector<ComplexType> _cw_iq_data; /// Buffer for raw, simulation-long I/Q data.
290 std::mutex _cw_mutex; ///< Mutex for handling CW data.
291 };
292}
A class representing a vector in spherical coordinates.
Represents a simulation platform with motion and rotation paths.
Definition platform.h:31
Represents a radar system on a platform.
Definition radar_obj.h:46
Manages radar signal reception and response processing.
Definition receiver.h:36
void addInterferenceToLog(std::unique_ptr< serial::Response > response) noexcept
Adds a pulsed interference response to the receiver's CW-mode log.
Definition receiver.cpp:34
void setCwSample(size_t index, ComplexType sample)
Sets a single IQ sample at a specific index for CW simulation.
Definition receiver.cpp:120
std::mt19937 & getRngEngine() noexcept
Gets the receiver's internal random number generator engine.
Definition receiver.h:144
void setActive(const bool active) noexcept
Sets the active state of the receiver.
Definition receiver.h:163
bool checkFlag(RecvFlag flag) const noexcept
Checks if a specific flag is set.
Definition receiver.h:86
std::vector< std::unique_ptr< serial::Response > > drainInbox() noexcept
Moves all responses from the inbox into a RenderingJob.
Definition receiver.cpp:40
void setFlag(RecvFlag flag) noexcept
Sets a receiver flag.
Definition receiver.h:201
const std::vector< SchedulePeriod > & getSchedule() const noexcept
Retrieves the list of active reception periods.
Definition receiver.h:255
~Receiver() override=default
bool isActive() const noexcept
Checks if the receiver is currently active (listening).
Definition receiver.h:157
unsigned getWindowCount() const noexcept
Gets the number of radar windows.
Definition receiver.cpp:96
Receiver & operator=(Receiver &&)=delete
RealType getWindowStart(unsigned window) const
Retrieves the start time of a specific radar window.
Definition receiver.cpp:103
std::optional< RealType > getNextWindowTime(RealType time) const
Determines the next valid window start time at or after the given time.
Definition receiver.cpp:130
RecvFlag
Enumeration for receiver configuration flags.
Definition receiver.h:43
void prepareCwData(size_t numSamples)
Prepares the internal storage for CW IQ data.
Definition receiver.cpp:114
std::vector< ComplexType > & getMutableCwData()
Retrieves the collected CW IQ data for modification.
Definition receiver.h:234
const std::vector< std::unique_ptr< serial::Response > > & getPulsedInterferenceLog() const
Retrieves the log of pulsed interferences for CW mode.
Definition receiver.h:240
void setSchedule(std::vector< SchedulePeriod > schedule)
Sets the active schedule for the receiver.
Definition receiver.cpp:128
void enqueueFinalizerJob(core::RenderingJob &&job)
Adds a completed RenderingJob to the finalizer queue.
Definition receiver.cpp:48
Receiver & operator=(const Receiver &)=delete
RealType getNoiseTemperature() const noexcept
Retrieves the noise temperature of the receiver.
Definition receiver.h:93
OperationMode getMode() const noexcept
Gets the operational mode of the receiver.
Definition receiver.h:151
RealType getWindowPrf() const noexcept
Retrieves the pulse repetition frequency (PRF) of the radar window.
Definition receiver.h:107
bool waitAndDequeueFinalizerJob(core::RenderingJob &job)
Waits for and dequeues a RenderingJob from the finalizer queue.
Definition receiver.cpp:57
RealType getWindowSkip() const noexcept
Retrieves the window skip time.
Definition receiver.h:114
void setWindowProperties(RealType length, RealType prf, RealType skip) noexcept
Sets the properties for radar windows.
Definition receiver.cpp:88
void addResponseToInbox(std::unique_ptr< serial::Response > response) noexcept
Adds a response to the receiver's pulsed-mode inbox.
Definition receiver.cpp:28
const std::vector< ComplexType > & getCwData() const
Retrieves the collected CW IQ data.
Definition receiver.h:228
Receiver(const Receiver &)=delete
void setNoiseTemperature(RealType temp)
Sets the noise temperature of the receiver.
Definition receiver.cpp:78
RealType getWindowLength() const noexcept
Retrieves the radar window length.
Definition receiver.h:100
Receiver(Receiver &&)=delete
double RealType
Type for real numbers.
Definition config.h:27
std::complex< RealType > ComplexType
Type for complex numbers.
Definition config.h:35
OperationMode
Defines the operational mode of a radar component.
Definition radar_obj.h:36
Defines the Radar class and associated functionality.
Defines the data packet for asynchronous receiver finalization.
Classes for managing radar signal responses.