ExampleImpedanceSweeperΒΆ

 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
// ExampleImpedanceSweeper instantiates a sweeper module and prepares
// all settings for an impedance sweep over 30 data points.
// The results are written to a file.
public static void ExampleImpedanceSweeper(string dev = DEFAULT_DEVICE) // Timeout(40000)
{
  ziDotNET daq = connect(dev);
  // This example only works for devices with installed
  // Impedance Analyzer (IA) option.
  if (!hasOption(daq, dev, "IA"))
  {
    daq.disconnect();
    Skip("Not supported by device.");
  }

  resetDeviceToDefault(daq, dev);
  // Enable impedance control
  daq.setInt(String.Format("/{0}/imps/0/enable", dev), 1);
  // Return D and Cs
  daq.setInt(String.Format("/{0}/imps/0/model", dev), 4);
  // Enable user compensation
  daq.setInt(String.Format("/{0}/imps/0/calib/user/enable", dev), 1);

  // ensure correct settings of order and oscselect
  daq.setInt(String.Format("/{0}/imps/0/demod/order", dev), 8);
  daq.setInt(String.Format("/{0}/imps/0/demod/oscselect", dev), 0);
  daq.sync();

  ziModule sweep = daq.sweeper();
  // Sweeper settings
  sweep.setByte("device", dev);
  sweep.setDouble("start", 1e3);
  sweep.setDouble("stop", 5e6);
  sweep.setDouble("samplecount", 30);
  sweep.setDouble("order", 8);
  sweep.setDouble("settling/inaccuracy", 0.0100000);
  sweep.setDouble("bandwidthcontrol", 2);
  sweep.setDouble("maxbandwidth", 10.0);
  sweep.setDouble("bandwidthoverlap", 1);
  sweep.setDouble("xmapping", 1);
  sweep.setDouble("omegasuppression", 100.0);
  sweep.setDouble("averaging/sample", 200);
  sweep.setDouble("averaging/time", 0.100);
  sweep.setDouble("averaging/tc", 20.0);
  String path = String.Format("/{0}/imps/0/sample", dev);
  sweep.subscribe(path);
  sweep.execute();
  while (!sweep.finished())
  {
    System.Threading.Thread.Sleep(100);
    double progress = sweep.progress() * 100;
    System.Diagnostics.Trace.WriteLine(progress, "Progress");
  }
  Lookup lookup = sweep.read();
  double[] grid = lookup[path][0].sweeperImpedanceWaves[0].grid;
  double[] x = lookup[path][0].sweeperImpedanceWaves[0].realz;
  double[] y = lookup[path][0].sweeperImpedanceWaves[0].imagz;
  double[] param0 = lookup[path][0].sweeperImpedanceWaves[0].param0;
  double[] param1 = lookup[path][0].sweeperImpedanceWaves[0].param1;
  UInt64[] flags = lookup[path][0].sweeperImpedanceWaves[0].flags;
  // Save measurement data to file
  String fileName = Environment.CurrentDirectory + "/impedance.txt";
  System.IO.StreamWriter file = new System.IO.StreamWriter(fileName);
  ZIChunkHeader header = lookup[path][0].header;
  // Raw system time is the number of microseconds since linux epoch
  file.WriteLine("Raw System Time: {0}", header.systemTime);
  // Use the utility function ziSystemTimeToDateTime to convert to DateTime of .NET
  file.WriteLine("Converted System Time: {0}", ziUtility.ziSystemTimeToDateTime(lookup[path][0].header.systemTime));
  file.WriteLine("Created Timestamp: {0}", header.createdTimeStamp);
  file.WriteLine("Changed Timestamp: {0}", header.changedTimeStamp);
  for (int i = 0; i < grid.Length; ++i)
  {
    file.WriteLine("{0} {1} {2} {3} {4} {5}",
      grid[i],
      x[i],
      y[i],
      param0[i],
      param1[i],
      flags[i]);
  }
  file.Close();

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

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