Minimalist Java controller example for the Humanoid Robot Wrestling Competition. Demonstrates how to play a simple motion file. We use the Motion class from Webots. It could also be programmed in Python, C, C++ or Java.
Here is the participant.java file:
import com.cyberbotics.webots.controller.Robot;
import com.cyberbotics.webots.controller.Motion;
public class participant {
public static void main(String[] args) {
// Robot initialization
Robot robot = new Robot();
// Motion files are text files containing pre-recorded positions of the robot's joints
Motion handWave = new Motion("../motions/HandWave.motion");
// We play the hand-waving motion on loop
handWave.setLoop(true);
handWave.play();
int timeStep = (int) Math.round(robot.getBasicTimeStep());
// Mandatory function to make the simulation run
while (robot.step(timeStep) != -1);
}
}
To compile the Java code, the Dockerfile must be updated: we use the eclipse-temurin:17-jdk
Docker image to get the latest OpenJDK 17 Java Development Kit installed on top of Webots's image. We compile participant.java
into a class file and then we run it with the entrypoint command:
FROM eclipse-temurin:17-jdk as source
FROM cyberbotics/webots.cloud:R2023a-ubuntu20.04
ENV JAVA_HOME=/opt/java/openjdk
ENV PATH "${JAVA_HOME}/bin:${PATH}"
COPY --from=source $JAVA_HOME $JAVA_HOME
# Environment variables needed for Webots
# https://cyberbotics.com/doc/guide/running-extern-robot-controllers#remote-extern-controllers
ENV LD_LIBRARY_PATH=${WEBOTS_HOME}/lib/controller:${LD_LIBRARY_PATH}
ARG WEBOTS_CONTROLLER_URL
ENV WEBOTS_CONTROLLER_URL=${WEBOTS_CONTROLLER_URL}
# Copies all the files of the controllers folder into the docker container
RUN mkdir -p /usr/local/webots-project/controllers
COPY . /usr/local/webots-project/controllers
WORKDIR /usr/local/webots-project/controllers/participant
# Build controller
RUN javac -Xlint -classpath /usr/local/webots/lib/controller/java/Controller.jar:. participant.java
ENTRYPOINT ["java", \
"-classpath", "/usr/local/webots/lib/controller/java/Controller.jar:/usr/local/webots-project/controllers/participant", \
"-Djava.library.path=/usr/local/webots/lib/controller/java", "participant"]
Bob is a more advanced robot controller able to win against Alice.