how can I build a closed-loop control via pico

Post your VB and VBA discussions here
Post Reply
yangzhaoshu
Newbie
Posts: 0
Joined: Sat Jan 05, 2013 12:46 pm

how can I build a closed-loop control via pico

Post by yangzhaoshu »

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

Hitesh

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

Post by Hitesh »

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,

yangzhaoshu
Newbie
Posts: 0
Joined: Sat Jan 05, 2013 12:46 pm

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

Post by yangzhaoshu »

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

Hitesh

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

Post by Hitesh »

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,

yangzhaoshu
Newbie
Posts: 0
Joined: Sat Jan 05, 2013 12:46 pm

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

Post by yangzhaoshu »

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

Hitesh

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

Post by Hitesh »

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,

yangzhaoshu
Newbie
Posts: 0
Joined: Sat Jan 05, 2013 12:46 pm

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

Post by yangzhaoshu »

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.

Hitesh

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

Post by Hitesh »

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,

yangzhaoshu
Newbie
Posts: 0
Joined: Sat Jan 05, 2013 12:46 pm

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

Post by yangzhaoshu »

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

Post by yangzhaoshu »

dear Hetish
I wonder if the 2203 could be regarded as a constant current source or just a constant voltage source

Hitesh

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

Post by Hitesh »

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.

Post Reply