DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
ArbitraryFunctionFromCSV.cpp
Go to the documentation of this file.
1// This file is part of DynExp.
2
3#include "stdafx.h"
5
6namespace DynExpModule
7{
8
13
17
22
26
28 {
29 {
30 auto ModuleParams = DynExp::dynamic_Params_cast<ArbitraryFunctionFromCSV>(Instance->ParamsGetter());
31 auto ModuleData = DynExp::dynamic_ModuleData_cast<ArbitraryFunctionFromCSV>(Instance->ModuleDataGetter());
32
33 Instance->LockObject(ModuleParams->FunctionGenerator, ModuleData->FunctionGenerator);
34
35 if (ModuleParams->Communicator.ContainsID())
36 {
37 Instance->LockObject(ModuleParams->Communicator, ModuleData->Communicator);
39 }
40 } // ModuleParams and ModuleData unlocked here.
41
42 OnTrigger(Instance);
43 }
44
46 {
47 auto ModuleData = DynExp::dynamic_ModuleData_cast<ArbitraryFunctionFromCSV>(Instance->ModuleDataGetter());
48
49 ModuleData->FunctionGenerator->Stop();
50 Instance->UnlockObject(ModuleData->FunctionGenerator);
51 Instance->UnlockObject(ModuleData->Communicator);
52
54 }
55
57 {
58 std::string CSVDataPath;
59 size_t SkipLines{};
60 double TimeStretch{}, TimeOffset{}, ValueStretch{}, ValueOffset{};
61
62 {
63 auto ModuleParams = DynExp::dynamic_Params_cast<ArbitraryFunctionFromCSV>(Instance->ParamsGetter());
64
65 CSVDataPath = ModuleParams->CSVDataPath.GetPath().string();
66 SkipLines = ModuleParams->SkipLines;
67 TimeStretch = ModuleParams->TimeStretch;
68 TimeOffset = ModuleParams->TimeOffset;
69 ValueStretch = ModuleParams->ValueStretch;
70 ValueOffset = ModuleParams->ValueOffset;
71 } // ModuleParams unlocked here.
72
73 // Reading and parsing potentially very heavy. So, unlock every mutex before.
74 auto CSVData = Util::ReadFromFile(CSVDataPath);
75
76 std::vector<DynExpInstr::BasicSample> BasicSamples;
77 try
78 {
79 try
80 {
81 // Try to parse format time;value...
82 auto ParsedCSV = Util::ParseCSV<double, double>(CSVData, ';', SkipLines);
83
84 for (const auto& ParsedTuple : ParsedCSV)
85 BasicSamples.emplace_back(std::get<1>(ParsedTuple) * ValueStretch + ValueOffset, std::get<0>(ParsedTuple) * TimeStretch + TimeOffset);
86 }
87 catch ([[maybe_unused]] std::ios_base::failure& e)
88 {
89 // ...did not work. So, try to parse list of values.
90 auto ParsedCSV = Util::ParseCSV<double>(CSVData, ';', SkipLines);
91
92 for (const auto& ParsedTuple : ParsedCSV)
93 BasicSamples.emplace_back(std::get<0>(ParsedTuple) * ValueStretch + ValueOffset);
94 }
95 }
96 catch ([[maybe_unused]] std::ios_base::failure& e)
97 {
98 throw Util::InvalidDataException("Error parsing the CSV file \"" + CSVDataPath + "\".");
99 }
100
101 auto ModuleData = DynExp::dynamic_ModuleData_cast<ArbitraryFunctionFromCSV>(Instance->ModuleDataGetter());
102 ModuleData->FunctionGenerator->Clear();
103 ModuleData->FunctionGenerator->SetArbitraryFunction(std::move(BasicSamples), true);
104 }
105}
Implementation of a module to read a waveform from a CSV file and to store it in a data stream of a f...
void ResetImpl(dispatch_tag< ModuleDataBase >) override final
void ResetImpl(dispatch_tag< ModuleBase >) override final
void OnExit(DynExp::ModuleInstance *Instance) const override final
This event is triggered right before the module thread terminates (not due to an exception,...
void OnInit(DynExp::ModuleInstance *Instance) const override final
This event is triggered right before the module thread starts. Override it to lock instruments this m...
Util::DynExpErrorCodes::DynExpErrorCodes ModuleMainLoop(DynExp::ModuleInstance &Instance) override final
Module main loop. The function is executed periodically by the module thread. Also refer to GetMainLo...
void OnTrigger(DynExp::ModuleInstance *Instance) const
static void Register(const ModuleBase &Listener, CallableT EventFunc, ItemIDType CommunicatorID=ItemIDNotSet)
Registers/Subscribes module Listener to the event with the event function EventFunc....
Definition Module.h:1262
static void Deregister(const ModuleBase &Listener)
Deregisters/unsubscribes module Listener from the event, regardless of the inter-module communicator ...
Definition Module.h:1271
const std::unique_ptr< ModuleDataType > ModuleData
Module data belonging to this ModuleBase instance.
Definition Module.h:788
Refer to ParamsBase::dispatch_tag.
Definition Module.h:191
Defines data for a thread belonging to a ModuleBase instance. Refer to RunnableInstance.
Definition Module.h:840
const ModuleBase::ModuleDataGetterType ModuleDataGetter
Getter for module's data. Refer to ModuleBase::ModuleDataGetterType.
Definition Module.h:872
Refer to ParamsBase::dispatch_tag.
Definition Object.h:2018
const Object::ParamsGetterType ParamsGetter
Invoke to obtain the parameters (derived from ParamsBase) of Owner.
Definition Object.h:3710
void UnlockObject(LinkedObjectWrapperContainer< ObjectT > &ObjectWrapperContainer)
Unlocks an Object instance stored in the LinkedObjectWrapperContainer ObjectWrapperContainer....
Definition Object.h:3609
void LockObject(const ParamsBase::Param< ObjectLink< ObjectT > > &LinkParam, LinkedObjectWrapperContainer< ObjectT > &ObjectWrapperContainer, std::chrono::milliseconds Timeout=ObjectLinkBase::LockObjectTimeoutDefault)
Locks an Object instance referenced by a parameter LinkParam of type ParamsBase::Param< ObjectLink< O...
Definition Object.h:3593
Data to operate on is invalid for a specific purpose. This indicates a corrupted data structure or fu...
Definition Exception.h:164
DynExp's module namespace contains the implementation of DynExp modules which extend DynExp's core fu...
DynExpErrorCodes
DynExp's error codes
Definition Exception.h:22
std::string ReadFromFile(const QString &Filename)
Reads the entire content from a text file.
Definition QtUtil.cpp:250
Accumulates include statements to provide a precompiled header.