From 3131147c7d396ba88fc791d46f33ba9abeaf98b6 Mon Sep 17 00:00:00 2001 From: johan9107 Date: Mon, 19 Sep 2016 08:51:15 +0200 Subject: [PATCH 1/3] PWM and motor drivers Added a pulse stop signal to the motor driver and changed the clk prescaler value in the pwm driver --- UAV-ControlSystem/src/drivers/motors.c | 27 ++++++++++++++++++++++---- UAV-ControlSystem/src/drivers/pwm.c | 3 +-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/UAV-ControlSystem/src/drivers/motors.c b/UAV-ControlSystem/src/drivers/motors.c index 4e84c15..75e7af7 100644 --- a/UAV-ControlSystem/src/drivers/motors.c +++ b/UAV-ControlSystem/src/drivers/motors.c @@ -13,7 +13,7 @@ #include "drivers/pwm.h" #include "drivers/motors.h" -#define MOTOR_PWM_INIT_PERIODE 65535 +#define MOTOR_PWM_INIT_PERIODE 2000 #define MOTOR_PWM_INIT_PULSE MOTOR_PWM_INIT_PERIODE/2 /* A struct of a pwm motor profile */ @@ -25,6 +25,16 @@ typedef struct uint32_t channel; //TIM_CHANNEL_1/TIM_CHANNEL_1/.. }motorProfile; +/************************************************************************** +* BRIEF: Returns the final pulse of a motor driver call +* INFORMATION: The pulse is not allowed to be higher then 94 % of the total periode of the pwm signal, otherwise the pwm won't perform correctly +* Example - pwmEnableMotor(MOTOR_1) +**************************************************************************/ +uint16_t checkPulse(uint16_t pulse) +{ + return ((pulse/MOTOR_PWM_INIT_PERIODE)*100 < 94)? pulse: MOTOR_PWM_INIT_PERIODE*0.94; +} + /************************************************************************** * BRIEF: Returns a profile of a certain motor * INFORMATION: Each Motor has a certain profile which includes a pin, port, timer and a timer channel @@ -152,7 +162,16 @@ void pwmDeactivateMotor(uint8_t motor) /************************************************************************** * BRIEF: Activates all motors (Activates a pwm signal to all motor pins to it's last state)* * * INFORMATION: * -* Example - pwmActivateAllMotors() * +* Example - pwmActivateAllMotors() ***************************************** +* 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: * **************************************************************************/ void pwmActivateAllMotors(void) { @@ -178,7 +197,7 @@ void pwmAdjustSpeedOfMotor(uint8_t motor, uint16_t pulse) { motorProfile profile = getMotorProfile(motor); - setPwmPulse(profile.channel, profile.tim, pulse); + setPwmPulse(profile.channel, profile.tim, checkPulse(pulse)); } /************************************************************************** @@ -191,5 +210,5 @@ void pwmAdjustSpeedOfMotorDutyCycle(uint8_t motor, uint16_t DutyCycle) uint16_t pulse = (DutyCycle*MOTOR_PWM_INIT_PERIODE)/100; /* Converts the DutyCycle to a pulse */ motorProfile profile = getMotorProfile(motor); - setPwmPulse(profile.channel, profile.tim, pulse); + setPwmPulse(profile.channel, profile.tim, checkPulse(pulse)); } diff --git a/UAV-ControlSystem/src/drivers/pwm.c b/UAV-ControlSystem/src/drivers/pwm.c index 91f229b..e7e6992 100644 --- a/UAV-ControlSystem/src/drivers/pwm.c +++ b/UAV-ControlSystem/src/drivers/pwm.c @@ -82,10 +82,9 @@ void pwmInit(GPIO_TypeDef * GPIO, uint16_t pin, TIM_TypeDef * tim, uint32_t Chan TIM_HandleTypeDef TimHandle; - uint32_t uwPrescalerValue =2; TimHandle.Instance = profile.tim; /* Sets timer */ TimHandle.Init.Period = period; /* Sets period of pwm */ - TimHandle.Init.Prescaler = uwPrescalerValue; + TimHandle.Init.Prescaler = 88; /* pwm prescaler of clk */ TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; From 2198adce0c3da79a9b0e407f8ad3b5e8ad7d5b0d Mon Sep 17 00:00:00 2001 From: johan9107 Date: Mon, 19 Sep 2016 09:05:02 +0200 Subject: [PATCH 2/3] Error in motor drivers Bug fix --- UAV-ControlSystem/src/drivers/motors.c | 1 - 1 file changed, 1 deletion(-) diff --git a/UAV-ControlSystem/src/drivers/motors.c b/UAV-ControlSystem/src/drivers/motors.c index 75e7af7..d35b494 100644 --- a/UAV-ControlSystem/src/drivers/motors.c +++ b/UAV-ControlSystem/src/drivers/motors.c @@ -167,7 +167,6 @@ void pwmDeactivateMotor(uint8_t motor) * INFORMATION: * * Example - pwmDeactivateAllMotors() * **************************************************************************/ -void pwmDeactivateAllMotors(void); /************************************************************************** * BRIEF: Activates all motors (Activates a pwm signal to all motor pins to it's last state)* * From ea3e59956a361a10488d152cee0ea545a69ec191 Mon Sep 17 00:00:00 2001 From: johan9107 Date: Mon, 19 Sep 2016 09:07:03 +0200 Subject: [PATCH 3/3] Error fix Deleted comments --- UAV-ControlSystem/src/drivers/motors.c | 9 --------- 1 file changed, 9 deletions(-) diff --git a/UAV-ControlSystem/src/drivers/motors.c b/UAV-ControlSystem/src/drivers/motors.c index d35b494..8bf89a3 100644 --- a/UAV-ControlSystem/src/drivers/motors.c +++ b/UAV-ControlSystem/src/drivers/motors.c @@ -159,15 +159,6 @@ void pwmDeactivateMotor(uint8_t motor) stopPwm(profile.channel, profile.tim); } -/************************************************************************** -* BRIEF: Activates all motors (Activates a pwm signal to all motor pins to it's last state)* * -* INFORMATION: * -* Example - pwmActivateAllMotors() ***************************************** -* BRIEF: Deactivates all motors (Deactivate a pwm signal to all motor pins) * -* INFORMATION: * -* Example - pwmDeactivateAllMotors() * -**************************************************************************/ - /************************************************************************** * BRIEF: Activates all motors (Activates a pwm signal to all motor pins to it's last state)* * * INFORMATION: *