From c3a3a356edf2375bf5a7a7d93e3ed7cbc4c934f3 Mon Sep 17 00:00:00 2001 From: franchioping Date: Mon, 16 Mar 2026 14:23:52 +0000 Subject: [PATCH] lots of changes, added some functions for cmath, separated in different files, build working with idf.py --- .nvim.lua | 17 +++++++ Cargo.toml | 2 +- build.rs | 9 +++- cbindgen.toml | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/ccont.rs | 96 +++++++++++++++++++++++++++++++++++ src/cmath.rs | 73 +++++++++++++++++++++++++++ src/lib.rs | 109 ++------------------------------------- 7 files changed, 336 insertions(+), 107 deletions(-) create mode 100644 .nvim.lua create mode 100644 cbindgen.toml create mode 100644 src/ccont.rs create mode 100644 src/cmath.rs diff --git a/.nvim.lua b/.nvim.lua new file mode 100644 index 0000000..c026d8d --- /dev/null +++ b/.nvim.lua @@ -0,0 +1,17 @@ +-- You must enable the exrc setting in neovim for this config file to be used. +local rust_analyzer = { + cargo = { + target = "xtensa-esp32-none-elf", + allTargets = false, + }, +} +rust_analyzer.cargo.extraEnv = { RUST_TOOLCHAIN = "esp" } +rust_analyzer.check = { extraEnv = { RUST_TOOLCHAIN = "esp" } } +rust_analyzer.server = { extraEnv = { RUST_TOOLCHAIN = "stable" } } + +-- Note the neovim name of the language server is rust_analyzer with an underscore. +vim.lsp.config("rust_analyzer", { + settings = { + ["rust-analyzer"] = rust_analyzer + }, +}) diff --git a/Cargo.toml b/Cargo.toml index 0720f12..e67ca1d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ version = "0.1.0" edition = "2024" [lib] -crate-type = ["staticlib"] +crate-type = ["staticlib", "lib"] [build-dependencies] cbindgen = "0.29" diff --git a/build.rs b/build.rs index d34d8a3..7449918 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,5 @@ -use std::env; use std::path::{Path, PathBuf}; +use std::{env, time}; fn main() { let cargo_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); @@ -19,4 +19,11 @@ fn run_cbindgen(cargo_dir: &Path, target_dir: &Path) { .write_to_file(&out); println!("cargo:rerun-if-changed={}", out.display()); + println!( + "cargo:rerun-if-changed={}", + time::SystemTime::now() + .duration_since(time::UNIX_EPOCH) + .unwrap() + .as_secs() + ); } diff --git a/cbindgen.toml b/cbindgen.toml new file mode 100644 index 0000000..226d713 --- /dev/null +++ b/cbindgen.toml @@ -0,0 +1,137 @@ +# This is a template cbindgen.toml file with all of the default values. +# Some values are commented out because their absence is the real default. +# +# See https://github.com/mozilla/cbindgen/blob/main/docs.md#cbindgentoml +# for detailed documentation of every option here. + + +language = "C++" + + +############## Options for Wrapping the Contents of the Header ################# + +# header = "/* Text to put at the beginning of the generated file. Probably a license. */" +# trailer = "/* Text to put at the end of the generated file */" +# include_guard = "my_bindings_h" +# pragma_once = true +# autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */" +include_version = false +namespace = "dcont" +# namespaces = [] +using_namespaces = [] +sys_includes = [] +includes = [] +no_includes = false +# cpp_compat = true +after_includes = "" + + +############################ Code Style Options ################################ + +braces = "SameLine" +line_length = 100 +tab_width = 2 +documentation = true +documentation_style = "auto" +documentation_length = "full" +line_endings = "LF" # also "CR", "CRLF", "Native" + + +############################# Codegen Options ################################## + +style = "both" +sort_by = "Name" # default for `fn.sort_by` and `const.sort_by` +usize_is_size_t = true + + +[defines] +# "target_os = freebsd" = "DEFINE_FREEBSD" +# "feature = serde" = "DEFINE_SERDE" + + +[export] +include = [] +exclude = [] +# prefix = "CAPI_" +item_types = [] +renaming_overrides_prefixing = false + + +[export.rename] + + +[export.body] + + +[export.mangle] + + +[fn] +rename_args = "None" +# must_use = "MUST_USE_FUNC" +# deprecated = "DEPRECATED_FUNC" +# deprecated_with_note = "DEPRECATED_FUNC_WITH_NOTE" +# no_return = "NO_RETURN" +# prefix = "START_FUNC" +# postfix = "END_FUNC" +args = "auto" +sort_by = "Name" + + +[struct] +rename_fields = "None" +# must_use = "MUST_USE_STRUCT" +# deprecated = "DEPRECATED_STRUCT" +# deprecated_with_note = "DEPRECATED_STRUCT_WITH_NOTE" +rename_associated_constant = "None" +derive_constructor = false +derive_eq = false +derive_neq = false +derive_lt = false +derive_lte = false +derive_gt = false +derive_gte = false + + +[enum] +rename_variants = "None" +# must_use = "MUST_USE_ENUM" +# deprecated = "DEPRECATED_ENUM" +# deprecated_with_note = "DEPRECATED_ENUM_WITH_NOTE" +add_sentinel = false +prefix_with_name = false +derive_helper_methods = false +derive_const_casts = false +derive_mut_casts = false +# cast_assert_name = "ASSERT" +derive_tagged_enum_destructor = false +derive_tagged_enum_copy_constructor = false +enum_class = true +private_default_tagged_enum_constructor = false + + +[const] +allow_static_const = true +allow_constexpr = false +sort_by = "Name" + + +[macro_expansion] +bitflags = false + + +############## Options for How Your Rust library Should Be Parsed ############## + +[parse] +parse_deps = false +# include = [] +exclude = [] +clean = false +extra_bindings = [] + + +[parse.expand] +crates = [] +all_features = false +default_features = true +features = [] diff --git a/src/ccont.rs b/src/ccont.rs new file mode 100644 index 0000000..07a7526 --- /dev/null +++ b/src/ccont.rs @@ -0,0 +1,96 @@ +use crate::*; +use nalgebra as na; + +#[unsafe(no_mangle)] +pub extern "C" fn create(c: config::ControllerConfig) -> *mut StackedController { + return Box::into_raw(Box::new(StackedController::new(c))); +} + + +#[unsafe(no_mangle)] +pub unsafe extern "C" fn destroy(ptr: *mut StackedController) { + assert!(!ptr.is_null()); + let _x = unsafe { Box::from_raw(ptr) }; +} + +#[repr(C)] +pub struct MotorThrottles { + pub values: [f32; 4], +} + + + + +#[unsafe(no_mangle)] +pub extern "C" fn get_throttles(ptr: *mut StackedController) -> MotorThrottles { + let controller = unsafe { + assert!(!ptr.is_null()); + &mut *ptr + }; + + return MotorThrottles { + values: controller.get_motor_throttles(), + }; +} + +#[unsafe(no_mangle)] +pub extern "C" fn set_cur_time(ptr: *mut StackedController, time: f32) { + let controller = unsafe { + assert!(!ptr.is_null()); + &mut *ptr + }; + + controller.set_time(time); +} + +#[unsafe(no_mangle)] +pub extern "C" fn set_input(ptr: *mut StackedController, inp: Input) { + let controller = unsafe { + assert!(!ptr.is_null()); + &mut *ptr + }; + + controller.set_input(inp); +} + +#[unsafe(no_mangle)] +pub extern "C" fn set_cur_rot(ptr: *mut StackedController, parts: Vec3C, scalar: f32) { + let controller = unsafe { + assert!(!ptr.is_null()); + &mut *ptr + }; + + let quat: na::UnitQuaternion = + na::UnitQuaternion::from_quaternion(na::Quaternion::from_parts(scalar, parts.into())); + controller.set_rotation(quat); +} + +#[unsafe(no_mangle)] +pub extern "C" fn set_cur_angvel(ptr: *mut StackedController, angvel: Vec3C) { + let controller = unsafe { + assert!(!ptr.is_null()); + &mut *ptr + }; + + controller.set_angular_velocity(angvel.into()); +} + +#[unsafe(no_mangle)] +pub extern "C" fn set_cur_linvel(ptr: *mut StackedController, vel: Vec3C) { + let controller = unsafe { + assert!(!ptr.is_null()); + &mut *ptr + }; + + controller.set_linvel(vel.into()); +} + +#[unsafe(no_mangle)] +pub extern "C" fn set_cur_pos(ptr: *mut StackedController, pos: Vec3C) { + let controller = unsafe { + assert!(!ptr.is_null()); + &mut *ptr + }; + + controller.set_pos(pos.into()); +} diff --git a/src/cmath.rs b/src/cmath.rs new file mode 100644 index 0000000..7fe4c52 --- /dev/null +++ b/src/cmath.rs @@ -0,0 +1,73 @@ +use nalgebra as na; + +#[repr(C)] +pub struct Vec3C { + pub x: f32, + pub y: f32, + pub z: f32, +} + +#[repr(C)] +pub struct QuatC { + pub i: f32, + pub j: f32, + pub k: f32, + pub w: f32, +} + +impl Into> for QuatC { + fn into(self) -> na::UnitQuaternion { + na::UnitQuaternion::from_quaternion(na::Quaternion::from_parts( + self.w, + na::vector![self.i, self.j, self.k], + )) + } +} + +impl Into> for Vec3C { + fn into(self) -> na::Vector3 { + na::vector![self.x, self.y, self.z] + } +} + +impl From> for Vec3C { + fn from(value: na::Vector3) -> Self { + Vec3C { x: value.x, y: value.y, z: value.z } + } +} + + + +#[unsafe(no_mangle)] +pub extern "C" fn apply_rot(vec_ptr: *const Vec3C, quat_ptr: *const QuatC) -> Vec3C{ + assert!(!vec_ptr.is_null()); + assert!(!quat_ptr.is_null()); + unsafe { + let quat: na::UnitQuaternion = std::ptr::read(quat_ptr).into(); + let v: na::Vector3 = std::ptr::read(vec_ptr).into(); + return quat.transform_vector(&v).into(); + } +} + +#[unsafe(no_mangle)] +pub extern "C" fn multiply_vec_by(vec_ptr: *mut Vec3C, k: f32) { + assert!(!vec_ptr.is_null()); + + unsafe { + (*vec_ptr).x *= k; + (*vec_ptr).y *= k; + (*vec_ptr).z *= k; + } +} + + +#[unsafe(no_mangle)] +pub extern "C" fn add_to_vec(vec_ptr: *mut Vec3C, to_add_ptr: *const Vec3C){ + assert!(!vec_ptr.is_null()); + assert!(!to_add_ptr.is_null()); + unsafe { + (*vec_ptr).x += (*to_add_ptr).x; + (*vec_ptr).y += (*to_add_ptr).y; + (*vec_ptr).z += (*to_add_ptr).z; + } +} diff --git a/src/lib.rs b/src/lib.rs index 2190629..ce245b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,3 @@ -use nalgebra::{self as na, Quaternion, Unit}; - pub mod config; pub mod controller; pub mod input; @@ -10,106 +8,7 @@ pub use controller::*; pub use input::*; pub use stacked::*; -#[unsafe(no_mangle)] -pub extern "C" fn create(c: config::ControllerConfig) -> *mut StackedController { - return Box::into_raw(Box::new(StackedController::new(c))); -} - - -#[unsafe(no_mangle)] -pub unsafe extern "C" fn destroy(ptr: *mut StackedController) { - assert!(!ptr.is_null()); - let _x = unsafe { Box::from_raw(ptr) }; -} - -#[repr(C)] -pub struct MotorThrottles { - pub values: [f32; 4], -} - -#[repr(C)] -pub struct Vec3C { - pub x: f32, - pub y: f32, - pub z: f32, -} - -impl Into> for Vec3C { - fn into(self) -> na::Vector3 { - na::vector![self.x, self.y, self.z] - } -} - -#[unsafe(no_mangle)] -pub extern "C" fn get_throttles(ptr: *mut StackedController) -> MotorThrottles { - let controller = unsafe { - assert!(!ptr.is_null()); - &mut *ptr - }; - - return MotorThrottles { - values: controller.get_motor_throttles(), - }; -} - -#[unsafe(no_mangle)] -pub extern "C" fn set_cur_time(ptr: *mut StackedController, time: f32) { - let controller = unsafe { - assert!(!ptr.is_null()); - &mut *ptr - }; - - controller.set_time(time); -} - -#[unsafe(no_mangle)] -pub extern "C" fn set_input(ptr: *mut StackedController, inp: Input) { - let controller = unsafe { - assert!(!ptr.is_null()); - &mut *ptr - }; - - controller.set_input(inp); -} - -#[unsafe(no_mangle)] -pub extern "C" fn set_cur_rot(ptr: *mut StackedController, parts: Vec3C, scalar: f32) { - let controller = unsafe { - assert!(!ptr.is_null()); - &mut *ptr - }; - - let quat: na::UnitQuaternion = - na::UnitQuaternion::from_quaternion(Quaternion::from_parts(scalar, parts.into())); - controller.set_rotation(quat); -} - -#[unsafe(no_mangle)] -pub extern "C" fn set_cur_angvel(ptr: *mut StackedController, angvel: Vec3C) { - let controller = unsafe { - assert!(!ptr.is_null()); - &mut *ptr - }; - - controller.set_angular_velocity(angvel.into()); -} - -#[unsafe(no_mangle)] -pub extern "C" fn set_cur_linvel(ptr: *mut StackedController, vel: Vec3C) { - let controller = unsafe { - assert!(!ptr.is_null()); - &mut *ptr - }; - - controller.set_linvel(vel.into()); -} - -#[unsafe(no_mangle)] -pub extern "C" fn set_cur_pos(ptr: *mut StackedController, pos: Vec3C) { - let controller = unsafe { - assert!(!ptr.is_null()); - &mut *ptr - }; - - controller.set_pos(pos.into()); -} +pub mod ccont; +pub mod cmath; +pub use ccont::*; +pub use cmath::*;