Asked — Edited
Resolved Resolved by JustinRatliff!

Comparing Time

I'm having a headache trying to comparing time. I have a variable $CustomerTime . It holds the last time motion was detected in the customer area. My plan is to have my robot look like he has fallen asleep. So I have the current time $time being stuffed into $CustomerTime each time motion is detected. So I want the robot to go to sleep when there has not been any motion in the last 10 minuets. I have the sleep and wake scripts working fine. But this comparing time is a pain.


ARC Pro

Upgrade to ARC Pro

Subscribe to ARC Pro, and your robot will become a canvas for your imagination, limited only by your creativity.

#1  

Are you going to share your code on how you are doing that, or trying to do that?

#2  

$waitUntilDate = ""
$waitUntilTime = ""
$dateLength = 0
$waitUntilDate = addminutes(cdatetime($date),10)
$dateLength = Length($WaitUntilDate)
$waitUntilTime = substring($waitUntilDate,$DateLength-11,11)
if(substring($waitUntilTime,0,1)=" ")
  $waitUntilTime = substring($waitUntilTime,1,6)
else
  $waitUntilTime = substring($waitUntilTime,0,7)
endif
$waitUntilHour = substring($waitUntilTime,0,1)
$waitUntilMinute = substring($waitUntilTime,2,2)

#use this in a script to wait until an hour and minute to run something.
#WaitUntilTime( $waitUntilHour, $waitUntilMinute ) 

#3  

you will have to adjust this some but it is an example of how you could go about doing this. More logic would need to be put in place to account for hours greater than 9, but it could easily be moved up to be held within the if statement.

#4  

This is perhaps an easier way to perform what you are seeking. Instead of making $CustomerTime = $time which gives you hour, minutes and seconds, you really only need to look at the minutes after motion is detected.


#When motion is detected
if ($Motion = "Y")
$CustomerTime = $Minute
EndIf

#to go sleep
if ($Minute > $CustomerTime + 9)
Print ("I'm sleeping")
#run your print sleep script
endif

#5  

That won't work if it goes past 60. Your logic only works if the current minute is less than 52. You have to account for the hour also.

#6  

Sorry I did not post my code. There wasn't enough code to call it code yet. Bt here it is:


# check for motion
if(getDigital(D18)=1)
Print (" associate detected")
$Associate = 1
$AssociateTime = $time
sayEZB(" I SEE a associate")
sleep(3000)
endif

if(getDigital(D17)=1)
$Customer = 1
$CustomerTime = $time
Print ("customer detected")
sayEZB(" I SEE an Customer. ")
sleep(3000)
endif

 #Lets see if its time to sleep.
 $SleepTrip = $time +  5
 if($AssociateTime =   ) or ($CustomerTime =  )
 
 endif



As you can see I did not get vary far. I was stumbling over the hours vers min. In my variable $AssociateTime holds the hole time and not just the min, because it would be possible for the minuets to be less then the limit and yet have the hour gone over the limit. So I gues first I must compare hours then minuets. I'm thinking I will re doo my $AssociateTime and change that to $AssociateTimeHour and on $AssociateTimeMin . I will then compare the hours first and last the min. hhmmm.....

#7  

I think he only wants to check to see if 10 minutes have passed with no Motion for his robot to pretend to sleep.

If you needed to go passed 60, you would need to incorporate the $hour variable, and that is very good catch David, if the time Motion was detected happened to be 50 or higher the logic of the script would fail, but it can be fixed with another variable by checking the hour.


#When motion is detected
if ($Motion = "Y")
$CustomerTime = $Minute
$CustomerHour = $Hour
EndIf

#to go sleep
if ($Minute > $CustomerTime + 9)
Print ("I'm sleeping")
#run your print sleep script
Elseif ($hour > $CustomerHour)
Print ("I'm going to sleep")
#run your print sleep script
Elseif ($hour < $CustomerHour)
Print ("I'm going to sleep now")
#run your print sleep script

endif

Edited: I added a less than hour check incase the time was 2300 hours

#8  

$waitUntilDate = addminutes(cdatetime($date),10)
$dateLength = Length($WaitUntilDate)
$waitUntilTime = substring($waitUntilDate,$DateLength-11,11)
if(substring($waitUntilTime,0,1)=" ")
$waitUntilTime = substring($waitUntilTime,1,6)
$waitUntilHour = substring($waitUntilTime,0,1)
$waitUntilMinute = substring($waitUntilTime,2,2)
else
$waitUntilTime = substring($waitUntilTime,0,7)
$waitUntilHour = substring($waitUntilTime,0,2)
$waitUntilMinute = substring($waitUntilTime,3,2)
endif

#use this in a script to wait until an hour and minute to run something.
#WaitUntilTime( $waitUntilHour, $waitUntilMinute )

this allows the system to calculate the hour and minute calculation for you.

#9  

If this were a business critical application, you would also have to account for year, month, and day, but I dont think your robot is going to take down your business. For example, if it were Dec 31, 2015 11:59:00 PM and you added 10 minutes, it would affect month, day, year, and hour.

I think the example provided would be good enough unless you want to spend the time accounting for all of these things. The example I gave you handles all of these situations.

#10  

Ok here is what I have come up with:



# check for motion
if(getDigital(D18)=1)
ControlCommand( "Script Manager", ScriptStart, "Wake" )
Print (" associate detected")
$Associate = 1
$AssociateTime = $Minute
$AssociateHour = $hour
sayEZB(" I SEE a associate")
sleep(3000)
endif

