Oculus Prime - Script Examples using the Telnet API

Oculus Prime - Script Examples using the Telnet API

This page contains examples of customizing the functionality of Oculus Prime using Python scripts, interfacing with the robot using the telnet API, and importing the Oculus Prime python module.

On this page:
   Synth Voice on Detection
   Listen for Noise While Docked
   Wait to Reach Waypoint
   Loop Through Waypoints

Synth Voice on Detection

The following script, useful for running along with patrol routes, has the robot make an audible announcement whenever any of its sensing functions (sound, motion, human) detect anything.

It sets oculusprime.reconnect to True before connecting, which causes the script to keep trying to connect and re-connect to the server, even if the server is stopped and re-started. This way the script can be run by default on system boot.

The sensing functions write results to the streamactivity state variable, so the script uses the python module’s waitForReplySearch function to wait for any writes to that variable, then speaks up using the speech command:

#!/usr/bin/env python
import oculusprimesocket
oculusprimesocket.reconnect = True
oculusprimesocket.connect()
while True: # loop forever
# wait for non-null streamactivity state value
oculusprimesocket.waitForReplySearch("<state> streamactivity (?!null)")
# synth voice
oculusprimesocket.sendString("speech intruder alert")

view rawspeech_on_detect.py hosted with ❤ by GitHub


Listen for Noise While Docked

The script below waits until the robot is docked by requesting the dockstatus state variable, then turns on the microphone and enables sound detection. If it hears a noise, it will launch a pre-defined patrol route named “recon” to go and check out the situation. This presumes that the “recon” route is scheduled to run anytime; ie., set to 7 days a week, with ‘stop after’ set to 24 hours.

When the route is complete and the robot re-docks, the route is cancelled – so the route will only run once, ignoring the scheduled minutes between runs.

#!/usr/bin/env python
import time, oculusprimesocket
oculusprimesocket.reconnect = True
oculusprimesocket.connect()
while True: # loop forever
# wait for docked
dockstatus = None
while not dockstatus == "docked":
time.sleep(10)
oculusprimesocket.sendString("state dockstatus")
s = oculusprimesocket.waitForReplySearch("<state> dockstatus")
dockstatus = s.split()[3]
time.sleep(10) # wait for camera to turn off (if just auto-docked)
oculusprimesocket.sendString("cancelroute") # cancel active patrol route
# turn on mic and enable sound detection
oculusprimesocket.sendString("publish mic")
oculusprimesocket.sendString("setstreamactivitythreshold 0 40")
# wait for detection
oculusprimesocket.waitForReplySearch("<state> streamactivity (?!null)")
# run patrol route named 'recon'
oculusprimesocket.sendString("runroute recon")
# wait for route start and un-dock before restarting loop
oculusprimesocket.waitForReplySearch("<state> dockstatus un-docked")

view rawlisten_while_docked.py hosted with ❤ by GitHub


Wait to Reach Waypoint

Below is a simple example of having a script wait for the robot to reach a waypoint. The ROS node remote_nav.py handles sending navigation commands and status back and forth between ROS and the Oculus Prime Server Application, and it helps to understand a bit how that works:

Waypoints aren’t sent to ROS by name, ROS only understands ‘where to go next.’ So instead, the server application assigns the waypoint coordinates (x meters, y meters, angle in radians) to the rossetgoal state variable. The ROS node continuously checks for that state variable for new goal coordinates to send the robot to. When it receives and accepts a new goal, it deletes the rossetgoal state variable and applies the coordinates to the roscurrentgoal state variable. When it reaches the goal, the node deletes roscurrentgoal.

The script below sets a goal using the gotowaypoint command, then waits until the telnet stream reports that the roscurrentgoal state value has been deleted. It then assumes that the waypoint has been reached, and exits.

#!/usr/bin/env python
import oculusprimesocket
oculusprimesocket.connect()
oculusprimesocket.sendString("gotowaypoint some waypoint")
# when waypoint reached, state var roscurrentgoal is deleted by remote_nav node:
oculusprimesocket.waitForReplySearch("<state> deleted: roscurrentgoal")
print "waypoint reached"

view rawwait_for_waypoint.py hosted with ❤ by GitHub


Loop Through Waypoints

Expanding on the example above, this script assigns a set of waypoints to a python list, then iterates through the list, causing the robot to drive through each waypoint forever (or until the battery dies).

#!/usr/bin/env python
import oculusprimesocket
import time
oculusprimesocket.connect()
# define waypoint names in loop
waypoints = ["waypoint1", "waypoint2", "waypoint3", "waypoint4"]
i=0
while True:
print "setting waypoint: "+waypoints[i]
oculusprimesocket.sendString("gotowaypoint "+waypoints[i])
# wait to reach waypoint:
oculusprimesocket.waitForReplySearch("<state> deleted: roscurrentgoal")
time.sleep(1)
i += 1
if i >= len(waypoints):
i=0

view rawwaypoints_loop.py hosted with ❤ by GitHub