19 #ifdef TGT_OS_TYPE_VXWORKS 20 #define M_PI (22.0 / 7.0) 30 : SignalGenComponentBase(name),
33 signalAmplitude(0.0f),
36 sigType(SignalType::SINE),
41 m_dpInProgress(false),
53 F32 SignalGen::generateSample(U32 ticks) {
59 F32 samplesPerPeriod =
static_cast<F32>(this->sampleFrequency) / static_cast<F32>(this->signalFrequency);
60 U32 halfSamplesPerPeriod = samplesPerPeriod / 2;
62 if (halfSamplesPerPeriod == 0) {
66 switch (this->sigType.e) {
67 case SignalType::TRIANGLE: {
68 F32 m = this->signalAmplitude /
static_cast<F32>(halfSamplesPerPeriod);
69 val = m *
static_cast<F32>(ticks % halfSamplesPerPeriod);
72 case SignalType::SINE: {
73 F32 normalizedFrequency = 1.0f / samplesPerPeriod;
74 val = this->signalAmplitude * std::sin((2.0 * M_PI * normalizedFrequency * static_cast<F32>(ticks)) +
75 (this->signalPhase * 2.0 * M_PI));
78 case SignalType::SQUARE: {
79 val = this->signalAmplitude *
80 ((ticks %
static_cast<U32
>(samplesPerPeriod) < halfSamplesPerPeriod) ? 1.0f : -1.0f);
83 case SignalType::NOISE: {
84 val = this->signalAmplitude * (std::rand() /
static_cast<double>(RAND_MAX));
93 void SignalGen::schedIn_handler(
FwIndexType portNum,
102 if (not this->running) {
106 if (not this->skipOne) {
107 value = this->generateSample(this->ticks);
109 this->skipOne =
false;
112 SignalPair pair = SignalPair(this->ticks, value);
115 for (U32 i = 1; i < this->sigHistory.SIZE; i++) {
116 this->sigHistory[i - 1] = this->sigHistory[i];
117 this->sigPairHistory[i - 1] = this->sigPairHistory[i];
119 this->sigHistory[this->sigHistory.SIZE - 1] = value;
120 this->sigPairHistory[this->sigPairHistory.SIZE - 1] = pair;
123 SignalInfo sigInfo(this->sigType, this->sigHistory, this->sigPairHistory);
126 this->tlmWrite_Type(this->sigType);
127 this->tlmWrite_Output(value);
128 this->tlmWrite_PairOutput(pair);
129 this->tlmWrite_History(this->sigHistory);
130 this->tlmWrite_PairHistory(this->sigPairHistory);
131 this->tlmWrite_Info(sigInfo);
134 if (this->m_dpInProgress) {
137 this->m_dpBytes += SignalInfo::SERIALIZED_SIZE;
140 this->log_WARNING_LO_DpRecordFull(this->m_currDp, this->m_dpBytes);
141 this->cleanupAndSendDp();
142 }
else if (this->m_currDp == this->m_numDps) {
143 this->log_ACTIVITY_LO_DpComplete(this->m_numDps, this->m_dpBytes);
144 this->cleanupAndSendDp();
147 this->tlmWrite_DpBytes(this->m_dpBytes);
148 this->tlmWrite_DpRecords(this->m_currDp);
154 void SignalGen::Settings_cmdHandler(
FwOpcodeType opCode,
159 const Ref::SignalType& SigType) {
163 if ((Frequency == 0) ||
164 (static_cast<U32>((static_cast<F32>(this->sampleFrequency) / static_cast<F32>(Frequency)) / 2) == 0)) {
168 this->signalFrequency = Frequency;
169 this->signalAmplitude = Amplitude;
170 this->signalPhase = Phase;
171 this->sigType = SigType;
174 for (U32 i = 0; i < SignalSet::SIZE; i++) {
175 this->sigHistory[i] = 0.0f;
177 for (U32 i = 0; i < SignalPairSet::SIZE; i++) {
178 this->sigPairHistory[i].set_time(0.0f);
179 this->sigPairHistory[i].set_value(0.0f);
181 this->log_ACTIVITY_LO_SettingsChanged(this->signalFrequency, this->signalAmplitude, this->signalPhase,
183 this->tlmWrite_Type(SigType);
190 this->running = !this->running;
198 this->skipOne =
true;
204 const Ref::SignalGen_DpReqType& reqType,
209 this->log_WARNING_HI_InSufficientDpRecords();
215 if (not this->isConnected_productGetOut_OutputPort(0)) {
216 this->log_WARNING_HI_DpsNotConnected();
223 FwSizeType dpSize = records * SIZE_OF_DataRecord_RECORD;
224 this->m_numDps = records;
227 this->log_ACTIVITY_LO_DpMemRequested(dpSize);
228 if (Ref::SignalGen_DpReqType::IMMEDIATE == reqType) {
229 Fw::Success stat = this->dpGet_DataContainer(dpSize, this->m_dpContainer);
232 this->log_WARNING_HI_DpMemoryFail();
235 this->m_dpInProgress =
true;
236 this->log_ACTIVITY_LO_DpStarted(records);
237 this->log_ACTIVITY_LO_DpMemReceived(this->m_dpContainer.getBuffer().getSize());
240 this->m_dpContainer.setPriority(this->m_dpPriority);
242 }
else if (Ref::SignalGen_DpReqType::ASYNC == reqType) {
243 this->dpRequest_DataContainer(dpSize);
251 void SignalGen::cleanupAndSendDp() {
252 this->dpSend(this->m_dpContainer);
253 this->m_dpInProgress =
false;
263 void SignalGen ::dpRecv_DataContainer_handler(DpContainer& container,
Fw::Success::T status) {
266 this->m_dpContainer = container;
267 this->m_dpInProgress =
true;
269 this->m_dpContainer.setPriority(this->m_dpPriority);
270 this->log_ACTIVITY_LO_DpStarted(this->m_numDps);
272 this->log_WARNING_HI_DpMemoryFail();
274 this->m_dpInProgress =
false;
FwIdType FwOpcodeType
The type of a command opcode.
PlatformSizeType FwSizeType
U32 FwDpPriorityType
The type of a data product priority.
No room left in the buffer to serialize data.
SerializeStatus
forward declaration for string
float F32
32-bit floating point
Command successfully executed.
Command had execution error.
PlatformIndexType FwIndexType
Command failed validation.
~SignalGen()
Destroy a SignalGen.
SignalGen(const char *compName)
Construct a SignalGen.