diff --git a/README.md b/README.md index e0a981c..9716057 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ * **PID** Control * Schematic * PCB Layout (*Now I'm watting for PCB*) +* 使用PWM外接商品电调驱动无刷电机 * **Wi-Fi**(`ESP8266`) - * 使用PWM外接商品电调驱动无刷电机 * PCB Test * PID debug * Design **frame** diff --git a/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/inc/key.h b/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/inc/key.h new file mode 100644 index 0000000..42446d9 --- /dev/null +++ b/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/inc/key.h @@ -0,0 +1,10 @@ +#pragma once +#ifndef _KEY_H +#define _KEY_H +void delay(volatile unsigned int count); +#define KEY1 BIT_ADDR((GPIOC_BASE+8), 13) +#define PAUSE() {while(KEY1);delay(200);} + +void Key_init(); + +#endif diff --git a/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/inc/motor.h b/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/inc/motor.h index c67107a..053aa55 100644 --- a/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/inc/motor.h +++ b/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/inc/motor.h @@ -6,6 +6,9 @@ #define MOTOR1 TIM3->CCR2 #define MOTOR2 TIM3->CCR1 +#define MOTOR_MAX 27360 +#define MOTOR_MIN 10080 + void PWM_Init(unsigned short arr, unsigned short psc); //72MHz / (arr + 1)*(psc + 1) diff --git a/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/src/key.c b/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/src/key.c new file mode 100644 index 0000000..910bce7 --- /dev/null +++ b/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/src/key.c @@ -0,0 +1,8 @@ +#include "key.h" +#include "stm32f10x.h" +void Key_init() { + RCC->APB2ENR |= 1<<4; //GPIOC + GPIOC->CRH &= 0xFF0FFFFF; //Clean Pin 13 + GPIOC->CRH |= 0x00800000; //上拉输入 + GPIOC->ODR |= 1<<13; +} diff --git a/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/src/motor.c b/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/src/motor.c index 39a71d3..b67dfd3 100644 --- a/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/src/motor.c +++ b/libs/STM32_USB-FS-Device_Lib_V4.0.0/Libraries/STM32F10x_StdPeriph_Driver/src/motor.c @@ -14,7 +14,7 @@ void PWM_Init(unsigned short arr, unsigned short psc) { TIM3->ARR = arr - 1; TIM3->PSC = psc - 1; - TIM3->CCMR1 |= 7<<12; //CH2 Set OC2M[2:0]: PWM Mode + TIM3->CCMR1 |= 6<<12; //CH2 Set OC2M[2:0]: PWM Mode TIM3->CCMR1 |= 1<<11; //CH2 Set OC2PF: Enable TIM3->CCMR1 |= 7<<4; //CH1 Set OC2M[2:0]: PWM Mode TIM3->CCMR1 |= 1<<3; //CH1 Set OC2PF: Enable diff --git a/src/main.c b/src/main.c index 8eeaa5b..5230591 100644 --- a/src/main.c +++ b/src/main.c @@ -5,7 +5,7 @@ #include "motor.h" #include "uart.h" -#include "wifi.h" +#include "key.h" #define Kp 100.0f //比例增益支配率(常量) #define Ki 0.002f //积分增益支配率 @@ -30,16 +30,14 @@ void pid(float setPoint, float d) { if(g_sumErro > SUM_ERRO_MAX) g_sumErro = SUM_ERRO_MAX; //积分限幅 else if(g_sumErro < SUM_ERRO_MIN) g_sumErro = SUM_ERRO_MIN; - unsigned short resu = (short)(g_iErro * K_P + g_sumErro * K_I + d * K_D); //PID输出 + short resu = (short)(g_iErro * K_P + g_sumErro * K_I + d * K_D); //PID输出 - MOTOR1 += resu; - MOTOR2 -= resu; - if(MOTOR1 > 7199) MOTOR1 = 7199; - else if(MOTOR1 < 800) MOTOR1 = 800; - - if(MOTOR2 > 7199) MOTOR2 = 7199; - else if(MOTOR2 < 800) MOTOR2 = 800; + if((MOTOR1 + resu) > MOTOR_MAX) + MOTOR1 = MOTOR_MAX; + else if((MOTOR1 + resu) < MOTOR_MIN) + MOTOR1 = MOTOR_MIN; + else MOTOR1 += resu; } //ms @@ -100,11 +98,15 @@ void Comput(SixAxis cache) { g_Yaw = atan2(2 * (g_q1 * g_q2 + g_q0 * g_q3), g_q0*g_q0 + g_q1*g_q1 - g_q2*g_q2 - g_q3*g_q3) * 57.3; } - int main() { initLED(); + Key_init(); - PWM_Init(7200,10); + PWM_Init(28800,5); + MOTOR1 = MOTOR_MAX; + PAUSE(); + MOTOR1 = MOTOR_MIN; + PAUSE(); uart_init(72, 115200); @@ -112,14 +114,10 @@ int main() { SixAxis sourceData; - wifi_init(); - MOTOR1 = 7190; - MOTOR2 = 7190; - delay(3000); - while(0) { + while(1) { MPU6050_getStructData(&sourceData); Comput(sourceData);