Author Topic: stm32f429 servo problem  (Read 8060 times)

0 Members and 1 Guest are viewing this topic.

Offline sfiberTopic starter

  • Regular Contributor
  • *
  • Posts: 107
  • Country: tr
stm32f429 servo problem
« on: June 17, 2014, 09:44:55 am »
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: [Select]
#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[4];


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 active
          Auto-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[0];
  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 yapayim
 
ADC_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[0]*3000)/4096)+3000);
TIM3->CCR2=CCR2_Val;

CCR1_Val=(((ADCConvertedValue[1]*3000)/4096)+3000);
TIM3->CCR1=CCR1_Val;

CCR3_Val=(((ADCConvertedValue[2]*3000)/4096)+3000);
TIM3->CCR3=CCR3_Val;

CCR4_Val=(((ADCConvertedValue[3]*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.
 

Offline sfiberTopic starter

  • Regular Contributor
  • *
  • Posts: 107
  • Country: tr
Re: stm32f429 servo problem
« Reply #1 on: June 17, 2014, 09:48:33 am »
you can add this code any of the STM32F429I-Discovery_FW_V1.0.1.maybe you gonna need to add tim library.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: stm32f429 servo problem
« Reply #2 on: June 17, 2014, 10:32:55 am »
I have no idea about your problem, but

Code: [Select]
SystemInit();
   SystemCoreClockUpdate();

isn't necessary as _main() runs them for you automagically.
================================
https://dannyelectronics.wordpress.com/
 

Offline sfiberTopic starter

  • Regular Contributor
  • *
  • Posts: 107
  • Country: tr
Re: stm32f429 servo problem
« Reply #3 on: June 17, 2014, 11:02:42 am »
I have no idea about your problem, but

Code: [Select]
SystemInit();
   SystemCoreClockUpdate();

isn't necessary as _main() runs them for you automagically.

Actually it is not an affect to this program.when I removed it,nothing change.it s all about system frequency.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: stm32f429 servo problem
« Reply #4 on: June 17, 2014, 11:07:16 am »
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.
================================
https://dannyelectronics.wordpress.com/
 

Offline sfiberTopic starter

  • Regular Contributor
  • *
  • Posts: 107
  • Country: tr
Re: stm32f429 servo problem
« Reply #5 on: June 17, 2014, 11:40:47 am »
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.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10450
  • Country: nz
Re: stm32f429 servo problem
« Reply #6 on: June 17, 2014, 11:57:25 am »
Check that you have
- Correct pulse frequency (40-70hz)
- Correct pulse polarity (positive pulse)
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline sfiberTopic starter

  • Regular Contributor
  • *
  • Posts: 107
  • Country: tr
Re: stm32f429 servo problem
« Reply #7 on: June 17, 2014, 12:42:54 pm »
@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
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: stm32f429 servo problem
« Reply #8 on: June 17, 2014, 12:51:52 pm »
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...
================================
https://dannyelectronics.wordpress.com/
 

Offline sfiberTopic starter

  • Regular Contributor
  • *
  • Posts: 107
  • Country: tr
Re: stm32f429 servo problem
« Reply #9 on: June 17, 2014, 01:13:15 pm »
@dannyf
here is my output signal.joystick in middle position

 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: stm32f429 servo problem
« Reply #10 on: June 17, 2014, 01:27:13 pm »
Is the waveform what you expect?

If so, the problem is not in the code.
================================
https://dannyelectronics.wordpress.com/
 

Offline sfiberTopic starter

  • Regular Contributor
  • *
  • Posts: 107
  • Country: tr
Re: stm32f429 servo problem
« Reply #11 on: June 17, 2014, 01:30:05 pm »
yeah,that s the what I expect.
I will try to amplify signal to 5v peak value.
 

Offline Skimask

  • Super Contributor
  • ***
  • Posts: 1433
  • Country: us
Re: stm32f429 servo problem
« Reply #12 on: June 17, 2014, 01:33:18 pm »
What's the power source?
I didn't take it apart.
I turned it on.

The only stupid question is, well, most of them...

Save a fuse...Blow an electrician.
 

Offline sfiberTopic starter

  • Regular Contributor
  • *
  • Posts: 107
  • Country: tr
Re: stm32f429 servo problem
« Reply #13 on: June 17, 2014, 01:35:43 pm »
What's the power source?

I use my homemade +5 gnd -5 power supply.every voltage value supplied by with 2 regulator.I mean 5 v with 2 7805.
 

Offline gxti

  • Frequent Contributor
  • **
  • Posts: 507
  • Country: us
Re: stm32f429 servo problem
« Reply #14 on: June 17, 2014, 01:41:21 pm »
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.
 

Offline sfiberTopic starter

  • Regular Contributor
  • *
  • Posts: 107
  • Country: tr
Re: stm32f429 servo problem
« Reply #15 on: June 17, 2014, 01:48:48 pm »
thanks @gxti

I will try.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: stm32f429 servo problem
« Reply #16 on: June 17, 2014, 01:52:51 pm »
Quote
that s the what I expect.

Then no point in focusing on the code anymore.

Figure out what signal / power source the servo needs and provide that to it.
================================
https://dannyelectronics.wordpress.com/
 

Offline sfiberTopic starter

  • Regular Contributor
  • *
  • Posts: 107
  • Country: tr
Re: stm32f429 servo problem
« Reply #17 on: June 17, 2014, 02:38:18 pm »
@dannyf,@gxti
I 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.
 

Offline Skimask

  • Super Contributor
  • ***
  • Posts: 1433
  • Country: us
Re: stm32f429 servo problem
« Reply #18 on: June 17, 2014, 11:58:57 pm »
...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.
I didn't take it apart.
I turned it on.

The only stupid question is, well, most of them...

Save a fuse...Blow an electrician.
 

Offline mrflibble

  • Super Contributor
  • ***
  • Posts: 2051
  • Country: nl
Re: stm32f429 servo problem
« Reply #19 on: June 18, 2014, 12:27:12 am »
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.
 

Offline sfiberTopic starter

  • Regular Contributor
  • *
  • Posts: 107
  • Country: tr
Re: stm32f429 servo problem
« Reply #20 on: June 18, 2014, 12:27:15 pm »
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
 

Offline sfiberTopic starter

  • Regular Contributor
  • *
  • Posts: 107
  • Country: tr
Re: stm32f429 servo problem
« Reply #21 on: June 26, 2014, 08:50:19 am »
hi my friends I solved my problem with high efficiency 3a 7.2 v power supply.thanks to everybody.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf