From ca351f536b6e13dd3e32a4c368d85c2efbf4f3a9 Mon Sep 17 00:00:00 2001 From: johan9107 Date: Fri, 16 Sep 2016 10:43:15 +0200 Subject: [PATCH] Drivers PWM and motors Added comment to drivers and a clk prescaler to the pwm signal --- UAV-ControlSystem/inc/drivers/motors.h | 39 ++++++++++++++++++++++++++ UAV-ControlSystem/inc/drivers/pwm.h | 10 +++++++ UAV-ControlSystem/src/drivers/motors.c | 6 ++-- UAV-ControlSystem/src/drivers/pwm.c | 20 ++++++++++--- 4 files changed, 68 insertions(+), 7 deletions(-) diff --git a/UAV-ControlSystem/inc/drivers/motors.h b/UAV-ControlSystem/inc/drivers/motors.h index 7176194..bc98b25 100644 --- a/UAV-ControlSystem/inc/drivers/motors.h +++ b/UAV-ControlSystem/inc/drivers/motors.h @@ -8,20 +8,59 @@ #ifndef DRIVERS_MOTORS_H_ #define DRIVERS_MOTORS_H_ +/************************************************************************** +* 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); +/************************************************************************** +* 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); +/************************************************************************** +* BRIEF: Deactivates a motor (Disables the pwm signal for the motor pin ) +* INFORMATION: +* Example - pwmDeactivateMotor(MOTOR_1) +**************************************************************************/ void pwmDeactivateMotor(uint8_t motor); +/************************************************************************** +* BRIEF: If a motor is Deactivated (no pwm signal), this function will activate the motor to it's last state (the pwm signal will go back to is's last state) * +* INFORMATION: +* Example - pwmActivateMotor(MOTOR_1) +**************************************************************************/ void pwmActivateMotor(uint8_t motor); +/************************************************************************** +* BRIEF: Deactivates all motors (Deactivate a pwm signal to all motor pins) * +* INFORMATION: * +* Example - pwmDeactivateAllMotors() * +**************************************************************************/ void pwmDeactivateAllMotors(void); +/************************************************************************** +* BRIEF: Activates all motors (Activates a pwm signal to all motor pins to it's last state)* * +* INFORMATION: * +* Example - pwmActivateAllMotors() * +**************************************************************************/ void pwmActivateAllMotors(void); +/************************************************************************** +* BRIEF: Change the speed of a certain motor if it's active * +* INFORMATION: The speed is changes by switching the the pulse of a pwm signal * +* Example - pwmAdjustSpeedOfMotor(MOTOR_2, 200) * +**************************************************************************/ void pwmAdjustSpeedOfMotor(uint8_t motor, uint16_t pulse); +/************************************************************************** +* BRIEF: Change the speed of a certain motor if it's active * +* INFORMATION: The speed is change by switching the the Duty Cycle of a pwm signal (The Duty Cycle may only take values between 0 - 100 %) * +* Example - pwmAdjustSpeedOfMotor(MOTOR_2, 50); * +**************************************************************************/ void pwmAdjustSpeedOfMotorDutyCycle(uint8_t motor, uint16_t DutyCycle); diff --git a/UAV-ControlSystem/inc/drivers/pwm.h b/UAV-ControlSystem/inc/drivers/pwm.h index 04337d7..8fb5337 100644 --- a/UAV-ControlSystem/inc/drivers/pwm.h +++ b/UAV-ControlSystem/inc/drivers/pwm.h @@ -31,8 +31,18 @@ void pwmInit(GPIO_TypeDef * GPIO, uint16_t pin, TIM_TypeDef * tim, uint32_t Chan **************************************************************************/ void setPwmPulse(uint32_t Channel, TIM_TypeDef * tim, uint16_t newPulse); +/************************************************************************** +* BRIEF: startPwm Activates a pwm signal for a certain timer on a certain channel +* INFORMATION: +* Example - startPwm(TIM_CHANNEL_4, TIM3); +**************************************************************************/ void startPwm(uint32_t Channel, TIM_TypeDef * tim); +/************************************************************************** +* BRIEF: stopPwm Deactivates a pwm signal for a certain timer on a certain channel +* INFORMATION: +* Example - stopPwm(TIM_CHANNEL_4, TIM3); +**************************************************************************/ void stopPwm(uint32_t Channel, TIM_TypeDef * tim); #endif /* DRIVERS_PWM_H_ */ diff --git a/UAV-ControlSystem/src/drivers/motors.c b/UAV-ControlSystem/src/drivers/motors.c index f2ff5cc..4e84c15 100644 --- a/UAV-ControlSystem/src/drivers/motors.c +++ b/UAV-ControlSystem/src/drivers/motors.c @@ -13,8 +13,8 @@ #include "drivers/pwm.h" #include "drivers/motors.h" -#define MOTOR_PWM_INIT_PERIODE 2000 -#define MOTOR_PWM_INIT_PULSE 100 +#define MOTOR_PWM_INIT_PERIODE 65535 +#define MOTOR_PWM_INIT_PULSE MOTOR_PWM_INIT_PERIODE/2 /* A struct of a pwm motor profile */ typedef struct @@ -150,7 +150,7 @@ void pwmDeactivateMotor(uint8_t motor) } /************************************************************************** -* BRIEF: Activates all motors (Activates a pwm signal to all motor pins to it's last state) * * +* BRIEF: Activates all motors (Activates a pwm signal to all motor pins to it's last state)* * * INFORMATION: * * Example - pwmActivateAllMotors() * **************************************************************************/ diff --git a/UAV-ControlSystem/src/drivers/pwm.c b/UAV-ControlSystem/src/drivers/pwm.c index edfcbf6..91f229b 100644 --- a/UAV-ControlSystem/src/drivers/pwm.c +++ b/UAV-ControlSystem/src/drivers/pwm.c @@ -82,7 +82,7 @@ void pwmInit(GPIO_TypeDef * GPIO, uint16_t pin, TIM_TypeDef * tim, uint32_t Chan TIM_HandleTypeDef TimHandle; - uint32_t uwPrescalerValue =0; + uint32_t uwPrescalerValue =2; TimHandle.Instance = profile.tim; /* Sets timer */ TimHandle.Init.Period = period; /* Sets period of pwm */ TimHandle.Init.Prescaler = uwPrescalerValue; @@ -91,6 +91,7 @@ void pwmInit(GPIO_TypeDef * GPIO, uint16_t pin, TIM_TypeDef * tim, uint32_t Chan TIM_OC_InitTypeDef PWMConfig; + //PWMConfig.Pulse = 840; PWMConfig.Pulse = 840; PWMConfig.OCMode = TIM_OCMODE_PWM1; PWMConfig.OCPolarity = TIM_OCPOLARITY_HIGH; @@ -141,10 +142,11 @@ void pwmInit(GPIO_TypeDef * GPIO, uint16_t pin, TIM_TypeDef * tim, uint32_t Chan } /************************************************************************** -* BRIEF: setPwmPulse changes a certain pulse for an initialized pwm signal * +* BRIEF: setPwmPulse changes a certain pulse for an initialized pwm signal * INFORMATION: It's only possible to change the pwm output for a certain timer -* on a certain channel, not a pin output -* Example - setPwmPulse(TIM_CHANNEL_4, TIM3, 500); * +* on a certain channel, not a pin output. This function will only run is a pwm is active. +* The pwm is activated and deactivated at startPwm/stopPwm +* Example - setPwmPulse(TIM_CHANNEL_4, TIM3, 500); **************************************************************************/ void setPwmPulse(uint32_t Channel, TIM_TypeDef * tim, uint16_t newPulse) { @@ -175,6 +177,11 @@ void setPwmPulse(uint32_t Channel, TIM_TypeDef * tim, uint16_t newPulse) } } +/************************************************************************** +* BRIEF: startPwm Activates a pwm signal for a certain timer on a certain channel +* INFORMATION: +* Example - startPwm(TIM_CHANNEL_4, TIM3); +**************************************************************************/ void startPwm(uint32_t Channel, TIM_TypeDef * tim) { pwmOk = true; @@ -184,6 +191,11 @@ void startPwm(uint32_t Channel, TIM_TypeDef * tim) HAL_TIM_PWM_Start(&TimHandle, Channel); } +/************************************************************************** +* BRIEF: stopPwm Deactivates a pwm signal for a certain timer on a certain channel +* INFORMATION: +* Example - stopPwm(TIM_CHANNEL_4, TIM3); +**************************************************************************/ void stopPwm(uint32_t Channel, TIM_TypeDef * tim) { pwmOk = false;