if(getDigital(D17)=1)
$Customer = 1
$CustomerTime = $Minute
$CustomerHour = $Hour
ControlCommand( "Script Manager", ScriptStart, "Wake" )
Print ("customer detected")
sayEZB(" I SEE an Customer. ")
sleep(3000)
endif

 #Lets see if its time to sleep.
   
   if ($Minute > $CustomerTime + 9) and ($Minute > $AssociateTime + 9)
     Print ("I'm sleeping")
     ControlCommand( "Script Manager", ScriptStart, "Sleep" )
     #run your print sleep script
   Elseif ($hour > $CustomerHour) and ($hour > $AssociateHour)
     Print ("I'm going to sleep")
     ControlCommand( "Script Manager", ScriptStart, "Sleep" )
     #run your print sleep script
   Elseif ($hour < $CustomerHour) and ($hour < $AssociateHour)
     Print ("I'm going to sleep")
     ControlCommand( "Script Manager", ScriptStart, "Sleep" )
     #run your print sleep script
   endif  



#11  

d.cochran ,

No its not a mission critical job my rot is doing. He is just greeting the customer and going over our rates. The robots job is to keep the customer entertained until I can get from the back of my shop to the front where the customer area is.

This portion of the project is to make my robot look as if he has gone to sleep after say 10 min. of no motion. So what he will do is turn on 2 relays. These relays turn off his eyes. Then we stop motion tracking on the cam only. The pir's will keep right on going. The speech is also paused. Then as soon as motion is detected from the PIR then he wakes up greets the customer and keeps them busy for a bit.

#12  

I understand. I have worked for a very long time as a business developer so I guess I just account for these things when I code. All good and I am glad you found an answer.

#13  

I've been away the last few days so I haven't had a chance to post this until now. Here's a couple of scripts I've been using, they were mostly written by Rich. It monitors motion thru a PIR and when there hasn't been any motion for 100 sec (you can set that for what ever you want) the robot powers down or it can do what ever you want. When motion is detected it powers up again and the monitor starts over again. Monitor


# Set a lable for the start
:start

# Reset the counter
$Monitor_Counter = 0

# Repeat
# Monitor for movement for 30 seconds or what ever length of time you want
repeatuntil($Monitor_Counter = 100)

# Increase the counter by 1
$Monitor_Counter++

# Check for detection on PIR - since we need to check 5 times per second use repeat

# checking counter needed
$Monitor_Check_Counter = 0

repeatuntil($Monitor_Check_Counter = 5)
# Add 1 to the check counter
$Monitor_Check_Counter++

# Check the status of the digital port
#Check the status of the ADC port
#$PIR = GetDigital(D2)
$PIR = GetADC(ADC7)

# Check if it is high or low and take action
if($PIR > 140)

$motion = 1
Else
$motion = 0
endif
if($motion = 1)
 # Motion detected, return to start
 Goto(start)
endif
# Note - no else or elseif required since nothing happens if motion isnt detected
print($PIR)
# Wait a while to save system resources
Sleep(200)
endrepeatuntil

# 1 second has passed by this point
# repeat 30 times
endrepeatuntil



# 30 seconds passed - Enter low power plus monitoring mode
ControlCommand("Script Manager", ScriptStart, "LowPowerMonitoring")

# End the script
#HALT()

Low Power Monitoring


# Enter your low power mode commands here
$response = GetRandomUnique(0,2)
if($response = 0)
Sayezb("I am powering down")

elseif($response = 1)
Sayezb("let's give these servos a rest")

endif

sleep(1500)
ControlCommand("Auto Release", PauseOff)
ControlCommand("Personality Generator", PauseOn)
sleep(500)



:start

# Once in low power mode wait for a change on the PIR ADC port
WaitForChange(GetADC(ADC7))
$PIR = GetADC(ADC7)
if($PIR > 180)
$motion = 1
#print($PIR)

# Power up robot
Sleep(2000)
ControlCommand("Script Manager", ScriptStart, "Power Up")
sleep(500)
ControlCommand("Auto Release", PauseOn)
sleep(500)

endif


#14  

d.cochran and JustinRatliff,

Thanks for all your help. Here is the completed functioning script. Boy I learned a few things on this one. Mainly that you should save your work OFTEN. I had a few hours into it and everything was working really well then I made one change and ARC crashed and so did my work. Lockley had some of it paste3d into note pad. But it was still a few hours to get it back. But at last it works just fine.


# check for motion
if(getDigital(D18)=1)
Print (" associate detected")
$Associate = 1
$AssociateTime = $Minute
$AssociateHour = $Hour
sayEZB(" I SEE a associate")
sleep(3000)
endif

if(getDigital(D17)=1)
$Customer = 1
$CustomerTime = $Minute
$CustomerHour = $Hour
Print ("customer detected")
sayEZB(" I SEE an Customer. ")
sleep(3000)
endif

 #Lets see if its time to sleep.
if ($Minute > $CustomerTime + $SleepTimeOut) and ($Minute > $AssociateTime + $SleepTimeOut) and ($Sleepx = 0) and ($AssociateTime != "null") 
     Print ("I'm sleeping 1")
     ControlCommand( "Script Manager", ScriptStart, "Sleep" )
     #run your print sleep script 
Elseif ($hour > $CustomerHour) and ($hour > $AssociateHour) and ($Sleepx = 0)
     Print ("I'm going to sleep 2")
     $SleepX = 1
     ControlCommand( "Script Manager", ScriptStart, "Sleep" )
     #run your print sleep script
 Elseif ($hour > $CustomerHour) and ($hour > $AssociateHour) and ($Sleepx = 0)
     Print ("I'm going to sleep 3")
     $SleepX = 1
     ControlCommand( "Script Manager", ScriptStart, "Sleep" ) and ($Sleepx = 0)
     #run your print sleep script
 
 endif