• 0
Sign in to follow this  
Matt B

Scope, Script and StaticIO

Question

Hello, 

I am having a issue writing a script to turn on a digital output for pulse width measurement. I have two measurements. One has a pulse width of about 45us and the other around 80us. I have tried to display the Pulse Width using script and I am having issues. I would like to use the pulse width measurements and check it for tolerance. If measurement is good, turn on output. 

Any help would be appreciated.

 

Thanks,

Matt

Share this post


Link to post
Share on other sites

15 answers to this question

Recommended Posts

  • 0

Hi @Matt B,

I'm not certain how to do this in script, but you can choose to set a trigger to occur based on the length of a pulse width input, though I don't think it will readily test between a range of pulse times. I have attached a screenshot of what the settings would look like for the trigger.

Let me know if you have any questions about this.

Thank you,
JColvin

ad2 trigger.png

Share this post


Link to post
Share on other sites
  • 0
Posted (edited)

I do not think that will work for this application. I am actually testing a sensor. I am using the trigger to get the pulse from the sensor. The pulse responds differently to rotation in the clockwise/cc direction. I am measuring that Pulse width and checking to make sure it is in tolerance. If it is within tolerance I will activate StaticIO output.

 

If I could get the measurement of the NegWidth or PosWidth from the Scope into a variable in Script, I may be able figure this out.

Edited by Matt B

Share this post


Link to post
Share on other sites
  • 0

Hi @Matt B

The beta version adds access to Logic Analyzer measurements:
https://forum.digilentinc.com/topic/8908-waveforms-beta-download/

The following Script changes the DIO15 output based on DIO0 negative width measurement at 60us threshold.

Logic.run()
while(Logic.wait()){
    var sw = Logic.Channels.DIO0.measure("NegWidth")
    StaticIO.Channel1.DIO15.value = sw>60e-6?1:0
}

image.thumb.png.350ef08b32bb8e11547f21f8f02be8cf.png

 

Share this post


Link to post
Share on other sites
  • 0

Are you saying I do not have access to NegWidth measured value with the latest copy of the software? 

Share this post


Link to post
Share on other sites
  • 0

I tried to setup the scope like you did above and I didn't get the same results. More than likely my issue is the sensor. This sensor is used for speed. I have a trigger as 940mv and this is based on the resister I have hooked up. Once I trigger the sensor I see one pulse and that pulse width will be either 45 or 90 us for a good sensor. 

When I wired the StaicIO to the sensor to use logic to get the pulse width, the values that I was seeing in Logic was wrong. It was not the same as the scope reading.

Is there another way to get "NegWidth" into a variable so I can use it with scripting? 

My goal is to send output from discovery 2 to a PLC for pass/fail based on Pulse width. measurements.

image.thumb.png.e4ef1b125b799e54c1fd0864eaa88a68.png

Share this post


Link to post
Share on other sites
  • 0

Hi @Matt B

I see you have negative voltage readings. Shouldn't be these positive levels?
Make sure you have ground connection between devices/circuits, you are using the positive scope input (1+) and the 1- is connect to ground if you are not using it deferentially.

You can access Scope measurement the same way:

Scope.run()
while(Scope.wait()){
    var sw = Scope.Channel1.measure("NegWidth")
    StaticIO.Channel1.DIO15.value = sw>60e-6?1:0
}

image.thumb.png.a89e7220ae481510af6ae6b9122af63f.png

Share this post


Link to post
Share on other sites
  • 0

The reason I have negative voltage readings is I working in a noisy environment. We have multiple inverter drives running sporadically all day long. I swapped my scope leads and inverted my trigger and voltage to get the pulse. I did this to get rid of the noise. Before I made this change it would trigger automatically in ambient air because of the voltage trigger.

I was able to get the measured Pulse Width this time. I had to multiply to get the decimal in the correct spot. 

Thanks to everyone for the help.

 

Matt

  

Share this post


Link to post
Share on other sites
  • 0
Posted (edited)

I have another question. Below is the output of the script. Since it is not in a array, I am having issues trying to average the two different readings below. I assume that there is a JavaScript function to do this?

Thanks, 

Matt

image.png.2714fcbafc1d67b8ab7b6bcd2f7f27f8.png

Edited by Matt B

Share this post


Link to post
Share on other sites
  • 0

Hi @Matt B

var rgusw = []
var cw = 0
var sumusw = 0
Scope.run()
for(var i = 0; i < 10 && Scope.wait(); i++){
    var usw = Scope.Channel1.measure("NegWidth")*1e6 // us
    rgusw.push(usw) // array
    sumusw += usw
    cw++
}
print(rgusw)
var uswavg = sumusw/cw
print("Average:",uswavg)

image.png.5a5a9d12004fb6cb2d585612f3649b68.png

Share this post


Link to post
Share on other sites
  • 0

Hi @Matt B

The Script tool can be started manually or by passing argument to the application.
The script code can be repeated with loops, while, for...

