Skip to content

robocomp/webots-bridge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Webots2Robocomp

This project aims to build middleware that facilitates communication and integration between the Webots robotics simulator and the Robocomp framework.

Webots Robot Configuration

For the component to effectively communicate with the robot in Webots, it's essential to configure the robot's controller to <extern> in your Webots scene. This ensures that the robot's behavior is externally controlled by a program outside Webots, in this case, our middleware.

Steps to Configure Robot in Webots

Open your Webots world file: This is typically a .wbt file where your robot and environment are defined.

Select your robot node: In the scene tree (usually on the left side of the Webots interface), find and select your robot node.

Modify the controller field: In the robot's properties (usually on the right side of the Webots interface), find the controller field. Change its value to <extern>.

Save the world file: Ensure you save the .wbt file after making this change.

Now, when you run the simulation in Webots, the robot will wait for an external controller (Webots2Robocomp in this case) to connect and take control.

Measures

This component implements diferents interfaces that works with the following measures:

  • Lidar3D: milimeters.

Connecting Middleware to Webots

Once the robot's controller is set to <extern>, ensure your middleware is ready to connect. Typically, you'd do this:

· Start Webots and play the simulation.
· Launch the middleware. It should automatically detect the robot waiting for an external controller and establish a connection.

Configuration parameters

Like any other component, Webots2Robocomp requires a configuration file to start. In etc/config or etc/config.toml, you can find an example of the configuration file.

Starting the component

To avoid modifying the config file directly in the repository, you can copy it to the component's home directory. This prevents changes from being overridden by future git pull commands:

cd <Webots2Robocomp's path> 
cp etc/config etc/yourConfig

After editing the new config file we can run the component:

cmake -B build && make -C build -j12 # Compile the component
bin/Webots2Robocomp etc/yourConfig # Execute the component


Developer Notes

This section explains how to work with the generated code of Webots2Robocomp, including what can be modified and how to use key features.

Editable Files

You can freely edit the following files:

  • etc/* – Configuration files
  • src/* – Component logic and implementation
  • README.md – Documentation

The generated folder contains autogenerated files. Do not edit these files directly, as they will be overwritten every time the component is regenerated with RoboComp.

ConfigLoader

The ConfigLoader simplifies fetching configuration parameters. Use the get<>() method to retrieve parameters from the configuration file.

// Syntax
type variable = this->configLoader.get<type>("ParameterName");

// Example
int computePeriod = this->configLoader.get<int>("Period.Compute");

StateMachine

RoboComp components utilize a state machine to manage the main execution flow. The default states are:

  1. Initialize:
    • Executes once after the constructor.
    • May use for parameter initialization, opening devices, and calculating constants.
  2. Compute:
    • Executes cyclically after Initialize.
    • Place your functional logic here. If an emergency is detected, call goToEmergency() to transition to the Emergency state.
  3. Emergency:
    • Executes cyclically during emergencies.
    • Once resolved, call goToRestore() to transition to the Restore state.
  4. Restore:
    • Executes once to restore the component after an emergency.
    • Transitions automatically back to the Compute state.

Setting and Getting State Periods

You can get the period of some state with de function getPeriod and set with setPeriod

int currentPeriod = getPeriod("Compute");   // Get the current Compute period
setPeriod("Compute", currentPeriod * 0.5); // Set Compute period to half

Creating Custom States

To add a custom state, follow these steps in the constructor:

  1. Define Your State Use GRAFCETStep to create your state. If any function is not required, use nullptr.
states["CustomState"] = std::make_unique<GRAFCETStep>("CustomState", period, 
                                                      std::bind(&SpecificWorker::customLoop, this),  // Cyclic function
                                                      std::bind(&SpecificWorker::customEnter, this), // On-enter function
                                                      std::bind(&SpecificWorker::customExit, this)); // On-exit function
  1. Define Transitions Add transitions between states using addTransition. You can trigger transitions using Qt signals such as entered() and exited() or custom signals in .h.
// Syntax
states[srcState]->addTransition(originOfSignal, signal, dstState)

// Example
states["CustomState"]->addTransition(states["CustomState"].get(), SIGNAL(entered()), states["OtherState"].get());
states["Compute"]->addTransition(this, SIGNAL(customSignal()), states["CustomState"].get());
  1. Add State to the StateMachine Include your state in the state machine:
statemachine.addState(states["CustomState"].get());

Hibernation Flag

The #define HIBERNATION_ENABLED flag in specificworker.h activates hibernation mode. When enabled, the component reduces its state execution frequency to 500ms if no method calls are received within 5 seconds. Once a method call is received, the period is restored to its original value.

Default hibernation monitoring runs every 500ms.

Changes Introduced in the New Code Generator

If you’re regenerating or adapting old components, here’s what has changed:

  • Deprecated classes removed: CommonBehavior, InnerModel, AGM, Monitors, and src/config.h.
  • Configuration parsing replaced with the new ConfigLoader, supporting both .toml and legacy configuration formats.
  • Skeleton code split: generated (non-editable) and src (editable).
  • Component period is now configurable in the configuration file.
  • State machine integrated with predefined states: Initialize, Compute, Emergency, and Restore.
  • With the dsr option, you generate G in the GenericWorker, making the viewer independent. If you want to use the dsrviewer, you will need the Qt GUI (QMainWindow) and the dsr option enabled in the CDSL.
  • Strings in the legacy config now need to be enclosed in quotes ("").

Adapting Old Components

To adapt older components to the new structure:

  1. Add Period.Compute and Period.Emergency and swap Endpoints and Proxies with their names in the etc/config file.
  2. Merge the new src/CMakeLists.txt and the old CMakeListsSpecific files.
  3. Modify specificworker.h:
    • Add the HIBERNATION_ENABLED flag.
    • Update the constructor signature.
    • Replace setParams with state definitions (Initialize, Compute, etc.).
  4. Modify specificworker.cpp:
    • Refactor the constructor entirely.
    • Move setParams logic to the initialize state using ConfigLoader.get<>().
    • Remove the old timer and period logic and replace it with getPeriod() and setPeriod().
    • Add the new function state Emergency, and Restore.
    • Add the following code to the implements and publish functions:
      #ifdef HIBERNATION_ENABLED
          hibernation = true;
      #endif
  5. Update Configuration Strings, ensure all strings in the config under legacy are enclosed in quotes (""), as required by the new structure.
  6. Using DSR, if you use the DSR option, note that G is generated in GenericWorker, making the viewer independent. However, to use the dsrviewer, you must integrate a Qt GUI (QMainWindow) and enable the dsr option in the CDSL.
  7. Installing toml++, to use the new .toml configuration format, install the toml++ library:
mkdir ~/software 2> /dev/null; git clone https://github.com/marzer/tomlplusplus.git ~/software/tomlplusplus
cd ~/software/tomlplusplus && cmake -B build && sudo make install -C build -j12 && cd -
  1. Installing qt6 Dependencies
sudo apt install qt6-base-dev qt6-declarative-dev qt6-scxml-dev libqt6statemachineqml6 libqt6statemachine6

mkdir ~/software 2> /dev/null; git clone https://github.com/GillesDebunne/libQGLViewer.git ~/software/libQGLViewer
cd ~/software/libQGLViewer && qmake6 *.pro && make -j12 && sudo make install && sudo ldconfig && cd -
  1. Generated Code, When the component is generated, a generated folder is created containing non-editable files. You can delete everything in the src directory except for:
  • src/specificworker.h
  • src/specificworker.cpp
  • src/CMakeLists.txt
  • src/mainUI.ui
  • README.md
  • etc/config
  • etc/config.toml
  • Your Clases...

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 10