Funciones: Ejecutar Guadar Vertex2D Draw No-draw Stop End

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
JuanCamilo93
Date:
Wed Mar 14 00:43:30 2018 +0000
Parent:
1:1e86d6321238
Commit message:
Corte 1

Changed in this revision

ClasesConfiguracion/Dibujar.cpp Show diff for this revision Revisions of this file
ClasesConfiguracion/Dibujar.h Show diff for this revision Revisions of this file
ClasesConfiguracion/Memoria.cpp Show diff for this revision Revisions of this file
Comunicacion.cpp Show annotated file Show diff for this revision Revisions of this file
Comunicacion.h Show annotated file Show diff for this revision Revisions of this file
ControlMemoria.cpp Show annotated file Show diff for this revision Revisions of this file
ControlMemoria.h Show annotated file Show diff for this revision Revisions of this file
Dibujar.cpp Show annotated file Show diff for this revision Revisions of this file
Dibujar.h Show annotated file Show diff for this revision Revisions of this file
--- a/ClasesConfiguracion/Dibujar.cpp	Tue Mar 13 02:12:53 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,79 +0,0 @@
-#include "Dibujar.h"
-#include "mbed.h"
-#include "math.h"
-
-PwmOut myServoX(PB_3);
-PwmOut myServoY(PB_4);
-PwmOut myServoZ(PB_5);
-
-uint8_t  posx_old=0;     // posición anterior del eje X
-uint8_t  posy_old=0;     // posición anterior del eje Y
-uint8_t  ss_time=100;     // tiempo  de espera para moverse 1 mm en microsegundos
-
-void put_sstime(uint8_t vtime){
-    ss_time=vtime;
-    
-}
-
-int coord2us(float coord)
-{
-    if(0 <= coord <= MAXPOS)
-        return int(750+coord*1900/50);// u6
-    return 750;
-
-}
-
-void sstime(uint8_t x, uint8_t y)
-{
-    double dx=abs(x-posx_old);
-    double dy=abs(y-posy_old);
-    double dist= sqrt(dx*dx+dy*dy);
-    wait_ms((int)(ss_time*dist));
-    posx_old =x;
-    posy_old=y;
-    
- }
- 
-void draw(){
-myServoZ.pulsewidth_us(POSDRAW);
-wait_ms(ss_time*2);
-}
-
-void nodraw(){
-myServoZ.pulsewidth_us(MAXPOS);
-wait_ms(ss_time*2);
-}
-
-
-void vertex2d(uint8_t x, uint8_t y){
-
-    int pulseX = coord2us(x);
-    int pulseY = coord2us(y);
-    
-    myServoX.pulsewidth_us(pulseX);
-    myServoY.pulsewidth_us(pulseY);
-   
-    sstime(x,y); 
-
-}
-
-void initdraw(float x, float y){
-    vertex2d (x,y);
-    draw();
-}
-
-void home(){
-    nodraw();
-    vertex2d(0 ,0);
-}    
-
-
-
-void init_servo()
-{
-   myServoX.period_ms(20);
-   myServoY.period_ms(20);
-   myServoZ.period_ms(20);
-
-    
-}
\ No newline at end of file
--- a/ClasesConfiguracion/Dibujar.h	Tue Mar 13 02:12:53 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,19 +0,0 @@
-#ifndef DRAW_H   
-#define DRAW_H  
-
-#include "mbed.h"
-
-#define MAXPOS 50       // en milimetros
-#define POSDRAW 10   
-
-
-void init_servo();
-void draw();
-void nodraw();
-void vertex2d(uint8_t x, uint8_t y);
-
-void home();
-void initdraw(float x, float y);
-void put_sstime(uint8_t vtime);
-
-#endif //  DRAW_H 
\ No newline at end of file
--- a/ClasesConfiguracion/Memoria.cpp	Tue Mar 13 02:12:53 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,53 +0,0 @@
-#include "mbed.h"
- 
-#define MEM_SIZE 10
-#define MEM_TYPE uint32_t
-int mem_head = 0;
-int mem_tail = 0;
-uint8_t full = 0;
- 
-MEM_TYPE buffer[MEM_SIZE];
-Serial command(USBTX, USBRX); 
- 
-uint32_t mem_free()
-{
- mem_head=0;
- full=0;
-}
- 
-uint32_t mem_put(MEM_TYPE data)
-{
- 
-    if (full)
-        return 1;
-    buffer[mem_head] = data;
-    mem_head += 1;
-    if (mem_head == MEM_SIZE)
-        full =1;
-    return 0;
-}
-uint32_t mem_get(MEM_TYPE* data)
-{
-    if (mem_head == 0)
-        return 1; 
-    if (mem_head == mem_tail)
-        return 1; 
-    
- 
-    *data = buffer[mem_tail];
-    mem_tail += 1;
-  
-    return 0;
-}
-
- /*
-int main(){
-    
-    mem_put(command.getc());
-    mem_put(command.getc());
-    mem_put(command.getc());
-    mem_put(command.getc());
-    MEM_TYPE val;
-    mem_get(&val);
-    command.putc(val);
-    }*/
\ No newline at end of file
--- a/Comunicacion.cpp	Tue Mar 13 02:12:53 2018 +0000
+++ b/Comunicacion.cpp	Wed Mar 14 00:43:30 2018 +0000
@@ -1,6 +1,12 @@
+/*
+Código para controlar la comunicación entre la tarjeta y el pc a través de terminal
+*/
 #include "mbed.h"
