Marlon Daniel Quiroga / Mbed 2 deprecated Entrega2crte5

Dependencies:   mbed

Committer:
MarlonQ
Date:
Fri Apr 12 02:06:11 2019 +0000
Revision:
3:aa9cafc61475
Parent:
2:0cc71fa2f3fd
Entrega segundo corte;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fabeltranm 0:5e6b543c1179 1 #include "mbed.h"
fabeltranm 0:5e6b543c1179 2
fabeltranm 0:5e6b543c1179 3 /*****************************************************************************
fabeltranm 0:5e6b543c1179 4 generar un programa que controle por el puerto serial el sentido de giro del motor nema,
fabeltranm 0:5e6b543c1179 5 y el nímero de pasos. en este caso dejar la velocidad cosntantes pero con un define .
fabeltranm 0:5e6b543c1179 6
fabeltranm 0:5e6b543c1179 7 por medio de la comunicacion serial el comando es
fabeltranm 0:5e6b543c1179 8
fabeltranm 0:5e6b543c1179 9 | | | |
fabeltranm 0:5e6b543c1179 10 | INITCMD | sentido | N_pasos |
fabeltranm 0:5e6b543c1179 11 | 0xff | 0x00- 0x01 | 0x00 - 0xff |
fabeltranm 0:5e6b543c1179 12
fabeltranm 0:5e6b543c1179 13 para enviar los comandos usar el programa Coolterm http://freeware.the-meiers.org/
fabeltranm 0:5e6b543c1179 14
fabeltranm 0:5e6b543c1179 15 # para el motor nena se configurar el driver drv8825:
fabeltranm 0:5e6b543c1179 16 #
fabeltranm 0:5e6b543c1179 17 #
fabeltranm 0:5e6b543c1179 18 # <------period 4us min-------->
fabeltranm 0:5e6b543c1179 19 # <-1.9us min -> <-1.9us min ->
fabeltranm 0:5e6b543c1179 20 # _____________ _____________
fabeltranm 0:5e6b543c1179 21 # ____| |______________| |___________ signal step
fabeltranm 0:5e6b543c1179 22 # ____ ____
fabeltranm 0:5e6b543c1179 23 # _| |________________________| |____________________ dir modex 1-cw 0ccw
fabeltranm 0:5e6b543c1179 24 # <-> min 650ns <-> min 650ns
fabeltranm 0:5e6b543c1179 25 #
fabeltranm 0:5e6b543c1179 26 *****************************************************************************/
fabeltranm 0:5e6b543c1179 27
fabeltranm 0:5e6b543c1179 28
fabeltranm 0:5e6b543c1179 29 Serial command(USBTX, USBRX);
fabeltranm 0:5e6b543c1179 30 DigitalOut stepper_step(PB_4);
fabeltranm 0:5e6b543c1179 31 DigitalOut steppeer_dir(PB_5);
MarlonQ 3:aa9cafc61475 32 DigitalOut stepper_step2(PB_6);
MarlonQ 3:aa9cafc61475 33 DigitalOut steppeer_dir2(PB_7);
fabeltranm 0:5e6b543c1179 34
fabeltranm 0:5e6b543c1179 35 /*INGRESE LA CONFIGURACION DE LOS MOTORES*/
fabeltranm 0:5e6b543c1179 36
fabeltranm 0:5e6b543c1179 37 #define INITCMD 0xFF
MarlonQ 2:0cc71fa2f3fd 38
fabeltranm 0:5e6b543c1179 39
fabeltranm 0:5e6b543c1179 40 // definición de las variables globales
MarlonQ 3:aa9cafc61475 41 uint32_t VELOCITY = 2500 ; // Tiempo enmicro segundos
MarlonQ 2:0cc71fa2f3fd 42 uint8_t N_pasos ; // varable almacena el numero de pasos que se mueve el motor en leer_datos()
MarlonQ 2:0cc71fa2f3fd 43 uint8_t Coman ; //variable que nosindica el movimiento del motor
MarlonQ 3:aa9cafc61475 44 int8_t Lectura [ ] = {} ;
fabeltranm 0:5e6b543c1179 45 // definición de las funciones
fabeltranm 0:5e6b543c1179 46 void setup_uart();
fabeltranm 0:5e6b543c1179 47
MarlonQ 2:0cc71fa2f3fd 48 void mover_steper_nema( uint8_t num_pasos , uint8_t _Coman );
fabeltranm 0:5e6b543c1179 49
MarlonQ 2:0cc71fa2f3fd 50 void leer_datos();
fabeltranm 0:5e6b543c1179 51
fabeltranm 0:5e6b543c1179 52 int main() {
fabeltranm 0:5e6b543c1179 53
fabeltranm 0:5e6b543c1179 54 setup_uart();
fabeltranm 0:5e6b543c1179 55 //command.printf("inicio de programa");
fabeltranm 0:5e6b543c1179 56 while(1){
fabeltranm 0:5e6b543c1179 57 leer_datos();
MarlonQ 3:aa9cafc61475 58 mover_steper_nema( Lectura [ 1 ] , Lectura [ 0 ] );
fabeltranm 0:5e6b543c1179 59 }
fabeltranm 0:5e6b543c1179 60 }
fabeltranm 0:5e6b543c1179 61
fabeltranm 0:5e6b543c1179 62 void setup_uart(){
MarlonQ 2:0cc71fa2f3fd 63 command.baud( 115200 ) ;
fabeltranm 0:5e6b543c1179 64 }
fabeltranm 0:5e6b543c1179 65
fabeltranm 0:5e6b543c1179 66
fabeltranm 0:5e6b543c1179 67
fabeltranm 0:5e6b543c1179 68 void leer_datos(){
MarlonQ 3:aa9cafc61475 69
MarlonQ 2:0cc71fa2f3fd 70 while(command.getc()!= INITCMD) ;
MarlonQ 3:aa9cafc61475 71 uint8_t i ;
MarlonQ 3:aa9cafc61475 72 for ( i = 0 ; i < 2 ; i++){
MarlonQ 3:aa9cafc61475 73
MarlonQ 3:aa9cafc61475 74 Lectura [ i ] = command.getc();
MarlonQ 3:aa9cafc61475 75 printf ( " %4d ", Lectura [ i ]);
MarlonQ 3:aa9cafc61475 76 }
fabeltranm 0:5e6b543c1179 77
fabeltranm 0:5e6b543c1179 78 }
fabeltranm 0:5e6b543c1179 79
MarlonQ 2:0cc71fa2f3fd 80 void mover_steper_nema( uint8_t num_pasos , uint8_t Coman ){
fabeltranm 0:5e6b543c1179 81
MarlonQ 2:0cc71fa2f3fd 82 uint8_t i = 0;
MarlonQ 3:aa9cafc61475 83 uint8_t j = 1;
MarlonQ 3:aa9cafc61475 84
MarlonQ 3:aa9cafc61475 85 switch ( Coman ){
MarlonQ 2:0cc71fa2f3fd 86
MarlonQ 2:0cc71fa2f3fd 87 case 5:
MarlonQ 3:aa9cafc61475 88 steppeer_dir = 1 ;
MarlonQ 3:aa9cafc61475 89 steppeer_dir2 = 0 ;
MarlonQ 2:0cc71fa2f3fd 90 wait_us( 1 );
MarlonQ 2:0cc71fa2f3fd 91
MarlonQ 3:aa9cafc61475 92 for ( j ; j <= num_pasos ; j++){
MarlonQ 3:aa9cafc61475 93
MarlonQ 3:aa9cafc61475 94 for ( i= 0 ; i <= 200 ; i++ ){
MarlonQ 2:0cc71fa2f3fd 95
MarlonQ 3:aa9cafc61475 96 stepper_step = 1 ;
MarlonQ 3:aa9cafc61475 97 stepper_step2 = 1;
MarlonQ 3:aa9cafc61475 98 wait_us(VELOCITY);
MarlonQ 3:aa9cafc61475 99 stepper_step = 0;
MarlonQ 3:aa9cafc61475 100 stepper_step2 = 1;
MarlonQ 3:aa9cafc61475 101 wait_us(VELOCITY);
MarlonQ 2:0cc71fa2f3fd 102
MarlonQ 3:aa9cafc61475 103 }
MarlonQ 3:aa9cafc61475 104
MarlonQ 3:aa9cafc61475 105 }
MarlonQ 3:aa9cafc61475 106
MarlonQ 2:0cc71fa2f3fd 107 break;
MarlonQ 2:0cc71fa2f3fd 108
MarlonQ 2:0cc71fa2f3fd 109 case 6:
MarlonQ 2:0cc71fa2f3fd 110 steppeer_dir = 0;
MarlonQ 3:aa9cafc61475 111 steppeer_dir2 = 1;
MarlonQ 2:0cc71fa2f3fd 112 wait_us( 1 );
fabeltranm 0:5e6b543c1179 113
MarlonQ 3:aa9cafc61475 114 for ( j ; j <= num_pasos ; j++){
MarlonQ 3:aa9cafc61475 115
MarlonQ 3:aa9cafc61475 116 for ( i= 0 ; i <= 200 ; i++ ){
MarlonQ 2:0cc71fa2f3fd 117
MarlonQ 3:aa9cafc61475 118 stepper_step = 1 ;
MarlonQ 3:aa9cafc61475 119 stepper_step2 = 1;
MarlonQ 3:aa9cafc61475 120 wait_us(VELOCITY);
MarlonQ 3:aa9cafc61475 121 stepper_step = 0;
MarlonQ 3:aa9cafc61475 122 stepper_step2 = 1;
MarlonQ 3:aa9cafc61475 123 wait_us(VELOCITY);
MarlonQ 2:0cc71fa2f3fd 124
MarlonQ 3:aa9cafc61475 125 }
MarlonQ 3:aa9cafc61475 126
MarlonQ 3:aa9cafc61475 127 }
MarlonQ 3:aa9cafc61475 128
MarlonQ 2:0cc71fa2f3fd 129 break;
MarlonQ 2:0cc71fa2f3fd 130
MarlonQ 2:0cc71fa2f3fd 131 case 7:
MarlonQ 3:aa9cafc61475 132
MarlonQ 2:0cc71fa2f3fd 133 steppeer_dir = 1;
MarlonQ 2:0cc71fa2f3fd 134 wait_us( 1 );
MarlonQ 2:0cc71fa2f3fd 135
MarlonQ 3:aa9cafc61475 136 for ( j ; j <= num_pasos ; j++){
MarlonQ 3:aa9cafc61475 137
MarlonQ 3:aa9cafc61475 138 for ( i= 0 ; i <= 50 ; i++ ){
MarlonQ 2:0cc71fa2f3fd 139
MarlonQ 3:aa9cafc61475 140 stepper_step = 1 ;
MarlonQ 3:aa9cafc61475 141 stepper_step2 = 1;
MarlonQ 3:aa9cafc61475 142 wait_us(VELOCITY);
MarlonQ 3:aa9cafc61475 143 stepper_step = 0;
MarlonQ 3:aa9cafc61475 144 stepper_step2 = 1;
MarlonQ 3:aa9cafc61475 145 wait_us(VELOCITY);
MarlonQ 3:aa9cafc61475 146
MarlonQ 3:aa9cafc61475 147 }
MarlonQ 3:aa9cafc61475 148
MarlonQ 3:aa9cafc61475 149 }
MarlonQ 3:aa9cafc61475 150
MarlonQ 2:0cc71fa2f3fd 151 break;
MarlonQ 2:0cc71fa2f3fd 152
MarlonQ 2:0cc71fa2f3fd 153 case 8:
fabeltranm 0:5e6b543c1179 154 steppeer_dir = 0;
MarlonQ 2:0cc71fa2f3fd 155 wait_us( 1 );
MarlonQ 2:0cc71fa2f3fd 156
MarlonQ 3:aa9cafc61475 157 for ( j ; j <= num_pasos ; j++){
MarlonQ 3:aa9cafc61475 158
MarlonQ 3:aa9cafc61475 159 for ( i= 0 ; i <= 50 ; i++ ){
MarlonQ 2:0cc71fa2f3fd 160
MarlonQ 3:aa9cafc61475 161 stepper_step = 1 ;
MarlonQ 3:aa9cafc61475 162 stepper_step2 = 1;
MarlonQ 3:aa9cafc61475 163 wait_us(VELOCITY);
MarlonQ 3:aa9cafc61475 164 stepper_step = 0;
MarlonQ 3:aa9cafc61475 165 stepper_step2 = 1;
MarlonQ 3:aa9cafc61475 166 wait_us(VELOCITY);
MarlonQ 3:aa9cafc61475 167
MarlonQ 3:aa9cafc61475 168 }
MarlonQ 3:aa9cafc61475 169
MarlonQ 3:aa9cafc61475 170 }
MarlonQ 3:aa9cafc61475 171
MarlonQ 2:0cc71fa2f3fd 172 break;
MarlonQ 2:0cc71fa2f3fd 173
MarlonQ 2:0cc71fa2f3fd 174 case 9:
MarlonQ 3:aa9cafc61475 175
MarlonQ 3:aa9cafc61475 176 switch ( num_pasos ){
MarlonQ 3:aa9cafc61475 177
MarlonQ 3:aa9cafc61475 178 case 1:
MarlonQ 3:aa9cafc61475 179
MarlonQ 3:aa9cafc61475 180 VELOCITY = 400 ;
MarlonQ 3:aa9cafc61475 181
MarlonQ 3:aa9cafc61475 182 break;
MarlonQ 3:aa9cafc61475 183
MarlonQ 3:aa9cafc61475 184 case 2:
MarlonQ 3:aa9cafc61475 185
MarlonQ 3:aa9cafc61475 186 VELOCITY = 2500 ;
MarlonQ 3:aa9cafc61475 187
MarlonQ 3:aa9cafc61475 188 break;
MarlonQ 3:aa9cafc61475 189
MarlonQ 3:aa9cafc61475 190 case 3:
MarlonQ 3:aa9cafc61475 191
MarlonQ 3:aa9cafc61475 192 VELOCITY = 5000 ;
MarlonQ 3:aa9cafc61475 193
MarlonQ 3:aa9cafc61475 194 break;
MarlonQ 3:aa9cafc61475 195
MarlonQ 3:aa9cafc61475 196 }
MarlonQ 3:aa9cafc61475 197
MarlonQ 2:0cc71fa2f3fd 198 break;
MarlonQ 2:0cc71fa2f3fd 199 }
fabeltranm 0:5e6b543c1179 200 }