
hoaaaaaaaaaaaaa
Revision 0:1e31c3d92e90, committed 2017-12-21
- Comitter:
- Jecours
- Date:
- Thu Dec 21 04:16:17 2017 +0000
- Commit message:
- mbed otita;
Changed in this revision
diff -r 000000000000 -r 1e31c3d92e90 Encoder.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Encoder.h Thu Dec 21 04:16:17 2017 +0000 @@ -0,0 +1,7 @@ +#ifndef ENCODER_H +#define ENCODER_H + +void EncoderInit(TIM_Encoder_InitTypeDef * encoder, TIM_HandleTypeDef * timer, TIM_TypeDef * TIMx, uint32_t maxcount, uint32_t encmode); + +#endif +
diff -r 000000000000 -r 1e31c3d92e90 EncoderInit.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EncoderInit.cpp Thu Dec 21 04:16:17 2017 +0000 @@ -0,0 +1,35 @@ +#include "mbed.h" + +void EncoderInit(TIM_Encoder_InitTypeDef * encoder, TIM_HandleTypeDef * timer, TIM_TypeDef * TIMx, uint32_t maxcount, uint32_t encmode) +{ + + timer->Instance = TIMx; + timer->Init.Period = maxcount; + timer->Init.CounterMode = TIM_COUNTERMODE_UP; + timer->Init.Prescaler = 0; + timer->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + + encoder->EncoderMode = encmode; + + encoder->IC1Filter = 0x0F; + encoder->IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING; + encoder->IC1Prescaler = TIM_ICPSC_DIV4; + encoder->IC1Selection = TIM_ICSELECTION_DIRECTTI; + + encoder->IC2Filter = 0x0F; + encoder->IC2Polarity = TIM_INPUTCHANNELPOLARITY_FALLING; + encoder->IC2Prescaler = TIM_ICPSC_DIV4; + encoder->IC2Selection = TIM_ICSELECTION_DIRECTTI; + + + if (HAL_TIM_Encoder_Init(timer, encoder) != HAL_OK) { + printf("Couldn't Init Encoder\r\n"); + while (1) {} + } + + if(HAL_TIM_Encoder_Start(timer,TIM_CHANNEL_1)!=HAL_OK) { + printf("Couldn't Start Encoder\r\n"); + while (1) {} + } +} +
diff -r 000000000000 -r 1e31c3d92e90 EncoderMspInitF7.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EncoderMspInitF7.cpp Thu Dec 21 04:16:17 2017 +0000 @@ -0,0 +1,30 @@ +#include "mbed.h" + +/* + TIM1_CH1: PA8 + TIM1_CH2: PA9 + TIM2_CH1: PA5 + TIM2_CH2: PB3 + TIM3_CH1: PB4 + TIM3_CH2: PB5 + TIM4_CH1: PB6 + TIM4_CH2: PB7 + TIM8_CH1: PC6 + TIM8_CH2: PC7 + +*/ + +void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim) +{ + GPIO_InitTypeDef GPIO_InitStruct; + + if(htim->Instance == TIM1){ + __TIM1_CLK_ENABLE(); + __GPIOA_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } +}
diff -r 000000000000 -r 1e31c3d92e90 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Dec 21 04:16:17 2017 +0000 @@ -0,0 +1,52 @@ +#include "mbed.h" +#include "Encoder.h" + +PwmOut motor1(PC_8); +PwmOut motor2(PC_9); +DigitalOut led1(LED1); +TIM_Encoder_InitTypeDef encoder1; +TIM_Handle_TypeDef timer; + +Ticker t; + +float kp = 2.0f; +float kd = 0.0f; +float ki = 0.0f; + +void motor(int16_t a); + +uint16_t count = 0; +uint16_t countd = 0; +uint16_t integral= 0; + +uint16_t counter = 32000; + +int main() +{ + EncoderInit(encoder1,timer,TIM8,65535,TIM_ENCODERMODE_TI12); + while(1) { + led1 = !led1; + wait(1); + } +} + +void calc_control(){ + countd = count; + count = __HAL_TIM_GET_COUNTER(&timer); + integral += count-countd; + int output = (counter - count)*kp + (count - countd)*kd + integral * ki; + motor(output); +} + +void motor(int16_t a) +{ + float x = a/32768; + if(x>0) { + motor1.write(x); + motor2.write(0); + }else{ + x = abs(x); + motor2.write(x); + motor1.write(0); + } +} \ No newline at end of file
diff -r 000000000000 -r 1e31c3d92e90 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Dec 21 04:16:17 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/e7ca05fa8600 \ No newline at end of file