pausa

Dependencies:   mbed

Fork of primercorte by edson antonio vargas villarreal

Committer:
nicolasrojas
Date:
Tue Jun 05 23:01:22 2018 +0000
Revision:
2:c457572a9bb2
Parent:
1:6ed951d975cc
Child:
4:244a242e0428
picolo

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"
nicolasrojas 2:c457572a9bb2 4
nicolasrojas 2:c457572a9bb2 5
ANTONIO_VARGAS 1:6ed951d975cc 6
ANTONIO_VARGAS 0:0119b611fc51 7 Serial command(USBTX, USBRX);
ANTONIO_VARGAS 1:6ed951d975cc 8 stepmotor smotor1(D9,D10,D11,D12);
nicolasrojas 2:c457572a9bb2 9 stepmotor smotor2(D2,D3,D14,D15);
ANTONIO_VARGAS 1:6ed951d975cc 10
nicolasrojas 2:c457572a9bb2 11 void Rx_interrupt();
ANTONIO_VARGAS 0:0119b611fc51 12
nicolasrojas 2:c457572a9bb2 13 int main() {
nicolasrojas 2:c457572a9bb2 14
ANTONIO_VARGAS 0:0119b611fc51 15 init_servo();
ANTONIO_VARGAS 0:0119b611fc51 16 init_serial();
ANTONIO_VARGAS 0:0119b611fc51 17 draw();
nicolasrojas 2:c457572a9bb2 18 Rx_interrupt ();
ANTONIO_VARGAS 0:0119b611fc51 19 nodraw();
ANTONIO_VARGAS 0:0119b611fc51 20 home();
ANTONIO_VARGAS 0:0119b611fc51 21 debug_m("inicio \n");
ANTONIO_VARGAS 0:0119b611fc51 22 uint32_t read_cc;
ANTONIO_VARGAS 0:0119b611fc51 23 while(1)
ANTONIO_VARGAS 0:0119b611fc51 24 {
ANTONIO_VARGAS 0:0119b611fc51 25 read_cc=read_command();
ANTONIO_VARGAS 0:0119b611fc51 26 switch (read_cc) {
ANTONIO_VARGAS 0:0119b611fc51 27 case CM_DRAWING: drawing(); break;
ANTONIO_VARGAS 0:0119b611fc51 28 case CM_SAVING: saving(); break;
nicolasrojas 2:c457572a9bb2 29 case CM_PAUSA: Rx_interrupt(); break;
nicolasrojas 2:c457572a9bb2 30
nicolasrojas 2:c457572a9bb2 31 default: debug_m("error de comando. \nSe espera 0xFEF0 o 0xfff0 \n");break ;
ANTONIO_VARGAS 0:0119b611fc51 32 }
ANTONIO_VARGAS 0:0119b611fc51 33 }
ANTONIO_VARGAS 0:0119b611fc51 34 }
ANTONIO_VARGAS 0:0119b611fc51 35
ANTONIO_VARGAS 0:0119b611fc51 36
ANTONIO_VARGAS 0:0119b611fc51 37
ANTONIO_VARGAS 0:0119b611fc51 38 uint32_t read_command()
ANTONIO_VARGAS 0:0119b611fc51 39 {
nicolasrojas 2:c457572a9bb2 40
ANTONIO_VARGAS 0:0119b611fc51 41
ANTONIO_VARGAS 0:0119b611fc51 42 uint32_t val=0;
ANTONIO_VARGAS 0:0119b611fc51 43 uint8_t cnt=0;
ANTONIO_VARGAS 1:6ed951d975cc 44
ANTONIO_VARGAS 0:0119b611fc51 45 char endc=command.getc();
ANTONIO_VARGAS 0:0119b611fc51 46
ANTONIO_VARGAS 0:0119b611fc51 47 while(endc != CM_END && cnt <4) {
ANTONIO_VARGAS 0:0119b611fc51 48 if(endc!=CM_END)
ANTONIO_VARGAS 0:0119b611fc51 49 val=((val<<8) +endc);
ANTONIO_VARGAS 0:0119b611fc51 50 endc=command.getc();
ANTONIO_VARGAS 0:0119b611fc51 51 cnt++;
ANTONIO_VARGAS 0:0119b611fc51 52 }
ANTONIO_VARGAS 0:0119b611fc51 53 if(endc==CM_END)
ANTONIO_VARGAS 0:0119b611fc51 54 return val;
nicolasrojas 2:c457572a9bb2 55 return 0;
ANTONIO_VARGAS 0:0119b611fc51 56 }
nicolasrojas 2:c457572a9bb2 57 void init_serial(){
nicolasrojas 2:c457572a9bb2 58
nicolasrojas 2:c457572a9bb2 59 command.baud(9600);
nicolasrojas 2:c457572a9bb2 60
ANTONIO_VARGAS 0:0119b611fc51 61 }
nicolasrojas 2:c457572a9bb2 62
nicolasrojas 2:c457572a9bb2 63
ANTONIO_VARGAS 0:0119b611fc51 64
ANTONIO_VARGAS 0:0119b611fc51 65
ANTONIO_VARGAS 0:0119b611fc51 66 void drawing(){
nicolasrojas 2:c457572a9bb2 67
ANTONIO_VARGAS 0:0119b611fc51 68 debug_m("se esta ejecutando el dibujo... \n");
ANTONIO_VARGAS 0:0119b611fc51 69
ANTONIO_VARGAS 0:0119b611fc51 70 uint8_t error=0;
ANTONIO_VARGAS 0:0119b611fc51 71 MEM_TYPE dato;
ANTONIO_VARGAS 0:0119b611fc51 72
ANTONIO_VARGAS 0:0119b611fc51 73 tail_reset();
ANTONIO_VARGAS 0:0119b611fc51 74
ANTONIO_VARGAS 0:0119b611fc51 75 while(error==0){
ANTONIO_VARGAS 0:0119b611fc51 76 error = mem_get(&dato);
nicolasrojas 2:c457572a9bb2 77
nicolasrojas 2:c457572a9bb2 78
ANTONIO_VARGAS 0:0119b611fc51 79 if (error==0) {
ANTONIO_VARGAS 1:6ed951d975cc 80
nicolasrojas 2:c457572a9bb2 81 switch (dato) {
nicolasrojas 2:c457572a9bb2 82
ANTONIO_VARGAS 0:0119b611fc51 83 case CM_DRAW:
ANTONIO_VARGAS 0:0119b611fc51 84 debug_m("-> Baja Z\n");
ANTONIO_VARGAS 0:0119b611fc51 85 draw();
ANTONIO_VARGAS 0:0119b611fc51 86 break ;
ANTONIO_VARGAS 0:0119b611fc51 87 case CM_NODRAW:
ANTONIO_VARGAS 0:0119b611fc51 88 debug_m("-> Sube Z\n");
ANTONIO_VARGAS 0:0119b611fc51 89 nodraw();
ANTONIO_VARGAS 0:0119b611fc51 90 break ;
ANTONIO_VARGAS 1:6ed951d975cc 91
ANTONIO_VARGAS 1:6ed951d975cc 92
ANTONIO_VARGAS 0:0119b611fc51 93
ANTONIO_VARGAS 1:6ed951d975cc 94 default:
ANTONIO_VARGAS 1:6ed951d975cc 95 int y = (uint8_t)(dato);
ANTONIO_VARGAS 1:6ed951d975cc 96 int x = (uint8_t)(dato>>8);
ANTONIO_VARGAS 0:0119b611fc51 97 char ncomm = (uint8_t)(dato>>16);
nicolasrojas 2:c457572a9bb2 98
nicolasrojas 2:c457572a9bb2 99 if (ncomm == CM_PAUSA){
nicolasrojas 2:c457572a9bb2 100 debug_m("-pausa");
nicolasrojas 2:c457572a9bb2 101 command.attach(&Rx_interrupt, Serial::RxIrq);
nicolasrojas 2:c457572a9bb2 102 }
nicolasrojas 2:c457572a9bb2 103 else if (ncomm == CM_VERTEX2D) {
ANTONIO_VARGAS 0:0119b611fc51 104 debug_m("-> Mover piccolo x a %d y y a %d \n",x,x, y);
ANTONIO_VARGAS 0:0119b611fc51 105 vertex2d(x,y);
ANTONIO_VARGAS 1:6ed951d975cc 106
ANTONIO_VARGAS 1:6ed951d975cc 107 }else if (ncomm == CM_MOTOR){
ANTONIO_VARGAS 1:6ed951d975cc 108 debug_m("-> mover motor paso a paso en x \n");
ANTONIO_VARGAS 1:6ed951d975cc 109
ANTONIO_VARGAS 1:6ed951d975cc 110 smotor1.step(x*4096,(bool) y);
ANTONIO_VARGAS 1:6ed951d975cc 111 }
ANTONIO_VARGAS 1:6ed951d975cc 112
ANTONIO_VARGAS 0:0119b611fc51 113 debug_m("-> ERROR DE COMMANDO: %d %d %d \n " ,ncomm,x,y,y);
ANTONIO_VARGAS 0:0119b611fc51 114 break;
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 debug_m("fin del comando dibujar..\n");
ANTONIO_VARGAS 0:0119b611fc51 120
ANTONIO_VARGAS 0:0119b611fc51 121 }
ANTONIO_VARGAS 0:0119b611fc51 122
ANTONIO_VARGAS 0:0119b611fc51 123
ANTONIO_VARGAS 0:0119b611fc51 124 void saving(){
ANTONIO_VARGAS 0:0119b611fc51 125 debug_m("se inicia el comando guardar..\n");
ANTONIO_VARGAS 0:0119b611fc51 126
ANTONIO_VARGAS 0:0119b611fc51 127 MEM_TYPE dato=0;
ANTONIO_VARGAS 0:0119b611fc51 128 mem_free();
ANTONIO_VARGAS 0:0119b611fc51 129 while(dato !=CM_STOP){
ANTONIO_VARGAS 0:0119b611fc51 130 dato = read_command();
ANTONIO_VARGAS 0:0119b611fc51 131 if (dato !=CM_STOP)
ANTONIO_VARGAS 0:0119b611fc51 132 mem_put(dato);
ANTONIO_VARGAS 0:0119b611fc51 133 }
ANTONIO_VARGAS 0:0119b611fc51 134 debug_m("fin del comado guardar..\n");
ANTONIO_VARGAS 0:0119b611fc51 135
ANTONIO_VARGAS 0:0119b611fc51 136 }
nicolasrojas 2:c457572a9bb2 137 void Rx_interrupt() {
nicolasrojas 2:c457572a9bb2 138 debug_m(" dibujo en pausa ... \n");
nicolasrojas 2:c457572a9bb2 139
nicolasrojas 2:c457572a9bb2 140 uint8_t error=0;
nicolasrojas 2:c457572a9bb2 141 MEM_TYPE dato;
nicolasrojas 2:c457572a9bb2 142 tail_reset();
nicolasrojas 2:c457572a9bb2 143 while(error==0){
nicolasrojas 2:c457572a9bb2 144 error = mem_get(&dato);
nicolasrojas 2:c457572a9bb2 145 if (error==0) {
nicolasrojas 2:c457572a9bb2 146
nicolasrojas 2:c457572a9bb2 147 switch (dato) {
nicolasrojas 2:c457572a9bb2 148
nicolasrojas 2:c457572a9bb2 149 case CM_PAUSA:
nicolasrojas 2:c457572a9bb2 150 debug_m("-> pausa \n");
nicolasrojas 2:c457572a9bb2 151 Rx_interrupt();
nicolasrojas 2:c457572a9bb2 152 break ;
nicolasrojas 2:c457572a9bb2 153
nicolasrojas 2:c457572a9bb2 154
nicolasrojas 2:c457572a9bb2 155
nicolasrojas 2:c457572a9bb2 156
nicolasrojas 2:c457572a9bb2 157 }
nicolasrojas 2:c457572a9bb2 158 command.attach(&Rx_interrupt, Serial::RxIrq);
nicolasrojas 2:c457572a9bb2 159
nicolasrojas 2:c457572a9bb2 160 }
nicolasrojas 2:c457572a9bb2 161 }
nicolasrojas 2:c457572a9bb2 162 }
ANTONIO_VARGAS 0:0119b611fc51 163
ANTONIO_VARGAS 0:0119b611fc51 164 void debug_m(char *s , ... ){
ANTONIO_VARGAS 0:0119b611fc51 165 #if DEBUG
ANTONIO_VARGAS 0:0119b611fc51 166 command.printf(s);
ANTONIO_VARGAS 0:0119b611fc51 167 #endif
ANTONIO_VARGAS 0:0119b611fc51 168 }