This repository has been archived on 2020-06-14. You can view files and clone it, but cannot push or open issues or pull requests.
johan9107 b589ff8f1b New drivers to PWM and Motors
Created new drivers for the PWM signals and Motors
2016-09-16 08:28:19 +02:00

196 lines
7.5 KiB
C

/**************************************************************************
* NAME: Johan Gärtner *
* PURPOSE: Driver for the motor signal to certain outputs *
* 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
* **************************************************************************/
#include "stm32f4xx.h"
#include "stm32f4xx_revo.h"
#include "drivers/pwm.h"
#include "drivers/motors.h"
#define MOTOR_PWM_INIT_PERIODE 2000
#define MOTOR_PWM_INIT_PULSE 100
/* A struct of a pwm motor profile */
typedef struct
{
GPIO_TypeDef * port; //GPIOA/B/C
uint16_t pin; //GPIO_PIN_0/1/2/3/..
TIM_TypeDef * tim; //TIM1/2/3/..
uint32_t channel; //TIM_CHANNEL_1/TIM_CHANNEL_1/..
}motorProfile;
/**************************************************************************
* 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
* Each motor configuration is changed in the "stm32f4xx_revo.h" file
* Example - getMotorProfile(MOTOR_1)
**************************************************************************/
motorProfile getMotorProfile(uint8_t motor)
{
motorProfile profile;
switch(motor)
{
case MOTOR_1:
profile.port = MOTOR_1_PORT;
profile.pin = MOTOR_1_PIN;
profile.tim = MOTOR_1_TIM;
profile.channel = MOTOR_1_CHANNEL;
break;
case MOTOR_2:
profile.port = MOTOR_2_PORT;
profile.pin = MOTOR_2_PIN;
profile.tim = MOTOR_2_TIM;
profile.channel = MOTOR_2_CHANNEL;
break;
case MOTOR_3:
profile.port = MOTOR_3_PORT;
profile.pin = MOTOR_3_PIN;
profile.tim = MOTOR_3_TIM;
profile.channel = MOTOR_3_CHANNEL;
break;
case MOTOR_4:
profile.port = MOTOR_4_PORT;
profile.pin = MOTOR_4_PIN;
profile.tim = MOTOR_4_TIM;
profile.channel = MOTOR_4_CHANNEL;
break;
case MOTOR_5:
profile.port = MOTOR_5_PORT;
profile.pin = MOTOR_5_PIN;
profile.tim = MOTOR_5_TIM;
profile.channel = MOTOR_5_CHANNEL;
break;
case MOTOR_6:
profile.port = MOTOR_6_PORT;
profile.pin = MOTOR_6_PIN;
profile.tim = MOTOR_6_TIM;
profile.channel = MOTOR_6_CHANNEL;
break;
case MOTOR_7:
profile.port = MOTOR_7_PORT;
profile.pin = MOTOR_7_PIN;
profile.tim = MOTOR_7_TIM;
profile.channel = MOTOR_7_CHANNEL;
break;
case MOTOR_8:
profile.port = MOTOR_8_PORT;
profile.pin = MOTOR_8_PIN;
profile.tim = MOTOR_8_TIM;
profile.channel = MOTOR_8_CHANNEL;
break;
case MOTOR_9:
profile.port = MOTOR_9_PORT;
profile.pin = MOTOR_9_PIN;
profile.tim = MOTOR_9_TIM;
profile.channel = MOTOR_9_CHANNEL;
break;
case MOTOR_10:
profile.port = MOTOR_10_PORT;
profile.pin = MOTOR_10_PIN;
profile.tim = MOTOR_10_TIM;
profile.channel = MOTOR_10_CHANNEL;;
break;
default:
break;
}
return profile;
}
/**************************************************************************
* 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)
{
motorProfile profile = getMotorProfile(motor);
pwmInit(profile.port, profile.pin, profile.tim, profile.channel, MOTOR_PWM_INIT_PERIODE, MOTOR_PWM_INIT_PULSE);
}
/**************************************************************************
* 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)
{
for (uint8_t i = 1; i < 11; i++ ) pwmEnableMotor(i);
}
/**************************************************************************
* 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)
{
motorProfile profile = getMotorProfile(motor);
startPwm(profile.channel, profile.tim);
}
/**************************************************************************
* BRIEF: Deactivates a motor (Disables the pwm signal for the motor pin )
* INFORMATION:
* Example - pwmDeactivateMotor(MOTOR_1)
**************************************************************************/
void pwmDeactivateMotor(uint8_t motor)
{
motorProfile profile = getMotorProfile(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() *
**************************************************************************/
void pwmActivateAllMotors(void)
{
for (uint8_t i = 1; i < 11; i++ ) pwmDeactivateMotor(i);
}
/**************************************************************************
* BRIEF: Deactivates all motors (Deactivate a pwm signal to all motor pins) *
* INFORMATION: *
* Example - pwmDeactivateAllMotors() *
**************************************************************************/
void pwmDeactivateAllMotors(void)
{
for (uint8_t i = 1; i < 11; i++ ) pwmActivateMotor(i);
}
/**************************************************************************
* 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)
{
motorProfile profile = getMotorProfile(motor);
setPwmPulse(profile.channel, profile.tim, 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)
{
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);
}