1
votes

I am trying to control a servo motor (link). It is a brushless DC motor with an interface similar to a stepper motor.

The motor rotates for a defined distance based on the number of pulses it receives from the PWM. The speed is determined by the pulse frequency of the PWM, like a stepper motor.

To control this motor I am using a microcontroller STM32F407ZET6. I can easily change the frequency and Duty Cycle of PWM, but my doubt is the following:

How do I generate a fixed number of pulses in the PWM? For example, I want the PWM to send 1000 pulses at a certain time with a frequency of 20KHz and a Duty Cycle of 50%. 20KHz and 50% Duty Cycle are easy to define, but I can't determine how to generate the 1000 fixed pulses.

One of the solutions I tried was to connect the PWM back to a timer in counter mode and stop the PWM when the required number of pulses has been generated. But the number of pulses is not always fixed, sometimes ranging from 998 to 1005 (for example).

Is it possible to do this without the need for feedback?

2
I think you are on the right track, getting the microcontroller to stop itself, perhaps by entering sleep or stop mode. The program needs to leave it alone that long, too, which can be done with a variety of timer functions for 50 ms, like poll(0,0,50). If the user wants to interact with the program while it is controlling the motor, then get a timestamp when you command it gettimeofday() and leave it alone until the 50 ms is up unless you need an emergency stop mode. I guess this only goes in one direction and does not need power to hold it in position?David G. Pickett
@DavidG.Pickett I'd like to see your example of the poll and gettimeofday() working on the STM32. I'd like also to see the program using those functions doing what OP wants.0___________
No, I envision a controlling PC connected to the micro-controller, sending commands and then not interfering with those commands until they are scheduled to be done.David G. Pickett

2 Answers

0
votes

Simpest way:

  1. UG interrupt = count cycles. After n cycles disable the timer.

  2. In the memory create a buffer with the timer register values and use timer burst mode.

  3. Configure DMA mem-mem with n cycles same source and destination address. After n cycles end of transaction interrupt will be generated - disable the timer.

  4. Use slave timer counting when the PWM is updating (overflowing). Set the overflow interrupt and disable the PWM timer.

    • many other methods.
0
votes

This can be easily achieved by combining the so called "One-pulse mode" (reference manual page 551) with the repetition counter (page 529). All you've got to do is enabling this mode (OPM bit in CR1), set the repetition counter (RCR) and start the timer. IIRC you also have to force an update event in order to get RCR loaded. The reference manual will have more information on that.