Brushless Motor Done✌️

This commit is contained in:
but0n 2016-08-19 16:21:13 +08:00
parent 02e8755b99
commit 9113736122
6 changed files with 37 additions and 18 deletions

View File

@ -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**

View File

@ -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

View File

@ -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)

View File

@ -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;
}

View File

@ -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

View File

@ -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);