Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Motor Control Pulse Width Modulator (MCPWM) #92

Open
usbalbin opened this issue Jun 22, 2022 · 13 comments
Open

Motor Control Pulse Width Modulator (MCPWM) #92

usbalbin opened this issue Jun 22, 2022 · 13 comments
Labels
enhancement New feature or request

Comments

@usbalbin
Copy link
Contributor

Hi!

Is there any way to access the MCPWM peripheral found in ESP32 and ESP32-S3 through this crate?

@usbalbin
Copy link
Contributor Author

usbalbin commented Jun 22, 2022

I found the esp32 crate which seems to have this. However I am assuming that should not be used at the same time as this crate due to the crates having their own individual locks?

@MabezDev
Copy link
Member

Correct, this crate utilizes esp-idf functions and creates safe Rust wrappers around them. If you would like to try, you can create a wrapper around the MCPWM api and submit a PR (happy to review & mentor if you need some help). If not, someone will implement this eventually :).

@usbalbin
Copy link
Contributor Author

Maybe I will give it a try if I get time. Thanks! :)

@usbalbin
Copy link
Contributor Author

Do you think looking at something like ledc.rs and its correlation to the idf would be a good starting point for how to design the api?

@MabezDev
Copy link
Member

Do you think looking at something like ledc.rs and its correlation to the idf would be a good starting point for how to design the api?

Yes! I would definitely spend some time looking at LEDC, and some other peripherals just to get a feel of how to use the esp-idf functions within Rust, then you can start to think about how to safely wrap the MCPWM API in Rust :).

Feel free to join the matrix chat if you want: https://matrix.to/#/#esp-rs:matrix.org

@usbalbin
Copy link
Contributor Author

usbalbin commented Jun 22, 2022

Am I missing something or are there no mcpwm related functions such as mcpwm_init in esp_idf_sys?

@MabezDev
Copy link
Member

So we don't produce bindings for everything in esp-idf, we have a whitelist in this file, inside esp-idf-sys. What you'll need to do is modify esp-idf-sys to include the mcpwm headers.

// binding.h
#include "driver/mcpwm.h"

Hopefully, that should be fairly simple! Let me know if you run into any issues or need some help :).

@usbalbin
Copy link
Contributor Author

Thanks a lot. I will try to take a look :)

@usbalbin
Copy link
Contributor Author

As far as I can see #include "driver/mcpwm.h" is already present in that file (just below the inclusion of driver/ledc.h).

When searching for ledc in the repo I can not find any more places than the file mentioned above (exept for what I believe is the corresponding file for the esp8266) where ledc is mentioned.

@MabezDev
Copy link
Member

Did you actually try to use the MCPWM functions in the HAL? I think the docs are built for the esp32c3, which doesn't have the MCPWM peripheral so it won't show up there.

@usbalbin
Copy link
Contributor Author

Oh, just checked locally and yes esp_idf_sys::mcpwm_init and friends are there.

@usbalbin
Copy link
Contributor Author

@MabezDev do you think an API that would allow you to write something roughly like this would make sense?

let peripherals = Peripherals::take().unwrap();
let config = OperatorConfig::default().frequency(25.kHz().into());
let mcpwm = Mcpwm::new(peripherals.mcpwm0.mcpwm)?;
let mut operator = Operator::new(
    peripherals.mcpwm0.operator0,
    &mcpwm,
    &config,
    peripherals.pins.gpio4,
    peripherals.pins.gpio5,
)?;

operator.set_duty_a(my_duty_percentage_a)?;
operator.set_duty_b(my_duty_percentage_b)?;

see #93, or do you have any other suggestions :)

@ilikepi63
Copy link

I ran into a similar problem and decided to write my own library over esp-idf-sys.

You can find it here: https://crates.io/crates/esp-idf-servo

@Vollbrecht Vollbrecht added the enhancement New feature or request label Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Todo
Development

No branches or pull requests

4 participants