Embedded Rust: PWM signal and servo control from a Cortex M0

Поділитися
Вставка
  • Опубліковано 1 жов 2024
  • I am providing a very brief description of PWM signals and show how a simple rust program can be used to generate such a signal and control the position of a servo motor.
    Some of the hardware used in this video:
    - A Logic level shifter to convert from a 3.3V signal to a 5V signal: www.google.com...
    - A SG90 Servo: www.google.com...
    - A STM32 M0 board of ali express: www.google.com...
    This is an updated version, after the first one had missing audio. Sorry about that.
    Example code available in the branch github.com/and...

КОМЕНТАРІ • 7

  • @XanderSoldaat1
    @XanderSoldaat1 2 роки тому +1

    I am not sure the servo is broken, those cheap small ones are terribly inaccurate, and always require fiddling with the min and max duty cycles. No single servo is exactly the same, either, so the values for servo A may not work for servo B.

  • @nikidimitrow
    @nikidimitrow 2 роки тому +1

    Thank you for the video provided about Rust, PWM and Cortex in such deep and well structure information.

  • @JeremyChone
    @JeremyChone 2 роки тому

    I have another question about the pwm and frequency.
    In the rp-hal pico examples, pico_pwm_servo.rs, we have:
    pwm.set_div_int(20u8); // 50 hz (with default top/wrap).
    And it works (checked with a multimeter, close enough).
    However, I do not understand the math.
    I thought the simplistic math was this:
    divider = Ceil(16*fc/65536*freq)/16 (where fc is 125_000_000).
    But I am off by a factor of 2? So, if I want the div & top for 50hz, I need to have freq = 100.
    I even ported the micropython C function to Rust, to get a "better" Div and Top, and it works as long as I put 2x the HZ I want I give a 2x frequency of what I need.
    I am missing something obvious, but cannot figure it out.

  • @JeremyChone
    @JeremyChone 2 роки тому

    First great video. One thing I am confused about is how to read from a PWM. I have a very simple PICO circuit with a photoresistor and I just want to read the pwm value as I would do in micro python. Somehow, I cannot find the way to do it. I am using rp-hal. I must be missing something obvious.

    • @JeremyChone
      @JeremyChone 2 роки тому

      Ok, I found the reason. I am still learning Embedded programming. We have to use ADC. Good example in github rp-hal/rp2040-hall/src/adc.rs has a good example.
      let mut adc = Adc::new(peripherals.ADC, &mut peripherals.RESETS);
      let mut adc_pin_0 = pins.gpio26.into_floating_input();
      let pin_adc_counts: u16 = adc.read(&mut adc_pin_0).unwrap();

    • @embedded-rust
      @embedded-rust  2 роки тому

      Hi, yes, PWM is generally for output and you do not read it as much. If you do read them, you would get positions of rising/falling edges of the signal.
      For photoresitors, they change resistance based on light, so you would need to measure "how much resitance", using some form of voltage divider (quick google search led me to docs.onion.io/omega2-arduino-dock-starter-kit/arduino-kit-reading-a-photoresistor.html for some diagrams).
      Using an ADC is the right way to go about it. make sure the voltage range is covering as much as possible from 0 to max (5V for RPi, 3.3V for STM32). If you want to see something similar, this is also how analog values are read from things like joysticks or water level sensors.

    • @JeremyChone
      @JeremyChone 2 роки тому

      Thanks a lot for the context. Still learning embedded programming, but having a lot of fun doing so.