Sz_Insper / Mbed 2 deprecated BLDC_IHM07M1_v1

Dependencies:   mbed

Fork of BLDC_IHM07M1_v1 by Raphael Pires

Committer:
praphael94
Date:
Wed Nov 10 20:52:20 2021 +0000
Revision:
0:261598b4ddd7
BLDC Motor with X-Nucleo IHM 07M1_v1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
praphael94 0:261598b4ddd7 1 //============================================================================//
praphael94 0:261598b4ddd7 2 // ACIONAMENTO DO MOTOR BRUSHLESS COM MODELAGEM SIX-STEP //
praphael94 0:261598b4ddd7 3 // //
praphael94 0:261598b4ddd7 4 // Acionamento do tipo Sensorless //
praphael94 0:261598b4ddd7 5 // Utiliza Potenciômetro para controlar a velocidade //
praphael94 0:261598b4ddd7 6 // Utiliza Botão do Usuário para mudar o sentido e parar //
praphael94 0:261598b4ddd7 7 // Programa para placa NUCLEO F303RE com X-NUCLEO IHM 07M1 //
praphael94 0:261598b4ddd7 8 //============================================================================//
praphael94 0:261598b4ddd7 9
praphael94 0:261598b4ddd7 10
praphael94 0:261598b4ddd7 11 //============================= BIBLIOTECAS ==================================//
praphael94 0:261598b4ddd7 12 #include "mbed.h" //Inclusão da Biblioteca Mbed
praphael94 0:261598b4ddd7 13
praphael94 0:261598b4ddd7 14
praphael94 0:261598b4ddd7 15 //=============================== PINAGEM ====================================//
praphael94 0:261598b4ddd7 16 PwmOut in1(PA_8);
praphael94 0:261598b4ddd7 17 PwmOut in2(PA_9); //Pinos de entrada da placa IHM07M1
praphael94 0:261598b4ddd7 18 PwmOut in3(PA_10);
praphael94 0:261598b4ddd7 19
praphael94 0:261598b4ddd7 20 DigitalOut en1(PC_10);
praphael94 0:261598b4ddd7 21 DigitalOut en2(PC_11); //Pinos de enable da placa IHM07M1
praphael94 0:261598b4ddd7 22 DigitalOut en3(PC_12);
praphael94 0:261598b4ddd7 23
praphael94 0:261598b4ddd7 24 DigitalIn botao(PC_13); //Pino do User Button
praphael94 0:261598b4ddd7 25 AnalogIn pot(PB_1); //Pino do potenciômetro da placa IHM07M1
praphael94 0:261598b4ddd7 26
praphael94 0:261598b4ddd7 27
praphael94 0:261598b4ddd7 28 //============================= CONSTANTES ===================================//
praphael94 0:261598b4ddd7 29 #define maxDelay 0.0200f //Máximo delay entre a comutação das fases
praphael94 0:261598b4ddd7 30 #define minDelay 0.0010f //Mínimo delay entre a comutação das fases
praphael94 0:261598b4ddd7 31
praphael94 0:261598b4ddd7 32
praphael94 0:261598b4ddd7 33 //============================= VARIÁVEIS ====================================//
praphael94 0:261598b4ddd7 34 //Matriz que contém a sequência para os pinos de enable
praphael94 0:261598b4ddd7 35 // U,V,W
praphael94 0:261598b4ddd7 36 int enSeq[7][3] = {{1,0,1},{0,1,1},{1,1,0},{1,0,1},{0,1,1},{1,1,0},{0,0,0}};
praphael94 0:261598b4ddd7 37 //Matriz que contém a sequência para os pinos de entrada
praphael94 0:261598b4ddd7 38 // U,V,W
praphael94 0:261598b4ddd7 39 int inSeq[7][3] = {{1,0,0},{0,1,0},{0,1,0},{0,0,1},{0,0,1},{1,0,0},{0,0,0}};
praphael94 0:261598b4ddd7 40
praphael94 0:261598b4ddd7 41 float delay = 0; //Variável usada para calcular o delay aplicado
praphael94 0:261598b4ddd7 42 int i = 0; //Índice para contagem da sequência de acionamento
praphael94 0:261598b4ddd7 43 int sentido = 0; //Variável usada para controlar o sentido e a parada
praphael94 0:261598b4ddd7 44 bool press = 0; //Flag que monitora o botão
praphael94 0:261598b4ddd7 45
praphael94 0:261598b4ddd7 46
praphael94 0:261598b4ddd7 47 //========================= PROGRAMA PRINCIPAL ===============================//
praphael94 0:261598b4ddd7 48 int main(){
praphael94 0:261598b4ddd7 49 en1.write(0);
praphael94 0:261598b4ddd7 50 en2.write(0); //Coloca os pinos de enable em Alta Impedância
praphael94 0:261598b4ddd7 51 en3.write(0);
praphael94 0:261598b4ddd7 52
praphael94 0:261598b4ddd7 53 in1.period_ms(1);
praphael94 0:261598b4ddd7 54 in2.period_ms(1); //Configura o período do PWM
praphael94 0:261598b4ddd7 55 in3.period_ms(1);
praphael94 0:261598b4ddd7 56
praphael94 0:261598b4ddd7 57 in1.write(0);
praphael94 0:261598b4ddd7 58 in2.write(0); //Configura o duty-cycle para 0%
praphael94 0:261598b4ddd7 59 in3.write(0);
praphael94 0:261598b4ddd7 60
praphael94 0:261598b4ddd7 61
praphael94 0:261598b4ddd7 62 //============================ LOOP INFINITO =================================//
praphael94 0:261598b4ddd7 63 while(1){
praphael94 0:261598b4ddd7 64
praphael94 0:261598b4ddd7 65 in1.write(inSeq[i][0]); //Seta as entradas conforme a combinação da
praphael94 0:261598b4ddd7 66 in2.write(inSeq[i][1]); //matriz, selecionando as linhas pelo índice
praphael94 0:261598b4ddd7 67 in3.write(inSeq[i][2]); //e mantendo as colunas fixas
praphael94 0:261598b4ddd7 68
praphael94 0:261598b4ddd7 69 en1.write(enSeq[i][0]); //Seta os enables conforme a combinação da
praphael94 0:261598b4ddd7 70 en2.write(enSeq[i][1]); //matriz, selecionando as linhas pelo índice
praphael94 0:261598b4ddd7 71 en3.write(enSeq[i][2]); //e mantendo as colunas fixas
praphael94 0:261598b4ddd7 72
praphael94 0:261598b4ddd7 73 switch(sentido){
praphael94 0:261598b4ddd7 74 case 0: i = 6; break; //Pára o motor
praphael94 0:261598b4ddd7 75 case 1: i++; if(i > 5){i = 0;} break; //Incrementa o índice
praphael94 0:261598b4ddd7 76 case 2: i = 6; break; //Pára o motor
praphael94 0:261598b4ddd7 77 case 3: i--; if(i < 0){i = 5;} break; //Decrementa o índice
praphael94 0:261598b4ddd7 78 default: break;
praphael94 0:261598b4ddd7 79 }
praphael94 0:261598b4ddd7 80
praphael94 0:261598b4ddd7 81 if(botao == 0 && press == 0){press = 1;} //Botão presionado
praphael94 0:261598b4ddd7 82 if(press == 1 && botao == 1){ //Botão solto
praphael94 0:261598b4ddd7 83 sentido++; //Incrementa sentido
praphael94 0:261598b4ddd7 84 if(sentido > 3){sentido = 0;} //Zera sentido
praphael94 0:261598b4ddd7 85 press = 0; //Zera a flag do botão
praphael94 0:261598b4ddd7 86 }
praphael94 0:261598b4ddd7 87
praphael94 0:261598b4ddd7 88 //Calcula delay com valores entre "minDelay" e "maxDelay"
praphael94 0:261598b4ddd7 89 delay = (pot*(maxDelay - minDelay)) + minDelay;
praphael94 0:261598b4ddd7 90 wait(delay); //Delay conforme o resultado
praphael94 0:261598b4ddd7 91 }
praphael94 0:261598b4ddd7 92 }
praphael94 0:261598b4ddd7 93