Clock

javadoc

The Clock can generate a regular series of messages for other Services to consume.  This can be very handy for simulating your robot is alive or for taking periodic measurements.  You can create multiple clock services for different functions within your robot.

clock = Runtime.start("clock","Clock")

will start a clock service but not start the clock.

clock.setInterval(1000)

will set the time between pulses to every 1000 milliseconds or 1Hz

clock.addListener("pulse", "python", "clock_pulse")

will cause the clock service to call the "clock_pulse" method in Python, you will need to have defined this method.  You can define the callback method like this

def clock_pulse(timedata):
    print("The clock has pulsed " + str(timedata))

The timedata is required to be in the definition and contains the current time.
There are other signals that can also be captured as well.

clock.addListener("clockStarted", "python", "clock_start")

def clock_start():
    print("The clock has started")

This will be called whenever the service is started.
You could use this to turn on or off an output or move a servo to open a slide while the clock is running.
The call back method would be like the following.

clock.addListener("clockStopped", "python", "clock_stopped")

def clock_stopped():
    print("The clock has been stopped")

This will be called when ever the clock service is stopped.
This can be used to start or stop a motor or to signal a method to put your robot to sleep.
The callback method would be on_stopped

The clock is capable of being started and stopped. Starting the clock can be done like this

clock.startClock()

The number passed in the function tells the clock service if it should call the pulse method at the very start of running (0) or wait until after the first period has elapsed (1)

clock.startClock(1)

This will reset the time pererod back to the start.
This can be useful for watchdog timmer or functions that will put your robot to sleep if there has been no activity for a predetermined amount of time, call this method where the is activity :-)

clock.stopClock()

This method will stop the clock from running, preventing further pulses from calling the pulse method.
Usefull onece your robot is already asleep.

An example of this would be to use two clock services.
One to blick eye lids on a robot, the other to put the robot to sleep when there was nothing else happening.
When the sleep timer clock pulses, it would stop it's own clock and that of the blink timer.
The blink timer would then when it's stop method is called close the eye lids.

When an event ocurs that wakes up the robot, the Sleep timer would be started, this in turn would start the blink timer and starting that would open the eye lids.
Each pulse of the blink timer would cause the ele lids to close then open again after a short interval.
The pulse routine for the blink timer could also set a new random time for the blink interval with the setInterval(time) method.

 

Example for develop:

#file : Clock.py edit raw

# start the services
clock = Runtime.start("clock","Clock")

# define the on_date event handler
def on_time(timedata):
    print("on_date " + str(timedata))

# define the on_epoch event handler
def on_epoch(timedata):
    print("on_epoch " + str(timedata))

# create a message routes
clock.addListener("publishTime", "python", "on_time")
clock.addListener("publishEpoch", "python", "on_epoch")

# start the clock
clock.setInterval(1000)
clock.startClock()
# optional : wait the first loop before execution start
# clock.startClock(1)
# clock.stopClock() when you are done