Servo in STM32 port

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
cyrano1960
Posts: 39
Joined: Fri Mar 29, 2019 7:08 pm

Servo in STM32 port

Post by cyrano1960 » Tue Dec 14, 2021 7:03 pm

Hello, in the source code of the stm32 port there are servo.h and servo.c files in the root directory of this port. But when I compile it I can´t import it. I have tried various possible ways:

Code: Select all

from Servo import servo
from pyb import Servo
and some other variations. I have listed all importable module with:

Code: Select all

help('modules')
and examine singel modules using:

Code: Select all

help(modulename)
but I can´t find the Servo module anyway... What am I doing wrong? I am using a NUCLEO-L476RG board.

Noch ein Nachtrag: die PWM-Klasse finde ich auch nicht :?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Servo in STM32 port

Post by dhylands » Tue Dec 14, 2021 8:13 pm

If you look at servo.c you'll see that the entire file is wrapped in a #if MICROPY_HW_ENABLE_SERVO:
https://github.com/micropython/micropyt ... ervo.c#L35

The pyboard enables it in it's mpconfigboard.h file:
https://github.com/micropython/micropyt ... oard.h#L10

Note that the actual pins used by servo.c are hardcoded: https://github.com/micropython/micropyt ... .c#L80-L90

and the timers which are used are also hard-coded (i.e. it uses channels 1 thru 4 of TIM5): https://github.com/micropython/micropyt ... #L126-L132

If you can live with those restrictions then you can probably enable MICROPY_HW_ENABLE_SERVO in your mpconfigboard.h file.

cyrano1960
Posts: 39
Joined: Fri Mar 29, 2019 7:08 pm

Re: Servo in STM32 port

Post by cyrano1960 » Tue Dec 14, 2021 9:01 pm

Hi Dave,
again you are my angel of Salvation :P

Thanks a lot!

henryd
Posts: 2
Joined: Sat Jan 15, 2022 4:49 pm

Re: Servo in STM32 port

Post by henryd » Mon Jan 17, 2022 4:07 pm

Has anyone got this to work correctly?
I have an STM32 based pyboard and cannot get the builtin Servo module to turn to an angle less than 0 degrees.
A typical SG90 servo has a midpoint of 1500 microsec, 90 degrees at 2500, -90 at 1000.
servo.calibration() shows values that correspond to the above.
Sending the servo to any negative angle and the doing servo.pulse_width() always shows 1500 i.e. 0 degrees

henryd
Posts: 2
Joined: Sat Jan 15, 2022 4:49 pm

Re: Servo in STM32 port

Post by henryd » Mon Jan 17, 2022 4:30 pm

Further to my previous post, I did some more experimentation
I set calibration as s4.calibration(640,2400,640,2470,2200)
Then manages to get to servo to move through 180 degrees as follows
servo.angle(0) went to left extreme
servo.angle(45) went to midpoint - 90 degrees
servo.angle(90) went to right extreme.

Comments?

Post Reply