Skip to content

Commit 653dd4a

Browse files
CoolSpy3omichel
andauthored
Reset WbFieldRef->last_update After Simulation Reset (#6758)
* send C_SUPERVISOR_SIMULATION_RESET when simulation is reset * reset field->last_update on reset * add test * fix WbFieldRef type * correctly set translation in world * fix string comparison * set different robot name * search for correct message type * fix assertion messages * fix initial assertion * fix test * move message id * add comment * update changelog * use WbFieldRef instead of WbFieldStruct* * formatting * use code block * remove old C_SUPERVISOR_SIMULATION_RESET definition Co-authored-by: Olivier Michel <[email protected]> * update test world header --------- Co-authored-by: Olivier Michel <[email protected]>
1 parent 34e7dde commit 653dd4a

File tree

9 files changed

+160
-1
lines changed

9 files changed

+160
-1
lines changed

docs/reference/changelog-r2025.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Fixed a bug preventing the `webots-controller` executable from running on arm-based mac devices ([#6806](https://github.com/cyberbotics/webots/pull/6806)).
1111
- Fixed a bug causing Webots to crash when multiple sounds were used with a [Speaker](speaker.md) node ([#6843](https://github.com/cyberbotics/webots/pull/6843)).
1212
- Fixed a bug causing the "Reload/Reset" buttons in the controller recompilation popup to not work on Windows ([#6844](https://github.com/cyberbotics/webots/pull/6844)).
13+
- Fixed a bug causing supervisors to occasionally read stale field values after the simulation was reset ([#6758](https://github.com/cyberbotics/webots/pull/6758)).
1314
- Fixed a bug causing Webots to occasionally crash when unloading a world ([#6857](https://github.com/cyberbotics/webots/pull/6857)).
1415

1516
## Webots R2025a

src/controller/c/messages.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
#define C_SUPERVISOR_RELOAD_WORLD 52
7676
#define C_SUPERVISOR_SET_LABEL 53
7777
#define C_SUPERVISOR_SIMULATION_QUIT 54
78-
#define C_SUPERVISOR_SIMULATION_RESET 55
7978
#define C_SUPERVISOR_SIMULATION_CHANGE_MODE 56
8079
#define C_SUPERVISOR_SIMULATION_RESET_PHYSICS 57
8180
#define C_SUPERVISOR_START_MOVIE 58
@@ -101,6 +100,7 @@
101100
#define C_SUPERVISOR_NODE_GET_PROTO 78
102101

103102
// ctr <-> sim
103+
#define C_SUPERVISOR_SIMULATION_RESET 55
104104
#define C_ROBOT_WAIT_FOR_USER_INPUT_EVENT 80
105105
#define C_ROBOT_WWI_MESSAGE 81
106106
#define C_SUPERVISOR_SAVE_WORLD 82

src/controller/c/supervisor.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,15 @@ static void supervisor_read_answer(WbDevice *d, WbRequest *r) {
12951295
case C_SUPERVISOR_MOVIE_STATUS:
12961296
movie_status = request_read_uchar(r);
12971297
break;
1298+
case C_SUPERVISOR_SIMULATION_RESET: {
1299+
// Clear cached field values
1300+
WbFieldStruct *field = field_list;
1301+
while (field) {
1302+
field->last_update = -DBL_MAX;
1303+
field = field->next;
1304+
}
1305+
break;
1306+
}
12981307
case C_SUPERVISOR_SAVE_WORLD:
12991308
save_status = request_read_uchar(r);
13001309
break;

src/webots/nodes/utils/WbSupervisorUtilities.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ WbSupervisorUtilities::WbSupervisorUtilities(WbRobot *robot) : mRobot(robot) {
280280
// otherwise, conflicts can occur in case of multiple controllers
281281
connect(this, &WbSupervisorUtilities::changeSimulationModeRequested, this, &WbSupervisorUtilities::changeSimulationMode,
282282
Qt::QueuedConnection);
283+
connect(WbWorld::instance(), &WbWorld::resetRequested, this, &WbSupervisorUtilities::simulationReset, Qt::QueuedConnection);
283284
}
284285

285286
WbSupervisorUtilities::~WbSupervisorUtilities() {
@@ -343,6 +344,7 @@ void WbSupervisorUtilities::initControllerRequests() {
343344
mNodesDeletedSinceLastStep.clear();
344345
mUpdatedFields.clear();
345346
mWatchedFields.clear();
347+
mSimulationReset = false;
346348
}
347349

348350
QString WbSupervisorUtilities::readString(QDataStream &stream) {
@@ -2347,6 +2349,11 @@ void WbSupervisorUtilities::writeAnswer(WbDataStream &stream) {
23472349
}
23482350
mVirtualRealityHeadsetOrientationRequested = false;
23492351
}
2352+
if (mSimulationReset) {
2353+
stream << (short unsigned int)0;
2354+
stream << (unsigned char)C_SUPERVISOR_SIMULATION_RESET;
2355+
mSimulationReset = false;
2356+
}
23502357
}
23512358

23522359
void WbSupervisorUtilities::writeConfigure(WbDataStream &stream) {
@@ -2419,3 +2426,8 @@ QString WbSupervisorUtilities::createLabelUpdateString(const WbWrenLabelOverlay
24192426
.arg(y)
24202427
.arg(text.replace("\n", "\\n"));
24212428
}
2429+
2430+
void WbSupervisorUtilities::simulationReset(bool restartControllers) {
2431+
if (!restartControllers) // If the controller is about to be restarted, there's no need to tell it to reset its own state
2432+
mSimulationReset = true;
2433+
}

src/webots/nodes/utils/WbSupervisorUtilities.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ private slots:
7373
void removeTrackedContactPoints(QObject *obj);
7474
void removeTrackedPoseNode(QObject *obj);
7575
void removeTrackedField(QObject *obj);
76+
void simulationReset(bool restartControllers);
7677

7778
private:
7879
WbRobot *mRobot;
@@ -111,6 +112,7 @@ private slots:
111112
bool mNodeExportStringRequest;
112113
bool mIsProtoRegenerated;
113114
bool mShouldRemoveNode;
115+
bool mSimulationReset;
114116

115117
// pointer to a single integer: if not NULL, the new status has to be sent to the libController
116118
int *mAnimationStartStatus;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/supervisor_reset_simulation_fields
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 1996-2023 Cyberbotics Ltd.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Webots Makefile system
16+
#
17+
# You may add some variable definitions hereafter to customize the build process
18+
# See documentation in $(WEBOTS_HOME_PATH)/resources/Makefile.include
19+
20+
21+
# Do not modify the following: this includes Webots global Makefile.include
22+
null :=
23+
space := $(null) $(null)
24+
WEBOTS_HOME_PATH?=$(subst $(space),\ ,$(strip $(subst \,/,$(WEBOTS_HOME))))
25+
include $(WEBOTS_HOME_PATH)/resources/Makefile.include
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Description: Test Supervisor device API
3+
* This file contains tests of reset simulation method
4+
*/
5+
6+
#include <webots/robot.h>
7+
#include <webots/supervisor.h>
8+
9+
#include "../../../lib/ts_assertion.h"
10+
#include "../../../lib/ts_utils.h"
11+
12+
#define TIME_STEP 32
13+
14+
int main(int argc, char **argv) {
15+
ts_setup(argv[1]);
16+
17+
const double initial_translation[3] = {0, 0, 1};
18+
const double new_translation[3] = {1, 0, 0};
19+
WbFieldRef translation_field = wb_supervisor_node_get_field(wb_supervisor_node_get_from_def("SOLID"), "translation");
20+
if (strcmp(argv[1], "supervisor_reset_simulation_fields_resetter") == 0) {
21+
wb_robot_step(TIME_STEP);
22+
wb_robot_step(TIME_STEP);
23+
24+
// <Other controller verifies SOLID's inital position>
25+
26+
wb_robot_step(TIME_STEP);
27+
wb_supervisor_simulation_reset();
28+
wb_robot_step(TIME_STEP); // We have to wait another step or else the reset will overwrite the new value
29+
wb_supervisor_field_set_sf_vec3f(translation_field, new_translation);
30+
} else {
31+
wb_robot_step(TIME_STEP);
32+
wb_robot_step(TIME_STEP);
33+
const double *first_translation = wb_supervisor_field_get_sf_vec3f(translation_field);
34+
ts_assert_vec3_equal(
35+
first_translation[0], first_translation[1], first_translation[2], initial_translation[0], initial_translation[1],
36+
initial_translation[2], "SOLID's initial position should be [%f, %f, %f] not [%f, %f, %f]", initial_translation[0],
37+
initial_translation[1], initial_translation[2], first_translation[0], first_translation[1], first_translation[2]);
38+
wb_robot_step(TIME_STEP);
39+
40+
// <Other controller resets the simulation>
41+
42+
wb_robot_step(TIME_STEP);
43+
44+
// <Other controller updates SOLID's position>
45+
46+
wb_robot_step(TIME_STEP);
47+
const double *second_translation = wb_supervisor_field_get_sf_vec3f(translation_field);
48+
ts_assert_vec3_equal(
49+
second_translation[0], second_translation[1], second_translation[2], new_translation[0], new_translation[1],
50+
new_translation[2], "SOLID's position should be [%f, %f, %f] not [%f, %f, %f] after reset", new_translation[0],
51+
new_translation[1], new_translation[2], second_translation[0], second_translation[1], second_translation[2]);
52+
}
53+
54+
ts_send_success();
55+
return EXIT_SUCCESS;
56+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#VRML_SIM R2025a utf8
2+
3+
EXTERNPROTO "webots://tests/default/protos/TestSuiteSupervisor.proto"
4+
EXTERNPROTO "webots://tests/default/protos/TestSuiteEmitter.proto"
5+
6+
WorldInfo {
7+
info [
8+
"Test that fields do not return stale values after reset"
9+
]
10+
basicTimeStep 8
11+
coordinateSystem "NUE"
12+
}
13+
Viewpoint {
14+
orientation 0.3011392881427894 -0.8944857953945254 -0.3304698034227591 4.845225060401701
15+
position 8.056890161252701 5.15602094221405 0.024334960209242112
16+
}
17+
Background {
18+
skyColor [
19+
0 0 1
20+
]
21+
}
22+
DirectionalLight {
23+
direction 0 -1 0
24+
}
25+
DEF SOLID Solid {
26+
translation 0 0 1
27+
}
28+
TestSuiteSupervisor {
29+
}
30+
Robot {
31+
controller "supervisor_reset_simulation_fields"
32+
controllerArgs [
33+
"supervisor_reset_simulation_fields_checker"
34+
]
35+
children [
36+
TestSuiteEmitter {
37+
}
38+
]
39+
name "checker robot"
40+
supervisor TRUE
41+
}
42+
Robot {
43+
controller "supervisor_reset_simulation_fields"
44+
controllerArgs [
45+
"supervisor_reset_simulation_fields_resetter"
46+
]
47+
children [
48+
TestSuiteEmitter {
49+
}
50+
]
51+
name "receiver robot"
52+
supervisor TRUE
53+
}

0 commit comments

Comments
 (0)