From c0c9aa8572a4e5b1df1d5efe849a0835099c0e78 Mon Sep 17 00:00:00 2001 From: johan9107 Date: Mon, 19 Sep 2016 15:54:32 +0200 Subject: [PATCH] Drivers Oneshot125 Added the protocol oneshot 125 to pwm drivers --- UAV-ControlSystem/inc/drivers/motors.h | 10 ++++- UAV-ControlSystem/inc/drivers/pwm.h | 2 +- UAV-ControlSystem/src/drivers/motors.c | 53 ++++++++++++++++++++------ UAV-ControlSystem/src/drivers/pwm.c | 4 +- 4 files changed, 52 insertions(+), 17 deletions(-) diff --git a/UAV-ControlSystem/inc/drivers/motors.h b/UAV-ControlSystem/inc/drivers/motors.h index bc98b25..282fba0 100644 --- a/UAV-ControlSystem/inc/drivers/motors.h +++ b/UAV-ControlSystem/inc/drivers/motors.h @@ -8,18 +8,24 @@ #ifndef DRIVERS_MOTORS_H_ #define DRIVERS_MOTORS_H_ +/* Struct of motor output protocol */ +typedef enum +{ +PWM, Oneshot125 +}motorOutput; + /************************************************************************** * BRIEF: Initializing a motor (maps a pwm signal to a certain pin) * * INFORMATION: The motor becomes active, Each motor configuration is changed in the "stm32f4xx_revo.h" file * * Example - pwmEnableMotor(MOTOR_1) * **************************************************************************/ -void pwmEnableMotor(uint8_t motor); +void pwmEnableMotor(uint8_t motor, motorOutput motorOutput); /************************************************************************** * BRIEF: Enables all motors (maps a pwm signal to all pins which will be connected to a motor) * * INFORMATION: All motors become active **************************************************************************/ -void pwmEnableAllMotors(void); +void pwmEnableAllMotors(motorOutput motorOutput); /************************************************************************** * BRIEF: Deactivates a motor (Disables the pwm signal for the motor pin ) diff --git a/UAV-ControlSystem/inc/drivers/pwm.h b/UAV-ControlSystem/inc/drivers/pwm.h index 8fb5337..5b89116 100644 --- a/UAV-ControlSystem/inc/drivers/pwm.h +++ b/UAV-ControlSystem/inc/drivers/pwm.h @@ -21,7 +21,7 @@ * period = Period of PWM signal * pulse = the "duty cycle" of the pwm signal * **************************************************************************/ -void pwmInit(GPIO_TypeDef * GPIO, uint16_t pin, TIM_TypeDef * tim, uint32_t Channel, uint16_t period, uint16_t pulse); +void pwmInit(GPIO_TypeDef * GPIO, uint16_t pin, TIM_TypeDef * tim, uint32_t Channel, uint16_t period, uint16_t pulse, uint32_t prescaler); /************************************************************************** * BRIEF: setPwmPulse changes a certain pulse for an initialized pwm signal * diff --git a/UAV-ControlSystem/src/drivers/motors.c b/UAV-ControlSystem/src/drivers/motors.c index 8bf89a3..37387fa 100644 --- a/UAV-ControlSystem/src/drivers/motors.c +++ b/UAV-ControlSystem/src/drivers/motors.c @@ -4,8 +4,8 @@ * INFORMATION: * * GLOBAL VARIABLES: * * Variable Type Description * -* MOTOR_PWM_INIT_PERIODE define The initializing periode of the pwm signal to each motor * -* MOTOR_PWM_INIT_PULSE define The initializing pulse of the pwm signal to each motor +* MotorPWMPeriode define The initializing periode of the pwm signal to each motor * +* MotorPWMInitPulse define The initializing pulse of the pwm signal to each motor * **************************************************************************/ #include "stm32f4xx.h" @@ -13,8 +13,8 @@ #include "drivers/pwm.h" #include "drivers/motors.h" -#define MOTOR_PWM_INIT_PERIODE 2000 -#define MOTOR_PWM_INIT_PULSE MOTOR_PWM_INIT_PERIODE/2 +int MotorPWMPeriode = 2000; //Micro seconds +int MotorPWMInitPulse = 1000; /* A struct of a pwm motor profile */ typedef struct @@ -32,7 +32,7 @@ typedef struct **************************************************************************/ uint16_t checkPulse(uint16_t pulse) { - return ((pulse/MOTOR_PWM_INIT_PERIODE)*100 < 94)? pulse: MOTOR_PWM_INIT_PERIODE*0.94; + return ((pulse/MotorPWMPeriode)*100 < 94)? pulse: MotorPWMPeriode*0.94; } /************************************************************************** @@ -114,25 +114,54 @@ motorProfile getMotorProfile(uint8_t motor) return profile; } +/************************************************************************** +* BRIEF: Sets the motor output protocol to be either oneshot125 or pwm +* INFORMATION: +* Example - uint32_t prescaler = setMotorOutput(Oneshot125) +*************************************************************************/ +uint32_t setMotorOutput(motorOutput motorOutput) +{ + uint32_t prescaler; + + switch(motorOutput) + { + case PWM: + MotorPWMPeriode = 2000; + prescaler = 88; + break; + case Oneshot125: + MotorPWMPeriode = 16000; + prescaler = 11; + break; + default: + MotorPWMPeriode = 2000; + prescaler = 88; + break; + } + + return prescaler; +} + /************************************************************************** * BRIEF: Initializing a motor (maps a pwm signal to a certain pin) * * INFORMATION: The motor becomes active, Each motor configuration is changed in the "stm32f4xx_revo.h" file * * Example - pwmEnableMotor(MOTOR_1) * **************************************************************************/ -void pwmEnableMotor(uint8_t motor) +void pwmEnableMotor(uint8_t motor, motorOutput motorOutput) { motorProfile profile = getMotorProfile(motor); + uint32_t prescaler = setMotorOutput(motorOutput); - pwmInit(profile.port, profile.pin, profile.tim, profile.channel, MOTOR_PWM_INIT_PERIODE, MOTOR_PWM_INIT_PULSE); + pwmInit(profile.port, profile.pin, profile.tim, profile.channel, MotorPWMPeriode, MotorPWMInitPulse, prescaler); } /************************************************************************** * BRIEF: Enables all motors (maps a pwm signal to all pins which will be connected to a motor) * * INFORMATION: All motors become active **************************************************************************/ -void pwmEnableAllMotors(void) +void pwmEnableAllMotors(motorOutput motorOutput) { - for (uint8_t i = 1; i < 11; i++ ) pwmEnableMotor(i); + for (uint8_t i = 1; i < 11; i++ ) pwmEnableMotor(i, motorOutput); } /************************************************************************** @@ -165,7 +194,7 @@ void pwmDeactivateMotor(uint8_t motor) **************************************************************************/ void pwmActivateAllMotors(void) { - for (uint8_t i = 1; i < 11; i++ ) pwmDeactivateMotor(i); + for (uint8_t i = 1; i < 11; i++ ) pwmActivateMotor(i); } /************************************************************************** @@ -175,7 +204,7 @@ void pwmActivateAllMotors(void) **************************************************************************/ void pwmDeactivateAllMotors(void) { - for (uint8_t i = 1; i < 11; i++ ) pwmActivateMotor(i); + for (uint8_t i = 1; i < 11; i++ ) pwmDeactivateMotor(i); } /************************************************************************** @@ -197,7 +226,7 @@ void pwmAdjustSpeedOfMotor(uint8_t motor, uint16_t pulse) **************************************************************************/ void pwmAdjustSpeedOfMotorDutyCycle(uint8_t motor, uint16_t DutyCycle) { - uint16_t pulse = (DutyCycle*MOTOR_PWM_INIT_PERIODE)/100; /* Converts the DutyCycle to a pulse */ + uint16_t pulse = (DutyCycle*MotorPWMPeriode)/100; /* Converts the DutyCycle to a pulse */ motorProfile profile = getMotorProfile(motor); setPwmPulse(profile.channel, profile.tim, checkPulse(pulse)); diff --git a/UAV-ControlSystem/src/drivers/pwm.c b/UAV-ControlSystem/src/drivers/pwm.c index e7e6992..8aee4cf 100644 --- a/UAV-ControlSystem/src/drivers/pwm.c +++ b/UAV-ControlSystem/src/drivers/pwm.c @@ -48,7 +48,7 @@ pwmProfile get_Pwm_Profile(GPIO_TypeDef * GPIO, uint16_t pin, uint32_t Channel, * period = Period of PWM signal * pulse = the "duty cycle" of the pwm signal * **************************************************************************/ -void pwmInit(GPIO_TypeDef * GPIO, uint16_t pin, TIM_TypeDef * tim, uint32_t Channel, uint16_t period, uint16_t pulse) +void pwmInit(GPIO_TypeDef * GPIO, uint16_t pin, TIM_TypeDef * tim, uint32_t Channel, uint16_t period, uint16_t pulse, uint32_t prescaler) { pwmProfile profile; @@ -84,7 +84,7 @@ void pwmInit(GPIO_TypeDef * GPIO, uint16_t pin, TIM_TypeDef * tim, uint32_t Chan TimHandle.Instance = profile.tim; /* Sets timer */ TimHandle.Init.Period = period; /* Sets period of pwm */ - TimHandle.Init.Prescaler = 88; /* pwm prescaler of clk */ + TimHandle.Init.Prescaler = prescaler; /* pwm prescaler of clk */ TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;