DynExp
Highly flexible laboratory automation for dynamically changing experiments.
HardwareAdapterSwabianInstrumentsPulseStreamer.h
Go to the documentation of this file.
1 // This file is part of DynExp.
2 
9 #pragma once
10 
11 #include "stdafx.h"
12 #include "HardwareAdaptergRPC.h"
13 
14 #include "pulse_streamer.pb.h"
15 #include "pulse_streamer.grpc.pb.h"
16 
17 namespace DynExpHardware
18 {
19  class SIPulseStreamerHardwareAdapter;
20 
21  class SIPulseStreamerHardwareAdapterParams : public gRPCHardwareAdapterParams<pulse_streamer::PulseStreamer>
22  {
23  public:
24  // No enum class to be usable with DynExp::ParamsBase::Param<>.
25  enum OutputChannelType : uint8_t { DO0 = 0, DO1, DO2, DO3, DO4, DO5, DO6, DO7, AO0, AO1 };
28 
32 
35 
36  virtual const char* GetParamClassTag() const noexcept override { return "SIPulseStreamerParams"; }
37 
39  "Trigger Edge", "Signal edge or event which triggers the pulse streamer.",
40  true, TriggerEdgeType::Immediate };
42  "Trigger Mode", "Determines whether the puslse streamer waits for subsequent trigger events (normal) or only for one (single).",
43  true, TriggerModeType::Normal };
44  Param<ParamsConfigDialog::NumberType> NumRuns = { *this, "NumRuns", "Number of Runs",
45  "Determines how often the stored pulse sequence should be repeated after triggering. -1 means indefinitely.",
46  true, 1, -1 };
47 
48  private:
51  };
52 
54  {
55  public:
58 
61 
62  private:
63  virtual DynExp::ParamsBasePtrType MakeParams(DynExp::ItemIDType ID, const DynExp::DynExpCore& Core) const override { return DynExp::MakeParams<SIPulseStreamerHardwareAdapterConfigurator>(ID, Core); }
64  };
65 
66  class SIPulseStreamerHardwareAdapter : public gRPCHardwareAdapter<pulse_streamer::PulseStreamer>
67  {
68  public:
72  struct PulseType
73  {
74  uint32_t ticks = 0;
75  uint8_t digi = 0;
76  int16_t ao0 = 0;
77  int16_t ao1 = 0;
78 
79  std::unique_ptr<pulse_streamer::PulseMessage> ToPulseMessage() const;
80  };
81 
85  struct SampleType
86  {
87  static int16_t MakeValueFromVoltage(const double Voltage);
88 
90  std::chrono::nanoseconds Timestamp;
91  int16_t Value;
92 
93  uint8_t ComposeDOValue() const;
94  };
95 
98 
99  constexpr static auto Name() noexcept { return "Swabian Instruments Pulse Streamer (gRPC)"; }
100  constexpr static auto Category() noexcept { return "I/O"; }
101 
104 
105  virtual std::string GetName() const override { return Name(); }
106  virtual std::string GetCategory() const override { return Category(); }
107 
108  void ResetDevice() const;
109  void SetConstantOutput(const PulseType& Pulse = {}) const;
110  void ForceFinalSample() const;
111  void SetSamples(SIPulseStreamerHardwareAdapterParams::OutputChannelType OutputChannel, const std::vector<SampleType>& NewSamples) const;
112  void SetNumRuns(int64_t NumRuns = -1) const;
114  SIPulseStreamerHardwareAdapterParams::TriggerModeType TriggerMode = SIPulseStreamerHardwareAdapterParams::TriggerModeType::Normal) const;
115  void ForceTrigger() const;
116  void RearmTrigger() const;
117 
118  bool IsStreaming() const;
119  bool HasSequence() const;
120  bool HasFinished() const;
121 
122  private:
123  template <typename MessageType>
124  using StubFuncPtrType = grpc::Status(pulse_streamer::PulseStreamer::Stub::*)(grpc::ClientContext*, const MessageType&, pulse_streamer::PulseStreamerReply*);
125 
126  void ResetImpl(dispatch_tag<gRPCHardwareAdapter>) override final;
128 
129  template <typename MessageType>
130  uint32_t InvokeStubFunc(StubFuncPtrType<MessageType> Func, const MessageType& Message) const;
131 
132  uint32_t InvokeStubFunc(StubFuncPtrType<pulse_streamer::VoidMessage> Func) const { return InvokeStubFunc(Func, pulse_streamer::VoidMessage()); }
133 
134  std::vector<PulseType> ComposePulseSequence() const;
135 
136  virtual void OpenUnsafeChild() override;
137 
138  void ResetDeviceUnsafe() const;
139  void SetConstantOutputUnsafe(const PulseType& Pulse = {}) const;
140  void ForceFinalSampleUnsafe() const;
141  void SetSamplesUnsafe(SIPulseStreamerHardwareAdapterParams::OutputChannelType OutputChannel, const std::vector<SampleType>& NewSamples) const;
142  void SetNumRunsUnsafe(int64_t NumRuns = -1) const;
144  SIPulseStreamerHardwareAdapterParams::TriggerModeType TriggerMode = SIPulseStreamerHardwareAdapterParams::TriggerModeType::Normal) const;
145  void ForceTriggerUnsafe() const;
146  void RearmTriggerUnsafe() const;
147 
148  bool IsStreamingUnsafe() const;
149  bool HasSequenceUnsafe() const;
150  bool HasFinishedUnsafe() const;
151 
152  mutable std::vector<SampleType> Samples;
153  mutable int64_t NumRuns;
154  };
155 
156  template <typename MessageType>
157  uint32_t SIPulseStreamerHardwareAdapter::InvokeStubFunc(StubFuncPtrType<MessageType> Func, const MessageType& Message) const
158  {
159  grpc::ClientContext Context;
160  pulse_streamer::PulseStreamerReply Reply;
161 
162  static const auto Timeout = std::chrono::milliseconds(1000);
163  Context.set_deadline(std::chrono::system_clock::now() + Timeout);
164 
165  auto Result = (GetStubUnsafe().*Func)(&Context, Message, &Reply);
166  CheckError(Result);
167 
168  return Reply.value();
169  }
170 
172 }
Implementation of a hardware adapter to communicate over TCP sockets using gRPC.
virtual DynExp::ParamsBasePtrType MakeParams(DynExp::ItemIDType ID, const DynExp::DynExpCore &Core) const override
Override to make derived classes call DynExp::MakeParams with the correct configurator type derived f...
static Util::TextValueListType< OutputChannelType > OutputChannelTypeStrList()
SIPulseStreamerHardwareAdapterParams(DynExp::ItemIDType ID, const DynExp::DynExpCore &Core)
virtual void ConfigureParamsImpl(dispatch_tag< SIPulseStreamerHardwareAdapterParams >)
static Util::TextValueListType< TriggerEdgeType > TriggerEdgeTypeStrList()
static Util::TextValueListType< TriggerModeType > TriggerModeTypeStrList()
void ConfigureParamsImpl(dispatch_tag< gRPCHardwareAdapterParams >) override final
virtual const char * GetParamClassTag() const noexcept override
This function is intended to be overridden once in each derived class returning the name of the respe...
void SetTrigger(SIPulseStreamerHardwareAdapterParams::TriggerEdgeType TriggerEdge, SIPulseStreamerHardwareAdapterParams::TriggerModeType TriggerMode=SIPulseStreamerHardwareAdapterParams::TriggerModeType::Normal) const
uint32_t InvokeStubFunc(StubFuncPtrType< pulse_streamer::VoidMessage > Func) const
virtual void ResetImpl(dispatch_tag< SIPulseStreamerHardwareAdapter >)
virtual void OpenUnsafeChild() override
Override to add additional initialization steps. Gets executed after the gRPC connection has been est...
void SetSamples(SIPulseStreamerHardwareAdapterParams::OutputChannelType OutputChannel, const std::vector< SampleType > &NewSamples) const
std::vector< SampleType > Samples
Combined sample vector of all channels. Assumed to be always sorted.
void SetSamplesUnsafe(SIPulseStreamerHardwareAdapterParams::OutputChannelType OutputChannel, const std::vector< SampleType > &NewSamples) const
virtual std::string GetName() const override
Returns the name of this Object type.
SIPulseStreamerHardwareAdapter(const std::thread::id OwnerThreadID, DynExp::ParamsBasePtrType &&Params)
int64_t NumRuns
How often to repeat the sample sequence. -1 means indefinitely.
void SetTriggerUnsafe(SIPulseStreamerHardwareAdapterParams::TriggerEdgeType TriggerEdge, SIPulseStreamerHardwareAdapterParams::TriggerModeType TriggerMode=SIPulseStreamerHardwareAdapterParams::TriggerModeType::Normal) const
void ResetImpl(dispatch_tag< gRPCHardwareAdapter >) override final
virtual std::string GetCategory() const override
Returns the category of this Object type.
uint32_t InvokeStubFunc(StubFuncPtrType< MessageType > Func, const MessageType &Message) const
grpc::Status(pulse_streamer::PulseStreamer::Stub::*)(grpc::ClientContext *, const MessageType &, pulse_streamer::PulseStreamerReply *) StubFuncPtrType
Configurator class for gRPCHardwareAdapter.
Parameter class for gRPCHardwareAdapter.
This template class provides basic functionality to design hardware adapters for instruments which co...
void CheckError(const grpc::Status Result, const std::source_location Location=std::source_location::current()) const
Checks whether Result denotes an error state. If this is the case, a respective gRPCException is cons...
gRPCStub::Stub & GetStubUnsafe() const
Getter for the gRPC stub.
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Definition: DynExpCore.h:127
Defines the base class for a hardware adapter object. Hardware adapters describe interfaces/connectio...
Configurator class for HardwareAdapterBase.
Parameter class for HardwareAdapterBase.
const std::thread::id OwnerThreadID
Thread id of the thread which has constructed (and owns) this Object instance.
Definition: Object.h:2302
const ParamsBasePtrType Params
Pointer to the parameter class instance belonging to this Object instance.
Definition: Object.h:2303
Refer to ParamsBase::dispatch_tag.
Definition: Object.h:2018
const ItemIDType ID
ID of the Object this parameter class instance belongs to.
Definition: Object.h:1779
const DynExpCore & Core
Reference to DynExp's core.
Definition: Object.h:1780
Tag for function dispatching mechanism within this class used when derived classes are not intended t...
Definition: Object.h:349
DynExp's hardware namespace contains the implementation of DynExp hardware adapters which extend DynE...
std::strong_ordering operator<=>(const SIPulseStreamerHardwareAdapter::SampleType &lhs, const SIPulseStreamerHardwareAdapter::SampleType &rhs)
@ Pulse
Manually defined pulses.
std::unique_ptr< ParamsBase > ParamsBasePtrType
Alias for a pointer to the parameter system base class ParamsBase.
Definition: Object.h:1807
size_t ItemIDType
ID type of objects/items managed by DynExp.
std::vector< std::pair< TextType, ValueType > > TextValueListType
Type of a list containing key-value pairs where key is a text of type Util::TextType.
Definition: QtUtil.h:37
Accumulates include statements to provide a precompiled header.
Swabian Instruments Pulse Streamer 8/2's internal representation of a single pulse.
int16_t ao1
Analog out channel 1 (-0x7FFF is -1.0V, 0x7FFF is 1.0V)
uint8_t digi
Digital out bit mask (LSB is channel 0, MSB is channel 7)
int16_t ao0
Analog out channel 0 (-0x7FFF is -1.0V, 0x7FFF is 1.0V)