-
+//Se incluye la clase dibujar
 #include "Dibujar.h"
+//Se incluye la clase para el control de la memoria
+#include "ControlMemoria.h"
+//Se establecen los valores de los diferentes comandos principales
 #define CM_EJECUTAR 0xff
 #define CM_GUARDAR 0xfe
 #define CM_VERTEX2D 0xfd
@@ -9,90 +15,194 @@
 #define CM_STOP 0xfa
 #define CM_END 0xf0
 
+//Variable para establecer la comunicación USB
 Serial command(USBTX, USBRX);
 
-void ejecutar(){
-    command.printf("se esta ejecutando el dibujo...");    
+//Variable para poder llevar el conteo de las líneas de código correctas
+int CantLineasCorr=0;
+
+//Función encargada de ejecutar todo el código
+void ejecutar()
+{
+    command.printf("Dibujando...");
+    uint32_t *full = 0;
+
+    if(CantLineasCorr>=1) {
+        command.printf("Se inicia dibujo");
+    } else {
+        command.printf("No se han encontrado lineas de comando");
+    }
+
+    for(int i=0; i<=CantLineasCorr; i++) {
+
+        //char Linea= mem_get(full);
+        command.printf("Dibujando");
+
+    }
+
     // ubicar acà el codigo
 }
 
+//Función encargada de empezar a guardar los comandos dentro de la pila
+void guardar()
+{
+    command.printf("Se inicia el comado de guardar..");
+    // ubicar acà el codigo
 
-void guardar(){
-    command.printf("se inicia el comado de guardar..");    
-    // ubicar acà el codigo
-    
 }
 
-
+//Inicio de comunicación serial a 9600
 void init_serial()
 {
-    command.baud(9600);    
+    command.baud(9600);
 }
 
-//int intTipoProceso=0;
+//Variables para determinar los valores iniciales de las posiciones X y Y
+//Se establecen en -1 para saber cuando estas están cargadas con los valores reales
+int intPosX=-1;
+int intPosY=-1;
+//Variable para identificar si la función que está siendo llamada es Vertex
+bool VertexOn=false;
 
-void RecibirDatos(){
+//Variable para almacenar una línea de comandos;
+char LineaCodigo;
+//Variable para determinar si la línea tiene el formato correcto
+bool LineaCorrecta=false;
 
+
+//Función para recibir los datos enviados desde el terminal
+void RecibirDatos()
+{
+    //Se establece en un estado=1
     int Estado=1;
 
-    while(Estado==1){
+    //Mientras que el estado sea = 1 la tarjeta va a recibir los datos de la terminal
+    while(Estado==1) {
 
         init_servo();
         init_serial();
         home();
+
+        //Variable para almacenar la información en Hexadecimal desde terminal
         char read_cc;
-    
-        while(1)
-        {
-            
-            read_cc=command.getc();
-            /*switch (read_cc) {
-                case  CM_EJECUTAR: 
-                        ejecutar(); 
-                        command.printf("ejecutar");
-                        break;
-                         
-                case  CM_GUARDAR: 
-                        guardar(); 
-                        command.printf("guardar");
-                        break ;
-                        
-                case  CM_DRAW: 
-                        draw(); 
-                        command.printf("dibujar");
-                        break ;
-                        
-                case  CM_NODRAW: 
-                        nodraw(); 
-                        command.printf("Detener dibujo");
-                        break ;
-                //default: command.printf("error de comando");break ;      
-            }*/
-            /*
-           if(read_cc==CM_DRAW || intTipoProceso==1 ){
-            
-                if(intTipoProceso==1)
-                {
-                
-                    intTipoProceso=1;
-                    int cantDatos=0;
-                    command.printf(read_cc + "");
-                    
-                    while (cantDatos<=3){
-                    
-                        cantDatos+=1;
-                        
+
+        //El while=1 garantiza que todo el tiempo la tarjeta está recibiendo información
+        while(1) {
+
+            //se valida cuantas líneas correctas se han enviado
+            if(CantLineasCorr<=10) {
+
+                //Se almacena el dato en la variable read_cc
+                read_cc=command.getc();
+
+                //Si el parámetro enviado es 0xff se invoca la función ejecutar
+                if(read_cc==CM_EJECUTAR) {
+                    //Se invoca el comando ejecutar para empezar a dibujar
+                    ejecutar();
+                }
+
+                //Si el parámetro enviado es 0xfe se invoca la función guardar
+                if(read_cc==CM_GUARDAR) {
+                    //Se hace el llamado a la función guardar
+                    guardar();
+                }
+
+                //Si el parámetro enviado es 0xfd se invoca la función Vertex2D
+                if(read_cc==CM_VERTEX2D || VertexOn) {
+
+                    //Se establece la variable VertexOn=true para poder recibir los valores en X y Y
+                    VertexOn=true;
+
+                    /*
+
+                        IMPORTANTE PARA LA EXPLICACIÓN AL PROFESOR:
+
+                        La validación del número se hace para evitar que lleguen parámetros no deseados a la función vertex,
+                        con un solo valor que llegue y no sea númerico puede generar fallos en la función Vertex
+
+                        Casos de error:
+                        Vertex2d(6,k);
+                        Vertex2d(k,6);
+
+                        Caso correcto:
+                        Vertex2d(5,5);
+
+                        La razón por la que X y Y están con un if y un elseif es porque si estuvieran en if separados,
+                        se podría almacenar 2 valores en X y Y quedaría =-1
+
+                    */
+
+
+                    //Se valida que el valor que está siendo ingresado sea un número
+                    if(read_cc>=0x30 && read_cc<=0x39) {
+
+                        //se realiza la resta de -48 al valor enviado para poder obtener el valor decimal exacto
+                        double Coord=(read_cc-48);
+
+                        //Se valida si la posición en X ya fue asignada
+                        if(intPosX==-1) {
+                            //Se asigna el valor en la posición X
+                            intPosX=Coord;
+                            //Se valida si la posición en Y ya fue asignada
+                        } else if(intPosY==-1) {
+                            //Se asigna el valor en la posición Y
+                            intPosY=Coord;
+                            //Se establece vertex en false para poder dar continuidad a la siguiente línea de código
+                            VertexOn=false;
+
+                            LineaCorrecta=true;
+                            LineaCodigo='FD ' +  intPosX + ',' + intPosY;
+
+                        }
+                    } else {
+                        //Se establece que la línea enviada no es correcta y se ignora el envío a la pila
+                        LineaCorrecta=true;
+                        //Si el valor enviado no es numérico el paquete debe descartarse
+                        VertexOn=false;
                     }
-                    
-                    
-                }else{
-                    intTipoProceso=1;
+
+                }
+
+                //Si el parámetro enviado es 0xfc se invoca la función DRAW
+                if(read_cc==CM_DRAW) {
+                    //Invocar la función Draw
+                    //Se establece que la línea está correcta y se almacena el valor en la línea de código
+                    LineaCorrecta=true;
+                    LineaCodigo='DRAW';
+                }
+
+                //Si el parámetro enviado es 0xfb se invoca la función No draw
+                if(read_cc==CM_NODRAW) {
+                    //Invocar el función no draw
+                    //Se establece que la línea está correcta y se almacena el valor en la línea de código
+                    LineaCorrecta=true;
+                    LineaCodigo='NO_DRAW';
                 }
-            intTipoProceso
-            }*/
-            
-            
+
+                //Si el parámetro enviado es 0xfa se invoca la función STOP
+                if(read_cc==CM_STOP) {
+                    //Invocar el función STOP
+                    LineaCorrecta=true;
+                    LineaCodigo='NO_DRAW';
+                }
+
+                //Si el parámetro enviado es 0xf0 se toma como fin de instrucción
+                //CUANDO EXPLIQUEN ESTO AL PROFESOR DEFINAN EL F0 COMO AL EQUIVALENTE A UN ;
+                if(read_cc==CM_END) {
+
+                    if( LineaCorrecta) {
+
+                        //Se aumenta el valor de las líneas correctas que se han enviado
+                        CantLineasCorr+=1;
+                        //Se envía la línea a la pila
+                        mem_set(LineaCodigo);
+                    }
+                }
+            } else {
+
+                command.printf("Se ha alcanzado el máximo de líneas");
+
+            }
         }
     }
 }
-
--- a/Comunicacion.h	Tue Mar 13 02:12:53 2018 +0000
+++ b/Comunicacion.h	Wed Mar 14 00:43:30 2018 +0000
@@ -1,6 +1,5 @@
 #ifndef DRAW_H   
 #define DRAW_H  
-
 #include "mbed.h"
 
 void ejecutar();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ControlMemoria.cpp	Wed Mar 14 00:43:30 2018 +0000
@@ -0,0 +1,53 @@
+#include "mbed.h"
+#define MEM_SIZE 10
+#define MEM_TYPE uint32_t
+int mem_head = 0;
+int mem_tail = 0;
+uint32_t full = 0;
+ 
+MEM_TYPE memBuffer[MEM_SIZE];
+//Serial command(USBTX, USBRX); 
+ 
+uint32_t mem_free()
+{
+ mem_head=0;
+ full=0;
+}
+ 
+uint32_t mem_set(MEM_TYPE data)
+{
+ 
+    if (full)
+        return 1;
+    memBuffer[mem_head] = data;
+    mem_head += 1;
+    if (mem_head == MEM_SIZE)
+        full =1;
+    return 0;
+}
+
+uint32_t mem_get(MEM_TYPE* data)
+{
+    if (mem_head == 0)
+        return 1; 
+    if (mem_head == mem_tail)
+        return 1; 
+    
+    *data = memBuffer[mem_tail];
+    mem_tail += 1;
+  
+    return 0;
+}
+ 
+/*
+int main(){
+    
+    mem_put(command.getc());
+    mem_put(command.getc());
+    mem_put(command.getc());
+    mem_put(command.getc());
+    MEM_TYPE val;
+    mem_get(&val);
+    command.putc(val);
+    
+}*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ControlMemoria.h	Wed Mar 14 00:43:30 2018 +0000
@@ -0,0 +1,7 @@
+#include "mbed.h"
+#define MEM_SIZE 10
+#define MEM_TYPE uint32_t
+ 
+uint32_t mem_free();
+uint32_t mem_set(MEM_TYPE data);
+uint32_t mem_get(MEM_TYPE* data);
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Dibujar.cpp	Wed Mar 14 00:43:30 2018 +0000
@@ -0,0 +1,79 @@
+#include "Dibujar.h"
+#include "mbed.h"
+#include "math.h"
+
+PwmOut myServoX(PB_3);
+PwmOut myServoY(PB_4);
+PwmOut myServoZ(PB_5);
+
+uint8_t  posx_old=0;     // posición anterior del eje X
+uint8_t  posy_old=0;     // posición anterior del eje Y
+uint8_t  ss_time=100;     // tiempo  de espera para moverse 1 mm en microsegundos
+
+void put_sstime(uint8_t vtime){
+    ss_time=vtime;
+    
+}
+
+int coord2us(float coord)
+{
+    if(0 <= coord <= MAXPOS)
+        return int(750+coord*1900/50);// u6
+    return 750;
+
+}
+
+void sstime(uint8_t x, uint8_t y)
+{
+    double dx=abs(x-posx_old);
+    double dy=abs(y-posy_old);
+    double dist= sqrt(dx*dx+dy*dy);
+    wait_ms((int)(ss_time*dist));
+    posx_old =x;
+    posy_old=y;
+    
+ }
+ 
+void draw(){
+myServoZ.pulsewidth_us(POSDRAW);
+wait_ms(ss_time*2);
+}
+
+void nodraw(){
+myServoZ.pulsewidth_us(MAXPOS);
+wait_ms(ss_time*2);
+}
+
+
+void vertex2d(uint8_t x, uint8_t y){
+
+    int pulseX = coord2us(x);
+    int pulseY = coord2us(y);
+    
+    myServoX.pulsewidth_us(pulseX);
+    myServoY.pulsewidth_us(pulseY);
+   
+    sstime(x,y); 
+
+}
+
+void initdraw(float x, float y){
+    vertex2d (x,y);
+    draw();
+}
+
+void home(){
+    nodraw();
+    vertex2d(0 ,0);
+}    
+
+
+
+void init_servo()
+{
+   myServoX.period_ms(20);
+   myServoY.period_ms(20);
+   myServoZ.period_ms(20);
+
+    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Dibujar.h	Wed Mar 14 00:43:30 2018 +0000
@@ -0,0 +1,19 @@
+#ifndef DRAW_H   
+#define DRAW_H  
+
+#include "mbed.h"
+
+#define MAXPOS 50       // en milimetros
+#define POSDRAW 10   
+
+
+void init_servo();
+void draw();
+void nodraw();
+void vertex2d(uint8_t x, uint8_t y);
+
+void home();
+void initdraw(float x, float y);
+void put_sstime(uint8_t vtime);
+
+#endif //  DRAW_H 
\ No newline at end of file