-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding webots dynamic simulation, tbd tested
- Loading branch information
Armando Bañuelos
committed
Apr 9, 2024
1 parent
e936093
commit efc4f85
Showing
6 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
""" | ||
Create a Webots cube in air and have it drop | ||
""" | ||
|
||
model scenic.simulators.webots.model | ||
|
||
class AdhocBox(WebotsObject): | ||
webotsAdhoc: {} | ||
|
||
ego = new AdhocBox at (0, 0, 10) | ||
workspace = Workspace(RectangularRegion((0, 0, 0), 0, 10, 10)) |
16 changes: 16 additions & 0 deletions
16
.../simulators/webots/dynamic/webots_data/controllers/scenic_supervisor/scenic_supervisor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from controller import Supervisor | ||
|
||
import scenic | ||
from scenic.simulators.webots import WebotsSimulator | ||
|
||
supervisor = Supervisor() | ||
simulator = WebotsSimulator(supervisor) | ||
|
||
path = supervisor.getCustomData() | ||
print(f"Loading Scenic scenario {path}") | ||
scenario = scenic.scenarioFromFile(path) | ||
|
||
while True: | ||
scene, _ = scenario.generate() | ||
print("Starting new simulation...") | ||
simulator.simulate(scene, verbosity=2) |
146 changes: 146 additions & 0 deletions
146
tests/simulators/webots/dynamic/webots_data/declare_externproto.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 1996-2023 Cyberbotics Ltd. | ||
# | ||
# 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 | ||
# | ||
# https://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. | ||
|
||
"""Makes local PROTO references extern to fix the compatibility between R2022a and R2022b""" | ||
|
||
|
||
import os | ||
import re | ||
import sys | ||
import argparse | ||
from pathlib import Path | ||
import xml.etree.ElementTree as ET | ||
|
||
parser = argparse.ArgumentParser(description='Declares EXTERNPROTO references') | ||
parser.add_argument('webots_path', nargs=1, default=None) | ||
args = parser.parse_args() | ||
|
||
# ensure it's a valid webots path | ||
if sys.platform.startswith('darwin'): | ||
protolist = os.path.join(args.webots_path[0], 'contents', 'resources', 'proto-list.xml') | ||
else: | ||
protolist = os.path.join(args.webots_path[0], 'resources', 'proto-list.xml') | ||
|
||
if not os.path.exists(protolist): | ||
raise RuntimeError(f'Path {protolist} is not a valid webots path, proto-list.xml not found') | ||
|
||
# parse proto-list.xml | ||
tree = ET.parse(protolist) | ||
root = tree.getroot() | ||
|
||
official_protos = {} | ||
for proto in root: | ||
official_protos[proto.find('name').text] = proto.find('url').text | ||
|
||
files = [] | ||
files.extend(Path('./protos').rglob('*.proto')) | ||
files.extend(Path('./worlds').rglob('*.wbt')) | ||
|
||
local_files = {} | ||
for file in files: | ||
local_files[file.stem] = file.resolve() | ||
|
||
for file, path in local_files.items(): | ||
# read asset | ||
with open(path, "r") as f: | ||
contents = f.read() | ||
|
||
# find any match by bruteforce, starting with local ones | ||
local_matches = [] | ||
for key, value in local_files.items(): | ||
identifier = key.replace('+', r'\+').replace('-', r'\-').replace('_', r'\_') | ||
regexp = re.compile(rf'{identifier}\s*' + re.escape('{')) | ||
if regexp.search(contents): | ||
if key not in local_matches: | ||
local_matches.append(key) | ||
|
||
# now among the official ones | ||
official_matches = [] | ||
for key, value in official_protos.items(): | ||
identifier = key.replace('+', r'\+').replace('-', r'\-').replace('_', r'\_') | ||
regexp = re.compile(rf'{identifier}\s*' + re.escape('{')) | ||
if regexp.search(contents): | ||
if key not in official_matches: | ||
official_matches.append(key) | ||
|
||
contents = contents.splitlines(keepends=True) | ||
|
||
header = [] | ||
index = None | ||
for n, line in enumerate(contents): | ||
if line.replace(' ', '').replace('\t', '') == '\n': | ||
continue | ||
clean_line = line.strip() | ||
if clean_line.startswith('#'): | ||
header.append(clean_line + '\n') | ||
else: | ||
index = n | ||
break | ||
|
||
if not header: | ||
raise RuntimeError(f'File {path.name} is invalid because it has no header') | ||
version = re.search(r'^#\s*VRML_SIM\s+([a-zA-Z0-9\-]+)\s+utf8', header[0]) | ||
if not version: | ||
raise RuntimeError(f'The header of {path.name} is not recognized') | ||
elif (version.group(1) >= 'R2022b'): | ||
print(f'Skipping "{path.name}" because header is already R2022b or higher') | ||
continue | ||
|
||
if index: | ||
# consume all the empty lines following the index or previous attempts at declaring EXTERNPROTO | ||
while contents[index] == '\n' or contents[index].startswith('EXTERNPROTO'): | ||
del contents[index] | ||
else: | ||
raise RuntimeError(f'File {proto} has an invalid structure') | ||
|
||
if len(local_matches) > 0 or len(official_matches) > 0: | ||
print(f'File "{path.name}" depends on:') | ||
for match in local_matches: | ||
print(f' [LOCAL] {match}.proto') | ||
for match in official_matches: | ||
if match not in local_matches: | ||
print(f' [OFFICIAL] {match}.proto') | ||
else: | ||
print(f'File "{path.name}" does not reference any PROTO') | ||
|
||
declarations = [] | ||
for match in local_matches: | ||
relative_path = os.path.relpath(local_files[match], path.parent) | ||
relative_path = relative_path.replace("\\", "/") | ||
declarations.append(f'EXTERNPROTO "{relative_path}"\n') | ||
|
||
for match in official_matches: | ||
if match in local_matches: # favor existing local PROTO over official one if the name is the same | ||
continue | ||
|
||
declarations.append(f'EXTERNPROTO "{official_protos[match]}"\n') | ||
|
||
# insert declaration in the PROTO file | ||
if len(declarations) > 0: | ||
contents.insert(index, '\n') | ||
for declaration in declarations: | ||
contents.insert(index, declaration) | ||
contents.insert(index, '\n') | ||
|
||
# update proto header | ||
contents = contents[n:] # remove old header | ||
header[0] = '#VRML_SIM R2022b utf8\n' | ||
contents = header + contents | ||
|
||
# write to file | ||
with open(path, "w") as f: | ||
contents = "".join(contents) | ||
f.write(contents) |
28 changes: 28 additions & 0 deletions
28
tests/simulators/webots/dynamic/webots_data/protos/ScenicObject.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#VRML_SIM R2023a utf8 | ||
|
||
PROTO ScenicObject [ | ||
field SFVec3f translation 0 0 0 | ||
field SFRotation rotation 0 0 1 0 | ||
field SFString name "solid" | ||
field SFVec3f angularVelocity 0 0 0 | ||
field MFString url "" | ||
] | ||
{ | ||
Solid { | ||
translation IS translation | ||
rotation IS rotation | ||
name IS name | ||
angularVelocity IS angularVelocity | ||
children [ | ||
CadShape { | ||
url IS url | ||
castShadows FALSE | ||
} | ||
] | ||
boundingObject Shape { | ||
geometry Mesh { | ||
url IS url | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
tests/simulators/webots/dynamic/webots_data/protos/ScenicObjectWithPhysics.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#VRML_SIM R2023a utf8 | ||
|
||
PROTO ScenicObjectWithPhysics [ | ||
field SFVec3f translation 0 0 0 | ||
field SFRotation rotation 0 0 1 0 | ||
field SFString name "solid" | ||
field SFVec3f angularVelocity 0 0 0 | ||
field MFString url "" | ||
field SFFloat density 1000 # kg / m^3 | ||
] | ||
{ | ||
Solid { | ||
translation IS translation | ||
rotation IS rotation | ||
name IS name | ||
angularVelocity IS angularVelocity | ||
children [ | ||
CadShape { | ||
url IS url | ||
castShadows FALSE | ||
} | ||
] | ||
boundingObject Shape { | ||
geometry Mesh { | ||
url IS url | ||
} | ||
} | ||
physics Physics { | ||
# density will be set by the simulator | ||
density IS density | ||
mass -1 | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
tests/simulators/webots/dynamic/webots_data/worlds/world.wbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#VRML_SIM R2023a utf8 | ||
|
||
IMPORTABLE EXTERNPROTO "../protos/ScenicObject.proto" | ||
IMPORTABLE EXTERNPROTO "../protos/ScenicObjectWithPhysics.proto" | ||
|
||
WorldInfo { | ||
gravity 0 | ||
basicTimeStep 16 | ||
contactProperties [ | ||
ContactProperties { | ||
coulombFriction [ | ||
0.8 | ||
] | ||
} | ||
] | ||
} | ||
Viewpoint { | ||
orientation -0.19729161510865992 0.07408415124219735 0.977541588446517 2.4380019050245862 | ||
position 6.683615307234937 -5.5006366813466805 4.16419445995153 | ||
} | ||
Robot { | ||
name "Supervisor" | ||
controller "scenic_supervisor" | ||
customData "../../dynamic.scenic" | ||
supervisor TRUE | ||
} |