Asked — Edited
Resolved Resolved by Rich!

How To Run A Script At Set Intervals?

It seems like an easy thing to do but I cant quite seem to figure how to trigger a script at set intervals. For example if I want to run a re-calibration script every 30 minutes to home an encoder. I did this and put it in the personality generator but sometimes it will run the homing script a lot more then necessary.

Any advice is welcomed.


ARC Pro

Upgrade to ARC Pro

Unleash your robot's full potential with the cutting-edge features and intuitive programming offered by Synthiam ARC Pro.

#1  

@Dave You can create a timer script to run in the background....


:top
ControlCommand("Open", ScriptStart) #Run a script called "open" every 30 mins
sleep(1800000) #sleep for 30 minutes
goto (top)

#2  

Thanks Richard but last time I tried to make a script sleep for a long time I couldn't get it to Sleep more then a minute or two. Cant remember how long it was but it wasn't very long. Maybe they changed that.

United Kingdom
#3  

In a rush, I'll add more later if needed but look at the $hour and $minute variables and the WaitUntil() commands if you want to "sleep" for a long period:)

United Kingdom
#4  

Damn, hit close before I hit post. Had just written a quick and nasty script for this.

Try again:)


:loop
$waithour = $hour
$waitminute = $minute

IF($waitminute > 29)
  $waitminute = $waitminute - 30
  $waithour++
  IF($waithour = 24)
    $waithour = 0
  EndIf
Else
  $waitminute = $waitminute + 30
EndIf

WaitUntilTime($waithour, $waitminute)

# Your commands here

Goto(loop)

That should grab the current time, add on 30 minutes (or add on an hour then subtract 30 minutes if the minutes are above 30), if the hour equals 24 it resets to 0 (no need for anything greater since it will never get that far without resetting to 0) wait until the calculated time, execute your commands then loop back to the start.

It's crude and could be written better but I'm rushing:)

#5  

I haven't tested long sleep intervals so you could very well be right, Dave... Maybe you could string a bunch of sleep commands of shorter intervals in a row to make up the 30 minutes? Not sure that will work either so maybe Rich has a better option for you....

United Kingdom
#6  

Another alternative for long sleeps (if a long sleep command wont work) is a loop...


$SleepTimeInMinutes = 30
$Counter = 0
RepeatUntil($Counter = $SleepTimeInMinutes)
  Sleep(60000)
  $Counter++
EndRepeatUntil

Or if 60000 is still too long a sleep


$SleepTimeInMinutes = 30
$Counter = 0

# Maths stuff...
$SleepTimeInSeconds = $SleepTimeInMinutes * 60

RepeatUntil($Counter = $SleepTimeInSeconds)
  Sleep(1000)
  $Counter++
EndRepeatUntil

Both of those will use more processing than the WaitUntilTime() version but would allow the inclusion of a countdown timer until next execution or whatever.

#7  

@Rich... Actually, that's a really good option (a counter)...:)

United Kingdom
#8  

To go one further, and to make it more complicated than it needs to be:D


:begin
$SleepTimeInMinutes = 30
$Counter = 0

# Maths stuff...
$SleepTimeInSeconds = $SleepTimeInMinutes * 60

RepeatUntil($Counter = $SleepTimeInSeconds)

  # More maths stuff...
  $CountdownSeconds = $SleepTimeInSeconds - $Counter
  $CountdownMinutes = Floor($CountdownSeconds/60)
  $CountdownSeconds = $CountdownSeconds - ($CountdownMinutes * 60)

  # Add a leading 0 to seconds (because I'm picky!)
  IF($CountdownSeconds < 10)
    $LeadingZero = ":0"
  Else
    $LeadingZero = ":"
  EndIf

  Print("                      ")
  Print("         " + $CountdownMinutes + $LeadingZero + $CountdownSeconds )
  Print(" Until Next Execution ")
  Print("                      ")

  Sleep(1000)
  $Counter++
EndRepeatUntil

Print("                      ")
Print("         TASK         ")
Print("      EXECUTING!      ")
Print("                      ")

# Your commands here For Example
# ControlCommand("Calibrate", ScriptStartWait)

Goto(begin)

Complete with countdown timer (and a good example of the use of the Floor() math function that's under used) and emulated display within the script window (if it is a stand alone script not in Script Manager, and the control not resized).

Change $SleepTimeInMinutes as required (Default is 30 minutes) Add your code where indicated (Use ScriptStartWait to pause the countdown until after the commands have been run) The rest is automatic.

#9  

You guys are great! I need to stiffen up my script writing skills. I've been spending all my "robot" time working on getting the B9 arms to a working prototype stage (just dropped the working prototype off at the machinist this morning to reproduce it. More to follow in a few weeks) . Thank goodness for people like you that are willing to give a guy a hand. I thought there was an elegant way to do this and you found several. :)

Thanks again!:D

#10  

BTW, I ran into the sleep() issue over a year ago. As I recall I couldn't get a script to sleep more then one minute. I brought it up on this forum then. If this is still the case then perhaps your sleep loop scripts are the way to go.

#11  

@Rich, Just a note to say that your script to run a script at set timed interval works perfectly. You said it was a ruff and crude script but I don't see how you could have improved it. It runs every half hour exactly based on when I started ARC.

Now if I wanted to adjust the time frame what would I need to change?

United Kingdom
#12  

@Dave, which script, I posted a few.

The last one is just adjusting the first variable $sleeptime from 30 to however many minutes you want. The other scripts, find any reference to 30 and change it to however many minutes you want (up to an hour) I think, I'd have to check properly but should be leaving for work really!

#13  

It's the first one where it checks the system clock.

Dont worrie. I'll get it figured out based on what you just stated. Get to work and have a good day. It's time for me to get to bed. :D

United Kingdom
#14  

First one;


:loop
$waithour = $hour
$waitminute = $minute

# Change the 29 to however many minutes minus 1 minute
IF($waitminute > 29)

# Change the 30 to however many minutes
$waitminute = $waitminute - 30
$waithour++
IF($waithour = 24)
$waithour = 0
EndIf
Else
# Change the 30 to however many minutes
$waitminute = $waitminute + 30
EndIf

WaitUntilTime($waithour, $waitminute)

# Your commands here

Goto(loop)

#15  

Rich, your help is very appreciated. You make the learning curve a lot less bumpy. :)