• 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

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
Posted (edited)

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
Posted (edited)

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
  • 0

Hi @Matt B

I don't know how, what application are you using for UART reception...
Is the same UART Rate specified on both sides? also verify the parity, polarity..
Do you have common ground connection between the devices?
 

Share this post


Link to post
Share on other sites
  • 0

Thanks @attila

I am using Terminal program to check the data output. Im using a USB to Serial adapter to the pc.  

Looks like the problem was the polarity. 

Once I changed it to inverted the data started working correctly.

Thank you for the help!!

Matt

 

Share this post


Link to post
Share on other sites
  • 0

@attila

Hey Thank you helping with my project a month ago..

Below is the code that you helped me with. Basically when the scope is triggered I average the number of readings that are given. 

It seems that when the data is averaged from the scope I am getting weird data within the array. for example I may get a quanity of  9 numbers and most of them are around 44.3, 44.9 or 45.23... Those numbers would be ok. But within the same averaged array I am getting numbers that are 80 and 60 and I am not sure how. Thank you for the help.

 

var rgusw = []
var cw = 0
var sumusw = 0
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
// This keeps the script running all the time. (while Statement)
while(SensorStep <= 3 && Scope1.State.running() && Logic1.State.running()){

for(var i = 0; i < 8 && 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(SensorStep ==1){
SensorA_CC_Tested = "1";
print("Counter Clockwise Average:",+uswavg+" uS")

 

Is it possible to average only  the numbers that are between high and low number or use the measured value that the Scope displays. The Scope measurement for Negwidth is perfect, but the data in the averaged array varies too much for whatever reason. 

Edited by Matt B

Share this post


Link to post
Share on other sites
  • 0

Hi @Matt B

The measurements return the average value on the current capture, on the visible area in the main Scope plot.

You could make use of the Min/Max values for the capture, like:
Scope.Channel1.measure("NegWidthMin")
Scope.Channel1.measure("NegWidthMax")

To investigate the measurement 'glitches' you could use the following script to stop the acquisition when this happens:

Scope.run()
while(Scope.wait()){
    var usw = Scope.Channel1.measure("NegWidth")*1e6 // us
    if(usw<50) continue
    // if more than 50us stop
    Scope.stop() 
    break;
}

 

Share this post


Link to post
Share on other sites
  • 0

 

Hey @attila thank you for the reply.

Today I worked to figure this issue out. I did find one issue that I could resolve. When I notice my numbers wrong, I would turn the scope off and back on. That would fix my data every single time. I tried to turn the scope off at the end of my script, but I have not been able to successfully turn it back on using my script, but I am working on that. 

Do you have any ideas?

I was able to stop the scope and restart it before the next cycle. So far everything is working well.

Matt

Edited by Matt B

Share this post


Link to post
Share on other sites
  • 0

Hi @Matt B

You can use the above script to stop the acquisition and investigate the capture with odd measurement.

The horizontal measurements use 10% and 90% as reference obtained from from histogram peaks, low and high.
I don't know what waveform input do you have, what could cause 'odd' measurement.

Please post some screenshots or workspace with your captures.

image.thumb.png.32da1a1578b5b987cd6c81d7003a87c6.png

Share this post


Link to post
Share on other sites
  • 0

image.png.f43ed89c3677f84c72f4b0cb39a91c0c.png

Above the measured value of NegWidth always displays the correct reading. 

Below is the desired result. I get 6 pulses in each direction. 

image.png.f682217dd522e76130e740d87895e5a9.png

Below is the result after running a few sensors. Once the data gets like this below the fix is cycling the scope off and back on.

image.png.34cf5dd5fd88120bb4e56904a83b67dc.png

SensorTestingA_Updated.js

 

I found that if I run the test and print out the usw & usw_B result  (Not Averaged) it has been correct all morning. I do not know if that will help us figure the issue out. Thank you again for the help. 

Edited by Matt B

Share this post


Link to post
Share on other sites
  • 0

Hi @Matt B

It is probably a timing issue between the scope captures and sensor operation.
In the odd measurements the first counter clockwise measurements are of the previous clockwise pulses, followed by 0.5us pulses...

You could stop the process on first odd measurement, like:
var usw = Scope.Channel1.measure("NegWidth")*1e6 // us
if(usw<40 || usw>90) throw "asd"

You can use the wait() function to create delay, like wait(3.5) is delay of 3.5 seconds.
The dummy for loop delay is inaccurate.

Edited by attila

Share this post


Link to post
Share on other sites
  • 0

Is there a easy way to shorten the number from scope. Example I get a 88.23456987655, I would like to capture or round to 88.23?

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