final nnnn

Dependencies:   mbed

Este proyecto es un Piccolo en el cual se envía comandos para la realización de un dibujo en especifico. El programa va a controlar servos y motores paso a paso teniendo libertad de movimiento en los ejes X,Y,Z; En el programa se utiliza Comunicacion Serial (TX,RX) y para leer el dato fue necesario concatenarlo debido a que recibe 8 Bits pero tenemos que recibir 4 paquetes de 8 Bits para llegar a 32 Bits y poder asi leerlo.

Se estan implementando algunas librerias para su ejecucion como la #include "memory.h" que es de memoria, #include memory_array_h, tambien definimos la memoria #define mm_size 10 de tipo Type Uint32_t, la libreria de operaciones #include "math.h" y la libreria para los motores #include "stepmotor.h".

Para su ejecucion se crearon variables de ejecucion:

CM_DRAWING 0XFF: Se ejecuta siempre y cuando exista datos validos para leer de memoria y nos muestra por medio de un mensaje que se esta ejecutando el dibujo

CM_SAVING 0XFE: Inicia el comando de guardar

CM_VERTEX2D 0XFD: Se encarga de dar las coordenadas a los servomotores en X,Y.

CM_DRAW 0XFC: Se encarga de mover nuestro motor en Z

CM_NODRAW 0XFB: Su funcion es volver a la posicion inicial el motor en el eje Z

CM_MOTOR 0XF9: Se encarga de mover los motores paso a paso para llegar a la ubicacion asignada ingresando el Numero de cuadrantes y su sentido de giro.

