April 6 Documentation AK (Lustre and Using Embedded Rust Structs in Embedded C) - GaloisInc/HighAssuranceControllerOfSelfBalancingRobotCapstone GitHub Wiki
Modified Lustre PID file.
- Lustre, got it to compile in Rust
Rust Issues
- Problems with Rust installation,
Rustupno longer working - Reinstalled Rust/Rustup
- Issues with "alloc-Cortex-m" library, this uses a
linked_list_allocatorthat's causing compile issues.- Found https://github.com/rust-embedded/alloc-cortex-m/issues/28 , my issue is the same
- 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
- Decided to search in error location:
/home/artem/.cargo/registry/src/github.com-1ecc6299db9ec823/ alloc-cortex-mtoml asking for version0.6.0oflinked_list_allocator, different from: https://github.com/rust-embedded/alloc-cortex-m/blob/master/Cargo.toml- Tried alternative
alloc-cortex-mversion in toml, nope. - Cargo.toml file version of pulled
alloc-cortex-m, does not match Github version... - Modified toml with
alloc-cortex-m = { git = "https://github.com/rust-embedded/alloc-cortex-m" }to pull from github instead of cargo crate
- Need to find a way to test generated library vs. current library.
- Current Library uses inputs:
Input, &Output, Setpoint, Kp, Ki, Kd, Now, &lastTime, SampleTime, lastInput &outputSum - Lustre Library uses:
Input, Now, Setpoint, Kp, Ki, Kd, SampleTime - Massive issue: Lustre generates with the assumption that Rust will host memory, so
lastInput&lastTimewill have to be allocated in memory by Rust - Rust memory allocation with Arduino causes hangups, have to fix this issue before moving forward
- Current Library uses inputs:
- Resolving memory allocation in Rust/C++
- Attempting to construct PIDC struct, using pointer to
create_PIDCfunction create_PIDCfunction is on Rust side, that creates a PIDC struct using PIDC::new method(?)- 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." - Rust creates structures on the stack, but the required "Box::new" which is using the heap, which is not working w/ embedded
- Finding alternative to using
box::new, not likely per unofficial embedded page, constructors and descon. require opaque pointers. - Test without returning
PIDCusing pointer, works!! - Test using
box::(new_PIDC)to allocate memory on the heap, works when not returning. - Test using
box::into_raw, works. - Test returning ptr with data in heap, does not work. So memory allocation works in Heap until it is return to C++.
- Returning in stack seems to works. Need to find way to access data from
Computemethod. - Alternative idea;
newin C++ to allocate memory on heap based on class, rust inits data at the pointer. - Able to create the controller, able to call the
Computemethod of the controller struct, able to returndoublefrom controller. Unable to view saved values. - Potential solution:
newin C++, pass ptr to Rust functioncreate_PIDCthat calls our::newconstructor create-PIDCderefs the pointer and places the struct in heap created by C++- Now pointer in C++ is pointing to the struct that is in heap
- Second Rust function is required to get the Struct, and to call the PIDC
::computemethod.
- Attempting to construct PIDC struct, using pointer to
- Modify struct
::computemethod to match old compute function. - Modified for use with C++ see otis-arduino rust PID controller
Using Rust structs in C++ Summary:
- Allocate memory in C++ using:
PIDC *p = new PIDC;(PIDC is our Rust struct), this returns a pointer to a shunk of memory. - Rust requires a shim function to deference the pointer because of namespacing issues(?):
See
create_PIDCin rust PID Controller example - Use the Rust constructor to save the data in the memory heap:
*PIDC_ref = PIDC::new(kp, ki, kd, SampleTime); - To use other methods, a function is required to deference the pointer:
See
compute_PIDCin rust PID Controller example