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:
Thu Apr 26 01:48:40 2018 +0000
Revision:
2:14fcd29abee0
Parent:
1:6ed951d975cc
explicacion programa piccolo

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