Share this post


Link to post
Share on other sites
  • 0

Hello @attila

I have my project working well. Now I am trying to send my results via serial using UART. I am seeing data, but I do not know how to decode it. I am using Terminal to test it and I am receiving junk. Here is the snippet of the Logic and my code. 

image.png.720b613d93ac64f01231a369eb1ce023.png

Scope1.run();

StaticIO.run();

Protocol.run();

 

var rgusw = []

var cw = 0

var sumusw = 0

var Sensor1LL_CC = 38 //Sensor A Counter Clockwise Lower Limit +-7

var Sensor1UL_CC = 52 //Sensor A Counter Clockwise Upper Limit +-7

var SensorA_CC_Tested = 0 //Sets Sensor Test Value to zero before test

var SensorStep = 1 //Sets the clockwise test into action -- Sets to zero before test

StaticIO.Channel0.DIO0.value = 0 //Sensor A Passed -- Set to zero beofre test

StaticIO.Channel0.DIO1.value = 0// Sensor A Failed -- Set to zero before test

 

while(SensorStep <= 3 && Scope1.State.running() && StaticIO.State.running()){

 

for(var i = 0; i < 9 && Scope.wait(); i++){

var usw = Scope.Channel1.measure("NegWidth")*1e6 // us

rgusw.push(usw) // array

sumusw += usw

cw++

}

//print(rgusw) //Uncomment to print all averaged data

var uswavg = sumusw/cw

 

if(uswavg > Sensor1LL_CC && uswavg < Sensor1UL_CC){

SensorA_CC_Tested = "1";

print("Sensor Upper Limit: ",+Sensor1UL_CC+" uS")

print("Average:",+uswavg+" uS")

print("Sensor Lower Limit: ",+Sensor1LL_CC+" uS")

print("Sensor Counter Clockwise Tested: "+SensorA_CC_Tested)

SensorStep = "2";

}

else{

print("Failed")

print("Sensor Counter Clockwise Tested: "+SensorA_CC_Tested)

print("Average:",+uswavg+" uS")

StaticIO.Channel0.DIO1.value = 1// Sensor A Failed

};

 

 

if(SensorStep ==2){

 

var rgusw_B = []

var cw_B = 0

var sumusw_B = 0

var Sensor1LL_C = 76 //Sensor A Clockwise Lower Limit +-14

var Sensor1UL_C = 104 //Sensor A Clockwise Upper Limit +-14

var SensorA_C_Tested = 0 //Sets Sensor Test Value to zero before test

for(var i = 0; i < 9 && Scope.wait(); i++){

var usw_B = Scope.Channel1.measure("NegWidth")*1e6 // us

rgusw_B.push(usw_B) // array

sumusw_B += usw_B

cw_B++

}

//print(rgusw_B) //Uncomment to print all averaged data

var uswavg_B = sumusw_B/cw_B

if(uswavg_B > Sensor1LL_C && uswavg_B < Sensor1UL_C){

SensorA_C_Tested = "1";

print("Sensor Upper Limit: ",+Sensor1UL_C+" uS")

print("Average:",+uswavg_B+" uS")

print("Sensor Lower Limit: ",+Sensor1LL_C+" uS")

print("Sensor Clockwise Tested: "+SensorA_C_Tested)

SensorStep = "1";

StaticIO.Channel0.DIO0.value = 1 //Pass both directions turn on output to plc

// *****Protocol Code below for comms to PLC*****

//Code below is for data Collections

if(!('Protocol' in this)) throw "Please open the Protocol tool";

 

Protocol.Mode.text = "UART" // make sure UART tab is selected

Protocol.UART.SendArray([Sensor1UL_CC,uswavg,Sensor1LL_CC,SensorA_CC_Tested,Sensor1UL_C,uswavg_B,Sensor1LL_C,SensorA_C_Tested])

}

else{

print("Failed")

print("Average:",uswavg)

print("Sensor Clockwise Tested: "+SensorA_C_Tested)

StaticIO.Channel0.DIO1.value = 1// Sensor A Failed

}

}

 

}

 

* Here is what the output of the script looks like

image.png.72458684ce6011677b131664b5947ce4.png

 

If I understand correctly, it looks like I will need a UART to RS232 Converter. Is this right?

Edited by Matt B
Updated

Share this post


Link to post
Share on other sites
  • 0

Hi @Matt B

With the Protocol.UART.SendArray(array) you are sending array for numbers. Like here [52, 50, 31] is sent, in hex [0x34, 0x32, 0x1F]

image.png.005a7cc9fb49e1cda80a2d291c92ceb6.png

 

If you want to send text, then convert the data to text format and use Protocol.UART.Send(string), like:
var sz = Sensor1UL_CC+" "+uswavg ...
Protocol.UART.Send(sz)

image.thumb.png.dc2776add63cd7e58ef34d6324cc8b7f.png

Edited by attila

Share this post


Link to post
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
Sign in to follow this