Hello everyone
I have an application that through a function that runs every 5 msg gives me the position of some motors and the value of some gauges, my intention is that when the user tightens a gauge and this has X value the motor moves at a speed determined.
Dim valorgf1 As String
Dim valorgt1 As String
Dim valorgf2 As String
Dim valorgt2 As String
Dim valorgf3 As String
Dim valorgt3 As String
Dim valorgf4 As String
Dim valorgt4 As String
Dim valorgf5 As String
Dim valorgt5 As String
Dim valormot1 As String = 0
Dim valormot2 As String = 0
Dim valormot3 As String = 0
Dim valormot4 As String = 0
Dim valormot5 As String = 0
Dim gf1 As Decimal
Dim gt1 As Decimal
Dim gf2 As Decimal
Dim gt2 As Decimal
Dim gf3 As Decimal
Dim gt3 As Decimal
Dim gf4 As Decimal
Dim gt4 As Decimal
Dim gf5 As Decimal
Dim gt5 As Decimal
'Dim limite = TextBox3.Text
Try
Using device As Usc = connectToDevice() ' Find a device and temporarily connect.
device.getVariables(st1)
'System.Threading.Thread.Sleep(2)
galfaf1 = st1(0).position
galfat1 = st1(1).position
galfaf2 = st1(7).position
galfat2 = st1(8).position
galfaf3 = st1(9).position
galfat3 = st1(10).position
galfaf4 = st1(13).position
galfat4 = st1(14).position
galfaf5 = st1(15).position
galfat5 = st1(16).position
slide1 = st1(2).position
' MsgBox(st1(2).position)
slide2 = st1(6).position
slide3 = st1(12).position
slide4 = st1(18).position
slide5 = st1(23).position
valorgf1 = (5 * galfaf1) / 1023
valorgt1 = (5 * galfat1) / 1023
valorgf2 = (5 * galfaf2) / 1023
valorgt2 = (5 * galfat2) / 1023
valorgf3 = (5 * galfaf3) / 1023
valorgt3 = (5 * galfat3) / 1023
valorgf4 = (5 * galfaf4) / 1023
valorgt4 = (5 * galfat4) / 1023
valorgf5 = (5 * galfaf5) / 1023
valorgt5 = (5 * galfat5) / 1023
If Desde1 <> "" Then
valormot1 = 0.1 * (slide1 / 4 - 1000)
End If
If Desde2 <> "" Then
valormot2 = 0.1 * (slide2 / 4 - 1000)
End If
If Desde3 <> "" Then
valormot3 = 0.1 * (slide3 / 4 - 1000)
End If
If Desde4 <> "" Then
valormot4 = 0.1 * (slide4 / 4 - 1000)
End If
If Desde5 <> "" Then
valormot5 = 0.1 * (slide5 / 4 - 1000)
End If
If Desde1 <> "" Then
If valorgf1 >= 0 And valorgf1 <= 0.1 Then
gf1 = (19.23 * valorgf1)
valor1 = (gf1 / 1000).ToString("0.0")
TrackBar6.Value = gf1
ElseIf valorgf1 > 0.1 And valorgf1 <= 1.3 Then
gf1 = (19.23 * valorgf1 + 25)
valor1 = (gf1 / 1000).ToString("0.0")
TrackBar6.Value = gf1
ElseIf valorgf1 > 1.3 And valorgf1 <= 2.5 Then
gf1 = (225 * (valorgf1 - 1.3) + 50)
valor1 = (gf1 / 1000).ToString("0.0")
TrackBar6.Value = gf1
ElseIf valorgf1 > 2.5 And valorgf1 <= 3.4 Then
gf1 = (755.5 * (valorgf1 - 2.5) + 320)
valor1 = (gf1 / 1000).ToString("0.0")
TrackBar6.Value = gf1
End If
If valorgt1 >= 0 And valorgt1 <= 0.1 Then
gt1 = (19.23 * valorgt1)
valor2 = (gt1 / 1000).ToString("0.0")
TrackBar7.Value = gt1
ElseIf valorgt1 > 0.1 And valorgt1 <= 1.3 Then
gt1 = (19.23 * valorgt1 + 25)
valor2 = (gt1 / 1000).ToString("0.0")
TrackBar7.Value = gt1
ElseIf valorgt1 > 1.3 And valorgt1 <= 2.5 Then
gt1 = (225 * (valorgt1 - 1.3) + 50)
valor2 = (gt1 / 1000).ToString("0.0")
TrackBar7.Value = gt1
ElseIf valorgt1 > 2.5 And valorgt1 <= 3.4 Then
gt1 = (755.5 * (valorgt1 - 2.5) + 320)
valor2 = (gt1 / 1000).ToString("0.0")
TrackBar7.Value = gt1
End If
End If
If Desde2 <> "" Then
If valorgf2 >= 0 And valorgf2 <= 0.1 Then
gf2 = (19.23 * valorgf2)
valor3 = (gf2 / 1000).ToString("0.0")
TrackBar15.Value = gf2
ElseIf valorgf2 > 0.1 And valorgf2 <= 1.3 Then
gf2 = (19.23 * valorgf2 + 25)
valor3 = (gf2 / 1000).ToString("0.0")
TrackBar15.Value = gf2
ElseIf valorgf2 > 1.3 And valorgf2 <= 2.5 Then
gf2 = (225 * (valorgf2 - 1.3) + 50)
valor3 = (gf2 / 1000).ToString("0.0")
TrackBar15.Value = gf2
ElseIf valorgf2 > 2.5 And valorgf2 <= 3.4 Then
gf2 = (755.5 * (valorgf2 - 2.5) + 320)
valor3 = (gf2 / 1000).ToString("0.0")
TrackBar15.Value = gf2
End If
If valorgt2 >= 0 And valorgt2 <= 0.1 Then
gt2 = (19.23 * valorgt2)
valor4 = (gt2 / 1000).ToString("0.0")
TrackBar14.Value = gt2
ElseIf valorgt2 > 0.1 And valorgt2 <= 1.3 Then
gt2 = (19.23 * valorgt2 + 25)
valor4 = (gt2 / 1000).ToString("0.0")
TrackBar14.Value = gt2
ElseIf valorgt2 > 1.3 And valorgt2 <= 2.5 Then
gt2 = (225 * (valorgt2 - 1.3) + 50)
valor4 = (gt2 / 1000).ToString("0.0")
TrackBar14.Value = gt2
ElseIf valorgt2 > 2.5 And valorgt2 <= 3.4 Then
gt2 = (755.5 * (valorgt2 - 2.5) + 320)
valor4 = (gt2 / 1000).ToString("0.0")
TrackBar14.Value = gt2
End If
End If
If Desde3 <> "" Then
If valorgf3 >= 0 And valorgf3 <= 0.1 Then
gf3 = (19.23 * valorgf3)
valor5 = (gf3 / 1000).ToString("0.0")
TrackBar8.Value = gf3
ElseIf valorgf3 > 0.1 And valorgf3 <= 1.3 Then
gf3 = (19.23 * valorgf3 + 25)
valor5 = (gf3 / 1000).ToString("0.0")
TrackBar8.Value = gf3
ElseIf valorgf3 > 1.3 And valorgf3 <= 2.5 Then
gf3 = (225 * (valorgf3 - 1.3) + 50)
valor5 = (gf3 / 1000).ToString("0.0")
TrackBar8.Value = gf3
ElseIf valorgf3 > 2.5 And valorgf3 <= 3.4 Then
gf3 = (755.5 * (valorgf3 - 2.5) + 320)
valor5 = (gf3 / 1000).ToString("0.0")
TrackBar8.Value = gf3
End If
If valorgt3 >= 0 And valorgt3 <= 0.1 Then
gt3 = (19.23 * valorgt3)
valor6 = (gt3 / 1000).ToString("0.0")
TrackBar9.Value = gt3
ElseIf valorgt3 > 0.1 And valorgt3 <= 1.3 Then
gt3 = (19.23 * valorgt3 + 25)
valor6 = (gt3 / 1000).ToString("0.0")
TrackBar9.Value = gt3
ElseIf valorgt3 > 1.3 And valorgt3 <= 2.5 Then
gt3 = (225 * (valorgt3 - 1.3) + 50)
valor6 = (gt3 / 1000).ToString("0.0")
TrackBar9.Value = gt3
ElseIf valorgt3 > 2.5 And valorgt3 <= 3.4 Then
gt3 = (755.5 * (valorgt3 - 2.5) + 320)
valor6 = (gt3 / 1000).ToString("0.0")
TrackBar9.Value = gt3
End If
End If
If Desde4 <> "" Then
If valorgf4 >= 0 And valorgf4 <= 0.1 Then
gf4 = (19.23 * valorgf4)
valor7 = (gf4 / 1000).ToString("0.0")
TrackBar10.Value = gf4
ElseIf valorgf4 > 0.1 And valorgf4 <= 1.3 Then
gf4 = (19.23 * valorgf4 + 25)
valor7 = (gf4 / 1000).ToString("0.0")
TrackBar10.Value = gf4
ElseIf valorgf4 > 1.3 And valorgf4 <= 2.5 Then
gf4 = (225 * (valorgf4 - 1.3) + 50)
valor7 = (gf4 / 1000).ToString("0.0")
TrackBar10.Value = gf4
ElseIf valorgf4 > 2.5 And valorgf4 <= 3.4 Then
gf4 = (755.5 * (valorgf4 - 2.5) + 320)
valor7 = (gf4 / 1000).ToString("0.0")
TrackBar10.Value = gf4
End If
If valorgt4 >= 0 And valorgt4 <= 0.1 Then
gt4 = (19.23 * valorgt4)
valor8 = (gt4 / 1000).ToString("0.0")
TrackBar11.Value = gt4
ElseIf valorgt4 > 0.1 And valorgt4 <= 1.3 Then
gt4 = (19.23 * valorgt4 + 25)
valor8 = (gt4 / 1000).ToString("0.0")
TrackBar11.Value = gt4
ElseIf valorgt4 > 1.3 And valorgt4 <= 2.5 Then
gt4 = (225 * (valorgt4 - 1.3) + 50)
valor8 = (gt4 / 1000).ToString("0.0")
TrackBar11.Value = gt4
ElseIf valorgt4 > 2.5 And valorgt4 <= 3.4 Then
gt4 = (755.5 * (valorgt4 - 2.5) + 320)
valor8 = (gt4 / 1000).ToString("0.0")
TrackBar11.Value = gt4
End If
End If
If Desde5 <> "" Then
If valorgf5 >= 0 And valorgf5 <= 0.1 Then
gf5 = (19.23 * valorgf5)
valor9 = (gf5 / 1000).ToString("0.0")
TrackBar13.Value = gf5
ElseIf valorgf5 > 0.1 And valorgf5 <= 1.3 Then
gf5 = (19.23 * valorgf5 + 25)
valor9 = (gf5 / 1000).ToString("0.0")
TrackBar13.Value = gf5
ElseIf valorgf5 > 1.3 And valorgf5 <= 2.5 Then
gf5 = (225 * (valorgf5 - 1.3) + 50)
valor9 = (gf5 / 1000).ToString("0.0")
TrackBar13.Value = gf5
ElseIf valorgf5 > 2.5 And valorgf5 <= 3.4 Then
gf5 = (755.5 * (valorgf5 - 2.5) + 320)
valor9 = (gf5 / 1000).ToString("0.0")
TrackBar13.Value = gf5
End If
If valorgt5 >= 0 And valorgt5 <= 0.1 Then
gf5 = (19.23 * valorgt5)
valor10 = (gt5 / 1000).ToString("0.0")
TrackBar12.Value = gf5
ElseIf valorgt5 > 0.1 And valorgt5 <= 1.3 Then
gt5 = (19.23 * valorgt5 + 25)
valor10 = (gt5 / 1000).ToString("0.0")
TrackBar12.Value = gf5
ElseIf valorgt5 > 1.3 And valorgt5 <= 2.5 Then
gt5 = (225 * (valorgt5 - 1.3) + 50)
valor10 = (gt5 / 1000).ToString("0.0")
TrackBar12.Value = gf5
ElseIf valorgt5 > 2.5 And valorgt5 <= 3.4 Then
gt5 = (755.5 * (valorgt5 - 2.5) + 320)
valor10 = (gt5 / 1000).ToString("0.0")
TrackBar12.Value = gf5
End If
End If
If CheckBox2.Checked = True Then
If valor2 >= 0 And valor2 <= 0.1 Then
Velo1 = 0
ElseIf valor2 > 0.1 And valor2 <= 2 Then
Velo1 = 2
ElseIf valor2 > 2 And valor2 <= 4 Then
Velo1 = 2
ElseIf valor2 > 4 And valor2 <= 6 Then
Velo1 = 3
ElseIf valor2 > 6 And valor2 <= 8 Then
Velo1 = 4
ElseIf valor2 > 8 And valor2 <= 10 Then
Velo1 = 5
End If
End If
array1.Add(valor1)
array2.Add(valor2)
array3.Add(valor3)
array4.Add(valor4)
array5.Add(valor5)
array6.Add(valor6)
array7.Add(valor7)
array8.Add(valor8)
array9.Add(valor9)
array10.Add(valor10)
array1.Sort()
array2.Sort()
array3.Sort()
array4.Sort()
array5.Sort()
array6.Sort()
array7.Sort()
array8.Sort()
array9.Sort()
array10.Sort()
array1.Reverse()
array2.Reverse()
array3.Reverse()
array4.Reverse()
array5.Reverse()
array6.Reverse()
array7.Reverse()
array8.Reverse()
array9.Reverse()
array10.Reverse()
TextBox5.Text = array1(0)
TextBox12.Text = array2(0)
TextBox7.Text = array3(0)
TextBox6.Text = array4(0)
TextBox9.Text = array5(0)
TextBox8.Text = array6(0)
TextBox11.Text = array7(0)
TextBox10.Text = array8(0)
TextBox14.Text = array9(0)
TextBox13.Text = array10(0)
TrackBar1.Value = valormot1
TrackBar5.Value = valormot2
TrackBar4.Value = valormot3
TrackBar3.Value = valormot4
TrackBar2.Value = valormot5
If CheckBox3.Checked = True Then
If TextBox5.Text >= TextBox4.Text OrElse TextBox7.Text >= TextBox4.Text OrElse TextBox9.Text >= TextBox4.Text OrElse TextBox11.Text >= TextBox4.Text OrElse TextBox14.Text >= TextBox4.Text Then
Timer16.Enabled = False
Timer1.Enabled = False
Button2.BackColor = Color.Crimson
Button2.Enabled = False
Button1.Enabled = True
Button1.BackColor = Color.Transparent
xTimer.Enabled = False
TrySetTarget(2, slide1)
System.Threading.Thread.Sleep(100)
TrySetTarget(6, slide2)
System.Threading.Thread.Sleep(100)
TrySetTarget(12, slide3)
System.Threading.Thread.Sleep(100)
TrySetTarget(18, slide4)
System.Threading.Thread.Sleep(100)
TrySetTarget(23, slide5)
End If
End If
If CheckBox4.Checked = True Then
If TextBox12.Text >= TextBox3.Text OrElse TextBox6.Text >= TextBox3.Text OrElse TextBox8.Text >= TextBox3.Text OrElse TextBox10.Text >= TextBox3.Text OrElse TextBox13.Text >= TextBox3.Text Then
Timer16.Enabled = False
Timer1.Enabled = False
Button2.BackColor = Color.Crimson
Button2.Enabled = False
Button1.Enabled = True
Button1.BackColor = Color.Transparent
xTimer.Enabled = False
TrySetTarget(2, slide1)
System.Threading.Thread.Sleep(100)
TrySetTarget(6, slide2)
System.Threading.Thread.Sleep(100)
TrySetTarget(12, slide3)
System.Threading.Thread.Sleep(100)
TrySetTarget(18, slide4)
System.Threading.Thread.Sleep(100)
TrySetTarget(23, slide5)
End If
End If
' device.Dispose() is called automatically when the "Using" block ends,
' allowing other functions and processes to use the device.
End Using
Catch exception As Exception ' Handle exceptions by displaying them to the user.
'displayException(exception)
End Try
This function using the position to obtained the values of the gauges and the position of the servos.After that there is a group of if that calculate the value of the gauges using that position and finally the last if calculate the speed of the first servo.
If CheckBox2.Checked = True Then
If valor2 >= 0 And valor2 <= 0.1 Then
Velo1 = 0
ElseIf valor2 > 0.1 And valor2 <= 2 Then
Velo1 = 2
ElseIf valor2 > 2 And valor2 <= 4 Then
Velo1 = 2
ElseIf valor2 > 4 And valor2 <= 6 Then
Velo1 = 3
ElseIf valor2 > 6 And valor2 <= 8 Then
Velo1 = 4
ElseIf valor2 > 8 And valor2 <= 10 Then
Velo1 = 5
End If
End If
Then on other part of the code the program run sending the servo to target position after calculate the speed.
Here it is:
Application.DoEvents()
System.Threading.Thread.Sleep(5)
Application.DoEvents()
'End If
'If slide1 = des1 Then
' Velo1 = 1
' TrySetTarget(2, has1)
'End If
While slide1 <> has1 ' Puede que sea des1, probar!!!
System.Threading.Thread.Sleep(5)
'Application.DoEvents()
If contador1 <> 0 Then
TextBox15.Text = TextBox15.Text - 1
If TextBox15.Text = 0 Then
Timer16.Enabled = False
Timer1.Enabled = False
Button2.BackColor = Color.Crimson
Button2.Enabled = False
Button1.Enabled = True
Button1.BackColor = Color.Transparent
xTimer.Enabled = False
contador1 = contador1 + 2
End If
End If
Application.DoEvents()
'System.Threading.Thread.Sleep(5)
'Application.DoEvents()
If Velo1 = 0 Then
TrySetTarget(2, slide1)
aviso = aviso + 1
Else
TrySetTarget(2, has1)
aviso = 0
End If
'Application.DoEvents()
If aviso = 4 Then
System.Threading.Thread.Sleep(1500)
TrySetTarget(2, slide1 + 300)
End If
If aviso = 6 Then
System.Threading.Thread.Sleep(1500)
TrySetTarget(2, slide1 + 300)
End If
If aviso = 8 Then
System.Threading.Thread.Sleep(1500)
TrySetTarget(2, slide1 + 300)
End If
If aviso = 10 Then
System.Threading.Thread.Sleep(1500)
TrySetTarget(2, slide1)
Timer16.Enabled = False
'Timer1.Enabled = False
Button2.BackColor = Color.Crimson
Button2.Enabled = False
Button1.Enabled = True
Button1.BackColor = Color.Transparent
xTimer.Enabled = False
'contador1 = contador1 + 2
MsgBox("Se finalizara la sesión debido a que el usuario no puede/quiere hacer el ejercicio")
End If
End While
My problem is that when the program run, at first the position is correct but when the while run again the position is the final, so there are no intermediate positions.