Skip to content

Commit a01ffad

Browse files
CoolSpy3omichel
andauthored
Fix cyberbotics#6578 Devices with Duplicate Names Cause Unexpected Behavior (cyberbotics#6579)
* add device constructors that accept a WbDeviceTag rather than a name * create populate Robot::deviceList by tag rather than name * update python api * remove duplicate Brake destructor * define WbDeviceTag in Robot * update java api * add api test * bug fixes * fix Java getDevice implementation * update documentation * fix warnings in test * run clang-format * make test objects constant Co-authored-by: Olivier Michel <Olivier.Michel@cyberbotics.com> * update changelog * minor clarification * fix capitalization of API Co-authored-by: Olivier Michel <Olivier.Michel@cyberbotics.com> * allow checking if const pointers are null * use non-const Robot --------- Co-authored-by: Olivier Michel <Olivier.Michel@cyberbotics.com>
1 parent 1c6c9a3 commit a01ffad

38 files changed

Lines changed: 375 additions & 182 deletions

docs/reference/changelog-r2024.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ Released on December **th, 2023.
1212
- Fixed error message on Windows when `libssl-3-x64.dll` was added to `PATH` ([#6553](https://github.com/cyberbotics/webots/pull/6553)).
1313
- Fixed length of arrays returned by `getPose()` in Java ([#6556](https://github.com/cyberbotics/webots/pull/6556)).
1414
- Fixed length of arrays returned by `CameraRecognitionObject.getColors()` in Java ([#6564](https://github.com/cyberbotics/webots/pull/6564))
15-
15+
- Fixed handling of device objects with the same name in the controller API ([#6579](https://github.com/cyberbotics/webots/pull/6579))

docs/reference/robot.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,11 +766,12 @@ WbDeviceTag wb_robot_get_device(const char *name);
766766
namespace webots {
767767
class Robot {
768768
Accelerometer *getAccelerometer(const std::string &name);
769-
Altimeter *getAltimeter(const std::string &name);
769+
Altimeter *getAltimeter(const std::string &name);
770770
Brake *getBrake(const std::string &name);
771771
Camera *getCamera(const std::string &name);
772772
Compass *getCompass(const std::string &name);
773773
Connector *getConnector(const std::string &name);
774+
Device *getDevice(const std::string &name);
774775
Display *getDisplay(const std::string &name);
775776
DistanceSensor *getDistanceSensor(const std::string &name);
776777
Emitter *getEmitter(const std::string &name);
@@ -826,6 +827,7 @@ public class Robot {
826827
public Camera getCamera(String name);
827828
public Compass getCompass(String name);
828829
public Connector getConnector(String name);
830+
public Device getDevice(String name);
829831
public Display getDisplay(String name);
830832
public DistanceSensor getDistanceSensor(String name);
831833
public Emitter getEmitter(String name);
@@ -874,7 +876,7 @@ Devices are available through their services.
874876

875877
*get a unique identifier to a device*
876878

877-
The `wb_robot_get_device` function (available in C, Python and MATLAB) returns a unique identifier for a device corresponding to a specified `name`.
879+
The `wb_robot_get_device` function returns a unique identifier for a device corresponding to a specified `name`.
878880
For example, if a robot contains a [DistanceSensor](distancesensor.md) node whose `name` field is "ds1", the function will return the unique identifier of that device.
879881
This `WbDeviceTag` identifier will be used subsequently for enabling, sending commands to, or reading data from this device.
880882
If the specified device is not found, the function returns 0 in C and MATLAB or `None` in Python.
@@ -885,6 +887,8 @@ Depending on the called function, this object can be an instance of a `Device` s
885887
For example, if a robot contains a [DistanceSensor](distancesensor.md) node whose `name` field is "ds1", the function `getDistanceSensor` will return a reference to a [DistanceSensor](distancesensor.md) object.
886888
If the specified device is not found, the function returns `NULL` in C++ or `null` in Java.
887889

890+
Note that if any two devices share the same name, `wb_robot_get_device` will return the first one it finds. In order to distinguish between devices with the same name, users should consider iterating over a robot's devices using `wb_robot_get_device_by_index` and `wb_robot_get_number_of_devices`.
891+
888892
---
889893

890894
#### `wb_robot_get_device_by_index`

include/controller/cpp/webots/Accelerometer.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace webots {
2121
class Accelerometer : public Device {
2222
public:
2323
explicit Accelerometer(const std::string &name) : Device(name) {} // Use Robot::getAccelerometer() instead
24+
explicit Accelerometer(WbDeviceTag tag) : Device(tag) {}
2425
virtual ~Accelerometer() {}
2526
virtual void enable(int samplingPeriod);
2627
virtual void disable();

include/controller/cpp/webots/Altimeter.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace webots {
2121
class Altimeter : public Device {
2222
public:
2323
explicit Altimeter(const std::string &name) : Device(name) {} // Use Robot::getAltimeter instead
24+
explicit Altimeter(WbDeviceTag tag) : Device(tag) {}
2425
virtual ~Altimeter() {}
2526
virtual void enable(int samplingPeriod);
2627
virtual void disable();

include/controller/cpp/webots/Brake.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace webots {
3030
Device(name),
3131
motor(NULL),
3232
positionSensor(NULL) {} // Use Robot::getBrake() instead
33+
explicit Brake(WbDeviceTag tag) : Device(tag), motor(NULL), positionSensor(NULL) {}
3334
virtual ~Brake() {}
3435
Type getType() const;
3536
void setDampingConstant(double dampingConstant) const;

include/controller/cpp/webots/Camera.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace webots {
2424
class Camera : public Device {
2525
public:
2626
explicit Camera(const std::string &name) : Device(name) {} // Use Robot::getCamera() instead
27+
explicit Camera(WbDeviceTag tag) : Device(tag) {}
2728
virtual ~Camera() {}
2829
virtual void enable(int samplingPeriod);
2930
virtual void disable();

include/controller/cpp/webots/Compass.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace webots {
2121
class Compass : public Device {
2222
public:
2323
explicit Compass(const std::string &name) : Device(name) {} // Use Robot::getCompass() instead
24+
explicit Compass(WbDeviceTag tag) : Device(tag) {}
2425
virtual ~Compass() {}
2526
virtual void enable(int samplingPeriod);
2627
virtual void disable();

include/controller/cpp/webots/Connector.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace webots {
2121
class Connector : public Device {
2222
public:
2323
explicit Connector(const std::string &name) : Device(name) {} // Use Robot::getConnector() instead
24+
explicit Connector(WbDeviceTag tag) : Device(tag) {}
2425
virtual ~Connector() {}
2526
virtual void enablePresence(int samplingPeriod);
2627
virtual void disablePresence();

include/controller/cpp/webots/Device.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace webots {
3232

3333
protected:
3434
explicit Device(const std::string &name);
35+
explicit Device(WbDeviceTag tag);
3536

3637
private:
3738
WbDeviceTag tag;

include/controller/cpp/webots/Display.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace webots {
2424
public:
2525
enum { RGB = 3, RGBA, ARGB, BGRA, ABGR };
2626
explicit Display(const std::string &name) : Device(name) {} // Use Robot::getDisplay() instead
27+
explicit Display(WbDeviceTag tag) : Device(tag) {}
2728
virtual ~Display() {}
2829
int getWidth() const;
2930
int getHeight() const;

0 commit comments

Comments
 (0)