How To Stop A Loop With Respect To Time

Universal Controller for CAN Applications
Post Reply
yhadad
Posts: 33
Joined: Tue 30. Oct 2018, 18:13

How To Stop A Loop With Respect To Time

Post by yhadad » Wed 20. Nov 2019, 22:22

Hi,

I am currently controlling a shift actuator that is also giving me position feedback in PWM signal. It works great by using my PCAN-MIO and an H-Bridge. I can control the shift actuator to turn in one direction and stop until it reaches a specific position based on the reading of the position sensor feedback. Here is the loop that I am using below. The only challenge that I have is that:

1) I also need to stop this loop with respect to time in 200ms in case it does not reach the target position due to gear clash. I want to add that logic so that I do not burn the shift actuator motor.

2) I would also like to set a condition to stop the loop if the current sense feedback exceeds a current limit.

Code: Select all

'Start loop logic 
  PrintToOutputWindow "Now start LOOP..."
  

 'Safety Condition Shift Range Limit Detector
     Var13.Value = 1
    PrintToOutputWindow "value set to : " & CStr(CInt(Var13.Value))
    
    Wait(1000)
    
    If Var14.Value > 730 Then 
       
       MsgBox "Shift Actuator is out of shift range",,"Error" 
            
       Exit Sub
    
    Elseif Var14.Value => 695.1 Then 
       
       MsgBox "Shift Actuator is already in 4 Low position",,"Error" 
            
       Exit Sub
       
    Elseif Var14.Value <= 220 Then  
       
       MsgBox "Shift Actuator is out of shift range",,"Error" 
          
       Exit Sub
    
    End If
         
    '------------------------------------------------------------------------------------------------------------------------------------
    'Start Shift
        
    
    If Var14.Value < 695 then  
    
    Var15.Value = 1
    PrintToOutputWindow "value set to : " & CStr(CInt(Var15.Value))
    
    Var20.Value = 1
    PrintToOutputWindow "value set to : " & CStr(CInt(Var20.Value))
    
       
    Do until Var14.Value > 695 
    
    PrintToOutputWindow "value set to : " & CStr(CInt(Var15.Value))
    
       
    loop
   
    
    Var15.Value = 0
    
    Wait(10)
    
    Var20.Value = 0
     
    Exit sub
    
    
    End if
I have tried several codes but had no success to Exit/ End Do loop :(

Can you please help?

M.Heidemann
Sales & Support
Sales & Support
Posts: 1083
Joined: Fri 20. Sep 2019, 13:31

Re: How To Stop A Loop With Respect To Time

Post by M.Heidemann » Thu 21. Nov 2019, 10:22

Hello,

The code you included looks like VBS, which leads to the question:

Since using PPCAN-Editor is the only way to program the PCAN-MIO,

How are you using VBS to control the PCAN-MIO?

Are you using PCAN-Explorer 6 here as well?


Please clarify how your current setup looks like and how you are using it.


Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

yhadad
Posts: 33
Joined: Tue 30. Oct 2018, 18:13

Re: How To Stop A Loop With Respect To Time

Post by yhadad » Thu 21. Nov 2019, 15:47

Hello Marvin,

I am using VBS that is in PCAN-Explorer 6. If you like, I can email you the .pem file so that you can see the entire code if that helps?

M.Heidemann
Sales & Support
Sales & Support
Posts: 1083
Joined: Fri 20. Sep 2019, 13:31

Re: How To Stop A Loop With Respect To Time

Post by M.Heidemann » Fri 22. Nov 2019, 11:37

Hello,

To use a value (of a signal for example) as a break-condition you could use a While-Loop, for example:

Code: Select all

 

Sub WaitForValue()
Value = 1 
    Do while Value < 156
     PrintToOutputWindow Value
     Value = Value + 1 'This simulates the value rising'
    Wait 1
    loop
 PrintToOutputWindow "Limit was reached " 
 End Sub

You could also use the function GetSystemTime, like this:

Code: Select all


Sub TestTimeWhile()

 Dim client, starttime, currenttime, timediff
     Set client = Connections(1).CommunicationObject.Parent
     startime = client.GetSystemTime
       Do while timediff < 200
         currenttime = client.GetSystemTime
         timediff = currenttime - starttime
         PrintToOutputWindow currenttime
         Wait 1
       PrintToOutputWindow "Done"
       Wait 1
    loop
End Sub
Note: You need an active connection to use "GetSystemTime" in your script.
GetSystemTime is a function of the PCANClient object.

Also note:

If you use "PrintToOutputWindow" in a loop like you do it will slow down the whole script,
preferably you initate a wait of 1 to prevent lockups.

You could also combine those two concepts like this:

Code: Select all

Sub TimeAndValue()

 Dim client, starttime, currenttime
    Set client = Connections(1).CommunicationObject.Parent
    startime = client.GetSystemTime
    
    Do until YourVariable.Value > 695
       currenttime = client.GetSystemTime
       If currenttime - starttime >= 200 Then
         Exit Do
       End If
       Wait 1
    loop
End Sub
Best Regards


Marvin
---
Marvin Heidemann
PEAK-Support Team

yhadad
Posts: 33
Joined: Tue 30. Oct 2018, 18:13

Re: How To Stop A Loop With Respect To Time

Post by yhadad » Mon 25. Nov 2019, 19:46

Thank you Marvin,

Your codes worked for adding time limit and reaching a limit value as well. I also added "Wait 1" to the rest of my script as you had suggested and things are running much better now :)

I just wanted to see if I understood you correctly about your note below:
M.Heidemann wrote:Note: You need an active connection to use "GetSystemTime" in your script.
GetSystemTime is a function of the PCANClient object.
Do you mean that I need to be connected to a Peak hardware such as PCAN-USB? or do you mean something else?
Last edited by K.Wagner on Tue 26. Nov 2019, 08:03, edited 1 time in total.
Reason: Quote formatting for better reading

M.Heidemann
Sales & Support
Sales & Support
Posts: 1083
Joined: Fri 20. Sep 2019, 13:31

Re: How To Stop A Loop With Respect To Time

Post by M.Heidemann » Tue 26. Nov 2019, 08:42

Hello,

You need the PCANClient object present.

The PCANClient object can be accessed by using the CommunicationObject property of the Connection object.

It also can be a virtual connection, if that is the case hardware does not have to be attached for this to work.

It represents an interface between PCAN-Explorer 6 and the CAN bus.

If you take a look at the PCAN-Explorer 6 documentation and search for "PCANClient Object" you will see, what the PCANClient "GetSystemTime" referenced as one of it's methods.

Best Regards


Marvin
---
Marvin Heidemann
PEAK-Support Team

Post Reply