Committer:
ANTONIO_VARGAS
Date:
Fri Apr 20 00:56:27 2018 +0000
Revision:
1:6ed951d975cc
Parent:
0:0119b611fc51
Child:
2:14fcd29abee0
hjv

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ANTONIO_VARGAS 0:0119b611fc51 1 #include "mbed.h"
ANTONIO_VARGAS 0:0119b611fc51 2 #include "main.h"
ANTONIO_VARGAS 0:0119b611fc51 3 #include "stepmotor.h"
ANTONIO_VARGAS 1:6ed951d975cc 4
ANTONIO_VARGAS 0:0119b611fc51 5 Serial command(USBTX, USBRX);
ANTONIO_VARGAS 1:6ed951d975cc 6 stepmotor smotor1(D9,D10,D11,D12);
ANTONIO_VARGAS 1:6ed951d975cc 7
ANTONIO_VARGAS 0:0119b611fc51 8
ANTONIO_VARGAS 0:0119b611fc51 9
ANTONIO_VARGAS 0:0119b611fc51 10
ANTONIO_VARGAS 0:0119b611fc51 11 int main() {
ANTONIO_VARGAS 0:0119b611fc51 12 init_servo();
ANTONIO_VARGAS 0:0119b611fc51 13 init_serial();
ANTONIO_VARGAS 0:0119b611fc51 14 draw();
ANTONIO_VARGAS 0:0119b611fc51 15 nodraw();
ANTONIO_VARGAS 0:0119b611fc51 16 home();
ANTONIO_VARGAS 0:0119b611fc51 17 debug_m("inicio \n");
ANTONIO_VARGAS 0:0119b611fc51 18 uint32_t read_cc;
ANTONIO_VARGAS 0:0119b611fc51 19 while(1)
ANTONIO_VARGAS 0:0119b611fc51 20 {
ANTONIO_VARGAS 0:0119b611fc51 21 read_cc=read_command();
ANTONIO_VARGAS 0:0119b611fc51 22 switch (read_cc) {
ANTONIO_VARGAS 0:0119b611fc51 23 case CM_DRAWING: drawing(); break;
ANTONIO_VARGAS 0:0119b611fc51 24 case CM_SAVING: saving(); break;
ANTONIO_VARGAS 0:0119b611fc51 25 default: debug_m("error de comando. \nSe espera 0xFEF0 o 0xFFF0 \n");break ;
ANTONIO_VARGAS 0:0119b611fc51 26 }
ANTONIO_VARGAS 0:0119b611fc51 27 }
ANTONIO_VARGAS 0:0119b611fc51 28 }
ANTONIO_VARGAS 0:0119b611fc51 29
ANTONIO_VARGAS 0:0119b611fc51 30
ANTONIO_VARGAS 0:0119b611fc51 31
ANTONIO_VARGAS 0:0119b611fc51 32 uint32_t read_command()
ANTONIO_VARGAS 0:0119b611fc51 33 {
ANTONIO_VARGAS 0:0119b611fc51 34 // retorna los byte recibidos concatenados en un entero, se reciben maximo 4 bytes,
ANTONIO_VARGAS 0:0119b611fc51 35 // recibe hasta que encuetra un fin de comando "CM_END".
ANTONIO_VARGAS 0:0119b611fc51 36 // Ejemplo: para el comando drawing se
ANTONIO_VARGAS 0:0119b611fc51 37 // espera un byte, por lo que al recibir 0xFF y 0xF0 la funcióm retorna el 0xff
ANTONIO_VARGAS 0:0119b611fc51 38 // siempre y cuando se reciba el fin de dato F0 de lo contrario retorna un cero
ANTONIO_VARGAS 0:0119b611fc51 39 // para el caso del comando vertex2d se espera recibir 3 bytes, 1 del comando
ANTONIO_VARGAS 0:0119b611fc51 40 // y dos bytes para x y y, se retorna la concatenación de los tres bytes siempre y
ANTONIO_VARGAS 0:0119b611fc51 41 // cuando el cuarto byte sea CM_END de lo contrario retorna un cero
ANTONIO_VARGAS 0:0119b611fc51 42
ANTONIO_VARGAS 0:0119b611fc51 43 uint32_t val=0;
ANTONIO_VARGAS 0:0119b611fc51 44 uint8_t cnt=0;
ANTONIO_VARGAS 1:6ed951d975cc 45
ANTONIO_VARGAS 0:0119b611fc51 46 char endc=command.getc();
ANTONIO_VARGAS 0:0119b611fc51 47
ANTONIO_VARGAS 0:0119b611fc51 48 while(endc != CM_END && cnt <4) {
ANTONIO_VARGAS 0:0119b611fc51 49 if(endc!=CM_END)
ANTONIO_VARGAS 0:0119b611fc51 50 val=((val<<8) +endc);
ANTONIO_VARGAS 0:0119b611fc51 51 endc=command.getc();
ANTONIO_VARGAS 0:0119b611fc51 52 cnt++;
ANTONIO_VARGAS 0:0119b611fc51 53 }
ANTONIO_VARGAS 0:0119b611fc51 54 if(endc==CM_END)
ANTONIO_VARGAS 0:0119b611fc51 55 return val;
ANTONIO_VARGAS 0:0119b611fc51 56 return 0; //al retornar 0 indica que no se recibe el comando
ANTONIO_VARGAS 0:0119b611fc51 57 }
ANTONIO_VARGAS 0:0119b611fc51 58 void init_serial()
ANTONIO_VARGAS 0:0119b611fc51 59 {
ANTONIO_VARGAS 0:0119b611fc51 60 command.baud(9600);
ANTONIO_VARGAS 0:0119b611fc51 61 }
ANTONIO_VARGAS 0:0119b611fc51 62
ANTONIO_VARGAS 0:0119b611fc51 63
ANTONIO_VARGAS 0:0119b611fc51 64 void drawing(){
ANTONIO_VARGAS 0:0119b611fc51 65 // la funcion se ejecuta siemrpe y cuando exista datos validos para leer de
ANTONIO_VARGAS 0:0119b611fc51 66 // memoria
ANTONIO_VARGAS 0:0119b611fc51 67 debug_m("se esta ejecutando el dibujo... \n");
ANTONIO_VARGAS 0:0119b611fc51 68
ANTONIO_VARGAS 0:0119b611fc51 69 uint8_t error=0;
ANTONIO_VARGAS 0:0119b611fc51 70 MEM_TYPE dato;
ANTONIO_VARGAS 0:0119b611fc51 71
ANTONIO_VARGAS 0:0119b611fc51 72 tail_reset();
ANTONIO_VARGAS 0:0119b611fc51 73
ANTONIO_VARGAS 0:0119b611fc51 74 while(error==0){
ANTONIO_VARGAS 0:0119b611fc51 75 error = mem_get(&dato);
ANTONIO_VARGAS 0:0119b611fc51 76 if (error==0) {
ANTONIO_VARGAS 1:6ed951d975cc 77
ANTONIO_VARGAS 0:0119b611fc51 78 switch (dato) {
ANTONIO_VARGAS 0:0119b611fc51 79 case CM_DRAW:
ANTONIO_VARGAS 0:0119b611fc51 80 debug_m("-> Baja Z\n");
ANTONIO_VARGAS 0:0119b611fc51 81 draw();
ANTONIO_VARGAS 0:0119b611fc51 82 break ;
ANTONIO_VARGAS 0:0119b611fc51 83 case CM_NODRAW:
ANTONIO_VARGAS 0:0119b611fc51 84 debug_m("-> Sube Z\n");
ANTONIO_VARGAS 0:0119b611fc51 85 nodraw();
ANTONIO_VARGAS 0:0119b611fc51 86 break ;
ANTONIO_VARGAS 1:6ed951d975cc 87
ANTONIO_VARGAS 1:6ed951d975cc 88
ANTONIO_VARGAS 0:0119b611fc51 89
ANTONIO_VARGAS 1:6ed951d975cc 90 default:
ANTONIO_VARGAS 1:6ed951d975cc 91 int y = (uint8_t)(dato);
ANTONIO_VARGAS 1:6ed951d975cc 92 int x = (uint8_t)(dato>>8);
ANTONIO_VARGAS 0:0119b611fc51 93 char ncomm = (uint8_t)(dato>>16);
ANTONIO_VARGAS 1:6ed951d975cc 94
ANTONIO_VARGAS 0:0119b611fc51 95 if (ncomm == CM_VERTEX2D) {
ANTONIO_VARGAS 0:0119b611fc51 96 debug_m("-> Mover piccolo x a %d y y a %d \n",x,x, y);
ANTONIO_VARGAS 0:0119b611fc51 97 vertex2d(x,y);
ANTONIO_VARGAS 1:6ed951d975cc 98
ANTONIO_VARGAS 1:6ed951d975cc 99 }else if (ncomm == CM_MOTOR){
ANTONIO_VARGAS 1:6ed951d975cc 100 debug_m("-> mover motor paso a paso en x \n");
ANTONIO_VARGAS 1:6ed951d975cc 101
ANTONIO_VARGAS 1:6ed951d975cc 102 smotor1.step(x*4096,(bool) y);
ANTONIO_VARGAS 1:6ed951d975cc 103 }
ANTONIO_VARGAS 1:6ed951d975cc 104
ANTONIO_VARGAS 0:0119b611fc51 105 debug_m("-> ERROR DE COMMANDO: %d %d %d \n " ,ncomm,x,y,y);
ANTONIO_VARGAS 0:0119b611fc51 106 break;
ANTONIO_VARGAS 0:0119b611fc51 107 }
ANTONIO_VARGAS 0:0119b611fc51 108 }
ANTONIO_VARGAS 0:0119b611fc51 109 }
ANTONIO_VARGAS 0:0119b611fc51 110
ANTONIO_VARGAS 0:0119b611fc51 111 debug_m("fin del comando dibujar..\n");
ANTONIO_VARGAS 0:0119b611fc51 112
ANTONIO_VARGAS 0:0119b611fc51 113 }
ANTONIO_VARGAS 0:0119b611fc51 114
ANTONIO_VARGAS 0:0119b611fc51 115
ANTONIO_VARGAS 0:0119b611fc51 116 void saving(){
ANTONIO_VARGAS 0:0119b611fc51 117 debug_m("se inicia el comando guardar..\n");
ANTONIO_VARGAS 0:0119b611fc51 118
ANTONIO_VARGAS 0:0119b611fc51 119 MEM_TYPE dato=0;
ANTONIO_VARGAS 0:0119b611fc51 120 mem_free();
ANTONIO_VARGAS 0:0119b611fc51 121 while(dato !=CM_STOP){
ANTONIO_VARGAS 0:0119b611fc51 122 dato = read_command();
ANTONIO_VARGAS 0:0119b611fc51 123 if (dato !=CM_STOP)
ANTONIO_VARGAS 0:0119b611fc51 124 mem_put(dato);
ANTONIO_VARGAS 0:0119b611fc51 125 }
ANTONIO_VARGAS 0:0119b611fc51 126 debug_m("fin del comado guardar..\n");
ANTONIO_VARGAS 0:0119b611fc51 127
ANTONIO_VARGAS 0:0119b611fc51 128 }
ANTONIO_VARGAS 0:0119b611fc51 129
ANTONIO_VARGAS 0:0119b611fc51 130 void debug_m(char *s , ... ){
ANTONIO_VARGAS 0:0119b611fc51 131 #if DEBUG
ANTONIO_VARGAS 0:0119b611fc51 132 command.printf(s);
ANTONIO_VARGAS 0:0119b611fc51 133 #endif
ANTONIO_VARGAS 0:0119b611fc51 134 }