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:
Wed Apr 11 02:19:13 2018 +0000
Revision:
0:0119b611fc51
Child:
1:6ed951d975cc
nnhjk

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