Skip to content

Commit

Permalink
Add API to load URDF from in-memory string.
Browse files Browse the repository at this point in the history
  • Loading branch information
kylc committed Nov 26, 2023
1 parent 1b0187c commit 78981f9
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions crates/optik-cpp/include/optik.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,19 @@ void SetParallelism(unsigned int n);

class Robot final {
public:
Robot(Robot& other) = delete;
Robot(Robot&& other) : inner_(std::move(other.inner_)) {
other.inner_ = nullptr;
};
~Robot();

Robot& operator=(Robot& other) = delete;
Robot& operator=(Robot&& other) {
inner_ = std::move(other.inner_);
other.inner_ = nullptr;
return *this;
};

//! Load a URDF model file from the given path, and build a chain from the
//! named base to the named end-effector link.
//!
Expand All @@ -49,6 +60,14 @@ class Robot final {
const std::string& base_link,
const std::string& ee_link);

//! See `FromUrdfFile`. Loads the model from an in-memory string.
//!
//! Panics if the string not contain a valid model file, or if either of the
//! links don't exist.
static Robot FromUrdfStr(const std::string& urdf,
const std::string& base_link,
const std::string& ee_link);

//! Draw a random generalized position vector from a uniform distribution
//! subject to the joint limits.
Eigen::VectorXd RandomConfiguration() const noexcept;
Expand Down
15 changes: 14 additions & 1 deletion crates/optik-cpp/src/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ extern void optik_set_parallelism(unsigned int n);
extern optik::detail::robot* optik_robot_from_urdf_file(const char* path,
const char* base_link,
const char* ee_link);
extern optik::detail::robot* optik_robot_from_urdf_str(const char* urdf,
const char* base_link,
const char* ee_link);
extern void optik_robot_free(optik::detail::robot* robot);

extern unsigned int optik_robot_dof(const optik::detail::robot* robot);
Expand All @@ -26,14 +29,24 @@ namespace optik {

void SetParallelism(unsigned int n) { optik_set_parallelism(n); }

Robot::~Robot() { optik_robot_free(inner_); }
Robot::~Robot() {
if (inner_ != nullptr) {
optik_robot_free(inner_);
}
}

Robot Robot::FromUrdfFile(const std::string& path, const std::string& base_link,
const std::string& ee_link) {
return optik_robot_from_urdf_file(path.c_str(), base_link.c_str(),
ee_link.c_str());
}

Robot Robot::FromUrdfStr(const std::string& urdf, const std::string& base_link,
const std::string& ee_link) {
return optik_robot_from_urdf_str(urdf.c_str(), base_link.c_str(),
ee_link.c_str());
}

Eigen::VectorXd Robot::RandomConfiguration() const noexcept {
double* q_data = optik_robot_random_configuration(inner_);
Eigen::VectorXd q = Eigen::Map<Eigen::VectorXd>(q_data, num_positions());
Expand Down
13 changes: 13 additions & 0 deletions crates/optik-cpp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ extern "C" fn optik_robot_from_urdf_file(
Box::into_raw(Box::new(Robot::from_urdf_file(path, base_link, ee_link)))
}

#[no_mangle]
extern "C" fn optik_robot_from_urdf_str(
urdf: *const c_char,
base_link: *const c_char,
ee_link: *const c_char,
) -> *mut Robot {
let urdf = to_str(urdf);
let base_link = to_str(base_link);
let ee_link = to_str(ee_link);

Box::into_raw(Box::new(Robot::from_urdf_str(urdf, base_link, ee_link)))
}

#[no_mangle]
extern "C" fn optik_robot_free(robot: *mut Robot) {
unsafe {
Expand Down
1 change: 1 addition & 0 deletions crates/optik/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rand = "0.8"
rand_chacha = "0.3"
rayon = "1.8"
slsqp-sys = { path = "../slsqp-sys" }
urdf-rs = "0.8"

[[example]]
name = "example"
Expand Down
12 changes: 11 additions & 1 deletion crates/optik/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,17 @@ impl Robot {
}

pub fn from_urdf_file(path: impl AsRef<Path>, base_link: &str, ee_link: &str) -> Self {
let chain = Chain::<f64>::from_urdf_file(path).expect("error parsing URDF file!");
let urdf = urdf_rs::read_file(path).expect("error parsing URDF file!");
Robot::from_urdf(&urdf, base_link, ee_link)
}

pub fn from_urdf_str(urdf: &str, base_link: &str, ee_link: &str) -> Self {
let urdf = urdf_rs::read_from_string(urdf).expect("error parsing URDF file!");
Robot::from_urdf(&urdf, base_link, ee_link)
}

pub fn from_urdf(urdf: &urdf_rs::Robot, base_link: &str, ee_link: &str) -> Self {
let chain = Chain::<f64>::from(urdf);

let base_link = chain
.find_link(base_link)
Expand Down

0 comments on commit 78981f9

Please sign in to comment.