(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Clojure rosjava project setup

Description: This explains how to use the Clojure language to interact with ROS via rosjava

Tutorial Level:

WARNING: This documentation refers to an outdated version of rosjava and is probably incorrect. Use at your own risk. Follow updates on site: https://github.com/rosjava/rosjava_core and https://github.com/tkruse/rosjava_wiki_tutorials


Groovy is a LISP language (not Common LISP) that is based on the Java Virtual Machine. As such we can use the rosjava libraries to interact with ROS.


Create a project just like in rosjava_tutorial_pubsub

Copy the build.gradle file from there, but make these additions:

apply plugin: 'clojure'

buildscript {
    repositories {
        mavenRepo name: 'clojars', urls: 'http://clojars.org/repo'
    dependencies {
        classpath 'clojuresque:clojuresque:1.5.0'

aotCompile = true

repositories {
  maven {
    url 'http://robotbrains.hideho.org/nexus/content/groups/ros-public'

dependencies {
  compile 'ros.rosjava_core:rosjava:0.0.0-SNAPSHOT'
  compile 'ros.rosjava_core:rosjava_messages:0.0.0-SNAPSHOT'
  compile 'ros.rosjava_core:rosjava_bootstrap:0.0.0-SNAPSHOT'
  compile 'org.clojure:clojure:1.2.1'

Note the above is not the complete file, only the modifications you have to merge with what you already have.

Note both the closure and clojuresque version may be outdated by the time you read this tutorial. It may be wise to find out the latest by trying out higher release numbers.

Creating a Listener

We will create a simple Listener as in the Publisher / Subscriber rosjava Tutorial:

Copy / Create the file src/main/java/org/ros/rosjava_tutorial_pubsub/Talker.java, we will reuse the same Talker. You can also use the tutorial talker from c++ or python tutorials, of course.

Create this file as src/main/clojure/org/ros/rosclojure_tutorial_pubsub/Listener.clj:

(ns org.ros.rosclojure_tutorial_pubsub
    (:import org.apache.commons.logging.Log

 :name org.ros.rosclojure_tutorial_pubsub.Listener
 :extends org.ros.node.AbstractNodeMain)

(defn -getDefaultNodeName [this]
  (new GraphName "rosclojure_tutorial_pubsub/listener"))

(defn -onStart [this, connectedNode]
      [log (.getLog connectedNode)
       subscriber (.newSubscriber
                     (. std_msgs.String _TYPE))
       messageListener (proxy 
                         (.info log (format  "I heard \"%1$s\"" 
                                             (.getData message)))))]
    (.addMessageListener subscriber messageListener)))

The file must go there because that's where gradle picks up clojure files.

Note I am not an experienced Clojure programmer, so the code above might be ugly and too verbose for Clojure, it is a rather literal translation from the Clojure tutorial.

build and run

To build, use gradle:

$ gradle installApp

To run, we use the same procedure as for rosjava:

$ roscore
$ ./build/install/rosjava_wiki_tutorial_pubsub/bin/rosjava_wiki_tutorial_pubsub org.ros.rosjava_tutorial_pubsub.Talker
$ ./build/install/rosjava_wiki_tutorial_pubsub/bin/rosjava_wiki_tutorial_pubsub org.ros.rosclojure_tutorial_pubsub.Listener

Wiki: rosjava_core/graveyard/rosclojure_project_setup (last edited 2013-10-19 12:06:15 by ThibaultKruse)