ejemplo 2

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
DANIELOM1916
Date:
Mon Oct 28 01:45:14 2019 +0000
Commit message:
ejemplo

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
memory.cpp Show annotated file Show diff for this revision Revisions of this file
memory.h Show annotated file Show diff for this revision Revisions of this file
stepmotor.cpp Show annotated file Show diff for this revision Revisions of this file
stepmotor.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Oct 28 01:45:14 2019 +0000
@@ -0,0 +1,171 @@
+#include "mbed.h"
+#include "main.h"
+
+/*
+El sitema tiene tres estados:
+1. Stand by: Estado que indica que esta en espera de un telecomando (Ejecutar o salvar)
+2. Ejecutar: Estado en donde el sistema ejecuta las ordenes guardadas en el array 
+             de memoria hasta encontrar el comando CM_STOP 
+3. Salvar:   Estado donde el sistema recibe los datos y los almacena en memoria acorde 
+             a los comandos establecidos y sale de este modo cuando se recibe el 
+             comando CM_STOP
+            
+todo telecomando debe finalizar con el dato CM_END
+*/
+DigitalOut activador = (D3);
+Serial command(USBTX, USBRX);
+
+int main() {
+    init_serial();
+    activador=1;        
+    debug_m("\nPROGRAMA SELECTORA DE CAJAS \n");
+    debug_m("\nEsperando comando... \n");
+    uint32_t read_cc;
+    while(1)
+    {
+        read_cc=read_command();
+        switch (read_cc) {
+            case  CM_EJECUTAR: locating(); break;
+            case  CM_SALVAR: saving(); break;
+            
+            default: debug_m("Error de comando. \nSe espera  0xFEF0 o 0xFFF0 \n");break ;      
+        }
+    }
+    
+}
+
+
+uint32_t read_command()  //Concatenar datos
+{
+   /* retorna los byte recibidos concatenados en un entero, se reciben maximo 4 bytes,
+    recibe hasta que encuetra  un fin de comando "CM_END".
+    Ejemplo: para el comando drawing se 
+    espera un byte,  por lo que al recibir 0xFF y 0xF0 la funcióm  retorna el 0xff
+    siempre y cuando se reciba el fin de dato  F0 de lo contrario retorna un cero
+    para el caso del comando vertex2d se espera  recibir 3 bytes, 1 del comando 
+    y dos bytes para x y y, se retorna la concatenación de los tres bytes  siempre y
+    cuando el cuarto byte sea CM_END de lo contrario retorna un cero 
+    */
+    
+    uint32_t val=0;
+    uint8_t  cnt=0;
+    
+    char endc=command.getc();
+    
+    while(endc != CM_END && cnt <4) {
+    if(endc!=CM_END)   
+        val=((val<<8) +endc);
+     endc=command.getc();
+     cnt++;
+    }   
+    if(endc==CM_END)   
+        return val;
+    return 0; 
+}
+
+void init_serial()
+{
+    command.baud(9600);    
+}
+
+void locating(){  /* La funcion se ejecuta siempre y cuando exista datos validos para 
+                     leer de memoria */ 
+    debug_m("\nSe esta efectuando la accion indicada... \n");       
+     
+    uint8_t error=0;
+    MEM_TYPE dato;
+
+    tail_reset();  //borra la cabeza de memoria
+
+    while(error==0){
+        error = mem_get(&dato);
+        if (error==0) {
+            
+            int y      = (uint8_t)(dato);
+            int x      = (uint8_t)(dato>>8);
+            char ncomm  = (uint8_t)(dato>>16);
+                    
+            switch (ncomm) {
+            case  CM_INGRESO: 
+                    debug_m("\n-> Efectuando ingreso de la pieza\n");
+                    
+                    activador=0;
+                    debug_m("\n-> ubicando ingreso\n");
+                    vertey2d (2,0);
+                    debug_m("\n-> adelante z\n");
+                    adelantez();
+                    onelec();
+                    debug_m("\n-> atras z\n");
+                    atrasz();
+                    debug_m("\n-> ubicando ficha cuadrante\n");
+                    matriz (x,y);
+                    debug_m("\n-> adelante z\n");
+                    adelantez();
+                    offelec();
+                    debug_m("\n-> atras z\n");
+                    atrasz();
+                    debug_m("\n-> retorno ingreso\n");
+                    matriz (x,0);  
+                    wait_ms (2000);
+                    debug_m("\n-> retorno home\n");
+                    vertey2d (2,1);
+                    activador=1;
+                    
+                    break;  
+                       
+            case  CM_EXTRACCION: 
+                    debug_m("\n-> Efectuando extraccion de la pieza\n");
+                    
+                    activador=0;
+                    debug_m("\n-> ubicando home\n");
+                    vertey2d (2,0);
+                    debug_m("\n-> ubicando ficha cuadrante\n");
+                    matriz (x,y);
+                    debug_m("\n-> adelante z\n");
+                    adelantez();
+                    onelec();
+                    debug_m("\n-> atras z\n");
+                    atrasz();
+                    debug_m("\n-> retorno ingreso\n");
+                    matriz (x,0);  
+                    wait_ms (2000);
+                    debug_m("\n-> ubicando caja en extraccion\n");
+                    vertey2d (1,1); 
+                    debug_m("\n-> adelante z\n");
+                    debug_m("\n-> adelante z\n");
+                    adelantez();
+                    offelec();
+                    debug_m("\n-> atras z\n");
+                    atrasz();
+                    debug_m("\n-> retorno home\n");
+                    vertey2d (1,1);
+                    activador=1;
+                    
+                    break ; 
+            default: 
+                   debug_m("\n-> Ejecutando programa en memoria...");
+                   break;   
+             }
+         }        
+    }  
+    debug_m("\nFin del comando ..\n");    
+}
+
+void saving(){
+    debug_m("\nGuardando los datos digitados...\n");    
+    
+    MEM_TYPE dato=0;
+    mem_free();
+    while(dato !=CM_STOP){
+        dato = read_command();
+        if (dato !=CM_STOP)
+               mem_put(dato);
+    }  
+    debug_m("Fin del comando guardar.\n");    
+}
+
+void debug_m(char *s , ... ){
+    #if DEBUG
+    command.printf(s);
+    #endif  
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Mon Oct 28 01:45:14 2019 +0000
@@ -0,0 +1,47 @@
+#ifndef MAIN_H   
+#define MAIN_H  
+
+#include "stepmotor.h"
+#include "memory.h"
+
+/******************************************************************************
+ Desarrollado por Andres Guio, Daniel Orjuela y John Rubio modificado para uso 
+ estudiantil, agradeciendo al profesor Ferney Beltran y respetando derechos de 
+ autor por estudiantes de ing. mecatronica.
+*******************************************************************************
+
+*******************************************************************************
+  COMANDOS
+  |POS_0|POS_1|POS_2| POS_3 | 
+  | #C  |  a  |  b  |   c   |
+
+  #C        ->  Indica el comando FF, FE, ...
+   a        ->  es el fin de comando F0 en otros casos es el valor de la coord X
+   b        ->  Coordenada Y
+   c        ->  fin de comando F0
+
+// Nota: El fin  de comando no se almacena 
+********************************************************************************/
+
+
+#define DEBUG 1
+
+
+void debug_m(char *s , ... );
+uint32_t read_command();
+void init_serial();
+void locating();
+void saving();
+
+
+/********************* PARAMETROS PARA DEFINIR  EL  COMANDO ******************/
+
+#define CM_EJECUTAR       0xff
+#define CM_SALVAR         0xfe
+#define CM_INGRESO        0xfd
+#define CM_EXTRACCION     0xfc
+#define CM_STOP           0xfa
+#define CM_END            0xf0
+
+
+#endif //  MAIN_H 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Oct 28 01:45:14 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/994bdf8177cb
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/memory.cpp	Mon Oct 28 01:45:14 2019 +0000
@@ -0,0 +1,43 @@
+#include "mbed.h"
+#include "memory.h"
+
+int mem_head = 0;  //cabeza de memoria
+int mem_tail = 0;  //cabeza de cola
+uint8_t full = 0;
+
+MEM_TYPE buffer[MEM_SIZE]; // reserve memoria tamaño 10
+
+void tail_reset()  // reinicio el valor de la cola en 0
+{
+ mem_tail=0;
+}
+
+void mem_free()  // reinicio el valor de la cabeza y full a 0
+{
+ mem_head=0;
+ full=0;
+}
+
+uint8_t mem_put(MEM_TYPE data) // poner dato en memoria "Escribir dato"
+{
+     if (full)
+        return 1;
+    buffer[mem_head] = data; // envio dato a grabar 
+    mem_head += 1;           // aumente la cabeza en 1
+    if (mem_head == MEM_SIZE)  // envio error memoria llena si cabeza es igual a 10(tamaño total de la memoria)
+        full =1;
+    return 0;
+}
+
+uint8_t mem_get(MEM_TYPE* data) // leer dato en memoria
+{
+    if (mem_head == 0) // envio error no hay nada en memoria
+        return 1; 
+    if (mem_head == mem_tail) // envio error memoria llena
+        return 1; 
+    
+    *data = buffer[mem_tail];  // apuntador selecciona memoria a leer
+    mem_tail += 1;            //
+  
+    return 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/memory.h	Mon Oct 28 01:45:14 2019 +0000
@@ -0,0 +1,18 @@
+#ifndef MEMORY_ARRAY_H   
+#define MEMORY_ARRAY_H  
+
+#include "mbed.h"
+
+#define MEM_SIZE 10
+#define MEM_TYPE uint32_t
+
+
+
+void tail_reset();
+void  mem_free();
+uint8_t mem_put(MEM_TYPE data);
+uint8_t mem_get(MEM_TYPE* data);
+
+
+
+#endif //  MEMORY_H 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stepmotor.cpp	Mon Oct 28 01:45:14 2019 +0000
@@ -0,0 +1,167 @@
+#include "stepmotor.h"
+#include "mbed.h"
+#include "math.h"
+
+DigitalOut motorx_paso =    (D4);
+DigitalOut motorx_direc =   (D5);
+
+DigitalOut motory_paso =    (D6);
+DigitalOut motory_direc =   (D7);
+
+DigitalOut motorz_paso =    (D8);
+DigitalOut motorz_direc =   (D9); 
+
+DigitalOut electroiman =    (D10);
+
+int t = 6250;
+
+void matriz (uint8_t x, uint8_t y){
+    
+    int cuadrante = x;
+   // int y=1;
+    
+    switch(cuadrante)
+        {  
+            case 1: vertex2d (4,y);
+                    vertey2d (3,y);
+                    break; 
+            case 2: vertex2d (3,y);
+                    vertey2d (3,y);
+                    break; 
+            case 3: vertex2d (2,y);
+                    vertey2d (3,y);
+                    break; 
+            case 4: vertex2d (1,y);
+                    vertey2d (3,y);
+                    break; 
+            case 5: vertex2d (4,y);
+                    vertey2d (2,y);
+                    break; 
+            case 6: vertex2d (3,y);
+                    vertey2d (2,y);
+                    break; 
+            case 7: vertex2d (2,y);
+                    vertey2d (2,y);
+                    break; 
+            case 8: vertex2d (1,y);
+                    vertey2d (2,y);
+                    break; 
+            case 9: vertex2d (4,y);
+                    vertey2d (1,y);
+                    break; 
+            case 10: vertex2d (3,y);
+                     vertey2d (1,y);
+                     break; 
+            case 11: vertex2d (2,y);
+                     vertey2d (1,y);
+                     break; 
+            case 12: vertex2d (1,y);
+                     vertey2d (1,y);
+                     break; 
+            case 13: vertex2d (4,y);
+                     vertey2d (0,y);
+                     break; 
+            case 14: vertex2d (3,y);
+                     vertey2d (0,y);
+                     break; 
+            case 15: vertex2d (2,y);
+                     vertey2d (0,y);
+                     break; 
+            case 16: vertex2d (1,y);
+                     vertey2d (0,y);
+                     break; 
+                                   
+            default: break;
+        }
+
+}
+
+void vertex2d(uint8_t x, uint8_t y){   // movimiento en eje x
+    
+    int s = y;
+    motorx_direc = s;
+   
+    int a=0; 
+    a=1375*x;
+  
+    for (int i=0; a>=i; i++){
+         motorx_paso = 1;
+         wait_us (t);
+         motorx_paso = 0;
+         wait_us (t);
+     }
+}
+
+void vertey2d(uint8_t x, uint8_t y){      // movimiento en eje y
+    
+    int s = y;
+    motory_direc = s;
+   
+    int a=0; 
+    a=1375*x;
+    
+    for (int i=0; a>=i; i++){
+         motory_paso = 1;
+         wait_us (t);
+         motory_paso = 0;
+         wait_us (t);         
+        } 
+}
+
+void adelantez(){
+          
+    motorz_direc = 1;
+    
+    int a=1305; //movimimiento de 32 mm
+        
+    for (int i=0; a>=i; i++){
+         motorz_paso = 1;
+         wait_us (t);
+         motorz_paso = 0;
+         wait_us (t);
+        }
+}
+
+void atrasz(){
+    
+    motorz_direc = 0;
+   
+    int a=1305; //movimimiento de 32 mm
+        
+    for (int i=0; a>=i; i++){
+         motorz_paso = 1;
+         wait_us (t);
+         motorz_paso = 0;
+         wait_us (t);
+        }
+}
+
+void onelec(){  
+    wait_ms (2000);
+    motory_direc = 1;
+   
+    int a=160; 
+      
+    for (int i=0; a>=i; i++){
+         motory_paso = 1;
+         wait_us (t);
+         motory_paso = 0;
+         wait_us (t);         
+        } 
+}
+
+void offelec(){
+    wait_ms (2000);
+    motory_direc = 0;
+   
+    int a=160; 
+      
+    for (int i=0; a>=i; i++){
+         motory_paso = 1;
+         wait_us (t);
+         motory_paso = 0;
+         wait_us (t);         
+        } 
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stepmotor.h	Mon Oct 28 01:45:14 2019 +0000
@@ -0,0 +1,15 @@
+#include "mbed.h"
+
+#ifndef STEPMOTOR_H   
+#define STEPMOTOR_H  
+
+void matriz (uint8_t x, uint8_t y);
+
+void onelec();
+void offelec();
+void adelantez();
+void atrasz();
+void vertex2d(uint8_t x, uint8_t y);
+void vertey2d(uint8_t x, uint8_t y);
+
+#endif    //  STEPMOTOR_H 
\ No newline at end of file