This is an example of how use Scala with ROS2, based on RCLJava. The following was tested on Ubuntu 20.04 using the ROS2 Foxy distribution.
-
Install ROS2 (this example is for Foxy).
-
Install Java and a JDK.
sudo apt install default-jdk -
Install Gradle. Make sure you check the gradle releases page to see if a newer version is available.
VERSION=7.3 wget https://services.gradle.org/distributions/gradle-${VERSION}-bin.zip -P /tmp sudo unzip -d /opt/gradle /tmp/gradle-${VERSION}-bin.zip -
Set up Gradle environment variables. Open a file with the following name:
sudo nano /etc/profile.d/gradle.shThen copy-and-paste the following
export GRADLE_HOME=/opt/gradle/latest export PATH=${GRADLE_HOME}/bin:${PATH}Save and close the file. Then make the script executable and source the script.
sudo chmod +x /etc/profile.d/gradle.sh -
Install Scala.
sudo apt-get install scala -
Install build tools.
sudo apt install curl python3-colcon-common-extensions python3-pip python3-vcstool -
Install Gradle extensions for colcon.
python3 -m pip install -U git+https://github.com/colcon/colcon-gradle python3 -m pip install --no-deps -U git+https://github.com/colcon/colcon-ros-gradle
-
Source your ROS2 installation, for example:
source /opt/ros/foxy/setup.bash -
Source the Gradle environment.
source /etc/profile.d/gradle.sh -
Download the ROS2 Java repositories into a workspace.
mkdir -p ros2_java_ws/src cd ros2_java_ws curl -skL https://raw.githubusercontent.com/ros2-java/ros2_java/main/ros2_java_desktop.repos | vcs import src -
Install ROS dependencies (on Linux). Note that you may need to skip other keys other than listed below if errors appear.
rosdep install --from-paths src -y -i --skip-keys "ament_tools" --skip-keys "ament_pep8" -
Build desktop packages.
colcon build --symlink-install
-
Add the Scala libraries to Java's class path.
CLASSPATH=/usr/share/scala-2.11/lib/*:$CLASSPATH export CLASSPATH -
Clone ros2-scala-example into your
srcdirectory.cd src git clone https://github.com/xander-will/ros2_scala_example.git cd .. -
Build the Scala example package.
colcon build --packages-select scala_example -
Source the setup files.
. install/setup.bash -
Run the code through java (unsure of how to expose the entry point to ROS2 as of now). If you open a second terminal to run both the publisher and subscriber at once, make sure you source the ROS2 setup script, the gradle environment script, and the local setup files, as well as readding the Scala lib to CLASSPATH.
java org.ros2.rcljava.scala.examples.PublisherLambda java org.ros2.rcljava.scala.examples.SubscriberLambda