April 6 Documentation AK (Lustre and Using Embedded Rust Structs in Embedded C) - GaloisInc/HighAssuranceControllerOfSelfBalancingRobotCapstone GitHub Wiki

Modified Lustre PID file.

  1. Lustre, got it to compile in Rust

Rust Issues

  1. Problems with Rust installation, Rustup no longer working
  2. Reinstalled Rust/Rustup
  3. Issues with "alloc-Cortex-m" library, this uses a linked_list_allocator that's causing compile issues.
    1. Found https://github.com/rust-embedded/alloc-cortex-m/issues/28 , my issue is the same
    2. Issue should have been resolved with the latest version of alloc-cortex-m, but for some reason, my download is using the wrong version of linked_list allocator
    3. Decided to search in error location: /home/artem/.cargo/registry/src/github.com-1ecc6299db9ec823/
    4. alloc-cortex-m toml asking for version 0.6.0 of linked_list_allocator, different from: https://github.com/rust-embedded/alloc-cortex-m/blob/master/Cargo.toml
    5. Tried alternative alloc-cortex-mversion in toml, nope.
    6. Cargo.toml file version of pulled alloc-cortex-m, does not match Github version...
    7. Modified toml with alloc-cortex-m = { git = "https://github.com/rust-embedded/alloc-cortex-m" } to pull from github instead of cargo crate
  4. Need to find a way to test generated library vs. current library.
    1. Current Library uses inputs: Input, &Output, Setpoint, Kp, Ki, Kd, Now, &lastTime, SampleTime, lastInput &outputSum
    2. Lustre Library uses: Input, Now, Setpoint, Kp, Ki, Kd, SampleTime
    3. Massive issue: Lustre generates with the assumption that Rust will host memory, so lastInput & lastTime will have to be allocated in memory by Rust
    4. Rust memory allocation with Arduino causes hangups, have to fix this issue before moving forward
  5. Resolving memory allocation in Rust/C++
    1. Attempting to construct PIDC struct, using pointer to create_PIDC function
    2. create_PIDC function is on Rust side, that creates a PIDC struct using PIDC::new method(?)
    3. Arduino hangs when attempting to create the struct, assuming this is an issue with: Box::new(), per documentation "Allocates memory on the heap and then places x into it."
    4. Rust creates structures on the stack, but the required "Box::new" which is using the heap, which is not working w/ embedded
    5. Finding alternative to using box::new, not likely per unofficial embedded page, constructors and descon. require opaque pointers.
    6. Test without returning PIDC using pointer, works!!
    7. Test using box::(new_PIDC) to allocate memory on the heap, works when not returning.
    8. Test using box::into_raw, works.
    9. Test returning ptr with data in heap, does not work. So memory allocation works in Heap until it is return to C++.
    10. Returning in stack seems to works. Need to find way to access data from Compute method.
    11. Alternative idea; new in C++ to allocate memory on heap based on class, rust inits data at the pointer.
    12. Able to create the controller, able to call the Compute method of the controller struct, able to return double from controller. Unable to view saved values.
    13. Potential solution: new in C++, pass ptr to Rust function create_PIDC that calls our ::new constructor
    14. create-PIDC derefs the pointer and places the struct in heap created by C++
    15. Now pointer in C++ is pointing to the struct that is in heap
    16. Second Rust function is required to get the Struct, and to call the PIDC ::compute method.
  6. Modify struct ::compute method to match old compute function.
  7. Modified for use with C++ see otis-arduino rust PID controller

Using Rust structs in C++ Summary:

  1. Allocate memory in C++ using: PIDC *p = new PIDC; (PIDC is our Rust struct), this returns a pointer to a shunk of memory.
  2. Rust requires a shim function to deference the pointer because of namespacing issues(?): See create_PIDC in rust PID Controller example
  3. Use the Rust constructor to save the data in the memory heap: *PIDC_ref = PIDC::new(kp, ki, kd, SampleTime);
  4. To use other methods, a function is required to deference the pointer: See compute_PIDC in rust PID Controller example