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. |
Scala project setup
Description: This explains how to use the Scala language to interact with ROS via rosjavaTutorial 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
Introduction
Scala is a functional programming language that is based on the Java Virtual Machine. As such we can use the rosjava libraries to interact with ROS.
Setup
Create a project just like in rosjava_tutorial_pubsub
Copy the build.gradle file from there, but make these modifications:
apply plugin: 'scala' 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' // Libraries needed to run the scala tools scalaTools 'org.scala-lang:scala-compiler:2.8.1' scalaTools 'org.scala-lang:scala-library:2.8.1' // Libraries needed for scala api compile 'org.scala-lang:scala-library:2.8.1' }
The scala version may be outdated by the time you read this tutorial.
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/scala/org/ros/rosscala_tutorial_pubsub/Listener.scala:
package org.ros.rosscala_tutorial_pubsub import org.ros.message.MessageListener import org.ros.node.AbstractNodeMain import org.ros.namespace.GraphName import org.ros.node.ConnectedNode import org.ros.node.NodeMain import org.ros.node.topic.Subscriber import org.apache.commons.logging.Log class Listener extends AbstractNodeMain { override def getDefaultNodeName(): GraphName = { new GraphName("rosjava_tutorial_pubsub/listener") } override def onStart(connectedNode: ConnectedNode) { var log = connectedNode.getLog(); var subscriber: Subscriber[std_msgs.String] = connectedNode.newSubscriber( "chatter", std_msgs.String._TYPE); subscriber.addMessageListener(new MessageListener[std_msgs.String]() { override def onNewMessage(message: std_msgs.String) { log.info("I heard: \"" + message.getData() + "\""); } }); } }
The file must go there because that's where gradle picks up scala files.
Note I am not an experienced Scala programmer, so the code above might be ugly and too verbose for scala, it is a rather literal translation from the rosjava 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.rosscala_tutorial_pubsub.Listener
Interactive Usage
Follow the instructions to create a start script as for groovysh
The start script should then look like this:
APP_HOME="`pwd -P`/build/install/rosscala_tutorial_pubsub" CLASSPATH=$APP_HOME/lib/rosscala_tutorial_pubsub-0.0.0-SNAPSHOT.jar:\ $APP_HOME/lib/rosjava-0.0.0-SNAPSHOT.jar:\ $APP_HOME/lib/rosjava_messages-0.0.0-SNAPSHOT.jar:\ $APP_HOME/lib/rosjava_bootstrap-0.0.0-SNAPSHOT.jar:\ $APP_HOME/lib/scala-library-2.9.2.jar:\ $APP_HOME/lib/commons-pool-1.6.jar:\ $APP_HOME/lib/jsr305-1.3.9.jar:\ $APP_HOME/lib/guava-12.0.jar:\ $APP_HOME/lib/com.springsource.org.apache.commons.lang-2.4.0.jar:\ $APP_HOME/lib/com.springsource.org.apache.commons.codec-1.3.0.jar:\ $APP_HOME/lib/com.springsource.org.apache.commons.io-1.4.0.jar:\ $APP_HOME/lib/netty-3.5.1.Final.jar:\ $APP_HOME/lib/com.springsource.org.apache.commons.logging-1.1.1.jar:\ $APP_HOME/lib/junit-3.8.2.jar:\ $APP_HOME/lib/xml-apis-1.0.b2.jar:\ $APP_HOME/lib/ws-commons-util-1.0.1.jar:\ $APP_HOME/lib/com.springsource.org.apache.commons.httpclient-3.1.0.jar:\ $APP_HOME/lib/apache_xmlrpc_common-0.0.0-SNAPSHOT.jar:\ $APP_HOME/lib/apache_xmlrpc_server-0.0.0-SNAPSHOT.jar:\ $APP_HOME/lib/apache_xmlrpc_client-0.0.0-SNAPSHOT.jar:\ $APP_HOME/lib/com.springsource.org.apache.commons.net-2.0.0.jar:\ $APP_HOME/lib/dnsjava-2.1.1.jar:\ $APP_HOME/lib/xercesImpl-2.9.1.jar:\ $APP_HOME/lib/commons-cli-1.3-SNAPSHOT.jar scala -cp $CLASSPATH