int min_;
int max_;
- Pid(float kp, float ki, float kd) {
+ Pid(float kp, float ki, float kd, int min, int max) {
this->kp_ = kp;
this->ki_ = ki;
this->kd_ = kd;
this->previous_error_ = 0;
this->error_sum_ = 0;
+ this->min_ = min;
+ this->max_ = max;
+
}
- void config(float kp, float ki, float kd) {
+ void config(float kp, float ki, float kd, int min, int max) {
this->kp_ = kp;
this->ki_ = ki;
this->kd_ = kd;
this->previous_error_ = 0;
this->error_sum_ = 0;
+ this->min_ = min;
+ this->max_ = max;
+
}
void set(float setpoint) {
output += (this->error_ - this->previous_error_) * kd_;
this->previous_error_ = this->error_;
- int integer_output = static_cast<int> (output);
+ int integer_output = static_cast<int>(output);
-// if(integer_output > this->max_)
-// integer_output = this->max_;
-// else if (integer_output < this->min_)
-// integer_output = this->min_;
+ //anti windup
+ if (integer_output > this->max_) {
+ integer_output = this->max_;
+ this->error_sum_ -= this->error_;
+ } else if (integer_output < this->min_){
+ integer_output = this->min_;
+ this->error_sum_ -= this->error_;
+ }
return integer_output;
float right_velocity;\r
\r
//PID\r
+int pid_min = 0;\r
+int pid_max = 0;\r
\r
-Pid left_pid(180, 200, 0);\r
-Pid right_pid(185, 195, 0);\r
-Pid cross_pid(50, 20, 0);\r
+Pid left_pid(180, 200, 0, pid_min, pid_max);\r
+Pid right_pid(185, 195, 0, pid_min, pid_max);\r
+Pid cross_pid(50, 20, 0, pid_min, pid_max);\r
\r
int left_dutycycle;\r
int right_dutycycle;\r
sleep1_Pin,\r
dir1_GPIO_Port,\r
dir1_Pin,\r
- &htim4,\r
- TIM_CHANNEL_4);\r
+ &htim4, TIM_CHANNEL_4);\r
MotorController left_motor(sleep2_GPIO_Port,\r
sleep2_Pin,\r
dir2_GPIO_Port,\r
dir2_Pin,\r
- &htim4,\r
- TIM_CHANNEL_3);\r
+ &htim4, TIM_CHANNEL_3);\r
\r
//Communication\r
uint8_t *tx_buffer;\r
left_motor.setup();\r
right_motor.setup();\r
\r
+ //right and left motors have the same parameters\r
+ pid_min = -left_motor.max_dutycycle_;\r
+ pid_max = left_motor.max_dutycycle_;\r
+\r
+ left_pid.config(180, 200, 0, pid_min, pid_max);\r
+ right_pid.config(185, 195, 0, pid_min, pid_max);\r
+ cross_pid.config(50, 20, 0, pid_min, pid_max);\r
+\r
left_motor.coast();\r
right_motor.coast();\r
\r
/** Configure the main internal regulator output voltage \r
*/\r
__HAL_RCC_PWR_CLK_ENABLE();\r
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);\r
- /** Initializes the CPU, AHB and APB busses clocks \r
+ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);/** Initializes the CPU, AHB and APB busses clocks\r
*/\r
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;\r
RCC_OscInitStruct.HSIState = RCC_HSI_ON;\r
static void MX_NVIC_Init(void) {\r
/* TIM3_IRQn interrupt configuration */\r
HAL_NVIC_SetPriority(TIM3_IRQn, 2, 1);\r
- HAL_NVIC_EnableIRQ(TIM3_IRQn);\r
+ HAL_NVIC_EnableIRQ (TIM3_IRQn);\r
/* TIM6_DAC_IRQn interrupt configuration */\r
HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 2, 2);\r
- HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);\r
+ HAL_NVIC_EnableIRQ (TIM6_DAC_IRQn);\r
/* USART6_IRQn interrupt configuration */\r
HAL_NVIC_SetPriority(USART6_IRQn, 1, 0);\r
- HAL_NVIC_EnableIRQ(USART6_IRQn);\r
+ HAL_NVIC_EnableIRQ (USART6_IRQn);\r
}\r
\r
/* USER CODE BEGIN 4 */\r