Test and Measurement Forum

how can I build a closed-loop control via pico

Post your VB and VBA discussions here

how can I build a closed-loop control via pico

Postby yangzhaoshu » Mon Mar 25, 2013 8:15 am

Hi
I am using a pico2203 to build a displacement control system, while it seems that I am confronted with a problem in building the feedback link.
My idea is that: firstly, generate a signal from pico2203 to drive the objects under control, then acquire the output data of it as a sequence and feed the sequence back into the pico, after a subtraction with the input signal and some other operations, a new sequence is acquired as the input sequence of AWG, by this way, a closed-loop control system is build.
But as I debugging the code, the error of overflow occurs, I tried to decrease the amplitude of input signal but it makes no difference.
Here is my codes and I beg someone could help me.
In the code the values_a denotes the input signal, and the values_b denotes the output, actually I am just building a typical PID control system.
Code: Select all
Private Sub arbgen()
Dim ok As Integer
Dim i As Integer
Dim delta As Long
Dim frequency As Long
Dim size As Integer
Dim errorput1(501) As Byte
Dim j As Integer

Dim err(501) As Integer

ok = 1
offsetvoltage = Int(Val(Text2(0).Text) * 1000)
pktopk = Int(Val(Text2(1).Text) * 1000)
erri = 0
size = 500
frequency = Int(Val(Text2(2).Text))
arbitrarywavesize = Int(Val(Text2(3).Text))


kp = Val(inputkp.Text)
ki = Val(inputki.Text)
kd = Val(inputkd.Text)

'ReDim arbitraryWaveform(500) As Byte
delta = ((frequency * arbitrarywavesize) / 4096) * 4294967296# * 0.00000002
Open "D:\Documents\Waveforms\ysquare11.csv" For Input As #2
Do While Not EOF(2)
  For i = 1 To UBound(arbitraryWaveform)
Input #2, arbitraryWaveform(i)
If closeloop.enabled = False Then
For j = 1 To i
err(j) = (values_a(j) - values_b(j)) / 1000
erri = err(j) + erri
If erri > 3000 Then
erri = 3000
End If
If erri < -3000 Then
erri = -3000
End If
Next j
errorput(i - 1) = (kp * (values_a(i) - values_b(i)) + ki * erri * 1000 * (values_time(i) - values_time(i - 1)) / 1000000 - kd * (values_a(i) - values_b(i) - values_a(i - 1) + values_b(i - 1)) / ((values_time(i) - values_time(i - 1)) / 1000000)) / 32767 * 255
If errorput(i - 1) < 0 Then
errorput(i - 1) = 0
End If
If errorput(i - 1) > 255 Then
errorput(i - 1) = 255
End If
Else
errorput(i - 1) = arbitraryWaveform(i)
End If
errorput1(i - 1) = CByte(errorput(i - 1))
Next i
Loop
Close #2

Select Case Combo2.Text
   Case "Up": sweeptype = 0
   Case "Down": sweeptype = 1
   Case "UpDown": sweeptype = 2
   Case "Downup": sweeptype = 3
End Select
If ps2000_handle Then
  ok = ps2000_set_sig_gen_arbitrary(ps2000_handle, offsetvoltage, pktopk, delta, delta, 0, 0, errorput1(0), arbitrarywavesize, sweeptype, 0)
End If
End Sub
yangzhaoshu
Newbie
 
Posts: 0
Joined: Sat Jan 05, 2013 12:46 pm

Re: how can I build a closed-loop control via pico

Postby Hitesh » Mon Mar 25, 2013 12:46 pm

Hello yangzhaoshu,

Which line is the overflow occurring at?

Are you capturing data in block or streaming mode? Have you checked the voltage ranges on channel A and B?

What are the peak to peak and offset voltages being used to set the arbitrary waveform generator?

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 1997
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: how can I build a closed-loop control via pico

Postby yangzhaoshu » Mon Mar 25, 2013 3:31 pm

Dear Hitesh
1. The error occurs in the line of
Code: Select all
errorput(i - 1) = (kp * (values_a(i) - values_b(i)) + ki * erri * 1000 * (values_time(i) - values_time(i - 1)) / 1000000 - kd * (values_a(i) - values_b(i) - values_a(i - 1) + values_b(i - 1)) / ((values_time(i) - values_time(i - 1)) / 1000000)) / 32767 * 255

