====== Synchronous Motor ====== First is important to understand how a synchronous motor works... A synchronous electric motor is an AC motor in which the rotation of the shaft is synchronized with the frequency of the AC supply current; the rotation period is exactly equal to an integral number of AC cycles. Synchronous motors contain electromagnets on the stator of the motor that create a magnetic field which rotates in time with the oscillations of the line current. The rotor turns in step with this field, at the same rate. Another thing very important to understand about the synchronous motor is that it's speed is determined by the number of magnetic poles and the frequency of the AC wave. There are two major types of synchronous motors: 'non-excited' and 'direct-current excited', for this project we are mainly interested in the non-excited motors with a construction of permanent magnet , in this motors, the rotor is made of solid steel. At synchronous speed it rotates in step with the rotating magnetic field of the stator, so it has an almost-constant magnetic field through it. The external stator field magnetizes the rotor, inducing the magnetic poles needed to turn it. This motors are very used in robotic so being able to control them in any way is going to be very useful. * This is an example of a brushless permanent magnet motor: {{ :ie0117_proyectos:final:stm32f4-discovery:bldc.jpg?400 |}} ===== How does a synchronous motor rotates ===== The synchronous motors need three sine waves with a delay of 120 degrees, as the ones shown in the next figure: {{ :ie0117_proyectos:final:stm32f4-discovery:ondas.png?400 |}} Now let's imagine that the stator has three equal windings, so the total magnetic flux is the graphic addition of the fluxes of each winding, the following image shows what it would look like: {{ :ie0117_proyectos:final:stm32f4-discovery:ondas2.png?400 |}} So as we can see this produces a rotating magnetic field in the stator which produces an induced current in the rotor that creates a magnetic field, that tries to align with the stator, generating the rotation of the motor. As our motor has a permanent magnet is like playing with the principles of attraction and repulsion of the north and south pole, like shown in this figure: {{ :ie0117_proyectos:final:stm32f4-discovery:ns.png?400 |}} ====== Generating a "sine wave" with PWM ====== First of all is important to understand what pulse width modulation means, it basically uses a rectangular pulse wave whose pulse width is modulated resulting in the variation of the average value of the waveform. There are many way of using PWM to simulate a sine wave, we are going to be using a simple method, basically what we are going to do is vary the pulse width according to the value of a sine wave, so if the angle is 90 degrees the duty cycle of the PWM is going to be at least 50%, and if the angle is 0 degrees so is going to be like 10%. The following image shows this: {{ :ie0117_proyectos:final:stm32f4-discovery:pwm.png?400 |}} The main advantage of PWM is that power loss in the switching devices is very low. When a switch is off there is practically no current, and when it is on, there is almost no voltage drop across the switch. Power loss, being the product of voltage and current, is thus in both cases close to zero. PWM also works well with digital controls, which, because of their on/off nature, can easily set the needed duty cycle. So the idea is to have six PWM signals, two per each phase, so those represent low and high inputs, as you can see in the image below. ===== Hardware ===== After generating the waves by software we needed something to deliver the power the motor needed, so for that we used a three-phase bridge or inverter, the configuration we had used an ir2110 the IC allows to use NMOS transistors instead of PMOS, the next image shows the configuration: {{ :ie0117_proyectos:final:stm32f4-discovery:apn.png?600 |}} The high input is at pin 10, and the low input at pin 12. Here www.irf.com/technical-info/appnotes/an-978.pdf you can find more information about some applications of the ir2110, including the one showed above. The transistors really works as switches as you can see in the next image is important not to activate the transistors for example Q1 and Q2 at the same time, because it will discharge all the current throw the transistors and it can damage the circuit. {{ :ie0117_proyectos:final:stm32f4-discovery:puente.png?600 |}} The question now is how to generate those waves with the stm32f4-discovery??? ====== Microcontroller stm32f4-discovery ====== First of all we realized we needed a timer that could generate PWM signals, so in the datasheet (look for it in the references section) was a block diagram and it showed the connections of the timers as you can see {{:ie0117_proyectos:final:stm32f4-discovery:block_diagram.pdf|here}}. The TIM1, was the one we selected, it has 16bits, and it can generate PWM signals, besides according with the block diagram it is connected to the 168MHz clock. The stm32f4-discovery has different timers, the one we used and the TIM8 are advanced control timers, and fortunately those have a lot of channels and functions, a diagram of this two timers can be seen {{:ie0117_proyectos:final:stm32f4-discovery:timers.pdf|here}}, but specially we are going to use from channel 1 to channel 3, including their alternate channel (those are going to be the inputs for the circuit), the following image shows the specific outputs we are going to use: {{ :ie0117_proyectos:final:stm32f4-discovery:tim1.png?600 |}} From the image you might observe that there is a counter (CNT), three compare registers, and six outputs CH1, CH1N, CH2, CH2N, CH3 and CH3N, the N comes from negated, meaning that that output will have the opposite sign than it's predecessor. This six channels will be the six signals we'll use to generate the sine wave. There are some details that must be set before generating the PWM ([[teaching:ie0117:proyectos:2012:i:final:stm32f4-discovery:setup|set_up.c]]): * We must start the clock * Every general purpose output that we are going to use must be enabled. * We have to enable the timer to accept interruptions, and we have to program what we want it to do. * One thing that is very important is to enable the deadtime of the complementary outputs, to ensure that they wont we on at the same time, because as we explained before a lot of current can be drain throw the mosfet damaging the circuit. ===== How to change PWM duty cycle --> TIM1 CC Interrupt ===== So basically the timer will be counting, and we set an initial capture compare value, so when the counter to the value of the compare register it will generate an event that activates and interruption(TIM1 CC interruption), that will change the value of the PWM duty cycle, and set the next compare value, the interruption is explained in the next diagram. {{ :ie0117_proyectos:final:stm32f4-discovery:cc_interrupt.png?1000 |}} ===== Changing the speed of the motor --> External Interrupt===== So now we have the motor rotating at a constant speed and we want it to change, so we used the user“s button to increase the frequency of the sine wave every time we press it, until it get to a max_sine_frequency we established, then it starts decreasing the frequency until a min_sine_frequency, when the user's button is pressed it activates an interruption, the following diagram explains what it does: {{ :ie0117_proyectos:final:stm32f4-discovery:button_logic1.png?800 |}} **So this changes the current frequency, which will change the angle of the sine, changing the duty cycle of the PWM, allowing the variation of the speed.** ===== Files ===== These are all the files needed to run it: - Setup file, this enables the GPIO, the acceptance of interruptions, the reset and clock control (rcc), sets the function the timer will be performing, and so on [[teaching:ie0117:proyectos:2012:i:final:stm32f4-discovery:setup|set_up.c]] - Interrupt handler, this file manages the interruption of the user button, and also changes the PWM duty cycle when the counter reaches the compare value [[teaching:ie0117:proyectos:2012:i:final:stm32f4-discovery:interrupt|interrupt_handler.c]] - PWM file this file just calls the other functions, to set the GPIO, the CLK, the TIM, and the EXTI (external interrupt) [[teaching:ie0117:proyectos:2012:i:final:stm32f4-discovery:pwm_c|pwm_6step_experiment.c]]. In the header are defined some constants [[teaching:ie0117:proyectos:2012:i:final:stm32f4-discovery:pwm_h|pwm_6step_experiment.h]] - Makefile, this is needed to compile the program [[teaching:ie0117:proyectos:2012:i:final:stm32f4-discovery:makefile|Makefile]] :-P Since we are going to be using mathematical functions like sine function we must add a flag in the //Makefile.include//, so it can recognize these functions. To add this flag you can do this: cd ~/local/src/libopencm3/examples/stm32/f4 gedit Makefile.include And find the LDFLAGS section, and at the end add **-lm**, as you can see in the following image: {{ :ie0117_proyectos:final:stm32f4-discovery:makefile.png?600 |}} [[ie0117_proyectos:final:stm32f4-discovery:|INDEX]]