Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 4:97d107efa17e
- Parent:
- 3:95a3a1fbc807
- Child:
- 6:d6e4872d38fe
--- a/main.cpp Thu Jun 25 17:52:14 2020 +0000 +++ b/main.cpp Wed Oct 28 13:02:18 2020 +0000 @@ -1,78 +1,113 @@ #include "mbed.h" -#include "L6230_BLDC.h" -#include "reScale.h" + +//---Pinos de enable dos braços---- +DigitalOut enable_ch1(PC_10); +DigitalOut enable_ch2(PC_11); +DigitalOut enable_ch3(PC_12); -L6230_BLDC motor(PC_10, PC_11, PC_12, PA_8, PA_9, PA_10); +//---Pinos de canais dos braços---- +PwmOut in_ch1(PA_8); +PwmOut in_ch2(PA_9); +PwmOut in_ch3(PA_10); -AnalogIn feedback_phase_A(PA_0); -AnalogIn feedback_phase_B(PC_1); -AnalogIn feedback_phase_C(PC_0); - +//---Pino de ADC da X-NUCLEO ---- AnalogIn pot(PB_1); +//---Pino do led da NUCLEO---- DigitalOut myled(LED1); +//---Pino do butao da NUCLEO---- DigitalIn sw(PC_13); +//---Pinos do serial-USB da NUCLEO---- Serial pc(USBTX, USBRX); -reScale _scale(0, 4095, 800, 5000); - -float read_current(char); - -int prevStep; +//---Prototipo das Funções---- +void motor_start(float SPEED, unsigned int MOTOR); +void motor_stop(unsigned int MOTOR); -long speed; -long adc; - -bool start = 0; - +//---Programa principal------- int main() { + //---Configura o periodo do PWM---- + in_ch1.period_ms(1); + in_ch2.period_ms(1); + in_ch3.period_ms(1); + + //---Inicia o PWM com 0% de duty-cicle---- + in_ch1.write(0); + in_ch2.write(0); + in_ch3.write(0); + + //---Inicia os braços em Z (alta impedancia)---- + enable_ch1 = 2; + enable_ch2 = 2; + enable_ch3 = 2; + + //---Configura o baudrate da serial para 115200--- pc.baud(115200); - pc.printf("Started\r"); + pc.printf("Started\r"); //Iniciando while(1) { - if((sw == 0) && (start == 0)) - { - start = 1; - wait(1); - } - - if((sw == 0) && (start == 1)) - { - start = 0; - wait(1); - } - - if(start == 1) - { - adc = pot.read() * 4096; - speed =_scale.from(adc); - //pc.printf("%i\r",speed); - motor.one_step(1, speed, &prevStep); - } - else - { - motor.stop(); - } + motor_start(0.5, 1); //Liga o motor 1 com 50% de duty-cicle + wait_ms(500); + motor_start(0.5, 2); //Liga o motor 2 com 50% de duty-cicle + wait_ms(500); + motor_start(0.5, 3); //Liga o motor 3 com 50% de duty-cicle + wait_ms(500); + + motor_stop(1); //Desliga o motor 1 + wait_ms(500); + motor_stop(2); //Desliga o motor 2 + wait_ms(500); + motor_stop(3); //Desliga o motor 3 + wait_ms(500); } } -float read_current(char PHASE) +//---Função de controle dos braços---------- +void motor_start(float SPEED, unsigned int MOTOR) { - float ADC_VALUE; + if(MOTOR == 1) + { + enable_ch1 = 1; + in_ch1.write(SPEED); + } - switch (PHASE) + if(MOTOR == 2) { - case 1: ADC_VALUE = feedback_phase_A.read() * 3.3f; break; //convert to volts - case 2: ADC_VALUE = feedback_phase_B.read() * 3.3f; break; //convert to volts - case 3: ADC_VALUE = feedback_phase_C.read() * 3.3f; break; //convert to volts + enable_ch2 = 1; + in_ch2.write(SPEED); } - float CURRENT = ADC_VALUE / 0.33f; // U = R*I, I = U/R (R = 330mOhm) + if(MOTOR == 3) + { + enable_ch3 = 1; + in_ch3.write(SPEED); + } +} + +//---Função de desligamento dos braços---------- +void motor_stop(unsigned int MOTOR) +{ + if(MOTOR == 1) + { + enable_ch1 = 2; //alta impedancia - Z + in_ch1.write(0); + } - return CURRENT; -} \ No newline at end of file + if(MOTOR == 2) + { + enable_ch2 = 2; //alta impedancia - Z + in_ch2.write(0); + } + + if(MOTOR == 3) + { + enable_ch3 = 2; //alta impedancia - Z + in_ch3.write(0); + } +} +