diff --git a/.gitignore b/.gitignore index 77617a1..e047265 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,8 @@ gradle-app.setting # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 # gradle/wrapper/gradle-wrapper.properties + +# Eclipse +.classpath +.project +.settings/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..76c5a6c --- /dev/null +++ b/.travis.yml @@ -0,0 +1,41 @@ +sudo: required + +language: generic + +services: + - docker + +before_install: +- docker pull theosakamg7/ros2java:latest +- cd /home/travis/build +- mkdir -p ament_ws/src +- cd /home/travis/build/ament_ws +- docker run -u "$UID" -it --rm -v `pwd`:`pwd` -w `pwd` theosakamg7/ros2java:latest sh -c "/usr/bin/wget https://gist.githubusercontent.com/Theosakamg/e6084cfafa6b7ea690104424cef970a2/raw/ament_java.repos" +- docker run -u "$UID" -it --rm -v `pwd`:`pwd` -w `pwd` theosakamg7/ros2java:latest sh -c "/usr/bin/vcs import src < ament_java.repos" +- docker run -u "$UID" -it --rm -v `pwd`:`pwd` -w `pwd` theosakamg7/ros2java:latest sh -c "src/ament/ament_tools/scripts/ament.py build --symlink-install --isolated" +- cd /home/travis/build +- mkdir -p ros2_java_ws/src +- cd /home/travis/build/ros2_java_ws +- docker run -u "$UID" -it --rm -v `pwd`:`pwd` -w `pwd` theosakamg7/ros2java:latest sh -c "/usr/bin/wget https://gist.githubusercontent.com/Theosakamg/d9259bbc708c5145255fbdeb25e65e19/raw/ros2_java_desktop.repos" +- docker run -u "$UID" -it --rm -v `pwd`:`pwd` -w `pwd` theosakamg7/ros2java:latest sh -c "/usr/bin/vcs import src < ros2_java_desktop.repos" +- rm -rf /home/travis/build/ros2_java_ws/src/ros2_java/ros2_java_examples +- ln -s /home/travis/build/ros2java-alfred/ros2_java_examples + /home/travis/build/ros2_java_ws/src/ros2_java/ros2_java_examples +- cd /home/travis/build/ros2_java_ws/src/ros2/rosidl_typesupport +- patch -p1 < ../../ros2_java/ros2_java/rosidl_ros2_java.diff +- cd /home/travis/build +- docker run -u "$UID" -it --rm -v `pwd`:`pwd` -w `pwd` theosakamg7/ros2java:latest sh -c ". ament_ws/install_isolated/local_setup.sh && cd /home/travis/build/ros2_java_ws && ament build --symlink-install --isolated" + +script: +- cd /home/travis/build && docker run -u "$UID" -it --rm -v `pwd`:`pwd` -w `pwd` theosakamg7/ros2java:latest sh -c ". ament_ws/install_isolated/local_setup.sh && cd /home/travis/build/ros2_java_ws && ament test --isolated --only rcljava_examples" + +after_success: + - coveralls + +notifications: + webhooks: + urls: + - https://webhooks.gitter.im/e/4aac82b42245203edceb + on_success: change # options: [always|never|change] default: always + on_failure: always # options: [always|never|change] default: always + on_start: never # options: [always|never|change] default: always diff --git a/README.md b/README.md index 8301426..3025690 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,29 @@ -# ROS2 Java examples +# ROS2 Java examples [![Build Status](https://travis-ci.org/ros2java-alfred/ros2_java_examples.svg?branch=master)](https://travis-ci.org/ros2java-alfred/ros2_java_examples) This is a collection of examples that use the Java bindings for ROS2 + +## Topics + +- `Listener.java`: chatter listener. + +- `ListenerBestEffort.java`: chatter listener (Sensor type). + +- `Talker.java`: chatter talker. + +- `TalkerBestEffort.java`: chatter talker (Sensor type). + +- `ImuListener.java`: IMU sensor listener. + +## Services + +- `AddTwoIntServer.java`: Add two int service provider. + +- `AddTwoIntClient.java`: Add two int service consumer. + +## Parameters + +- `ListParameters.java` : List Parameters. + +- `ParameterEvents.java` : Test Parameter events (when change) + +- `SetAndGetParameters.java` : Set and Get Parameters. diff --git a/rcljava_examples/.gitignore b/rcljava_examples/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/rcljava_examples/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/rcljava_examples/build.gradle b/rcljava_examples/build.gradle index 026325a..874ec8a 100644 --- a/rcljava_examples/build.gradle +++ b/rcljava_examples/build.gradle @@ -1,19 +1,52 @@ -apply plugin: 'java' -apply plugin: org.ros2.tools.gradle.AmentPlugin +plugins { + id "org.ros2.rcljava" version "0.4.6" +} -sourceCompatibility = 1.6 -targetCompatibility = 1.6 +apply plugin: 'org.ros2.rcljava' -buildscript { - repositories { - jcenter() - mavenCentral() - maven { - url "https://plugins.gradle.org/m2/" - } - } +repositories { + jcenter() +} + +dependencies { + testCompile 'junit:junit:4.12' +} - dependencies { - classpath 'gradle.plugin.org.ros2.tools.gradle:ament:0.1.0' - } +ament { + generateEclipse = true + + scripts { + list_parameters_java { + mainClassName = 'org.ros2.rcljava.examples.parameters.ListParameters' + } + parameter_events_java { + mainClassName = 'org.ros2.rcljava.examples.parameters.ParameterEvents' + } + set_and_get_parameters_java { + mainClassName = 'org.ros2.rcljava.examples.parameters.SetAndGetParameters' + } + + add_two_ints_client_java { + mainClassName = 'org.ros2.rcljava.examples.services.AddTwoIntsClient' + } + add_two_ints_server_java { + mainClassName = 'org.ros2.rcljava.examples.services.AddTwoIntsServer' + } + + imu_listener_java { + mainClassName = 'org.ros2.rcljava.examples.topics.ImuListener' + } + listener_java { + mainClassName = 'org.ros2.rcljava.examples.topics.Listener' + } + listener_best_effort_java { + mainClassName = 'org.ros2.rcljava.examples.topics.ListenerBestEffort' + } + talker_java { + mainClassName = 'org.ros2.rcljava.examples.topics.Talker' + } + talker_best_effort_java { + mainClassName = 'org.ros2.rcljava.examples.topics.TalkerBestEffort' + } + } } diff --git a/rcljava_examples/package.xml b/rcljava_examples/package.xml index 1771ee8..0efe352 100644 --- a/rcljava_examples/package.xml +++ b/rcljava_examples/package.xml @@ -13,16 +13,15 @@ rosidl_default_generators rosidl_generator_java - example_interfaces rcljava std_msgs sensor_msgs + example_interfaces - rosidl_generator_java - example_interfaces rcljava std_msgs sensor_msgs + example_interfaces ament_gradle diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/AddTwoIntsClient.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/AddTwoIntsClient.java deleted file mode 100644 index b1de5ab..0000000 --- a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/AddTwoIntsClient.java +++ /dev/null @@ -1,52 +0,0 @@ -/* Copyright 2016 Esteve Fernandez - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.ros2.rcljava.examples; - -import org.ros2.rcljava.Client; -import org.ros2.rcljava.Node; -import org.ros2.rcljava.RCLJava; - -import java.util.concurrent.Future; - - -public class AddTwoIntsClient { - - public static void main(final String[] args) throws InterruptedException, - Exception { - - // Initialize RCL - RCLJava.rclJavaInit(); - - // Let's create a new Node - Node node = RCLJava.createNode("add_two_ints_client"); - - Client client = node.< - example_interfaces.srv.AddTwoInts>createClient( - example_interfaces.srv.AddTwoInts.class, "add_two_ints"); - - example_interfaces.srv.AddTwoInts_Request request = - new example_interfaces.srv.AddTwoInts_Request(); - request.setA(2); - request.setB(3); - - Future future = - client.sendRequest(request); - - System.out.println("Result of add_two_ints: " + future.get().getSum()); - - RCLJava.shutdown(); - } -} diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/AddTwoIntsService.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/AddTwoIntsService.java deleted file mode 100644 index f5e25ae..0000000 --- a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/AddTwoIntsService.java +++ /dev/null @@ -1,67 +0,0 @@ -/* Copyright 2016 Esteve Fernandez - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.ros2.rcljava.examples; - -import org.ros2.rcljava.Node; -import org.ros2.rcljava.RCLJava; -import org.ros2.rcljava.RMWRequestId; -import org.ros2.rcljava.Service; -import org.ros2.rcljava.TriConsumer; - - -public class AddTwoIntsService { - - public static void main(final String[] args) throws InterruptedException, - Exception { - - // Initialize RCL - RCLJava.rclJavaInit(); - - // Let's create a new Node - Node node = RCLJava.createNode("add_two_ints_server"); - - Service service = node.< - example_interfaces.srv.AddTwoInts - >createService( - example_interfaces.srv.AddTwoInts.class, "add_two_ints", - new TriConsumer< - RMWRequestId, - example_interfaces.srv.AddTwoInts_Request, - example_interfaces.srv.AddTwoInts_Response - >() { - - // We define the callback inline, this works with Java 8's lambdas - // too, but we use our own TriConsumer interface because Android - // supports lambdas via retrolambda, but not the lambda API - @Override - public void accept( - final RMWRequestId header, - final example_interfaces.srv.AddTwoInts_Request request, - final example_interfaces.srv.AddTwoInts_Response response - ) { - System.out.println("Incoming request"); - System.out.println( - "a: " + request.getA() + " b: " + request.getB()); - response.setSum(request.getA() + request.getB()); - } - } - ); - - while (RCLJava.ok()) { - RCLJava.spinOnce(node); - } - } -} diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/Listener.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/Listener.java deleted file mode 100644 index 924c651..0000000 --- a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/Listener.java +++ /dev/null @@ -1,60 +0,0 @@ -/* Copyright 2016 Esteve Fernandez - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.ros2.rcljava.examples; - -import org.ros2.rcljava.Consumer; -import org.ros2.rcljava.Node; -import org.ros2.rcljava.RCLJava; -import org.ros2.rcljava.Subscription; - - -public class Listener { - - public static void chatterCallback(final std_msgs.msg.String msg) { - System.out.println("I heard: " + msg.getData()); - } - - public static void main(final String[] args) throws InterruptedException, - Exception { - - // Initialize RCL - RCLJava.rclJavaInit(); - - // Let's create a new Node - Node node = RCLJava.createNode("listener"); - - // Subscriptions are type safe, so we'll pass the message type. We use the - // fully qualified class name to avoid any collision with Java's String - // class - Subscription sub = - node.createSubscription(std_msgs.msg.String.class, - "chatter", new Consumer() { - - // We define the callback inline, this works with Java 8's lambdas - // too, but we use our own Consumer interface because Android - // supports lambdas via retrolambda, but not the lambda API - @Override - public void accept(final std_msgs.msg.String msg) { - chatterCallback(msg); - } - } - ); - - while (RCLJava.ok()) { - RCLJava.spinOnce(node); - } - } -} diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/Talker.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/Talker.java deleted file mode 100644 index 6d80539..0000000 --- a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/Talker.java +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright 2016 Esteve Fernandez - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.ros2.rcljava.examples; - -import org.ros2.rcljava.Node; -import org.ros2.rcljava.Publisher; -import org.ros2.rcljava.RCLJava; - - -public class Talker { - public static void main(String[] args) throws InterruptedException { - // Initialize RCL - RCLJava.rclJavaInit(); - - // Let's create a Node - Node node = RCLJava.createNode("talker"); - - // Publishers are type safe, make sure to pass the message type - Publisher chatterPublisher = - node.createPublisher( - std_msgs.msg.String.class, "chatter"); - - std_msgs.msg.String msg = new std_msgs.msg.String(); - - int i = 1; - - while (RCLJava.ok()) { - msg.setData("Hello World: " + i); - i++; - System.out.println("Publishing: \"" + msg.getData() + "\""); - chatterPublisher.publish(msg); - - // Sleep a little bit between each message - Thread.sleep(1000); - } - } -} diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/parameters/ListParameters.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/parameters/ListParameters.java new file mode 100644 index 0000000..65a4fee --- /dev/null +++ b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/parameters/ListParameters.java @@ -0,0 +1,67 @@ +/* Copyright 2016 Esteve Fernandez + * Copyright 2016 Mickael Gaillard + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.ros2.rcljava.examples.parameters; + +import java.util.Arrays; + +import org.ros2.rcljava.node.Node; +import org.ros2.rcljava.RCLJava; +import org.ros2.rcljava.node.parameter.ParameterVariant; +import org.ros2.rcljava.node.parameter.SyncParametersClient; + +public class ListParameters { + private static final String NODE_NAME = ListParameters.class.getSimpleName().toLowerCase(); + + /** + * @param args + */ + public static void main(String[] args) { + // Initialize RCL + RCLJava.rclJavaInit(); + + // Let's create a new Node + Node node = RCLJava.createNode(NODE_NAME); + + SyncParametersClient parameters_client = new SyncParametersClient(node); + + // Set several different types of parameters. + parameters_client.setParameters( + Arrays.>asList( + new ParameterVariant("foo", 2L), + new ParameterVariant("bar", "hello"), + new ParameterVariant("baz", 1.45), + new ParameterVariant("foo.first", 8L), + new ParameterVariant("foo.second", 42L), + new ParameterVariant("foobar", true) + )); + + // List the details of a few parameters up to a namespace depth of 10. + rcl_interfaces.msg.ListParametersResult parameters_and_prefixes = + parameters_client.listParameters(Arrays.asList("foo", "bar"), 10); + + for (String name : parameters_and_prefixes.getNames()) { + System.out.println(String.format("Parameter name: %s", name)); + } + + for (String prefix : parameters_and_prefixes.getPrefixes()) { + System.out.println(String.format("Parameter prefix: %s", prefix)); + } + + node.dispose(); + RCLJava.shutdown(); + } + +} diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/parameters/ParameterEvents.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/parameters/ParameterEvents.java new file mode 100644 index 0000000..95a1d9f --- /dev/null +++ b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/parameters/ParameterEvents.java @@ -0,0 +1,95 @@ +/* Copyright 2016 Esteve Fernandez + * Copyright 2016 Mickael Gaillard + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.ros2.rcljava.examples.parameters; + +import java.util.Arrays; + +import org.ros2.rcljava.node.Node; +import org.ros2.rcljava.RCLJava; +import org.ros2.rcljava.node.topic.Subscription; +import org.ros2.rcljava.node.parameter.ParameterEventConsumer; +import org.ros2.rcljava.node.parameter.ParameterVariant; +import org.ros2.rcljava.node.parameter.SyncParametersClient; + +import rcl_interfaces.msg.ParameterEvent; + +public class ParameterEvents { + private static final String NODE_NAME = ParameterEvents.class.getSimpleName().toLowerCase(); + + private static void onParameterEvent(final rcl_interfaces.msg.ParameterEvent event) + { + System.out.println("Parameter event:\n new parameters:"); + for (rcl_interfaces.msg.Parameter new_parameter : event.getNewParameters()) { + System.out.println(String.format(" %s", new_parameter.getName())); + } + System.out.println(" changed parameters:"); + for (rcl_interfaces.msg.Parameter changed_parameter : event.getChangedParameters()) { + System.out.println(String.format(" %s", changed_parameter.getName())); + } + System.out.println(" deleted parameters:"); + for (rcl_interfaces.msg.Parameter deleted_parameter : event.getDeletedParameters()) { + System.out.println(String.format(" %s", deleted_parameter.getName())); + } + } + + /** + * @param args + */ + public static void main(String[] args) { + // Initialize RCL + RCLJava.rclJavaInit(); + + // Let's create a new Node + Node node = RCLJava.createNode(NODE_NAME); + + SyncParametersClient parameters_client = new SyncParametersClient(node); + + // Setup callback for changes to parameters. + Subscription sub = parameters_client.onParameterEvent(new ParameterEventConsumer() { + @Override + public void onEvent(ParameterEvent event) { + ParameterEvents.onParameterEvent(event); + } + }); + + // Set several different types of parameters. +// ArrayList set_parameters_results = + parameters_client.setParameters(Arrays.>asList( + new ParameterVariant("foo", 2L), + new ParameterVariant("bar", "hello"), + new ParameterVariant("baz", 1.45), + new ParameterVariant("foobar", true) + )); + + // Change the value of some of them. +// set_parameters_results = + parameters_client.setParameters(Arrays.>asList( + new ParameterVariant("foo", 3L), + new ParameterVariant("bar", "world"), + new ParameterVariant("foobar", null) + )); + + for (int i = 0; i < 6; i++) { + RCLJava.spinOnce(node); + } + + + sub.dispose(); + node.dispose(); + RCLJava.shutdown(); + } + +} diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/parameters/SetAndGetParameters.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/parameters/SetAndGetParameters.java new file mode 100644 index 0000000..2d68d86 --- /dev/null +++ b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/parameters/SetAndGetParameters.java @@ -0,0 +1,70 @@ +/* Copyright 2016 Esteve Fernandez + * Copyright 2016 Mickael Gaillard + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.ros2.rcljava.examples.parameters; + +import java.util.Arrays; +import java.util.List; + +import org.ros2.rcljava.RCLJava; +import org.ros2.rcljava.node.Node; +import org.ros2.rcljava.node.parameter.ParameterVariant; +import org.ros2.rcljava.node.parameter.SyncParametersClient; + +public class SetAndGetParameters { + private static final String NODE_NAME = SetAndGetParameters.class.getSimpleName().toLowerCase(); + + /** + * @param args + */ + public static void main(String[] args) { + // Initialize RCL + RCLJava.rclJavaInit(); + + // Let's create a new Node + Node node = RCLJava.createNode(NODE_NAME); + + SyncParametersClient parameters_client = new SyncParametersClient(node); + + // Set several different types of parameters. + List set_parameters_results = + parameters_client.setParameters( + Arrays.>asList( + new ParameterVariant("foo", 2L), + new ParameterVariant("bar", "hello"), + new ParameterVariant("baz", 1.45), + new ParameterVariant("foobar", true) + )); + // Check to see if they were set. + for (rcl_interfaces.msg.SetParametersResult result : set_parameters_results) { + if (!result.getSuccessful()) { + System.out.println(String.format("Failed to set parameter: %s", result.getReason())); + } + } + + // Get a few of the parameters just set. + for (ParameterVariant parameter : parameters_client.getParameters(Arrays.asList("foo", "baz"))) { // "bar", "foobar" + System.out.println( + String.format("Parameter name %s = value (%s): %s", + parameter.getName(), + parameter.getTypeName(), + parameter.valueToString())); + } + + node.dispose(); + RCLJava.shutdown(); + } + +} diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/parameters/package-info.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/parameters/package-info.java new file mode 100644 index 0000000..5f781d9 --- /dev/null +++ b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/parameters/package-info.java @@ -0,0 +1,20 @@ +/* Copyright 2016 Esteve Fernandez + * Copyright 2016 Mickael Gaillard + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * ROS2 rcljava parameters examples. + */ +package org.ros2.rcljava.examples.parameters; diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/services/AddTwoIntsClient.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/services/AddTwoIntsClient.java new file mode 100644 index 0000000..7ca26f5 --- /dev/null +++ b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/services/AddTwoIntsClient.java @@ -0,0 +1,60 @@ +/* Copyright 2016 Esteve Fernandez + * Copyright 2016 Mickael Gaillard + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.ros2.rcljava.examples.services; + +import java.util.concurrent.Future; + +import org.ros2.rcljava.RCLJava; +import org.ros2.rcljava.node.Node; +import org.ros2.rcljava.node.service.Client; + +import example_interfaces.srv.AddTwoInts; +import example_interfaces.srv.AddTwoInts_Request; +import example_interfaces.srv.AddTwoInts_Response; + +public class AddTwoIntsClient { + private static final String NODE_NAME = AddTwoIntsClient.class.getSimpleName().toLowerCase(); + + public static void main(String[] args) throws Exception { + // Initialize RCL + RCLJava.rclJavaInit(); + + // Let's create a new Node + Node node = RCLJava.createNode(NODE_NAME); + + // Create client. + Client client = node.createClient(AddTwoInts.class, "add_two_ints"); + + // Set request. + AddTwoInts_Request request = new AddTwoInts_Request(); + request.setA(2); + request.setB(3); + + // Call service... + Future future = client.sendRequest(request); + if (future != null) { + System.out.println(String.format("Result of add_two_ints: %d", future.get().getSum())); + } else { + System.out.println("add_two_ints_client was interrupted. Exiting."); + } + + // Release all. + client.dispose(); + node.dispose(); + RCLJava.shutdown(); + } + +} diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/services/AddTwoIntsServer.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/services/AddTwoIntsServer.java new file mode 100644 index 0000000..0415811 --- /dev/null +++ b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/services/AddTwoIntsServer.java @@ -0,0 +1,77 @@ +/* Copyright 2016 Esteve Fernandez + * Copyright 2016 Mickael Gaillard + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.ros2.rcljava.examples.services; + +import org.ros2.rcljava.RCLJava; +import org.ros2.rcljava.node.Node; +import org.ros2.rcljava.node.service.Service; +import org.ros2.rcljava.node.service.TriConsumer; +import org.ros2.rcljava.node.service.RMWRequestId; + +import example_interfaces.srv.AddTwoInts; +import example_interfaces.srv.AddTwoInts_Request; +import example_interfaces.srv.AddTwoInts_Response; + +public class AddTwoIntsServer { + private static final String NODE_NAME = AddTwoIntsServer.class.getSimpleName().toLowerCase(); + + public static void handleAddTwoInts( + final AddTwoInts_Request request, + final AddTwoInts_Response response) { + + System.out.println("Incoming request"); + System.out.println(String.format("a: %d b: %d", request.getA(), request.getB())); + response.setSum(request.getA() + request.getB()); + } + + public static void main(String[] args) throws Exception { + + // Initialize RCL + RCLJava.rclJavaInit(); + + // Let's create a new Node + Node node = RCLJava.createNode(NODE_NAME); + + // Create a service. + Service service = node.createService( + AddTwoInts.class, + "add_two_ints", + new TriConsumer() { + + // We define the callback inline, this works with Java 8's + // lambdas + // too, but we use our own TriConsumer interface because + // Android + // supports lambdas via retrolambda, but not the lambda API + @Override + public void accept( + final RMWRequestId header, + final AddTwoInts_Request request, + final AddTwoInts_Response response) { + AddTwoIntsServer.handleAddTwoInts(request, response); + } + }); + + // Wait... + RCLJava.spin(node); + + // Release all. + service.dispose(); + node.dispose(); + RCLJava.shutdown(); + } + +} diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/services/package-info.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/services/package-info.java new file mode 100644 index 0000000..5f6d0a5 --- /dev/null +++ b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/services/package-info.java @@ -0,0 +1,20 @@ +/* Copyright 2016 Esteve Fernandez + * Copyright 2016 Mickael Gaillard + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * ROS2 rcljava services examples. + */ +package org.ros2.rcljava.examples.services; diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/ImuListener.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/ImuListener.java new file mode 100644 index 0000000..fe18c01 --- /dev/null +++ b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/ImuListener.java @@ -0,0 +1,64 @@ +/* Copyright 2016 Esteve Fernandez + * Copyright 2016 Mickael Gaillard + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.ros2.rcljava.examples.topics; + +import org.ros2.rcljava.qos.QoSProfile; +import org.ros2.rcljava.RCLJava; +import org.ros2.rcljava.node.Node; +import org.ros2.rcljava.node.topic.Consumer; +import org.ros2.rcljava.node.topic.Subscription; + +public class ImuListener { + private static final String NODE_NAME = Listener.class.getSimpleName().toLowerCase(); + + private static void imuCb(final sensor_msgs.msg.Imu msg) + { + System.out.println(String.format(" accel: [%+6.3f %+6.3f %+6.3f]\n", + msg.getLinearAcceleration().getX(), + msg.getLinearAcceleration().getY(), + msg.getLinearAcceleration().getZ())); + } + + public static void main(String[] args) throws InterruptedException { + // Initialize RCL + RCLJava.rclJavaInit(); + + // Let's create a new Node + Node node = RCLJava.createNode(NODE_NAME); + + // Subscriptions are type safe, so we'll pass the message type. + Subscription sub = node.createSubscription( + sensor_msgs.msg.Imu.class, + "imu", + new Consumer() { + // We define the callback inline, this works with Java 8's lambdas too, but we use + // our own Consumer interface because Android supports lambdas via retrolambda, but not + // the lambda API + @Override + public void accept(sensor_msgs.msg.Imu msg) { + ImuListener.imuCb(msg); + } + }, + QoSProfile.SENSOR_DATA); + + RCLJava.spin(node); + + sub.dispose(); + node.dispose(); + RCLJava.shutdown(); + } + +} diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/Listener.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/Listener.java new file mode 100644 index 0000000..31337a2 --- /dev/null +++ b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/Listener.java @@ -0,0 +1,58 @@ +/* Copyright 2016 Esteve Fernandez + * Copyright 2016 Mickael Gaillard + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.ros2.rcljava.examples.topics; + +import org.ros2.rcljava.RCLJava; +import org.ros2.rcljava.node.Node; +import org.ros2.rcljava.node.topic.Consumer; +import org.ros2.rcljava.node.topic.Subscription; + +public class Listener { + private static final String NODE_NAME = Listener.class.getSimpleName().toLowerCase(); + + public static void chatterCallback(final std_msgs.msg.String msg) { + System.out.println("I heard: [" + msg.getData() + "]"); + } + + public static void main(String[] args) throws InterruptedException { + // Initialize RCL + RCLJava.rclJavaInit(); + + // Let's create a new Node + Node node = RCLJava.createNode(NODE_NAME); + + // Subscriptions are type safe, so we'll pass the message type. We use the fully qualified + // class name to avoid any collision with Java's String class + Subscription sub = node.createSubscription( + std_msgs.msg.String.class, + "chatter", + new Consumer() { + // We define the callback inline, this works with Java 8's lambdas too, but we use + // our own Consumer interface because Android supports lambdas via retrolambda, but not + // the lambda API + @Override + public void accept(std_msgs.msg.String msg) { + Listener.chatterCallback(msg); + } + }); + + RCLJava.spin(node); + + sub.dispose(); + node.dispose(); + RCLJava.shutdown(); + } +} diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/ListenerBestEffort.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/ListenerBestEffort.java similarity index 85% rename from rcljava_examples/src/main/java/org/ros2/rcljava/examples/ListenerBestEffort.java rename to rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/ListenerBestEffort.java index e28fcc6..412ce4e 100644 --- a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/ListenerBestEffort.java +++ b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/ListenerBestEffort.java @@ -1,4 +1,5 @@ /* Copyright 2016 Esteve Fernandez + * Copyright 2016 Mickael Gaillard * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -12,15 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package org.ros2.rcljava.examples.topics; -package org.ros2.rcljava.examples; - -import org.ros2.rcljava.Consumer; -import org.ros2.rcljava.Node; -import org.ros2.rcljava.qos.QoSProfile; import org.ros2.rcljava.RCLJava; -import org.ros2.rcljava.Subscription; - +import org.ros2.rcljava.qos.QoSProfile; +import org.ros2.rcljava.node.Node; +import org.ros2.rcljava.node.topic.Consumer; +import org.ros2.rcljava.node.topic.Subscription; public class ListenerBestEffort { @@ -28,8 +27,7 @@ public static void chatterCallback(final std_msgs.msg.String msg) { System.out.println("I heard: " + msg.getData()); } - public static void main(final String[] args) throws InterruptedException, - Exception { + public static void main(final String[] args) throws InterruptedException { // Initialize RCL RCLJava.rclJavaInit(); @@ -57,5 +55,9 @@ public void accept(final std_msgs.msg.String msg) { while (RCLJava.ok()) { RCLJava.spinOnce(node); } + + sub.dispose(); + node.dispose(); + RCLJava.shutdown(); } } diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/Talker.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/Talker.java new file mode 100644 index 0000000..85d7ddd --- /dev/null +++ b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/Talker.java @@ -0,0 +1,56 @@ +/* Copyright 2016 Esteve Fernandez + * Copyright 2016 Mickael Gaillard + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.ros2.rcljava.examples.topics; + +import org.ros2.rcljava.RCLJava; +import org.ros2.rcljava.node.Node; +import org.ros2.rcljava.node.topic.Publisher; + +public class Talker { + private static final String NODE_NAME = Talker.class.getSimpleName().toLowerCase(); + + public static void main(String[] args) throws InterruptedException { + + int i = 1; + + // Initialize RCL + RCLJava.rclJavaInit(); + + // Let's create a Node + Node node = RCLJava.createNode(NODE_NAME); + + std_msgs.msg.String msg = new std_msgs.msg.String(); + Publisher chatter_pub = + node.createPublisher( + std_msgs.msg.String.class, + "chatter"); + + while(RCLJava.ok()) { + msg.setData("Hello World: " + i); + + System.out.println("Publishing: \"" + msg.getData() + "\""); + chatter_pub.publish(msg); + + // Sleep a little bit between each message + Thread.sleep(500); + i++; + } + + chatter_pub.dispose(); + node.dispose(); + RCLJava.shutdown(); + } +} diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/TalkerBestEffort.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/TalkerBestEffort.java similarity index 89% rename from rcljava_examples/src/main/java/org/ros2/rcljava/examples/TalkerBestEffort.java rename to rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/TalkerBestEffort.java index 1b27fd2..2b74c17 100644 --- a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/TalkerBestEffort.java +++ b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/TalkerBestEffort.java @@ -1,4 +1,5 @@ /* Copyright 2016 Esteve Fernandez + * Copyright 2016 Mickael Gaillard * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,10 +14,10 @@ * limitations under the License. */ -package org.ros2.rcljava.examples; +package org.ros2.rcljava.examples.topics; -import org.ros2.rcljava.Node; -import org.ros2.rcljava.Publisher; +import org.ros2.rcljava.node.Node; +import org.ros2.rcljava.node.topic.Publisher; import org.ros2.rcljava.qos.QoSProfile; import org.ros2.rcljava.RCLJava; diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/package-info.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/package-info.java new file mode 100644 index 0000000..fe2895c --- /dev/null +++ b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/topics/package-info.java @@ -0,0 +1,20 @@ +/* Copyright 2016 Esteve Fernandez + * Copyright 2016 Mickael Gaillard + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * ROS2 rcljava topics examples. + */ +package org.ros2.rcljava.examples.topics; diff --git a/rcljava_examples/src/test/java/org/ros2/rcljava/examples/test/SimpleTest.java b/rcljava_examples/src/test/java/org/ros2/rcljava/examples/test/SimpleTest.java new file mode 100644 index 0000000..287036f --- /dev/null +++ b/rcljava_examples/src/test/java/org/ros2/rcljava/examples/test/SimpleTest.java @@ -0,0 +1,103 @@ +/* Copyright 2016 Esteve Fernandez + * Copyright 2016 Mickael Gaillard + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.ros2.rcljava.examples.test; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.ros2.rcljava.NativeUtils; +import org.ros2.rcljava.qos.QoSProfile; +import org.ros2.rcljava.RCLJava; +import org.ros2.rcljava.node.Node; +import org.ros2.rcljava.node.topic.Publisher; + + +public class SimpleTest { + private static final String NODE_NAME = SimpleTest.class.getName(); + private static final Logger logger = LoggerFactory.getLogger(SimpleTest.class); + + @BeforeClass + public static void setUp() { + + } + + /** + * Test. + */ + @Test + public void testSample() { + // Initialize RCL + RCLJava.rclJavaInit(); + + // Let's create a Node + Node node = RCLJava.createNode(NODE_NAME); + + // Testing Publisher + //------------------------------------------------------------------------------------------------------------- + + // Publishers are type safe, make sure to pass the message type + Publisher chatter_pub = + node.createPublisher( + std_msgs.msg.String.class, + "chatter", + QoSProfile.DEFAULT); + + std_msgs.msg.String msg = new std_msgs.msg.String(); + msg.setData("Hello World"); + + System.out.println("Publishing: \"" + msg.getData() + "\""); + chatter_pub.publish(msg); + + // Testing Client + //------------------------------------------------------------------------------------------------------------- + +// Client client = node.createClient( +// AddTwoInts.class, +// "add_two_ints", +// QoSProfile.PROFILE_SERVICES_DEFAULT); +// +// // Set request. +// AddTwoInts.Request request = new AddTwoInts.Request(); +// request.setA(2); +// request.setB(3); + + // List native libraries + StringBuilder msgLog = new StringBuilder(); + for (String key : NativeUtils.getLoadedLibraries(SimpleTest.class.getClassLoader())) { + msgLog.append(key); + msgLog.append("\n"); + } + logger.debug("Native libraries : \n" + msgLog.toString()); + + // Sleep a little bit between each message + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + // Release all. + chatter_pub.dispose(); + node.dispose(); + RCLJava.shutdown(); + + Assert.assertTrue(true); + } +} diff --git a/rcljava_examples/src/test/java/org/ros2/rcljava/examples/test/package-info.java b/rcljava_examples/src/test/java/org/ros2/rcljava/examples/test/package-info.java new file mode 100644 index 0000000..79e4283 --- /dev/null +++ b/rcljava_examples/src/test/java/org/ros2/rcljava/examples/test/package-info.java @@ -0,0 +1,20 @@ +/* Copyright 2016 Esteve Fernandez + * Copyright 2016 Mickael Gaillard + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * ROS2 rcljava test examples. + */ +package org.ros2.rcljava.examples.test;