Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Python interfaces to some Ignition Gazebo methods #1219

Merged
merged 37 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
cd14e6a
Added Python interfaces to same Ignition Gazebo methods
ahcorde Nov 19, 2021
3bc8e37
Added feedback
ahcorde Nov 25, 2021
4106c2b
Fixed License style
ahcorde Nov 25, 2021
7a54950
Fix style in destroyable class
ahcorde Nov 25, 2021
dbe9c08
return optional
ahcorde Nov 30, 2021
894ed35
Merge branch 'ign-gazebo6' into ahcorde/python/gazebo
ahcorde Nov 30, 2021
b2b9f8c
Follow code conventions
ahcorde Dec 10, 2021
7cf6ce6
Merge branch 'ign-gazebo6' into ahcorde/python/gazebo
ahcorde Dec 10, 2021
4682207
Fix
ahcorde Dec 10, 2021
3a56759
Merge branch 'ign-gazebo6' into ahcorde/python/gazebo
ahcorde Dec 30, 2021
0b89181
Feedback
ahcorde Dec 30, 2021
517f110
Added tutorial
ahcorde Jan 5, 2022
81a732a
CMake changes
ahcorde Jan 11, 2022
673ad9b
Added feedback
ahcorde Jan 13, 2022
6a9d769
Merge branch 'ign-gazebo6' into ahcorde/python/gazebo
ahcorde Jan 13, 2022
999d6a6
Fixed python issue
ahcorde Jan 14, 2022
81d7a3e
Merge branch 'ign-gazebo6' into ahcorde/python/gazebo
ahcorde Jan 14, 2022
30d8e00
Merge branch 'ign-gazebo6' into ahcorde/python/gazebo
ahcorde Jan 17, 2022
51763e4
Merge branch 'ign-gazebo6' into ahcorde/python/gazebo
ahcorde Jan 19, 2022
bac0c08
added test
ahcorde Jan 20, 2022
5a27a27
Merge branch 'ahcorde/python/gazebo' of https://github.com/ignitionro…
ahcorde Jan 20, 2022
76ce104
Merge branch 'ign-gazebo6' into ahcorde/python/gazebo
chapulina Jan 21, 2022
d0f1365
Added feedback
ahcorde Jan 21, 2022
a727ebf
Merge branch 'ahcorde/python/gazebo' of https://github.com/ignitionro…
ahcorde Jan 21, 2022
f8a31d5
Added feedback
ahcorde Jan 21, 2022
5619121
Merge branch 'ign-gazebo6' into ahcorde/python/gazebo
ahcorde Jan 24, 2022
3eb5691
[python] Add worldEntity util (#1308)
chapulina Jan 25, 2022
5929ce9
upate license year
ahcorde Jan 25, 2022
eba46e7
Merge branch 'ign-gazebo6' into ahcorde/python/gazebo
ahcorde Jan 25, 2022
738d0ca
Merge branch 'ign-gazebo6' into ahcorde/python/gazebo
chapulina Jan 27, 2022
c1bd44b
Depend on python3-ignition-math on Focal
chapulina Jan 27, 2022
6bac808
expose python options
chapulina Jan 28, 2022
e5ded4c
Use CMAKE_INSTALL_LIBDIR to also support lib/x86_64-linux-gnu
chapulina Jan 29, 2022
7063380
Merge branch 'ign-gazebo6' into ahcorde/python/gazebo
j-rivero Feb 17, 2022
98fa03c
Merge branch 'ign-gazebo6' into ahcorde/python/gazebo
j-rivero Feb 24, 2022
85b540c
Merge remote-tracking branch 'origin/ign-gazebo6' into ahcorde/python…
ahcorde Feb 25, 2022
d5e34d5
fixed cmakelists
ahcorde Feb 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ find_package(ignition-cmake2 2.8.0 REQUIRED)
# Configure the project
#============================================================================
ign_configure_project(VERSION_SUFFIX)
set (CMAKE_CXX_STANDARD 17)

#============================================================================
# Set project-specific options
Expand Down Expand Up @@ -186,6 +187,8 @@ add_subdirectory(examples)
#============================================================================
ign_create_packages()

add_subdirectory(python)

#============================================================================
# Configure documentation
#============================================================================
Expand Down
22 changes: 22 additions & 0 deletions examples/python/gravity.sdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" ?>
<sdf version="1.6">
<world name="gravity">
<plugin
filename="ignition-gazebo-physics-system"
name="ignition::gazebo::systems::Physics">
</plugin>

<model name="falling">
<link name="link">
<inertial>
<inertia>
<ixx>0.4</ixx>
<iyy>0.4</iyy>
<izz>0.4</izz>
</inertia>
<mass>1.0</mass>
</inertial>
</link>
</model>
</world>
</sdf>
75 changes: 75 additions & 0 deletions examples/python/helperFixture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/python3
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
# Copyright (C) 2021 Open Source Robotics Foundation
#
# 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
#
# http://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.

import os
import time

from ignition.common import set_verbosity
from ignition.gazebo import HelperFixture, World
from ignition.math import Vector3d


set_verbosity(4)

file_path = os.path.dirname(os.path.realpath(__file__))

helper = HelperFixture(os.path.join(file_path, 'gravity.sdf'))

post_iterations = 0
iterations = 0
pre_iterations = 0


def on_configure_cb(worldEntity, _ecm):
print('World entity is ', worldEntity)
w = World(worldEntity)
v = w.gravity(_ecm)
print('Gravity ', v)
modelEntity = w.model_by_name(_ecm, 'falling')
print('Entity for falling model is: ', modelEntity)


def on_post_udpate_cb(_info, _ecm):
global post_iterations
post_iterations += 1
# print(_info.sim_time)


def on_pre_udpate_cb(_info, _ecm):
global pre_iterations
pre_iterations += 1


def on_udpate_cb(_info, _ecm):
global iterations
iterations += 1


helper.on_post_update(on_post_udpate_cb)
helper.on_update(on_udpate_cb)
helper.on_pre_update(on_pre_udpate_cb)
helper.on_configure(on_configure_cb)

helper.finalize()

server = helper.server()
server.run(False, 1000, False)

while(server.is_running()):
time.sleep(0.1)

print('iterations ', iterations)
print('post_iterations ', post_iterations)
print('pre_iterations ', pre_iterations)
99 changes: 99 additions & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
if(WIN32 AND CMAKE_BUILD_TYPE STREQUAL "Debug")
# pybind11 logic for setting up a debug build when both a debug and release
# python interpreter are present in the system seems to be pretty much broken.
# This works around the issue.
set(PYTHON_LIBRARIES "${PYTHON_DEBUG_LIBRARIES}")
endif()


if(USE_SYSTEM_PATHS_FOR_PYTHON_INSTALLATION)
if(${CMAKE_VERSION} VERSION_LESS "3.12.0")
execute_process(
COMMAND "${PYTHON_EXECUTABLE}" -c "if True:
from distutils import sysconfig as sc
print(sc.get_python_lib(plat_specific=True))"
OUTPUT_VARIABLE Python3_SITEARCH
OUTPUT_STRIP_TRAILING_WHITESPACE)
else()
# Get install variable from Python3 module
# Python3_SITEARCH is available from 3.12 on, workaround if needed:
find_package(Python3 COMPONENTS Interpreter)
endif()

if(USE_DIST_PACKAGES_FOR_PYTHON)
string(REPLACE "site-packages" "dist-packages" IGN_PYTHON_INSTALL_PATH ${Python3_SITEARCH})
else()
# custom cmake command is returning dist-packages
string(REPLACE "dist-packages" "site-packages" IGN_PYTHON_INSTALL_PATH ${Python3_SITEARCH})
endif()
else()
# If not a system installation, respect local paths
set(IGN_PYTHON_INSTALL_PATH ${IGN_LIB_INSTALL_DIR}/python)
endif()

set(IGN_PYTHON_INSTALL_PATH "${IGN_PYTHON_INSTALL_PATH}/ignition")

set(PYBIND11_PYTHON_VERSION 3)

find_package(pybind11 2.2 QUIET)
ahcorde marked this conversation as resolved.
Show resolved Hide resolved

if (NOT ${pybind11_FOUND})
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.11.0")
include(FetchContent)
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11
GIT_TAG v2.8.1
)

