Hello All
I am New to Embedded programming and control. I am building a balancing robot using STM32Fxx MCU and Dual H bridge Pololu motor driver. The motors can get power on or some times it is only one of the motors at time run, either forward or reverse. any help is appreciated.
my setting
IN1---->direction control
IN2---->direction control
PWM1D2---->PWM, D1 keep low with Jumper setting
the same setting for the other motor
SF----> Unused
FB----> Used for for current sensing
EN----> HIgh
Out1 and Out2 for the motor.
here below copy my code for the motor driver , PLEASE HELP!!
#include <stdlib.h>
#include <stddef.h>
#include "stm32f30x_conf.h"
#include "stm32f30x.h"
#include "stm32f30x_rcc.h"
#include "stm32f30x_tim.h"
#include "stm32f30x_gpio.h"
#include "Clocks.h"
#include "MotorDriver.h"
uint8_t MotorDriver_Drive_Tx1[32];
uint8_t MotorDriver_ReadSensors_Tx1[32];
uint8_t MotorDriver_ReadSensors_Rx1[32];
#define GPIO_DIR GPIOD
#define GPIO_Pin_DIR_M1IN1 GPIO_Pin_0
#define GPIO_Pin_DIR_M2IN1 GPIO_Pin_2
#define GPIO_Pin_DIR_M1IN2 GPIO_Pin_1
#define GPIO_Pin_DIR_M2IN2 GPIO_Pin_7
bool MotorDriver_Initialize(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
// Enable peripherals
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_DIR_M1IN1 | GPIO_Pin_DIR_M2IN1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIO_DIR, &GPIO_InitStructure);
GPIO_WriteBit(GPIO_DIR, GPIO_Pin_DIR_M1IN1, DIR_L_FORWARD);
GPIO_WriteBit(GPIO_DIR, GPIO_Pin_DIR_M2IN1, DIR_R_FORWARD);
// pin for the second input
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_DIR_M1IN2 | GPIO_Pin_DIR_M2IN2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIO_DIR, &GPIO_InitStructure);
GPIO_WriteBit(GPIO_DIR, GPIO_Pin_DIR_M1IN2, DIR_L_FORWARD);
GPIO_WriteBit(GPIO_DIR, GPIO_Pin_DIR_M2IN2, DIR_R_FORWARD);
// defining the PWM out put
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_2);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_2);
/* Time base configuration */
TIM_TimeBaseStructInit( &TIM_TimeBaseStructure );
TIM_TimeBaseStructure.TIM_Period = 1500;
TIM_TimeBaseStructure.TIM_Prescaler = 2;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV4;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration */
TIM_OCStructInit( &TIM_OCInitStructure );
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OCStructInit( &TIM_OCInitStructure );
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_ARRPreloadConfig(TIM2, ENABLE);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
return true;
}
/**
* @brief Send a command to the motor driver to set the motor speed
void SetM1speed(uint8_t Speed_Mleft)
{
unsigned char reverse=0;
if(Speed_Mleft < 0)
{
Speed_Mleft=-Speed_Mleft;// full forwared
reverse=1; // preserve the direction
}
if(Speed_Mleft > 255)
{
Speed_Mleft=255;
TIM2->CCR2=Speed_Mleft;
}
if(reverse)
GPIO_WriteBit(GPIO_DIR, GPIO_Pin_DIR_M1IN1, DIR_L_FORWARD);
GPIO_WriteBit(GPIO_DIR, GPIO_Pin_DIR_M1IN2, DIR_L_BACKWARD);
else
GPIO_WriteBit(GPIO_DIR, GPIO_Pin_DIR_M1IN1, DIR_R_FORWARD);
GPIO_WriteBit(GPIO_DIR, GPIO_Pin_DIR_M1IN2, DIR_R_BACKWARD);
}
void SetM2speed(uint8_t Speed_Mright)
{
unsigned char reverse=0;
if(Speed_Mright < 0)
{
Speed_Mright=-Speed_Mright;// full reverse
reverse=1;
}
if(Speed_Mright > 255)
{
Speed_Mright=255;
TIM2->CCR4=Speed_Mright;
}
if(reverse)
GPIO_WriteBit(GPIO_DIR, GPIO_Pin_DIR_M2IN1, DIR_L_FORWARD);
GPIO_WriteBit(GPIO_DIR, GPIO_Pin_DIR_M2IN2, DIR_L_BACKWARD);
else
GPIO_WriteBit(GPIO_DIR, GPIO_Pin_DIR_M2IN1, DIR_R_FORWARD);
GPIO_WriteBit(GPIO_DIR, GPIO_Pin_DIR_M2IN2, DIR_R_BACKWARD);
}
DIR_R_FORWARD-set to low
DIR_L_FORWARD-set to high
DIR_L_BACKRWARD…set to low
DIR_R_BACKRWARD…set to high