Problema que se resolvió: generar un programa que controle por el puerto serial el sentido de giro del motor nema, y el número de pasos. en este caso dejar la velocidad constante pero con un define .

Dependencies:   mbed

Committer:
CCastrop1012
Date:
Fri Sep 03 04:33:37 2021 +0000
Revision:
0:fb45b86a9e04
El programa controla el sentido de giro y velocidad de un motor paso a paso.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CCastrop1012 0:fb45b86a9e04 1 #include "mbed.h"
CCastrop1012 0:fb45b86a9e04 2
CCastrop1012 0:fb45b86a9e04 3 /*****************************************************************************
CCastrop1012 0:fb45b86a9e04 4 generar un programa que controle por el puerto serial el sentido de giro del motor nema,
CCastrop1012 0:fb45b86a9e04 5 y el número de pasos. en este caso dejar la velocidad constante pero con un define .
CCastrop1012 0:fb45b86a9e04 6
CCastrop1012 0:fb45b86a9e04 7 por medio de la comunicacion serial el comando es
CCastrop1012 0:fb45b86a9e04 8
CCastrop1012 0:fb45b86a9e04 9 | | | |
CCastrop1012 0:fb45b86a9e04 10 | INITCMD | sentido | N_pasos |
CCastrop1012 0:fb45b86a9e04 11 | 0xff | 0x00- 0x01 | 0x00 - 0xff |
CCastrop1012 0:fb45b86a9e04 12
CCastrop1012 0:fb45b86a9e04 13 para enviar los comandos usar el programa Coolterm http://freeware.the-meiers.org/
CCastrop1012 0:fb45b86a9e04 14
CCastrop1012 0:fb45b86a9e04 15 # para el motor nena se configurar el driver drv8825:
CCastrop1012 0:fb45b86a9e04 16 #
CCastrop1012 0:fb45b86a9e04 17 #
CCastrop1012 0:fb45b86a9e04 18 # <------period 4us min-------->
CCastrop1012 0:fb45b86a9e04 19 # <-1.9us min -> <-1.9us min ->
CCastrop1012 0:fb45b86a9e04 20 # _____________ _____________
CCastrop1012 0:fb45b86a9e04 21 # ____| |______________| |___________ signal step
CCastrop1012 0:fb45b86a9e04 22 # ____ ____
CCastrop1012 0:fb45b86a9e04 23 # _| |________________________| |____________________ dir modex 1-cw 0ccw
CCastrop1012 0:fb45b86a9e04 24 # <-> min 650ns <-> min 650ns
CCastrop1012 0:fb45b86a9e04 25 #
CCastrop1012 0:fb45b86a9e04 26 *****************************************************************************/
CCastrop1012 0:fb45b86a9e04 27
CCastrop1012 0:fb45b86a9e04 28
CCastrop1012 0:fb45b86a9e04 29 Serial command(USBTX, USBRX);
CCastrop1012 0:fb45b86a9e04 30 DigitalOut stepper_step(PB_4);
CCastrop1012 0:fb45b86a9e04 31 DigitalOut steppeer_dir(PB_5);
CCastrop1012 0:fb45b86a9e04 32
CCastrop1012 0:fb45b86a9e04 33 /*INGRESE LA CONFIGURACION DE LOS MOTORES*/
CCastrop1012 0:fb45b86a9e04 34
CCastrop1012 0:fb45b86a9e04 35 #define INITCMD 0xFF
CCastrop1012 0:fb45b86a9e04 36 #define VELOCITY 5 ///en micro segundos
CCastrop1012 0:fb45b86a9e04 37
CCastrop1012 0:fb45b86a9e04 38 // definición de las variables globales
CCastrop1012 0:fb45b86a9e04 39
CCastrop1012 0:fb45b86a9e04 40 uint8_t sentido_motor; // variable almacena el sentido de giro de motor en leer_datos()
CCastrop1012 0:fb45b86a9e04 41 uint8_t N_pasos; // varable almacena el numero de pasos que se mueve el motor en leer_datos()
CCastrop1012 0:fb45b86a9e04 42
CCastrop1012 0:fb45b86a9e04 43
CCastrop1012 0:fb45b86a9e04 44 // definición de las funciones
CCastrop1012 0:fb45b86a9e04 45 void setup_uart();
CCastrop1012 0:fb45b86a9e04 46
CCastrop1012 0:fb45b86a9e04 47 void mover_steper_nema(uint8_t sentido, uint8_t num_pasos);
CCastrop1012 0:fb45b86a9e04 48
CCastrop1012 0:fb45b86a9e04 49 void leer_datos();
CCastrop1012 0:fb45b86a9e04 50
CCastrop1012 0:fb45b86a9e04 51
CCastrop1012 0:fb45b86a9e04 52
CCastrop1012 0:fb45b86a9e04 53 int main() {
CCastrop1012 0:fb45b86a9e04 54
CCastrop1012 0:fb45b86a9e04 55 setup_uart();
CCastrop1012 0:fb45b86a9e04 56 //command.printf("inicio de programa");
CCastrop1012 0:fb45b86a9e04 57 while(1){
CCastrop1012 0:fb45b86a9e04 58 leer_datos();
CCastrop1012 0:fb45b86a9e04 59 mover_steper_nema(sentido_motor, N_pasos);
CCastrop1012 0:fb45b86a9e04 60 }
CCastrop1012 0:fb45b86a9e04 61 }
CCastrop1012 0:fb45b86a9e04 62
CCastrop1012 0:fb45b86a9e04 63
CCastrop1012 0:fb45b86a9e04 64
CCastrop1012 0:fb45b86a9e04 65 void setup_uart(){
CCastrop1012 0:fb45b86a9e04 66 command.baud(115200);
CCastrop1012 0:fb45b86a9e04 67 }
CCastrop1012 0:fb45b86a9e04 68
CCastrop1012 0:fb45b86a9e04 69
CCastrop1012 0:fb45b86a9e04 70
CCastrop1012 0:fb45b86a9e04 71 void leer_datos(){
CCastrop1012 0:fb45b86a9e04 72 while(command.getc()!= INITCMD);
CCastrop1012 0:fb45b86a9e04 73 sentido_motor=command.getc();
CCastrop1012 0:fb45b86a9e04 74 N_pasos=command.getc();
CCastrop1012 0:fb45b86a9e04 75
CCastrop1012 0:fb45b86a9e04 76 }
CCastrop1012 0:fb45b86a9e04 77
CCastrop1012 0:fb45b86a9e04 78 /*
CCastrop1012 0:fb45b86a9e04 79 void mover_steper_nema(uint8_t sentido, uint8_t num_pasos){
CCastrop1012 0:fb45b86a9e04 80
CCastrop1012 0:fb45b86a9e04 81 uint32_t dpulse=0;
CCastrop1012 0:fb45b86a9e04 82
CCastrop1012 0:fb45b86a9e04 83 /* complementar el código necesario */
CCastrop1012 0:fb45b86a9e04 84
CCastrop1012 0:fb45b86a9e04 85 if (sentido == 1)
CCastrop1012 0:fb45b86a9e04 86 {
CCastrop1012 0:fb45b86a9e04 87 steppeer_dir = 0;
CCastrop1012 0:fb45b86a9e04 88 } else if (sentido == 0)
CCastrop1012 0:fb45b86a9e04 89 {
CCastrop1012 0:fb45b86a9e04 90 steppeer_dir = 1;
CCastrop1012 0:fb45b86a9e04 91 }
CCastrop1012 0:fb45b86a9e04 92 /*esperar 650 nsegundo*/
CCastrop1012 0:fb45b86a9e04 93
CCastrop1012 0:fb45b86a9e04 94 /* complementar el código necesario */
CCastrop1012 0:fb45b86a9e04 95
CCastrop1012 0:fb45b86a9e04 96 while ( num_pasos != 0)
CCastrop1012 0:fb45b86a9e04 97 (
CCastrop1012 0:fb45b86a9e04 98 stepper_step = 1;
CCastrop1012 0:fb45b86a9e04 99 wait_us(VELOCITY);
CCastrop1012 0:fb45b86a9e04 100 stepper_step = 0;
CCastrop1012 0:fb45b86a9e04 101 wait_us(VELOCITY);
CCastrop1012 0:fb45b86a9e04 102 num_pasos --;
CCastrop1012 0:fb45b86a9e04 103 }
CCastrop1012 0:fb45b86a9e04 104
CCastrop1012 0:fb45b86a9e04 105
CCastrop1012 0:fb45b86a9e04 106 }