stm32f429 servo problem - Page 1
Forum section: electronics / general

author: sfiber
author_rank: Regular Contributor
timestamp: June 17, 2014, 09:44:55 am
content: hi everyone.I am going to start a new project which it is a robot arm.I m gonna use stm32f429 and f4 for this project but I have little bit problem about servos.I choosed to mg996r servos and applied signal with this codes.Code:#include "main.h"__IO uint16_t CCR1_Val;__IO uint16_t CCR2_Val;__IO uint16_t CCR3_Val;__IO uint16_t CCR4_Val;uint8_t PrescalerValue;__IO uint16_t ADCConvertedValue;TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;TIM_OCInitTypeDef TIM_OCInitStructure;ADC_InitTypeDef ADC_InitStructure;GPIO_InitTypeDef GPIO_InitStructure;ADC_CommonInitTypeDef ADC_CommonInitStructure;DMA_InitTypeDef DMA_InitStructure;void TIM_Config(void){RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;GPIO_InitStructure.GPIO_Pin=(GPIOC,GPIO_Pin_6|GPIO_Pin_7);GPIO_InitStructure.GPIO_Speed=GPIO_Speed_25MHz;GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;GPIO_Init(GPIOC,&GPIO_InitStructure);GPIO_Init(GPIOC,&GPIO_InitStructure);RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;GPIO_Init(GPIOB,&GPIO_InitStructure);GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_TIM3);GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_TIM3);GPIO_PinAFConfig(GPIOB,GPIO_PinSource0,GPIO_AF_TIM3);GPIO_PinAFConfig(GPIOB,GPIO_PinSource1,GPIO_AF_TIM3);}void PWM_Config(void){PrescalerValue=29;TIM_TimeBaseInitStructure.TIM_Period=59999;/*!< Specifies the period value to be loaded into the activeAuto-Reload Register at the next update event.This parameter must be a number between 0x0000 and 0xFFFF. */TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;TIM_TimeBaseInitStructure.TIM_ClockDivision=0;TIM_TimeBaseInitStructure.TIM_Prescaler=PrescalerValue;TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//TIM_OCInitStructure.TIM_Pulse=2656;TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;TIM_OC1Init(TIM3,&TIM_OCInitStructure);TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//TIM_OCInitStructure.TIM_Pulse=CCR2_Val;TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;TIM_OC2Init(TIM3,&TIM_OCInitStructure);TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//TIM_OCInitStructure.TIM_Pulse=500;TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;TIM_OC3Init(TIM3,&TIM_OCInitStructure);TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//TIM_OCInitStructure.TIM_Pulse=500;TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;TIM_OC4Init(TIM3,&TIM_OCInitStructure);TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable);TIM_ARRPreloadConfig(TIM3,ENABLE);TIM_Cmd(TIM3,ENABLE);}void ADC_PortF_Config(void){RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AN;GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_0;//burayida iki kanala cikarttim.GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;GPIO_Init(GPIOA,&GPIO_InitStructure);RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AN;GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_2;GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;GPIO_Init(GPIOC,&GPIO_InitStructure);RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);/* DMA2 Stream0 channel2 configuration **************************************/DMA_DeInit(DMA2_Stream4);DMA_InitStructure.DMA_Channel = DMA_Channel_0;DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCConvertedValue;DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;DMA_InitStructure.DMA_BufferSize = 4;DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;DMA_InitStructure.DMA_Priority = DMA_Priority_High;DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;DMA_Init(DMA2_Stream4, &DMA_InitStructure);DMA_Cmd(DMA2_Stream4, ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;ADC_CommonInit(&ADC_CommonInitStructure);ADC_DeInit();ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;ADC_InitStructure.ADC_ScanConvMode = ENABLE;//burasi disable di enable yapayimADC_InitStructure.ADC_ContinuousConvMode = ENABLE;ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;ADC_InitStructure.ADC_NbrOfConversion = 4;ADC_Init(ADC1, &ADC_InitStructure);ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 2, ADC_SampleTime_480Cycles);ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_480Cycles);ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 3, ADC_SampleTime_480Cycles);ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 4, ADC_SampleTime_480Cycles);/* Enable DMA request after last transfer (Single-ADC mode) */ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);/* Enable ADC3 DMA */ADC_DMACmd(ADC1, ENABLE);/* Enable ADC3 */ADC_Cmd(ADC1, ENABLE);ADC_SoftwareStartConv(ADC1);}void delay(){int a;for (a = 0; a < 0x0000FFFF; a++) ;}int main(void){SystemInit();SystemCoreClockUpdate();TIM_Config();PWM_Config();ADC_PortF_Config();while(1){CCR2_Val=(((ADCConvertedValue*3000)/4096)+3000);TIM3->CCR2=CCR2_Val;CCR1_Val=(((ADCConvertedValue*3000)/4096)+3000);TIM3->CCR1=CCR1_Val;CCR3_Val=(((ADCConvertedValue*3000)/4096)+3000);TIM3->CCR3=CCR3_Val;CCR4_Val=(((ADCConvertedValue*3000)/4096)+3000);TIM3->CCR4=CCR4_Val;}}when I check the signal wih an oscilloscope I give and excellent signal with peak max is a 3 v peak min is a 0v and a duty cycle change between 5% and %10. is 3 v can be a problem for triggering because I dissambly to servo but I couldnt find any datashhets about chip on the servos pcb.do you think it can be problem for the triggering of the chip.can anyone(who has a servo and 429i) try this codes.

