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