From: Xavier L. <Ba...@us...> - 2011-04-21 14:31:07
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "krobot". The branch, master has been updated via 62f90db8b68913bc816a7ee80a619333e226f1f4 (commit) from edd099fd4078acee928803c9e63934c83b783f19 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit 62f90db8b68913bc816a7ee80a619333e226f1f4 Author: Xavier Lagorce <Xav...@cr...> Date: Thu Apr 21 16:29:53 2011 +0200 [Controller_Motor_STM32] Added the possibility to limit PWM output in software. To allow for 'hard' and dirty limitation of motor voltage is nominal voltage is different from power bus voltage. ----------------------------------------------------------------------- Changes: diff --git a/elec/boards/Controller_Motor_STM32/Firmware/controller_motor_stm32/motor.c b/elec/boards/Controller_Motor_STM32/Firmware/controller_motor_stm32/motor.c index 16bec21..08cafb3 100644 --- a/elec/boards/Controller_Motor_STM32/Firmware/controller_motor_stm32/motor.c +++ b/elec/boards/Controller_Motor_STM32/Firmware/controller_motor_stm32/motor.c @@ -16,19 +16,21 @@ uint8_t enabledMotors = 0, indMotors = 0; signed char currentSpeedSign[] = {0, 0, 0, 0}; +int32_t maxPWMs[] = {MAX_PWM, MAX_PWM, MAX_PWM, MAX_PWM}; TIM_OCInitTypeDef TIM_OCInitStructure; -/* - Some dirty imports from the stm32lib from ST <stm32_lib> - */ - -//#define __IO volatile /*!< defines 'read / write' permissions */ -//typedef __IO uint16_t vu16; - - -/* - End of imports </stm32_lib> - */ +static int32_t staSpeed(int32_t speed, int32_t maxSpeed, uint8_t *ind) { + if (speed >= maxSpeed) { + *ind = 1; + return MAX_PWM; + } else if (speed <= -maxSpeed) { + *ind = 1; + return -maxSpeed; + } else { + *ind = 0; + return speed; + } +} /* * Initialises TIM2 for PWM generation and associated GPIOs @@ -174,32 +176,24 @@ void disableMotor(uint8_t motor) { void motorSetSpeed(uint8_t motor, int32_t speed) { uint8_t ind = 0; - - if (speed >= MAX_PWM) { - speed = MAX_PWM; - ind = 1; - } else if (speed <= -MAX_PWM) { - speed = -MAX_PWM; - ind = 1; - } else { - ind = 0; - } + int32_t app_speed; if (motor & MOTOR1) { - if(speed >= 0) { + app_speed = staSpeed(speed, maxPWMs[0], &ind); + if(app_speed >= 0) { if (currentSpeedSign[0] != 1) { currentSpeedSign[0] = 1; stm32_gpioPinWrite(((struct stm32_gpio *)GPIOC_BASE), BV(4), 1); stm32_gpioPinWrite(((struct stm32_gpio *)GPIOC_BASE), BV(5), 0); } - TIM_OCInitStructure.TIM_Pulse = (uint16_t)speed; + TIM_OCInitStructure.TIM_Pulse = (uint16_t)app_speed; } else { if (currentSpeedSign[0] != -1) { currentSpeedSign[0] = -1; stm32_gpioPinWrite(((struct stm32_gpio *)GPIOC_BASE), BV(4), 0); stm32_gpioPinWrite(((struct stm32_gpio *)GPIOC_BASE), BV(5), 1); } - TIM_OCInitStructure.TIM_Pulse = (uint16_t)(-speed); + TIM_OCInitStructure.TIM_Pulse = (uint16_t)(-app_speed); } TIM_OC1Init(TIM2, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); @@ -215,20 +209,21 @@ void motorSetSpeed(uint8_t motor, int32_t speed) { } } if (motor & MOTOR2) { - if(speed >= 0) { + app_speed = staSpeed(speed, maxPWMs[1], &ind); + if(app_speed >= 0) { if (currentSpeedSign[1] != 1) { currentSpeedSign[1] = 1; stm32_gpioPinWrite(((struct stm32_gpio *)GPIOB_BASE), BV(1), 1); stm32_gpioPinWrite(((struct stm32_gpio *)GPIOB_BASE), BV(14), 0); } - TIM_OCInitStructure.TIM_Pulse = (uint16_t)speed; + TIM_OCInitStructure.TIM_Pulse = (uint16_t)app_speed; } else { if (currentSpeedSign[1] != -1) { currentSpeedSign[1] = -1; stm32_gpioPinWrite(((struct stm32_gpio *)GPIOB_BASE), BV(1), 0); stm32_gpioPinWrite(((struct stm32_gpio *)GPIOB_BASE), BV(14), 1); } - TIM_OCInitStructure.TIM_Pulse = (uint16_t)(-speed); + TIM_OCInitStructure.TIM_Pulse = (uint16_t)(-app_speed); } TIM_OC2Init(TIM2, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); @@ -244,20 +239,21 @@ void motorSetSpeed(uint8_t motor, int32_t speed) { } } if (motor & MOTOR3) { - if(speed >= 0) { + app_speed = staSpeed(speed, maxPWMs[2], &ind); + if(app_speed >= 0) { if (currentSpeedSign[2] != 1) { currentSpeedSign[2] = 1; stm32_gpioPinWrite(((struct stm32_gpio *)GPIOC_BASE), BV(10), 1); stm32_gpioPinWrite(((struct stm32_gpio *)GPIOC_BASE), BV(11), 0); } - TIM_OCInitStructure.TIM_Pulse = (uint16_t)speed; + TIM_OCInitStructure.TIM_Pulse = (uint16_t)app_speed; } else { if (currentSpeedSign[2] != -1) { currentSpeedSign[2] = -1; stm32_gpioPinWrite(((struct stm32_gpio *)GPIOC_BASE), BV(10), 0); stm32_gpioPinWrite(((struct stm32_gpio *)GPIOC_BASE), BV(11), 1); } - TIM_OCInitStructure.TIM_Pulse = (uint16_t)(-speed); + TIM_OCInitStructure.TIM_Pulse = (uint16_t)(-app_speed); } TIM_OC3Init(TIM2, &TIM_OCInitStructure); TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable); @@ -273,20 +269,21 @@ void motorSetSpeed(uint8_t motor, int32_t speed) { } } if (motor & MOTOR4) { - if(speed >= 0) { + app_speed = staSpeed(speed, maxPWMs[0], &ind); + if(app_speed >= 0) { if (currentSpeedSign[3] != 1) { currentSpeedSign[3] = 1; stm32_gpioPinWrite(((struct stm32_gpio *)GPIOB_BASE), BV(5), 1); stm32_gpioPinWrite(((struct stm32_gpio *)GPIOB_BASE), BV(9), 0); } - TIM_OCInitStructure.TIM_Pulse = (uint16_t)speed; + TIM_OCInitStructure.TIM_Pulse = (uint16_t)app_speed; } else { if (currentSpeedSign[3] != -1) { currentSpeedSign[3] = -1; stm32_gpioPinWrite(((struct stm32_gpio *)GPIOB_BASE), BV(5), 0); stm32_gpioPinWrite(((struct stm32_gpio *)GPIOB_BASE), BV(9), 1); } - TIM_OCInitStructure.TIM_Pulse = (uint16_t)(-speed); + TIM_OCInitStructure.TIM_Pulse = (uint16_t)(-app_speed); } TIM_OC4Init(TIM2, &TIM_OCInitStructure); TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable); @@ -352,3 +349,33 @@ void motorStop(uint8_t motor, uint8_t mode) { } } } + +void motorSetMaxPWM(uint8_t motor, int32_t maxPWM) { + if (maxPWM < 0) + return; + + if (motor & MOTOR1) { + if (maxPWM <= MAX_PWM) + maxPWMs[0] = maxPWM; + else + maxPWMs[0] = MAX_PWM; + } + if (motor & MOTOR2) { + if (maxPWM <= MAX_PWM) + maxPWMs[1] = maxPWM; + else + maxPWMs[1] = MAX_PWM; + } + if (motor & MOTOR3) { + if (maxPWM <= MAX_PWM) + maxPWMs[2] = maxPWM; + else + maxPWMs[2] = MAX_PWM; + } + if (motor & MOTOR4) { + if (maxPWM <= MAX_PWM) + maxPWMs[3] = maxPWM; + else + maxPWMs[3] = MAX_PWM; + } +} diff --git a/elec/boards/Controller_Motor_STM32/Firmware/controller_motor_stm32/motor.h b/elec/boards/Controller_Motor_STM32/Firmware/controller_motor_stm32/motor.h index 13eb4bf..ff8ed53 100644 --- a/elec/boards/Controller_Motor_STM32/Firmware/controller_motor_stm32/motor.h +++ b/elec/boards/Controller_Motor_STM32/Firmware/controller_motor_stm32/motor.h @@ -31,5 +31,6 @@ void enableMotor(uint8_t motor); void disableMotor(uint8_t motor); void motorSetSpeed(uint8_t motor, int32_t speed); void motorStop(uint8_t motor, uint8_t mode); +void motorSetMaxPWM(uint8_t motor, int32_t maxPWM); #endif hooks/post-receive -- krobot |