FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
receiver.cpp
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.cpp
10 * @brief Implementation of the Receiver class.
11 */
12
13#include "receiver.h"
14
15#include <algorithm>
16#include <utility>
17
18#include "core/parameters.h"
19#include "serial/response.h"
20
21namespace radar
22{
23 Receiver::Receiver(Platform* platform, std::string name, const unsigned seed, const OperationMode mode,
24 const SimId id) noexcept :
25 Radar(platform, std::move(name), id == 0 ? SimIdGenerator::instance().generateId(ObjectType::Receiver) : id),
26 _mode(mode), _rng(seed)
27 {
28 }
29
30 void Receiver::addResponseToInbox(std::unique_ptr<serial::Response> response) noexcept
31 {
32 std::scoped_lock lock(_inbox_mutex);
33 _inbox.push_back(std::move(response));
34 }
35
36 void Receiver::addInterferenceToLog(std::unique_ptr<serial::Response> response) noexcept
37 {
38 std::scoped_lock lock(_interference_log_mutex);
39 _pulsed_interference_log.push_back(std::move(response));
40 }
41
42 std::vector<std::unique_ptr<serial::Response>> Receiver::drainInbox() noexcept
43 {
44 std::scoped_lock lock(_inbox_mutex);
45 std::vector<std::unique_ptr<serial::Response>> drained_responses;
46 drained_responses.swap(_inbox);
47 return drained_responses;
48 }
49
51 {
52 {
53 std::scoped_lock lock(_finalizer_queue_mutex);
54 _finalizer_queue.push(std::move(job));
55 }
56 _finalizer_queue_cv.notify_one();
57 }
58
60 {
61 std::unique_lock lock(_finalizer_queue_mutex);
62 _finalizer_queue_cv.wait(lock, [this] { return !_finalizer_queue.empty(); });
63
64 job = std::move(_finalizer_queue.front());
65 _finalizer_queue.pop();
66
67 // Check for shutdown signal (negative duration)
68 if (job.duration < 0.0)
69 {
70 return false; // Shutdown signal
71 }
72 return true;
73 }
74
76 {
77 return _noise_temperature + Radar::getNoiseTemperature(angle);
78 }
79
81 {
82 if (temp < -EPSILON)
83 {
84 LOG(logging::Level::FATAL, "Noise temperature for receiver {} is negative", getName());
85 throw std::runtime_error("Noise temperature must be positive");
86 }
87 _noise_temperature = temp;
88 }
89
90 void Receiver::setWindowProperties(const RealType length, const RealType prf, const RealType skip) noexcept
91 {
92 const auto rate = params::rate() * params::oversampleRatio();
93 _window_length = length;
94 _window_prf = 1 / (std::floor(rate / prf) / rate);
95 _window_skip = std::floor(rate * skip) / rate;
96 }
97
98 unsigned Receiver::getWindowCount() const noexcept
99 {
100 const RealType time = params::endTime() - params::startTime();
101 const RealType pulses = time * _window_prf;
102 return static_cast<unsigned>(std::ceil(pulses));
103 }
104
105 RealType Receiver::getWindowStart(const unsigned window) const
106 {
107 const RealType stime = static_cast<RealType>(window) / _window_prf + _window_skip;
108 if (!_timing)
109 {
110 LOG(logging::Level::FATAL, "Receiver must be associated with timing source");
111 throw std::logic_error("Receiver must be associated with timing source");
112 }
113 return stime;
114 }
115
116 void Receiver::prepareCwData(const size_t numSamples)
117 {
118 std::scoped_lock lock(_cw_mutex);
119 _cw_iq_data.resize(numSamples);
120 }
121
122 void Receiver::setCwSample(const size_t index, const ComplexType sample)
123 {
124 if (index < _cw_iq_data.size())
125 {
126 _cw_iq_data[index] += sample;
127 }
128 }
129
130 void Receiver::setSchedule(std::vector<SchedulePeriod> schedule) { _schedule = std::move(schedule); }
131
132 std::optional<RealType> Receiver::getNextWindowTime(RealType time) const
133 {
134 // If no schedule is defined, assume always on.
135 if (_schedule.empty())
136 {
137 return time;
138 }
139 for (const auto& period : _schedule)
140 {
141 // If time is within this period, it's valid.
142 if (time >= period.start && time <= period.end)
143 {
144 return time;
145 }
146 // If time is before this period, skip to the start of this period.
147 if (time < period.start)
148 {
149 return period.start;
150 }
151 // If time is after this period, continue to next period.
152 }
153 // Time is after the last scheduled period.
154 return std::nullopt;
155 }
156}
static SimIdGenerator & instance()
Get the singleton instance of SimIdGenerator.
Definition sim_id.h:48
A class representing a vector in spherical coordinates.
const std::string & getName() const noexcept
Retrieves the name of the object.
Definition object.h:79
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
std::shared_ptr< timing::Timing > _timing
Timing source for the radar.
Definition radar_obj.h:141
virtual RealType getNoiseTemperature(const math::SVec3 &angle) const noexcept
Gets the noise temperature of the radar.
Definition radar_obj.cpp:31
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::vector< std::unique_ptr< serial::Response > > drainInbox() noexcept
Moves all responses from the inbox into a RenderingJob.
Definition receiver.cpp:42
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
std::optional< RealType > getNextWindowTime(RealType time) const
Determines the next valid window start time at or after the given time.
Definition receiver.cpp:132
void prepareCwData(size_t numSamples)
Prepares the internal storage for CW IQ data.
Definition receiver.cpp:116
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
RealType getNoiseTemperature() const noexcept
Retrieves the noise temperature of the receiver.
Definition receiver.h:102
bool waitAndDequeueFinalizerJob(core::RenderingJob &job)
Waits for and dequeues a RenderingJob from the finalizer queue.
Definition receiver.cpp:59
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
void setNoiseTemperature(RealType temp)
Sets the noise temperature of the receiver.
Definition receiver.cpp:80
Receiver(Platform *platform, std::string name, unsigned seed, OperationMode mode, const SimId id=0) noexcept
Constructs a Receiver object.
Definition receiver.cpp:23
double RealType
Type for real numbers.
Definition config.h:27
constexpr RealType EPSILON
Machine epsilon for real numbers.
Definition config.h:51
std::complex< RealType > ComplexType
Type for complex numbers.
Definition config.h:35
#define LOG(level,...)
Definition logging.h:19
@ FATAL
Fatal level for severe error events.
RealType endTime() noexcept
Get the end time for the simulation.
Definition parameters.h:109
RealType rate() noexcept
Get the rendering sample rate.
Definition parameters.h:121
RealType startTime() noexcept
Get the start time for the simulation.
Definition parameters.h:103
unsigned oversampleRatio() noexcept
Get the oversampling ratio.
Definition parameters.h:151
OperationMode
Defines the operational mode of a radar component.
Definition radar_obj.h:37
Defines the Parameters struct and provides methods for managing simulation parameters.
Radar Receiver class for managing signal reception and response handling.
Classes for managing radar signal responses.
uint64_t SimId
64-bit Unique Simulation ID.
Definition sim_id.h:18
A data packet containing all information needed to process one receive window.
RealType duration
The duration of the receive window in seconds.