ExamplePidAdvisorΒΆ

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// ExamplePidAdvisor shows the usage of the PID advisor
public static void ExamplePidAdvisor(string dev = DEFAULT_DEVICE) // Timeout(40000)
{
  ziDotNET daq = connect(dev);
  if (!hasOption(daq, dev, "PID"))
  {
    daq.disconnect();
    Skip("Not supported by device.");
  }

  resetDeviceToDefault(daq, dev);

  daq.setInt(String.Format("/{0}/demods/*/rate", dev), 0);
  daq.setInt(String.Format("/{0}/demods/*/trigger", dev), 0);
  daq.setInt(String.Format("/{0}/sigouts/*/enables/*", dev), 0);
  daq.setInt(String.Format("/{0}/demods/*/enable", dev), 0);
  daq.setInt(String.Format("/{0}/scopes/*/enable", dev), 0);

  // now the settings relevant to this experiment
  // PID configuration.
  double target_bw = 10e3;    // Target bandwidth (Hz).
  int pid_input = 3;          // PID input (3 = Demod phase).
  int pid_input_channel = 0;  // Demodulator number.
  double setpoint = 0.0;      // Phase setpoint.
  int phase_unwrap = 1;       //
  int pid_output = 2;         // PID output (2 = oscillator frequency).
  int pid_output_channel = 0; // The index of the oscillator controlled by PID.
  double pid_center_frequency = 500e3;  // (Hz).
  double pid_limits = 10e3;            // (Hz).


  if (!isDeviceFamily(daq, dev, "HF2"))
  {
    daq.setInt(String.Format("/{0}/pids/0/input", dev), pid_input);
    daq.setInt(String.Format("/{0}/pids/0/inputchannel", dev), pid_input_channel);
    daq.setDouble(String.Format("/{0}/pids/0/setpoint", dev), setpoint);
    daq.setInt(String.Format("/{0}/pids/0/output", dev), pid_output);
    daq.setInt(String.Format("/{0}/pids/0/outputchannel", dev), pid_output_channel);
    daq.setDouble(String.Format("/{0}/pids/0/center", dev), pid_center_frequency);
    daq.setInt(String.Format("/{0}/pids/0/enable", dev), 0);
    daq.setInt(String.Format("/{0}/pids/0/phaseunwrap", dev), phase_unwrap);
    daq.setDouble(String.Format("/{0}/pids/0/limitlower", dev), -pid_limits);
    daq.setDouble(String.Format("/{0}/pids/0/limitupper", dev), pid_limits);
  }
  // Perform a global synchronisation between the device and the data server:
  // Ensure that the settings have taken effect on the device before starting
  // the pidAdvisor.
  daq.sync();

  // set up PID Advisor
  ziModule pidAdvisor = daq.pidAdvisor();

  // Turn off auto-calc on param change. Enabled
  // auto calculation can be used to automatically
  // update response data based on user input.
  pidAdvisor.setInt("auto", 0);
  pidAdvisor.setByte("device", dev);
  pidAdvisor.setDouble("pid/targetbw", target_bw);

  // PID advising mode (bit coded)
  // bit 0: optimize/tune P
  // bit 1: optimize/tune I
  // bit 2: optimize/tune D
  // Example: mode = 7: Optimize/tune PID
  pidAdvisor.setInt("pid/mode", 7);

  // PID index to use (first PID of device: 0)
  pidAdvisor.setInt("index", 0);

  // DUT model
  // source = 1: Lowpass first order
  // source = 2: Lowpass second order
  // source = 3: Resonator frequency
  // source = 4: Internal PLL
  // source = 5: VCO
  // source = 6: Resonator amplitude
  pidAdvisor.setInt("dut/source", 4);

  if (isDeviceFamily(daq, dev, "HF2"))
  {
    // Since the PLL and PID are 2 separate hardware units on the
    // device, we need to additionally specify that the PID
    // Advisor should model the HF2's PLL.
    pidAdvisor.setByte("pid/type", "pll");
  }

  // IO Delay of the feedback system describing the earliest response
  // for a step change. This parameter does not affect the shape of
  // the DUT transfer function
  pidAdvisor.setDouble("dut/delay", 0.0);

  // Other DUT parameters (not required for the internal PLL model)
  // pidAdvisor.setDouble('dut/gain', 1.0)
  // pidAdvisor.setDouble('dut/bw', 1000)
  // pidAdvisor.setDouble('dut/fcenter', 15e6)
  // pidAdvisor.setDouble('dut/damping', 0.1)
  // pidAdvisor.setDouble('dut/q', 10e3)

  // Start values for the PID optimization. Zero
  // values will imitate a guess. Other values can be
  // used as hints for the optimization process.
  pidAdvisor.setDouble("pid/p", 0);
  pidAdvisor.setDouble("pid/i", 0);
  pidAdvisor.setDouble("pid/d", 0);
  pidAdvisor.setInt("calculate", 0);

  // Start the module thread
  pidAdvisor.execute();
  System.Threading.Thread.Sleep(1000);

  // Advise
  pidAdvisor.setInt("calculate", 1);
  System.Diagnostics.Trace.WriteLine(
    "Starting advising. Optimization process may run up to a minute...");

  var watch = System.Diagnostics.Stopwatch.StartNew();
  while (true)
  {
    double progress = pidAdvisor.progress() * 100;
    System.Diagnostics.Trace.WriteLine(progress, "Progress");
    System.Threading.Thread.Sleep(1000);
    Int64 calc = pidAdvisor.getInt("calculate");
    if (calc == 0)
    {
      break;
    }
  }

  watch.Stop();
  var elapsedMs = watch.ElapsedMilliseconds;

  System.Diagnostics.Trace.WriteLine(
    String.Format("Advice took {0} s.", watch.ElapsedMilliseconds / 1000.0));

  // Get the advised values
  double p_adv = pidAdvisor.getDouble("pid/p");
  double i_adv = pidAdvisor.getDouble("pid/i");
  double d_adv = pidAdvisor.getDouble("pid/d");
  double dlimittimeconstant_adv =
    pidAdvisor.getDouble("pid/dlimittimeconstant");
  double rate_adv = pidAdvisor.getDouble("pid/rate");
  double bw_adv = pidAdvisor.getDouble("bw");

  System.Diagnostics.Trace.WriteLine(p_adv, "P");
  System.Diagnostics.Trace.WriteLine(i_adv, "I");
  System.Diagnostics.Trace.WriteLine(d_adv, "D");
  System.Diagnostics.Trace.WriteLine(dlimittimeconstant_adv, "D_tc");
  System.Diagnostics.Trace.WriteLine(rate_adv, "rate");
  System.Diagnostics.Trace.WriteLine(bw_adv, "bw");

  // copy the values from the Advisor to the device
  pidAdvisor.setInt("todevice", 1);

  // Get all calculated parameters.
  Lookup result = pidAdvisor.get("*");

  // extract bode plot and step response
  double[] grid = result["/bode"][0].advisorWaves[0].grid;
  double[] x = result["/bode"][0].advisorWaves[0].x;
  double[] y = result["/bode"][0].advisorWaves[0].y;
  String fileName = Environment.CurrentDirectory + "/pidAdvisor.txt";
  System.IO.StreamWriter file = new System.IO.StreamWriter(fileName);
  for (int i = 0; i < grid.Length; ++i)
  {
    file.WriteLine("{0} {1} {2}", grid[i], x[i], y[i]);
  }
  file.Close();

  AssertEqual(1.0, pidAdvisor.progress());
  AssertNotEqual(0, grid.Length);

  pidAdvisor.clear();  // Release module resources. Especially important if modules are created
                       // inside a loop to prevent excessive resource consumption.
  daq.disconnect();
}