author: sfiber
author_rank: Regular Contributor
timestamp: June 17, 2014, 09:48:33 am
content: you can add this code any of the STM32F429I-Discovery_FW_V1.0.1.maybe you gonna need to add tim library.

author: dannyf
author_rank: Super Contributor
timestamp: June 17, 2014, 10:32:55 am
content: I have no idea about your problem, butCode:SystemInit();SystemCoreClockUpdate();isn't necessary as _main() runs them for you automagically.

author: sfiber
author_rank: Regular Contributor
timestamp: June 17, 2014, 11:02:42 am
content: Quote from: dannyf on June 17, 2014, 10:32:55 amActually it is not an affect to this program.when I removed it,nothing change.it s all about system frequency.

author: dannyf
author_rank: Super Contributor
timestamp: June 17, 2014, 11:07:16 am
content: I was trying to tell you two things:1) the code isn't necessary - which you confirmed;2) nobody can understand a thing you are saying about your problem. Maybe you want to say it differently.

author: sfiber
author_rank: Regular Contributor
timestamp: June 17, 2014, 11:40:47 am
content: I just say,can anyone control this code with his servo.because I think there is a hardware problem.servo dont response to input signal and I try this with 5 different servo.I dont think its about supply voltage.

author: Psi
author_rank: Super Contributor
timestamp: June 17, 2014, 11:57:25 am
content: Check that you have- Correct pulse frequency (40-70hz)- Correct pulse polarity (positive pulse)

author: sfiber
author_rank: Regular Contributor
timestamp: June 17, 2014, 12:42:54 pm
content: @Psi I ve checked my code with an oscilloscope and I should say frequency is exactly 50 hz and I ve used joystick module and at the middle of the joystick duty cycle will be 7.5

author: dannyf
author_rank: Super Contributor
timestamp: June 17, 2014, 12:51:52 pm
content: I would try to isolate the problem. First, make sure that your code is outputing the right pulses: in frequency and duty cycle.Then go to the servo, to see if you feed it a correct signal if it works; and then the interface...

author: sfiber
author_rank: Regular Contributor
timestamp: June 17, 2014, 01:13:15 pm
content: @dannyfhere is my output signal.joystick in middle position

author: dannyf
author_rank: Super Contributor
timestamp: June 17, 2014, 01:27:13 pm
content: Is the waveform what you expect?If so, the problem is not in the code.

author: sfiber
author_rank: Regular Contributor
timestamp: June 17, 2014, 01:30:05 pm
content: yeah,that s the what I expect.I will try to amplify signal to 5v peak value.

author: Skimask
author_rank: Super Contributor
timestamp: June 17, 2014, 01:33:18 pm
content: What's the power source?

author: sfiber
author_rank: Regular Contributor
timestamp: June 17, 2014, 01:35:43 pm
content: Quote from: Skimask on June 17, 2014, 01:33:18 pmI use my homemade +5 gnd -5 power supply.every voltage value supplied by with 2 regulator.I mean 5 v with 2 7805.

author: gxti
author_rank: Frequent Contributor
timestamp: June 17, 2014, 01:41:21 pm
content: Because the frequency is low, you can shift the output up to 5 volts by setting the output to "open drain" mode and using a pull-up resistor. 1K should be okay, check the waveform to be sure.

author: sfiber
author_rank: Regular Contributor
timestamp: June 17, 2014, 01:48:48 pm
content: thanks @gxtiI will try.

author: dannyf
author_rank: Super Contributor
timestamp: June 17, 2014, 01:52:51 pm
content: QuoteThen no point in focusing on the code anymore.Figure out what signal / power source the servo needs and provide that to it.

author: sfiber
author_rank: Regular Contributor
timestamp: June 17, 2014, 02:38:18 pm
content: @dannyf,@gxtiI try to open drain and activate the pull up resistor then peak max value is be 3.78 v and nothing changed.But I notice that when I connect to servo my power supply,voltage will drop to 4.66 v.I think that's an issue.

author: Skimask
author_rank: Super Contributor
timestamp: June 17, 2014, 11:58:57 pm
content: ...which is why I asked what your power source was.A cheap eBay module that spits out a bit of power isn't going to do the trick. Servos need a fair amount of current to get going. If you're reading 4.6v, I wonder what it reads under the load of the servo trying to move.

author: mrflibble
author_rank: Super Contributor
timestamp: June 18, 2014, 12:27:12 am
content: FWIW: I use cheapo ebay servo's on an stm32f407, with open drain outputs and a 5V supply (also 50 HZ, same as you). And that works just fine. Also, at first I tried it with 3 Volt and that did NOT work just fine. ;-) Anyways, it looks like you need a stiffer supply, or shorter wires, or a reasonably sized capacitor. Those waveforms look okay, so as has been pointed out the code is not your problem. Looks like the power supply needs some attention.

author: sfiber
author_rank: Regular Contributor
timestamp: June 18, 2014, 12:27:15 pm
content: Hi friends,again.I wrote a code for pic 16f887 with same power supply.due to the pic works with 5v,I dont need to open drain settings and then I saw the waveforms on the oscilloscopes.at no load supply voltage downed to 4.66 v then I applied to pwm signal and supply peak min voltage downed to 2 volt time to time.Its all about to supply voltage and I will control 6 servos same time.what do you offer me for the power.should I buy lipo battery or 30v 3a power supply orr should I modify my power supply with lm2676 for high current.thanks

author: sfiber
author_rank: Regular Contributor
timestamp: June 26, 2014, 08:50:19 am
content: hi my friends I solved my problem with high efficiency 3a 7.2 v power supply.thanks to everybody.