motor brushless stephanie

Dependencies:   mbed

Committer:
stephanie_liu
Date:
Fri Nov 12 17:14:20 2021 +0000
Revision:
0:43361aa78bb0
aps 3 - motor brushless

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stephanie_liu 0:43361aa78bb0 1 #include "mbed.h"
stephanie_liu 0:43361aa78bb0 2 // -------------- Definição portas --------------
stephanie_liu 0:43361aa78bb0 3 PwmOut IN_1 (PA_8); // IHM07M1 PWM
stephanie_liu 0:43361aa78bb0 4 PwmOut IN_2 (PA_9);
stephanie_liu 0:43361aa78bb0 5 PwmOut IN_3 (PA_10);
stephanie_liu 0:43361aa78bb0 6 DigitalOut EN_1 (PC_10); // IHM07M1 ENABLE
stephanie_liu 0:43361aa78bb0 7 DigitalOut EN_2 (PC_11);
stephanie_liu 0:43361aa78bb0 8 DigitalOut EN_3 (PC_12);
stephanie_liu 0:43361aa78bb0 9 DigitalIn button(PC_13); // botão de usuário
stephanie_liu 0:43361aa78bb0 10 AnalogIn potenciometro(PB_1);
stephanie_liu 0:43361aa78bb0 11 Serial pc(USBTX, USBRX);
stephanie_liu 0:43361aa78bb0 12
stephanie_liu 0:43361aa78bb0 13 bool estado;
stephanie_liu 0:43361aa78bb0 14
stephanie_liu 0:43361aa78bb0 15 float map(float x, float in_min, float in_max, float out_min, float out_max){
stephanie_liu 0:43361aa78bb0 16 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
stephanie_liu 0:43361aa78bb0 17 }
stephanie_liu 0:43361aa78bb0 18
stephanie_liu 0:43361aa78bb0 19 int main(){
stephanie_liu 0:43361aa78bb0 20 // SETUP
stephanie_liu 0:43361aa78bb0 21 pc.baud(9600);
stephanie_liu 0:43361aa78bb0 22 estado = false;
stephanie_liu 0:43361aa78bb0 23 //3 braços começam desacoplados
stephanie_liu 0:43361aa78bb0 24 EN_1 = 2; // Braço A
stephanie_liu 0:43361aa78bb0 25 EN_2 = 2; // Braço B
stephanie_liu 0:43361aa78bb0 26 EN_3 = 2; // Braço C
stephanie_liu 0:43361aa78bb0 27 //frequencia de acionamento (gates dos transistores) PWM -> 1ms
stephanie_liu 0:43361aa78bb0 28 IN_1.period_ms(1);
stephanie_liu 0:43361aa78bb0 29 IN_2.period_ms(1);
stephanie_liu 0:43361aa78bb0 30 IN_3.period_ms(1);
stephanie_liu 0:43361aa78bb0 31 //pwm com duty cicle 0%
stephanie_liu 0:43361aa78bb0 32 IN_1.write(0);
stephanie_liu 0:43361aa78bb0 33 IN_2.write(0);
stephanie_liu 0:43361aa78bb0 34 IN_3.write(0);
stephanie_liu 0:43361aa78bb0 35 while(1) {
stephanie_liu 0:43361aa78bb0 36 int leitura = potenciometro.read();// leitura do potenciometro
stephanie_liu 0:43361aa78bb0 37 float delay = map(leitura, 0, 4095, 0.008, 0.003);
stephanie_liu 0:43361aa78bb0 38
stephanie_liu 0:43361aa78bb0 39 if (button == 0) {
stephanie_liu 0:43361aa78bb0 40 // VARIAÇÃO entre estado e !estado = Irá ligar ou desligar o motor
stephanie_liu 0:43361aa78bb0 41 estado = !estado;
stephanie_liu 0:43361aa78bb0 42 wait_ms(200);
stephanie_liu 0:43361aa78bb0 43 }
stephanie_liu 0:43361aa78bb0 44
stephanie_liu 0:43361aa78bb0 45 if(estado) {
stephanie_liu 0:43361aa78bb0 46 //acionamento de cada braço com diferentes polaridades seguidas de desacoplamento
stephanie_liu 0:43361aa78bb0 47 // 2 - desacoblado, 1 HIGH, 0 LOW (ENABLE)
stephanie_liu 0:43361aa78bb0 48 // 1 High - 0 LOW (INPUT)
stephanie_liu 0:43361aa78bb0 49 //STEP 1
stephanie_liu 0:43361aa78bb0 50 IN_1 = 1;
stephanie_liu 0:43361aa78bb0 51 EN_1 = 1;
stephanie_liu 0:43361aa78bb0 52 wait(delay);
stephanie_liu 0:43361aa78bb0 53 IN_1 = 0;
stephanie_liu 0:43361aa78bb0 54 EN_1 = 2;
stephanie_liu 0:43361aa78bb0 55
stephanie_liu 0:43361aa78bb0 56 //STEP 2
stephanie_liu 0:43361aa78bb0 57 IN_3 = 1;
stephanie_liu 0:43361aa78bb0 58 EN_3 = 0;
stephanie_liu 0:43361aa78bb0 59 wait(delay);
stephanie_liu 0:43361aa78bb0 60 IN_3 = 0;
stephanie_liu 0:43361aa78bb0 61 EN_3 = 2;
stephanie_liu 0:43361aa78bb0 62
stephanie_liu 0:43361aa78bb0 63 //STEP 3
stephanie_liu 0:43361aa78bb0 64 IN_2 = 1;
stephanie_liu 0:43361aa78bb0 65 EN_2 = 1;
stephanie_liu 0:43361aa78bb0 66 wait(delay);
stephanie_liu 0:43361aa78bb0 67 IN_2 = 0;
stephanie_liu 0:43361aa78bb0 68 EN_2 = 2;
stephanie_liu 0:43361aa78bb0 69
stephanie_liu 0:43361aa78bb0 70 // STEP 4
stephanie_liu 0:43361aa78bb0 71 IN_1 = 1;
stephanie_liu 0:43361aa78bb0 72 EN_1 = 0;
stephanie_liu 0:43361aa78bb0 73 wait(delay);
stephanie_liu 0:43361aa78bb0 74 IN_1 = 0;
stephanie_liu 0:43361aa78bb0 75 EN_1 = 2;
stephanie_liu 0:43361aa78bb0 76
stephanie_liu 0:43361aa78bb0 77 //STEP 5
stephanie_liu 0:43361aa78bb0 78 IN_3 = 1;
stephanie_liu 0:43361aa78bb0 79 EN_3 = 1;
stephanie_liu 0:43361aa78bb0 80 wait(delay);
stephanie_liu 0:43361aa78bb0 81 IN_3 = 0;
stephanie_liu 0:43361aa78bb0 82 EN_3 = 2;
stephanie_liu 0:43361aa78bb0 83
stephanie_liu 0:43361aa78bb0 84 //STEP 6
stephanie_liu 0:43361aa78bb0 85 IN_2 = 1;
stephanie_liu 0:43361aa78bb0 86 EN_2 = 0;
stephanie_liu 0:43361aa78bb0 87 wait(delay);
stephanie_liu 0:43361aa78bb0 88 IN_2 = 0;
stephanie_liu 0:43361aa78bb0 89 EN_2 = 2;
stephanie_liu 0:43361aa78bb0 90 }
stephanie_liu 0:43361aa78bb0 91 }
stephanie_liu 0:43361aa78bb0 92
stephanie_liu 0:43361aa78bb0 93 }