2. I am capturing data in block mode, the data in both channel are acquired smoothly so I think the data may not beyond the voltage range.

3. The pktopk voltage is set 4000mv, which is the max permitting voltage to run a output signal.
your help will be appreciated.
yangzhaoshu
yangzhaoshu
Newbie
 
Posts: 0
Joined: Sat Jan 05, 2013 12:46 pm

Re: how can I build a closed-loop control via pico

Postby Hitesh » Mon Mar 25, 2013 3:50 pm

Hello Yangzhaoshu,

If you set a breakpoint at that line, perhaps you can loop through until you find the instance at which the overflow occurs.

You can then manually calculate the value using the parameters to see whether the resulting value is greater than the maximum value for the data type you are using for errorput.

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 1997
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: how can I build a closed-loop control via pico

Postby yangzhaoshu » Tue Mar 26, 2013 9:37 am

Dear Hitesh
As your suggestion, a couple of experiments was conducted, and the fact was that there will be no problem if I put the signal generated by AWG into channel A or B directly without connecting the external load, but if I put the generated signal into an amplifier and driving object under control then detect the output signal of it and pass the detected signal into channel A or B, the overflow error occurs, which really confused me a lot.
A breakpoint was set, the value of variables under observation was listed below when the overflow occurs
i=143; j=144; errorput(i)=50; values_a(i)=90; values_b(i)=-436; values_time(i)=29286400; values_time(i-1)=29081600, err(j)=0
from my poit of view, the error may denotes that value of j is larger than i, for there is a line
Code: Select all
For j = 1 To i

And the err(j)=0, indicating that the value at moment j was failed to pass into err(j).
But how can I avoid that error making the value of j always beneath that of i, and why there will be no wrong if i connect the ports of signal out and channel A or B directly.
望眼欲穿您的回复(I am looking for your reply wistfully.)
Yangzhaoshu
yangzhaoshu
Newbie
 
Posts: 0
Joined: Sat Jan 05, 2013 12:46 pm

Re: how can I build a closed-loop control via pico

Postby Hitesh » Tue Mar 26, 2013 10:49 am

Hello Yangzhaoshu,

If you are amplifying the signal then the amplitude of the signal will be larger :D

Please check your calculation for errorput1. What is the value of errorput1 if you work out the formula with a calculator?

What voltage range have you set for Channel A and B?

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 1997
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: how can I build a closed-loop control via pico

Postby yangzhaoshu » Tue Mar 26, 2013 12:26 pm

dear hitesh
你太厉害了(You are too much )
the errorput(i) was calculated as 33048, beyond the permition value of integer, and can you give me some suggestion about how to transfer this excess value without causing overflow.
yangzhaoshu
Newbie
 
Posts: 0
Joined: Sat Jan 05, 2013 12:46 pm

Re: how can I build a closed-loop control via pico

Postby Hitesh » Tue Mar 26, 2013 2:06 pm

Hello Yanzhaoshu,

Perhaps you can perform a test on the value to see if it is greater than the max value for an Integer or reduce the amplification for the signal?

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 1997
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: how can I build a closed-loop control via pico

Postby yangzhaoshu » Tue Mar 26, 2013 3:01 pm

I redim the errorput as long, and the problem was solved, thank you for point me out the problem.
best regards
yangzhaoshu
yangzhaoshu
Newbie
 
Posts: 0
Joined: Sat Jan 05, 2013 12:46 pm

Re: how can I build a closed-loop control via pico

Postby yangzhaoshu » Mon Apr 01, 2013 11:48 am

dear Hetish
I wonder if the 2203 could be regarded as a constant current source or just a constant voltage source
yangzhaoshu
Newbie
 
Posts: 0
Joined: Sat Jan 05, 2013 12:46 pm

Re: how can I build a closed-loop control via pico

Postby Hitesh » Tue Apr 02, 2013 9:01 am

Hello Yangzhaoshu,

The signal generator can be used as a low voltage source (max. +2V) and has a 600 Ohm series impedance.

I hope this helps.
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 1997
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire


Return to VB and VBA (Excel, Access)

Who is online

Users browsing this forum: No registered users and 0 guests