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

WithPwm trait should be public #425

Open
Fomys opened this issue Jul 24, 2022 · 2 comments
Open

WithPwm trait should be public #425

Fomys opened this issue Jul 24, 2022 · 2 comments

Comments

@Fomys
Copy link
Contributor

Fomys commented Jul 24, 2022

I writing a generic struct that takes a pwm channel to control a motor.

We've tried this code:

struct Motor<TIM: TimerExt, const C: u8> {
    pwm: PwmChannel<TIM, C>,
}

And I get the following error:

error[E0599]: the method `get_max_duty` exists for struct `PwmChannel<TIM, C>`, but its trait bounds were not satisfied
  --> base-roulante/src/main.rs:64:73
   |
64 |         let consigne = pid_correction / Self::MAX_CORRECTION * self.pwm.get_max_duty();
   |                                                                         ^^^^^^^^^^^^ method cannot be called on `PwmChannel<TIM, C>` due to unsatisfied trait bounds
   |
  ::: /home/fomys/.cargo/registry/src/github.com-1ecc6299db9ec823/stm32f1xx-hal-0.9.0/src/timer/pwm.rs:89:1
   |
89 | pub struct PwmChannel<TIM, const C: u8> {
   | --------------------------------------- doesn't satisfy `PwmChannel<TIM, C>: PwmPin`
   |
   = note: the following trait bounds were not satisfied:
           `TIM: stm32f1xx_hal::timer::sealed::WithPwm`
           `TIM: stm32f1xx_hal::timer::sealed::WithPwm`
           which is required by `PwmChannel<TIM, C>: PwmPin`

The ideal resolution would be to add trait bound with WithPwm trait, but it's impossible as it's a sealed trait.

Shouldn't it be public in order write this trait bound ? If this isn't the case, how should I resolve ?

@burrbull
Copy link
Member

Yeah. Generic code with sealed trait is often problematic.
As fast fix you could use get_max_duty from PwmPin:

impl<TIM: Instance + WithPwm, const C: u8> embedded_hal::PwmPin for PwmChannel<TIM, C> {

@Fomys
Copy link
Contributor Author

Fomys commented Jul 31, 2022

To use PwmPin I have to add the trait bound PwmPin<Duty = u16> as the Duty is also generic.
Should I make a PR to make WithPwm public ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants