Giro en su propio eje Presentado por: Julio Fajardo, Cesar Pacheco, Angel Trujillo, Daniel Vizcaya

Dependencies:   mbed

Fork of 01_Embebidos by Daniel Vizcaya

01_main.cpp

Committer:
Bethory
Date:
2018-04-25
Revision:
6:87a37f4163bd
Parent:
5:8e100835b017

File content as of revision 6:87a37f4163bd:

#include "mbed.h"
#include "draw.h"
#include "stepmotor.h"

#define CM_EJECUTAR 0xFF
#define CM_GUARDAR 0xFE
#define CM_TRASLADAR 0xF9
#define CM_VERTEX2D 0xFD
#define CM_DRAW 0XFC
#define CM_NODRAW 0xFB
#define CM_STOP 0xFA
#define CM_ENTER 0xF0
#define CM_GIRO 0xF8

#define MEM_SIZE 100
#define MEM_TYPE uint32_t //significa que de ahora en más donde diga MEM_TYPE será tipo uint32_t
#define PASOSxCUADRANTE 250 //4096
uint8_t mem_head = 0;
uint8_t mem_tail = 0;
char temp;
bool full = 0;

MEM_TYPE buffer[MEM_SIZE];
stepmotor smotor1(D9,D10,D11,D12);
stepmotor smotor2(D2,D6,D7,D8);
Serial command(USBTX, USBRX);

void girar(uint8_t sentido)
{
    command.printf(">>>> %d \n",sentido);
    uint32_t speed = 1500;
    smotor1.set_speed(speed);
    smotor2.set_speed(speed);
    if(sentido==1)
    {
        command.printf("pa la derecha!!\n");
        smotor1.step(7* PASOSxCUADRANTE, 0);
        smotor2.step(7 * PASOSxCUADRANTE, 1);
        
    }else
    {
        command.printf("pa la izquierda!\n");
        smotor1.step(7 * PASOSxCUADRANTE, 1);
        smotor2.step(7 * PASOSxCUADRANTE, 0);   
    }
    wait(2);
}

void mem_free()
{ //Estaba como uint32_t no como void
    mem_head = 0; 
    full = 0;
}

bool mem_put(MEM_TYPE data)
{ //Escribir
    if (full)
        return 1;
    buffer[mem_head] = data; //carga en dato en el buffer
    mem_head += 1;
    if (mem_head == MEM_SIZE)
        full = 1;
    return 0;
}

bool mem_get(MEM_TYPE *data)
{ //Leer
    if (mem_head == 0)
        return 1;
    if (mem_head == mem_tail)
        return 1;
    *data = buffer[mem_tail];
    mem_tail += 1;
    return 0;
}

void ejecutar()
{
    command.printf("inicio de ejecutar...\n");
    for (int j = 0; j <= mem_head - 1; j++)
    {
        command.printf("%x\n", buffer[j]);
        if (buffer[j] == CM_NODRAW)
        {
            nodraw();
        }
        if (buffer[j] == CM_DRAW)
        {
            draw();
        }
        if (buffer[j] == CM_STOP)
        {
            mem_free();
            return;
        }
        temp = buffer[j] >> 24 & 0xFF;
        if (temp == CM_VERTEX2D)
        {
            uint8_t datoX = buffer[j] >> 16 & 0xFF;
            uint8_t datoY = buffer[j] >> 8 & 0xFF;
            vertex2d(datoX, datoY);
            //wait(1);
        }
        if (temp == CM_TRASLADAR) //no mover si draw se encuentra activo
        {
            uint32_t speed = 1500;
            smotor1.set_speed(speed);
            smotor2.set_speed(speed);
            uint32_t cuadrantes = buffer[j] >> 16 & 0xFF;
            bool direccion = buffer[j] >> 8 & 0xFF;
            smotor1.step(cuadrantes * PASOSxCUADRANTE, direccion);
            smotor2.step(cuadrantes * PASOSxCUADRANTE, direccion);
            wait(2);
        }
    }
    command.printf("fin de ejecutar...\n");
    mem_free();
}

void guardar()
{
    command.printf("inicio de guardar...\n");
    MEM_TYPE val; //significa que la variable "val" es del tipo uint32_t
    char dato1, dato2;
    do
    {
        dato1 = command.getc();
        if (dato1 == CM_STOP)
        {
            dato2 = command.getc();
            if (dato2 == CM_ENTER)
            {
                val = dato1;
                mem_put(val);
                return;
            }
        }
        if (dato1 == CM_NODRAW | dato1 == CM_DRAW)
        {
            dato2 = command.getc();
            if (dato2 == CM_ENTER)
            {
                val = dato1;
                mem_put(val);
            }
        }
        if (dato1 == CM_VERTEX2D)
        {
            dato2 = command.getc();
            if (dato2 <= 0x32)
            {
                val = CM_VERTEX2D;
                val = val << 8;
                val = val | dato2;
                dato2 = command.getc();
                if (dato2 <= 0x32)
                {
                    val = val << 8;
                    val = val | dato2;
                    dato2 = command.getc();
                    if (dato2 == CM_ENTER)
                    {
                        val = val << 8;
                        val = val | CM_ENTER;
                        mem_put(val);
                    }
                }
            }
        }
        if (dato1 == CM_TRASLADAR)
        {
            dato2 = command.getc();
            if (dato2 <= 0xEF)
            {
                val = CM_TRASLADAR;
                val = val << 8;
                val = val | dato2;
                dato2 = command.getc();
                if (dato2 <= 1)
                {
                    val = val << 8;
                    val = val | dato2;
                    dato2 = command.getc();
                    if (dato2 == CM_ENTER)
                    {
                        val = val << 8;
                        val = val | CM_ENTER;
                        mem_put(val);
                    }
                }
            }
        }
    } while (!full);
    command.printf("fin de guardar...\n");
}

int main()
{
    command.baud(9600);
    init_servo();
    //home(); //llama a no_draw y va a 0,0

    char read_cc;
    while (1)
    {
        read_cc = command.getc();
        switch (read_cc)
        {
        case CM_GUARDAR:
            if (read_cc = command.getc() == CM_ENTER)
            {
                guardar();
            }
            break; //FE
        case CM_EJECUTAR:
            if (read_cc = command.getc() == CM_ENTER)
            {
                ejecutar();
            }
            break; //FF
        case CM_GIRO:
            uint8_t sentido = command.getc();
            if (sentido <= 1)
            {
                read_cc = command.getc();
                if (read_cc = command.getc() == CM_ENTER)
                {
                    girar(sentido);
            } 
            }   
            break; //F8
        default:
            command.printf("Paila \n");
            break;
        }
    }
}
/*
FE F0 FD 12 34 F0 FB F0 FD 0A 0A F0 FD 28 0A F0 FD 28 28 F0 FD 0A 28 F0 F9 27 00 F0 FC F0 FA F0 FF F0
FE F0 F9 02 00 F0 F9 02 01 F0 F9 01 01 F0 F9 01 00 F0 FA F0 FF F0
    https://os.mbed.com/users/fabeltranm/
*/