Hello, I am trying to use my Analog Discovery 2 to stand in as an element of a feedback control system. Specifically what I mean by that is that I've designed a PID circuit and I would like to feed the output of the PID into my AD2 scope, do some calculations, then use the AWG to output a voltage back into my PID circuit. I have written a waveform script that *mostly* does what I want, but am having some trouble with getting the voltage and time data I want from the scope. One more piece of context is that the dynamics of this system are quite long (order minutes) which might effect the best way to do this.
With that context, here are my questions:
1) How can I grab the most recent datapoint acquired by the scope?
2) How can I get the amount of time between the last datapoint that I grabbed, and the most recent one?
Here is a section of the code that I have written so far, with comments of the part that I don't know how to do.
// Setup Instruments
Scope.Time.Mode.text("Shift")
Scope.Trigger.Trigger.text = "Repeated";
Wavegen.Channel1.Mode.text = "Simple";
Wavegen.Channel1.Simple.Offset.value = 0;
Scope.run();
Wavegen.run();
// Do the thing
var heading = start_heading;
while( true ){
if(!Scope.wait()) throw "Stopped";
// Don't know how to fill this part out
var this_sample_idx = ????;
var last_sample_idx = ????;
// Figure out timing for integration
var t_last_sample = Scope.Channel1.TimeOfIndex(last_sample_idx);
var t_this_sample = Scope.Channel1.TimeOfIndex(this_sample_idx);
var dt = t_this_sample - t_last_sample;
// Grab voltage data from scope
var voltage = Scope.Channel1.Sample(this_sample_idx);
// Simulated vehicle
var current_rudder_angle = voltage_to_rudder_angle(voltage);
var current_rate_of_turn = rudder_angle_to_rate_of_turn(current_rudder_angle);
heading = rate_of_turn_to_heading(current_rate_of_turn, heading, dt);
// Calculate difference
var err = set_heading - heading;
// Output error using AWG
Wavegen.Channel1.Simple.Offset.value = err;
}
Thanks so much for the help!
-Dylan