Juan Camilo Peña / Mbed 2 deprecated EntregaCorte1

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
JuanCamilo93
Date:
Tue Apr 24 05:12:26 2018 +0000
Commit message:
Primera versi?n estable al 80

Changed in this revision

draw.cpp Show annotated file Show diff for this revision Revisions of this file
draw.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
stepmotor.cpp Show annotated file Show diff for this revision Revisions of this file
stepmotor.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/draw.cpp	Tue Apr 24 05:12:26 2018 +0000
@@ -0,0 +1,84 @@
+
+#include "draw.h"
+#include "mbed.h"
+#include "math.h"
+
+PwmOut myServoX(D3);
+PwmOut myServoY(D5);
+PwmOut myServoZ(D6);
+
+uint8_t  posx_old=0;     // posición anterior del eje X
+uint8_t  posy_old=0;     // posición anterior del eje Y
+uint8_t  ss_time=100;     // tiempo  de espera para moverse 1 mm en microsegundos
+
+void put_sstime(uint8_t vtime){
+    ss_time=vtime;
+    
+}
+
+int coord2us(float coord)
+{
+    if(0 <= coord <= MAXPOS)
+        return int(750+coord*1900/50);// u6
+    return 750;
+
+}
+
+void sstime(uint8_t x, uint8_t y)
+{
+    double dx=abs(x-posx_old);
+    double dy=abs(y-posy_old);
+    double dist= sqrt(dx*dx+dy*dy);
+    wait_ms((int)(ss_time*dist));
+    posx_old =x;
+    posy_old=y;
+    
+ }
+ 
+void draw(){
+        
+    int pulseZ = coord2us(0);
+    myServoZ.pulsewidth_us(pulseZ);
+  //  sstime(5,0); 
+    
+}
+
+void nodraw(){
+ int pulseZ = coord2us(5);
+    myServoZ.pulsewidth_us(pulseZ);
+    //sstime(x,y); 
+}
+
+
+void vertex2d(uint8_t x, uint8_t y){
+
+    int pulseX = coord2us(x);
+    int pulseY = coord2us(y);
+    
+    myServoX.pulsewidth_us(pulseX);
+    myServoY.pulsewidth_us(pulseY);
+   
+    sstime(x,y); 
+
+}
+
+void initdraw(float x, float y){
+    vertex2d (x,y);
+    draw();
+}
+
+void home(){
+    nodraw();
+    vertex2d(0 ,0);
+}    
+
+
+
+void init_servo()
+{
+   myServoX.period_ms(20);
+   myServoY.period_ms(20);
+   myServoZ.period_ms(20);
+
+    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/draw.h	Tue Apr 24 05:12:26 2018 +0000
@@ -0,0 +1,19 @@
+#ifndef DRAW_H   
+#define DRAW_H  
+
+#include "mbed.h"
+
+#define MAXPOS 50       // en milimetros
+#define POSDRAW 10   
+
+
+void init_servo();
+void draw();
+void nodraw();
+void vertex2d(uint8_t x, uint8_t y);
+
+void home();
+void initdraw(float x, float y);
+void put_sstime(uint8_t vtime);
+
+#endif //  DRAW_H 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Apr 24 05:12:26 2018 +0000
@@ -0,0 +1,278 @@
+/*
+Código para controlar la comunicación entre la tarjeta y el pc a través de terminal
+*/
+#include "mbed.h"
+//Se incluye la clase dibujar
+#include "draw.h"
+
+#include "stepmotor.h"
+
+//Se establecen los valores de los diferentes comandos principales
+#define CM_EJECUTAR 0xff
+#define CM_GUARDAR 0xfe
+#define CM_VERTEX2D 0xfd
+#define CM_DRAW 0xfc
+#define CM_NODRAW 0xfb
+#define CM_STOP 0xfa
+#define CM_END 0xf0
+
+#define MEM_SIZE 10
+#define MEM_TYPE uint32_t
+
+//Variable para establecer la comunicación USB
+Serial command(USBTX, USBRX);
+
+uint8_t unitApuntador=0;
+uint8_t unitFin=0;
+
+MEM_TYPE bufferMemoria[MEM_SIZE];
+MEM_TYPE bufferSalida[MEM_SIZE];
+MEM_TYPE* data;
+
+MEM_TYPE buffer[MEM_SIZE];
+uint8_t mem_head = 0;
+uint8_t mem_tail = 0;
+
+bool full = 0;
+
+bool mem_set(MEM_TYPE data);
+int intValorMaximo=5000;
+int intValorActual=0;
+
+/*
+    Se manejan 3 estados:
+    1 Stand By (por default)
+    2 Guardando Datos
+    3 Ejecutando códigio/dibujando
+*/
+int intEstado=1;
+
+//Objeto para la comunicación serial entre el PC y la tarjeta
+Serial pc(USBTX,USBRX);
+
+
+//Función encargada de ejecutar todo el código
+void ejecutar()
+{
+    command.printf("se inicia el comando de ejecutar...\n");
+    uint8_t valLec=0;
+    uint8_t unintValX=0;
+    uint8_t unintValY=0;
+
+
+    for(int i=0; i<=mem_head; i++) {
+
+        char chValorRecibido = buffer[i];
+        //command.printf(""+chValorRecibido);
+
+        if(chValorRecibido==CM_DRAW) {
+
+            command.printf("Ejecutando comando Draw");
+            draw();
+
+        } else if(chValorRecibido==CM_NODRAW) {
+
+            command.printf("Ejecutando comando No Draw");
+            nodraw();
+
+        } else if(chValorRecibido==CM_STOP) {
+
+            command.printf("Ejecutando comando STOP");
+
+        } else {
+
+            command.printf("Piccolo dibujando, por favor espere");
+
+            valLec=buffer[i]>>8;
+    
+            if(valLec<=0x32) {
+
+                unintValY=valLec;
+                valLec=buffer[i]>>8>>8;
+
+                if(valLec<=0x32) {
+
+                    unintValX=valLec;
+                    valLec=buffer[i]>>8>>8;
+                    vertex2d(unintValX,unintValY);
+                    
+                    //command.printf("%x\n",valLec);
+                    //command.printf("%x\n",unintValY);
+                }
+
+            }
+        }
+    }
+    
+     intEstado=1;
+    
+}
+
+
+//Función encargada de empezar a guardar los comandos dentro de la pila
+void guardar()
+{
+    command.printf("___Se inicia el comando de guardar___\n");
+   
+    MEM_TYPE memValorEnviar;
+    char chValor1, chValor2;
+
+    //El while=1 garantiza que todo el tiempo la tarjeta está recibiendo información
+    while(intValorActual<=intValorMaximo && intEstado==2) {
+
+        chValor1=command.getc();
+
+        switch(chValor1) {
+
+            case CM_VERTEX2D:
+
+                chValor2=command.getc();
+                if(chValor2<=0x39) {
+                    memValorEnviar=CM_VERTEX2D;
+                    memValorEnviar=memValorEnviar<<8;
+                    memValorEnviar=memValorEnviar|chValor2;
+                    chValor2=command.getc();
+
+                    if(chValor2<=0x39) {
+                        memValorEnviar=memValorEnviar<<8;
+                        memValorEnviar=memValorEnviar|chValor2;
+                        chValor2=command.getc();
+
+                        if(chValor2==CM_END) {
+                            memValorEnviar=memValorEnviar<<8;
+                            memValorEnviar=memValorEnviar|CM_END;
+                            mem_set(memValorEnviar);
+                                
+command.printf("Recibio Vertex\n");
+
+                        }
+                    }
+                }
+
+                break;
+
+            case CM_DRAW:
+
+                chValor2=command.getc();
+                if(chValor2==CM_END) {
+                    memValorEnviar=chValor1;
+                    mem_set(memValorEnviar);
+                }
+                command.printf("Recibio draw\n");
+
+                break;
+
+            case CM_NODRAW:
+
+                chValor2=command.getc();
+                if(chValor2==CM_END) {
+                    memValorEnviar=chValor1;
+                    command.printf("recibio nodraw\n");
+                    mem_set(memValorEnviar);
+                }
+
+                break;
+
+            case CM_STOP:
+
+                if(chValor2==CM_END) {
+                    chValor2=command.getc();                    
+                    
+                    command.printf("Fin de programa\n");
+                chValor2=command.getc();
+                    
+                    
+                        //Se establece el estado en 1 para que el piccolo quede en Stand By
+                         intEstado=1;
+                    
+                }
+                break;
+        }
+
+    }
+   
+   
+
+}
+
+
+//Inicio de comunicación serial a 9600
+void init_serial()
+{
+    command.baud(9600);
+}
+
+
+//Función para recibir los datos enviados desde el terminal
+void RecibirComandos()
+{    
+    char chValor1Rec;
+    while(1)
+    {
+        chValor1Rec=command.getc();
+        
+        switch (chValor1Rec) {
+            
+            case  CM_EJECUTAR:
+                intEstado=3;
+                ejecutar(); 
+                break;
+                
+            case  CM_GUARDAR: 
+                intEstado=2;
+                guardar(); 
+                break;
+                
+            default: 
+                intEstado=1;
+                RecibirComandos();
+                command.printf("error de comando");
+                break ;
+        }
+    }
+}
+
+
+int main()
+{
+     init_servo();
+    init_serial();
+    home();
+    
+    RecibirComandos();
+}
+
+
+/* ***********************************************************************************
+                CONTROL MEMORIA
+/* ************************************************************************** */
+
+
+
+void mem_free()  //Estaba como uint32_t no como void
+{
+    mem_head=0;
+    full=0;
+}
+
+bool mem_set(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;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Apr 24 05:12:26 2018 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/aa5281ff4a02
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stepmotor.cpp	Tue Apr 24 05:12:26 2018 +0000
@@ -0,0 +1,72 @@
+/******************************************************************************
+        Desarrollado por ferney beltran fbeltran@ecci.edu.co
+ 
+libreria  ejemplo para el motor paso a paso unipolar de 4 fases
+ 
+******************************************************************************/
+ 
+//*****************************************************************************
+ 
+#include "stepmotor.h"
+#include "mbed.h"
+ 
+stepmotor::stepmotor(PinName in1, PinName in2, PinName in3, PinName in4) : motor_out(in1,in2,in3,in4) { 
+ 
+    motor_out=0x0;
+    nstep=0;
+    motorSpeed=1100;
+    
+}
+ 
+ 
+void stepmotor::move() { 
+   
+   switch(nstep)
+        { 
+            case 0: motor_out = 0x1; break;  // 0001
+            case 1: motor_out = 0x3; break;  // 0011
+            case 2: motor_out = 0x2; break;  // 0010   
+            case 3: motor_out = 0x6; break;  // 0110
+            case 4: motor_out = 0x4; break;  // 0100
+            case 5: motor_out = 0xC; break;  // 1100
+            case 6: motor_out = 0x8; break;  // 1000
+            case 7: motor_out = 0x9; break;  // 1001
+            
+            default: motor_out = 0x0; break; // 0000
+        }
+        wait_us(motorSpeed);
+        
+}
+
+ 
+void stepmotor::set_speed(int speed){
+    motorSpeed=speed; //set motor speed us 
+}
+
+
+uint32_t stepmotor::get_speed(){
+    return motorSpeed; // 
+}
+
+ 
+void stepmotor::step(uint32_t num_steps, bool cw) {
+// funcion para mover el motor N pasos CW o CCW   
+// num_steps  número de paso que da el motor 
+// cw =True  para dirección en sentido del reloj 
+// cw =False para dirección contraria de las manecillas del reloj         
+   
+    uint32_t count=num_steps ;
+    while(count){
+        
+        if (cw)   nstep++;     
+        else      nstep--;
+        
+        if (nstep>7) nstep=0;
+        if (nstep<0) nstep=7;
+        
+        move();
+        count--;
+ 
+    }
+  
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stepmotor.h	Tue Apr 24 05:12:26 2018 +0000
@@ -0,0 +1,31 @@
+ 
+/******************************************************************************
+        Desarrollado por ferney beltran fbeltran@ecci.edu.co
+ 
+libreria  ejemplo para el motor paso a paso unipolar de 4 fases
+ 
+******************************************************************************/
+ 
+//*****************************************************************************
+ 
+#ifndef STEP_MOTOR_H
+#define STEP_MOTOR_H
+ 
+#include "mbed.h"
+ 
+class stepmotor {
+public:
+ 
+    stepmotor(PinName in1, PinName in2, PinName in3, PinName in4); 
+    void step(uint32_t num_steps,bool cw);
+    void set_speed(int speed);
+    uint32_t get_speed();
+private:
+    BusOut motor_out;
+    uint32_t motorSpeed; 
+    int8_t nstep;
+    
+    void move();
+};
+ 
+#endif
\ No newline at end of file