Piccolo

Dependencies:   mbed

Fork of 02_LAB_serial_protocol by ferney alberto beltran molina

Committer:
fabeltranm
Date:
Wed Apr 26 18:04:57 2017 +0000
Revision:
0:55d11eeb0faf
Child:
1:0bcd96e56022
new

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fabeltranm 0:55d11eeb0faf 1 #include "mbed.h"
fabeltranm 0:55d11eeb0faf 2 #include "mbed.h"
fabeltranm 0:55d11eeb0faf 3
fabeltranm 0:55d11eeb0faf 4 Serial command(USBTX, USBRX);
fabeltranm 0:55d11eeb0faf 5
fabeltranm 0:55d11eeb0faf 6 #define DEBUG 1
fabeltranm 0:55d11eeb0faf 7
fabeltranm 0:55d11eeb0faf 8 //*****************************************************************************
fabeltranm 0:55d11eeb0faf 9 // COMANDO MOVER MOTOR
fabeltranm 0:55d11eeb0faf 10 // |POS 1|POS 2|POS 3|POS 4| POS 5|
fabeltranm 0:55d11eeb0faf 11 // | < | #M | , | #°G | > |
fabeltranm 0:55d11eeb0faf 12 //
fabeltranm 0:55d11eeb0faf 13 // #M -> indica el motor que se va a mover
fabeltranm 0:55d11eeb0faf 14 // #°G -> indica los grados a mover del servomotor
fabeltranm 0:55d11eeb0faf 15 // <,> -> inicio, separdor y fin de comando
fabeltranm 0:55d11eeb0faf 16 // el inicio de comando no se almacena en el buffer
fabeltranm 0:55d11eeb0faf 17 //*****************************************************************************
fabeltranm 0:55d11eeb0faf 18
fabeltranm 0:55d11eeb0faf 19 // VARIABLES PARA DEFINIR EL COMMANDO
fabeltranm 0:55d11eeb0faf 20 #define BUFF_SIZE 4
fabeltranm 0:55d11eeb0faf 21 #define COMM_NUM_MOTOR 0
fabeltranm 0:55d11eeb0faf 22 #define COMM_SEPARADOR 1
fabeltranm 0:55d11eeb0faf 23 #define COMM_GRADOS_MOTOR 2
fabeltranm 0:55d11eeb0faf 24
fabeltranm 0:55d11eeb0faf 25
fabeltranm 0:55d11eeb0faf 26 uint8_t buffer_command[BUFF_SIZE]={0,0,0,0};
fabeltranm 0:55d11eeb0faf 27
fabeltranm 0:55d11eeb0faf 28 // TODO : TIMEOUT UART SERIAL
fabeltranm 0:55d11eeb0faf 29 void Read_command()
fabeltranm 0:55d11eeb0faf 30 {
fabeltranm 0:55d11eeb0faf 31 for (uint8_t i=0; i<BUFF_SIZE;i++)
fabeltranm 0:55d11eeb0faf 32 buffer_command[i]=command.getc();
fabeltranm 0:55d11eeb0faf 33
fabeltranm 0:55d11eeb0faf 34 }
fabeltranm 0:55d11eeb0faf 35
fabeltranm 0:55d11eeb0faf 36 void echo_command()
fabeltranm 0:55d11eeb0faf 37 {
fabeltranm 0:55d11eeb0faf 38 for (uint8_t i=0; i<BUFF_SIZE;i++)
fabeltranm 0:55d11eeb0faf 39 command.putc(buffer_command[i]);
fabeltranm 0:55d11eeb0faf 40
fabeltranm 0:55d11eeb0faf 41 }
fabeltranm 0:55d11eeb0faf 42
fabeltranm 0:55d11eeb0faf 43
fabeltranm 0:55d11eeb0faf 44 void check_command()
fabeltranm 0:55d11eeb0faf 45 {
fabeltranm 0:55d11eeb0faf 46 if (buffer_command[BUFF_SIZE-1]== '>' && buffer_command[COMM_SEPARADOR]==','){
fabeltranm 0:55d11eeb0faf 47
fabeltranm 0:55d11eeb0faf 48 #if DEBUG
fabeltranm 0:55d11eeb0faf 49 command.printf("Mover Motor:");
fabeltranm 0:55d11eeb0faf 50 command.putc(buffer_command[COMM_NUM_MOTOR]+0x30);
fabeltranm 0:55d11eeb0faf 51 command.printf("->");
fabeltranm 0:55d11eeb0faf 52 command.putc(buffer_command[COMM_GRADOS_MOTOR]);
fabeltranm 0:55d11eeb0faf 53 command.printf(" grados \n");
fabeltranm 0:55d11eeb0faf 54 #endif
fabeltranm 0:55d11eeb0faf 55 }else{
fabeltranm 0:55d11eeb0faf 56 #if DEBUG
fabeltranm 0:55d11eeb0faf 57 command.printf("ERROR COMANDO -> ");
fabeltranm 0:55d11eeb0faf 58 echo_command();
fabeltranm 0:55d11eeb0faf 59 #endif
fabeltranm 0:55d11eeb0faf 60
fabeltranm 0:55d11eeb0faf 61 }
fabeltranm 0:55d11eeb0faf 62
fabeltranm 0:55d11eeb0faf 63 }
fabeltranm 0:55d11eeb0faf 64 int main() {
fabeltranm 0:55d11eeb0faf 65 #if DEBUG
fabeltranm 0:55d11eeb0faf 66 command.printf("inicio con debug\n");
fabeltranm 0:55d11eeb0faf 67 #else
fabeltranm 0:55d11eeb0faf 68 command.printf("inicio sin debug\n");
fabeltranm 0:55d11eeb0faf 69 #endif
fabeltranm 0:55d11eeb0faf 70 uint8_t val;
fabeltranm 0:55d11eeb0faf 71 while(1){
fabeltranm 0:55d11eeb0faf 72 val=command.getc();
fabeltranm 0:55d11eeb0faf 73 if (val== '<'){
fabeltranm 0:55d11eeb0faf 74 Read_command();
fabeltranm 0:55d11eeb0faf 75 #if DEBUG
fabeltranm 0:55d11eeb0faf 76 echo_command();
fabeltranm 0:55d11eeb0faf 77 #endif
fabeltranm 0:55d11eeb0faf 78 check_command();
fabeltranm 0:55d11eeb0faf 79
fabeltranm 0:55d11eeb0faf 80 }
fabeltranm 0:55d11eeb0faf 81 }
fabeltranm 0:55d11eeb0faf 82 }
fabeltranm 0:55d11eeb0faf 83