FetchContent_GetProperties(pybind11)
if(NOT pybind11_POPULATED)
FetchContent_Populate(pybind11)
add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR})
endif()
set(pybind11_FOUND TRUE)
endif()
endif()

if (${pybind11_FOUND})

# Set the build location and install location for a CPython extension
function(configure_build_install_location _library_name)
# Install library for actual use
install(TARGETS ${_library_name}
DESTINATION "${IGN_PYTHON_INSTALL_PATH}/"
)
endfunction()

pybind11_add_module(gazebo SHARED
src/ignition/gazebo/_ignition_gazebo_pybind11.cc
src/ignition/gazebo/Destroyable.cc
src/ignition/gazebo/EntityComponentManager
src/ignition/gazebo/EventManager.cc
src/ignition/gazebo/HelperSystem.cc
src/ignition/gazebo/Server.cc
src/ignition/gazebo/ServerConfig.cc
ahcorde marked this conversation as resolved.
Show resolved Hide resolved
src/ignition/gazebo/UpdateInfo.cc
src/ignition/gazebo/World.cc
)

target_link_libraries(gazebo PRIVATE
${PROJECT_LIBRARY_TARGET_NAME}
sdformat${SDF_VER}::sdformat${SDF_VER}
ignition-common${IGN_COMMON_VER}::ignition-common${IGN_COMMON_VER}
)

# TODO(ahcorde): Move this module to ign-common
pybind11_add_module(common SHARED
src/ignition/common/_ignition_common_pybind11.cc
src/ignition/common/Console.cc
)

target_link_libraries(common PRIVATE
ignition-common${IGN_COMMON_VER}::ignition-common${IGN_COMMON_VER}
)

configure_build_install_location(gazebo)
configure_build_install_location(common)

endif()
31 changes: 31 additions & 0 deletions python/src/ignition/common/Console.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2021 Open Source Robotics Foundation, Inc.
//
// 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
//
// http://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.

#include <ignition/common/Console.hh>

#include "Console.hh"

namespace ignition
{
namespace common
{
namespace python
{
void SetVerbosity(int _verbosity)
{
ignition::common::Console::SetVerbosity(_verbosity);
}
}
}
}
31 changes: 31 additions & 0 deletions python/src/ignition/common/Console.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2021 Open Source Robotics Foundation, Inc.
//
// 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
//
// http://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.

#ifndef IGNITION_GAZEBO_PYTHON__CONSOLE_HH_
#define IGNITION_GAZEBO_PYTHON__CONSOLE_HH_

#include <pybind11/pybind11.h>

namespace ignition
{
namespace common
{
namespace python
{
void SetVerbosity(int _verbosity);
}
}
}

#endif
25 changes: 25 additions & 0 deletions python/src/ignition/common/_ignition_common_pybind11.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2021 Open Source Robotics Foundation, Inc.
//
// 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
//
// http://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.

#include <pybind11/pybind11.h>

#include "Console.hh"

PYBIND11_MODULE(common, m) {
m.doc() = "Ignition Common Python Library.";

m.def(
"set_verbosity", &ignition::common::python::SetVerbosity,
"Set verbosity level.");
}
Loading