pausa

Dependencies:   mbed

Fork of primercorte by edson antonio vargas villarreal

Committer:
nicolasrojas
Date:
Wed Jun 06 18:02:21 2018 +0000
Revision:
4:244a242e0428
Parent:
2:c457572a9bb2
Child:
5:9187685429b3
pausa

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