Oculus Prime - Another Script Example, Auto-starting Scripts

Oculus Prime - Another Script Example, Auto-starting Scripts

This page describes a method of auto-starting a script on boot, so it will always be running to enhance the functionality of Oculus Prime. As usual, we’re sticking with Python scripts as an example, but the same method can easily be applied to other scripting languages.

Auto Starting Scripts

1 – Create a Script: Switch Between Navigation Routes Based on Time of Day

The example script below allows you to extend the web-browser GUI functionality, which (currently) only allows for scheduling a single navigation route. This script does the following:

-Connects to the server, setting oculusprimesocket.reconnect to True first, so the script won’t exit in case the script gets started before the server.
-Gets the name of the currently running route
-Gets the current hour of the day
-Runs one of two routes, depending on the hour of the day (if it isn’t already running)
-Restart loop

#!/usr/bin/env python
import time, datetime, oculusprimesocket
# connect to server app, keep trying if not running yet
oculusprimesocket.reconnect = True
oculusprimesocket.connect()
while True:
# get current route name by reading state variable 'navigationroute'
oculusprimesocket.clearIncoming() # clear buffer
oculusprimesocket.sendString("state navigationroute")
s = oculusprimesocket.waitForReplySearch("<state> navigationroute")
# return all text after 'navigationroute[SPACE]'
currentroute = s.split("navigationroute ",1)[1]
# get current hour
now = datetime.datetime.now()
currenthour = now.hour
# start appropriate route depending on hour of day, if not already running
if (currenthour > 20 or currenthour < 7) and not currentroute == "night inspection":
oculusprimesocket.sendString("runroute night inspection")
elif (currenthour < 20 and currenthour > 7) and not currentroute == "day patrol":
oculusprimesocket.sendString("runroute day patrol")
# wait a minute, then continue loop
time.sleep(60)

view rawmulti_routes.py hosted with ❤ by GitHub

2 – Save the Script Somewhere on Oculus Prime

The script will need to go somewhere on the robot. For this example, create a folder under the 'oculusPrime' directory in the home folder (or use another location of your choice):

$ mkdir /home/oculus/oculusPrime/telnet_scripts

Use a text editor to save the script above in this location, then set it to executable by doing:

$ chmod u+x /home/oculus/oculusPrime/telnet_scripts/multi_routes.py


3 – Script Support Files

Many scripts are stand-alone, but the one above happens to import the oculusprimesocket python module, so the module will have to be somewhere Python can find it. An easy way is to just put it in the same folder that the script is in. So, copy the file from here into the 'telnet_scripts' folder created above.

4 – Set to Run on Boot

There are several ways in Ubuntu to auto-run programs on boot, but probably the simplest is to add the appropriate commands to the bottom of the '/etc/rc.local' system startup script, just above the last 'exit 0' line, like this example:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# already added here by xaxxon default
/home/oculus/oculusPrime/access_point_manager/run &
# new custom script auto-start commands
cd /home/oculus/temp/telnet_scripts/
/home/oculus/temp/telnet_scripts/run multi_routes.py &
exit 0

view rawrc.local hosted with ❤ by GitHub

The first of the last 2 lines above 'exit 0' changes the working directory to the 'telnet_scripts' folder (required so Python can find the oculusprimesocket module), and the next line runs the script (notice the full path is required as part of the command).