Skip to content

Commit dbbbfa1

Browse files
committed
on pybinds
1 parent 450a725 commit dbbbfa1

File tree

15 files changed

+476
-24
lines changed

15 files changed

+476
-24
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ serow_layout.json:Zone.Identifier
1111
/evaluation/mujoco_test/build/
1212
/evaluation/mujoco_test/mujoco_data/*/est_*.bin
1313
/evaluation/mujoco_test/mujoco_data/*/serow_predictions.h5
14+
/python/serow/__pycache__/
15+
/python/build/
16+
/python/serow.egg-info/
17+
/python/serow/contact_ekf.cpython-310-x86_64-linux-gnu.so
18+
/python/serow/state.cpython-310-x86_64-linux-gnu.so

core/src/BaseEKF.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ class BaseEKF {
106106
* @param angular_velocity Angular velocity measurements.
107107
* @param linear_acceleration Linear acceleration measurements.
108108
*/
109-
void computeDiscreteDynamics(BaseState& state, double dt,
110-
Eigen::Vector3d angular_velocity,
109+
void computeDiscreteDynamics(BaseState& state, double dt, Eigen::Vector3d angular_velocity,
111110
Eigen::Vector3d linear_acceleration);
112111

113112
/**
@@ -132,10 +131,10 @@ class BaseEKF {
132131
* @param base_position_cov Covariance of base position measurements.
133132
* @param base_orientation_cov Covariance of base orientation measurements.
134133
*/
135-
void updateWithOdometry(BaseState& state, const Eigen::Vector3d& base_position,
136-
const Eigen::Quaterniond& base_orientation,
137-
const Eigen::Matrix3d& base_position_cov,
138-
const Eigen::Matrix3d& base_orientation_cov);
134+
void updateWithOdometry(BaseState& state, const Eigen::Vector3d& base_position,
135+
const Eigen::Quaterniond& base_orientation,
136+
const Eigen::Matrix3d& base_position_cov,
137+
const Eigen::Matrix3d& base_orientation_cov);
139138

140139
/**
141140
* @brief Updates the state of the robot with the provided state change and covariance matrix.

core/src/CoMEKF.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,9 @@ void CoMEKF::updateWithKinematics(CentroidalState& state, const KinematicMeasure
6969
}
7070

7171
void CoMEKF::updateWithImu(CentroidalState& state, const KinematicMeasurement& kin,
72-
const GroundReactionForceMeasurement& grf) {
72+
const GroundReactionForceMeasurement& grf) {
7373
updateWithCoMAcceleration(state, kin.com_linear_acceleration, grf.cop, grf.force,
74-
kin.com_linear_acceleration_cov,
75-
kin.com_angular_momentum_derivative);
74+
kin.com_linear_acceleration_cov, kin.com_angular_momentum_derivative);
7675
}
7776

7877
Eigen::Matrix<double, 9, 1> CoMEKF::computeContinuousDynamics(
@@ -127,11 +126,12 @@ CoMEKF::computePredictionJacobians(const CentroidalState& state,
127126
return std::make_tuple(Ac, Lc);
128127
}
129128

130-
void CoMEKF::updateWithCoMAcceleration(
131-
CentroidalState& state, const Eigen::Vector3d& com_linear_acceleration,
132-
const Eigen::Vector3d& cop_position, const Eigen::Vector3d& ground_reaction_force,
133-
const Eigen::Matrix3d& com_linear_acceleration_cov,
134-
const Eigen::Vector3d& com_angular_momentum_derivative) {
129+
void CoMEKF::updateWithCoMAcceleration(CentroidalState& state,
130+
const Eigen::Vector3d& com_linear_acceleration,
131+
const Eigen::Vector3d& cop_position,
132+
const Eigen::Vector3d& ground_reaction_force,
133+
const Eigen::Matrix3d& com_linear_acceleration_cov,
134+
const Eigen::Vector3d& com_angular_momentum_derivative) {
135135
double den = state.com_position.z() - cop_position.z();
136136
if (den == 0.0) {
137137
std::cerr << "CoM and COP are at the same height, setting to 1e-6" << std::endl;
@@ -168,8 +168,7 @@ void CoMEKF::updateWithCoMAcceleration(
168168
updateState(state, dx, P_);
169169
}
170170

171-
void CoMEKF::updateWithCoMPosition(CentroidalState& state,
172-
const Eigen::Vector3d& com_position,
171+
void CoMEKF::updateWithCoMPosition(CentroidalState& state, const Eigen::Vector3d& com_position,
173172
const Eigen::Matrix3d& com_position_cov) {
174173
const Eigen::Vector3d z = com_position - state.com_position;
175174
Eigen::Matrix<double, 3, 9> H = Eigen::Matrix<double, 3, 9>::Zero();

core/src/ContactEKF.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,9 @@ void ContactEKF::updateWithTerrain(BaseState& state,
411411
// Update only when the elevation at the contact points is available and updated in the map
412412
if (cs) {
413413
const std::array<float, 2> terrain_origin = terrain_estimator->getMapOrigin();
414-
const std::array<float, 2> con_pos_xy = {static_cast<float>(state.contacts_position.at(cf).x()) - terrain_origin[0],
415-
static_cast<float>(state.contacts_position.at(cf).y()) - terrain_origin[1]};
414+
const std::array<float, 2> con_pos_xy = {
415+
static_cast<float>(state.contacts_position.at(cf).x()) - terrain_origin[0],
416+
static_cast<float>(state.contacts_position.at(cf).y()) - terrain_origin[1]};
416417
auto elevation = terrain_estimator->getElevation(con_pos_xy);
417418
if (elevation.has_value() && elevation.value().updated) {
418419
// Construct the linearized measurement matrix H
@@ -524,8 +525,9 @@ void ContactEKF::update(BaseState& state, const KinematicMeasurement& kin,
524525
const Eigen::Vector3d con_pos_world =
525526
T_world_to_base * kin.base_to_foot_positions.at(cf);
526527
const std::array<float, 2> terrain_origin = terrain_estimator->getMapOrigin();
527-
const std::array<float, 2> con_pos_xy = {static_cast<float>(con_pos_world.x()) - terrain_origin[0],
528-
static_cast<float>(con_pos_world.y()) - terrain_origin[1]};
528+
const std::array<float, 2> con_pos_xy = {
529+
static_cast<float>(con_pos_world.x()) - terrain_origin[0],
530+
static_cast<float>(con_pos_world.y()) - terrain_origin[1]};
529531
const float con_pos_z = static_cast<float>(con_pos_world.z());
530532

531533
// Transform measurement noise to world frame

core/src/ContactEKF.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ class ContactEKF {
8383
std::optional<OdometryMeasurement> odom = std::nullopt,
8484
std::shared_ptr<TerrainElevation> terrain_estimator = nullptr);
8585

86-
const Eigen::Isometry3d& getMapPose() const;
87-
8886
private:
8987
int num_states_{}; ///< Number of state variables.
9088
int num_inputs_{}; ///< Number of input variables.

core/src/Serow.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,8 @@ void Serow::filter(ImuMeasurement imu, std::map<std::string, JointMeasurement> j
718718
} else if (params_.terrain_estimator_type == "local") {
719719
terrain_estimator_ = std::make_shared<LocalTerrainMapper>();
720720
} else {
721-
throw std::runtime_error("Invalid terrain estimator type: " + params_.terrain_estimator_type);
721+
throw std::runtime_error("Invalid terrain estimator type: " +
722+
params_.terrain_estimator_type);
722723
}
723724
terrain_estimator_->initializeLocalMap(terrain_height, 1e4,
724725
params_.minimum_terrain_height_variance);
@@ -944,7 +945,8 @@ void Serow::filter(ImuMeasurement imu, std::map<std::string, JointMeasurement> j
944945
float y = (j - half_map_dim) * resolution;
945946
const auto& cell = terrain_map[terrain_estimator_->locationToHashId(
946947
std::array<float, 2>{x, y})];
947-
local_map.data.push_back({x + map_origin_xy[0], y + map_origin_xy[1], cell.height});
948+
local_map.data.push_back(
949+
{x + map_origin_xy[0], y + map_origin_xy[1], cell.height});
948950
}
949951
}
950952
exteroception_logger_.log(local_map);

python/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Python Bindings
2+
3+
## Install the python package
4+
5+
Open a terminal in the current directory
6+
7+
```
8+
export PYTHONPATH=$PYTHONPATH:$(pwd)
9+
source ~/.bashrc
10+
python3 setup.py build_ext --inplace
11+
```
12+
13+
## Tests
14+
Run the example with:
15+
16+
```
17+
python3 serow/example.py
18+
```

python/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This file makes the python directory a Python package

python/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["setuptools>=42", "wheel", "cmake>=3.1", "pybind11>=2.6.0"]
3+
build-backend = "setuptools.build_meta"

python/serow/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(serow_python)
3+
4+
# Find required packages
5+
find_package(pybind11 REQUIRED)
6+
find_package(Eigen3 REQUIRED)
7+
8+
# Find the core library
9+
find_library(SEROW_CORE_LIB serow
10+
PATHS
11+
${CMAKE_SOURCE_DIR}/../../build/core/src
12+
${CMAKE_SOURCE_DIR}/../../build
13+
NO_DEFAULT_PATH
14+
)
15+
16+
if(NOT SEROW_CORE_LIB)
17+
message(FATAL_ERROR "Could not find serow library. Please build the core library first by running:\n"
18+
"cd ${CMAKE_SOURCE_DIR}/../..\n"
19+
"mkdir -p build\n"
20+
"cd build\n"
21+
"cmake ..\n"
22+
"make")
23+
endif()
24+
25+
# Add Python module
26+
pybind11_add_module(contact_ekf contact_ekf_bindings.cpp)
27+
pybind11_add_module(state state_bindings.cpp)
28+
29+
# Set include directories
30+
target_include_directories(contact_ekf PRIVATE ${CMAKE_SOURCE_DIR}/../../core/src)
31+
target_include_directories(state PRIVATE ${CMAKE_SOURCE_DIR}/../../core/src)
32+
33+
# Link against Eigen and core library
34+
target_link_libraries(contact_ekf PRIVATE Eigen3::Eigen ${SEROW_CORE_LIB})
35+
target_link_libraries(state PRIVATE Eigen3::Eigen ${SEROW_CORE_LIB})
36+
37+
# Set C++ standard
38+
set_target_properties(contact_ekf PROPERTIES CXX_STANDARD 17)
39+
set_target_properties(state PROPERTIES CXX_STANDARD 17)

0 commit comments

Comments
 (0)