pausa

Dependencies:   mbed

Fork of primercorte by edson antonio vargas villarreal

Committer:
nicolasrojas
Date:
Wed Jun 06 18:31:04 2018 +0000
Revision:
5:9187685429b3
Parent:
4:244a242e0428
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 5:9187685429b3 94 if (ncomm== CM_PAUSA){
nicolasrojas 5:9187685429b3 95 debug_m("-> dibujo en pausa\n");
nicolasrojas 5:9187685429b3 96 pausa()
nicolasrojas 5:9187685429b3 97
nicolasrojas 4:244a242e0428 98
nicolasrojas 5:9187685429b3 99
nicolasrojas 5:9187685429b3 100 }
nicolasrojas 5:9187685429b3 101
nicolasrojas 5:9187685429b3 102 else if (ncomm == CM_VERTEX2D) {
ANTONIO_VARGAS 0:0119b611fc51 103 debug_m("-> Mover piccolo x a %d y y a %d \n",x,x, y);
ANTONIO_VARGAS 0:0119b611fc51 104 vertex2d(x,y);
ANTONIO_VARGAS 1:6ed951d975cc 105
ANTONIO_VARGAS 1:6ed951d975cc 106 }else if (ncomm == CM_MOTOR){
ANTONIO_VARGAS 1:6ed951d975cc 107 debug_m("-> mover motor paso a paso en x \n");
ANTONIO_VARGAS 1:6ed951d975cc 108
ANTONIO_VARGAS 1:6ed951d975cc 109 smotor1.step(x*4096,(bool) y);
ANTONIO_VARGAS 1:6ed951d975cc 110 }
nicolasrojas 5:9187685429b3 111
nicolasrojas 4:244a242e0428 112
nicolasrojas 4:244a242e0428 113
ANTONIO_VARGAS 0:0119b611fc51 114 debug_m("-> ERROR DE COMMANDO: %d %d %d \n " ,ncomm,x,y,y);
nicolasrojas 4:244a242e0428 115
ANTONIO_VARGAS 0:0119b611fc51 116 }
ANTONIO_VARGAS 0:0119b611fc51 117 }
ANTONIO_VARGAS 0:0119b611fc51 118 }
ANTONIO_VARGAS 0:0119b611fc51 119
ANTONIO_VARGAS 0:0119b611fc51 120 debug_m("fin del comando dibujar..\n");
ANTONIO_VARGAS 0:0119b611fc51 121
ANTONIO_VARGAS 0:0119b611fc51 122 }
nicolasrojas 4:244a242e0428 123
ANTONIO_VARGAS 0:0119b611fc51 124
nicolasrojas 4:244a242e0428 125
ANTONIO_VARGAS 0:0119b611fc51 126 void saving(){
ANTONIO_VARGAS 0:0119b611fc51 127 debug_m("se inicia el comando guardar..\n");
ANTONIO_VARGAS 0:0119b611fc51 128
ANTONIO_VARGAS 0:0119b611fc51 129 MEM_TYPE dato=0;
ANTONIO_VARGAS 0:0119b611fc51 130 mem_free();
ANTONIO_VARGAS 0:0119b611fc51 131 while(dato !=CM_STOP){
ANTONIO_VARGAS 0:0119b611fc51 132 dato = read_command();
ANTONIO_VARGAS 0:0119b611fc51 133 if (dato !=CM_STOP)
ANTONIO_VARGAS 0:0119b611fc51 134 mem_put(dato);
ANTONIO_VARGAS 0:0119b611fc51 135
nicolasrojas 2:c457572a9bb2 136
nicolasrojas 2:c457572a9bb2 137 }
nicolasrojas 2:c457572a9bb2 138
nicolasrojas 4:244a242e0428 139 debug_m("fin del comado guardar..\n");
nicolasrojas 4:244a242e0428 140
nicolasrojas 2:c457572a9bb2 141 }
nicolasrojas 4:244a242e0428 142
nicolasrojas 4:244a242e0428 143
nicolasrojas 4:244a242e0428 144
nicolasrojas 5:9187685429b3 145 void pausa(){
nicolasrojas 4:244a242e0428 146
nicolasrojas 4:244a242e0428 147 char d=command.getc();
nicolasrojas 4:244a242e0428 148 if(d==CM_PAUSA){
nicolasrojas 4:244a242e0428 149
nicolasrojas 4:244a242e0428 150 command.attach(0,Serial::RxIrq);
nicolasrojas 4:244a242e0428 151 debug_m("interrumpe\n");
nicolasrojas 4:244a242e0428 152 while(command.getc()!=CM_PLAY){}
nicolasrojas 5:9187685429b3 153 command.attach(&pausa,Serial::RxIrq);
nicolasrojas 4:244a242e0428 154 return;}
nicolasrojas 4:244a242e0428 155 }
ANTONIO_VARGAS 0:0119b611fc51 156
nicolasrojas 4:244a242e0428 157
ANTONIO_VARGAS 0:0119b611fc51 158 void debug_m(char *s , ... ){
ANTONIO_VARGAS 0:0119b611fc51 159 #if DEBUG
ANTONIO_VARGAS 0:0119b611fc51 160 command.printf(s);
ANTONIO_VARGAS 0:0119b611fc51 161 #endif
ANTONIO_VARGAS 0:0119b611fc51 162 }