Jump to content
  • 0

Use AD2 as a feedback system


soak

Question

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

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 1

Hi @soak

The TimeOfIndex() refers to the sample index position in the capture.
The Time.taken for AD2 is software capture time and for ADP3,5 high precision software trigger time.
For high refresh rate set no trigger, high sample rate and low number of samples. 
Use config() to apply the Wavegen changes immediately otherwise sometime during a wait() will be applied.

image.thumb.png.1ee0f51ec4b45b61ecdd7364d14ac26f.png

Scope.run();
Wavegen.run();

var last_time = 0;
var last_voltage;

while(true){
    if(!Scope.wait()) throw "Stopped";
    var time = Scope.Time.taken;
    var voltage = Scope.Channel1.measure("Average");
    
    if(last_time){
        ...
        Wavegen.Channel1.Simple.Offset.value = err;
        Wavegen.Channel1.config();
    }
    last_time = time;
    last_voltage = voltage;
}

 

Link to comment
Share on other sites

  • 0

Hi @attila,

Thanks for the help, however, it seems that the Analog Discovery 2 that I have does not support the Scope.Time.taken command.  I have reached this conclusion by printing the result of Scope.Time.taken and getting an undefined result:

print(Scope.Time.taken) --> undefined

Is there an alternate approach to do timing using the Analog Discovery 2 hardware, or would you suggest that I keep track of timing in javascript, at the cost of reduced precision?

Thanks,

Dylan

Link to comment
Share on other sites

  • 0

Additionally, looking into Javascript timing there seem to be two methods.  Method 1 uses Date().getTime() which offers millisecond resolution (not very good).  Method 2 uses performance.now() and offers sub-microsecond resolution.  Method 2 would be preferable but I can't seem to run it natively in a waveforms script.  Is there a required import to make the performance package work?  I have tried `const { performance } = require('perf_hooks');` to import as would be required in node.js but that didn't seem to work.

Thanks again!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...