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

Dependencies:   mbed

Fork of 01_Embebidos by Daniel Vizcaya

Files at this revision

API Documentation at this revision

Comitter:
Bethory
Date:
Wed Apr 25 02:12:19 2018 +0000
Parent:
5:8e100835b017
Commit message:
Ejercicio 2do corte

Changed in this revision

01_main.cpp Show annotated file Show diff for this revision Revisions of this file
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
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
diff -r 8e100835b017 -r 87a37f4163bd 01_main.cpp
--- a/01_main.cpp	Wed Mar 14 05:17:52 2018 +0000
+++ b/01_main.cpp	Wed Apr 25 02:12:19 2018 +0000
@@ -1,150 +1,242 @@
 #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 10
+#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 mem_free(){ //Estaba como uint32_t no como void
-    mem_head=0;
-    full=0;
+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);
 }
 
-bool mem_put(MEM_TYPE data){ //Escribir
+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;
+        full = 1;
     return 0;
 }
 
-bool mem_get(MEM_TYPE* data){ //Leer
+bool mem_get(MEM_TYPE *data)
+{ //Leer
     if (mem_head == 0)
-        return 1; 
+        return 1;
     if (mem_head == mem_tail)
-        return 1; 
+        return 1;
     *data = buffer[mem_tail];
     mem_tail += 1;
     return 0;
 }
 
-void ejecutar(){
-    command.printf("se inicia el comando de ejecutar...\n");
-    for(int j=0;j<=mem_head;j++){
-        command.printf("%x\n",buffer[j]);
+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("se inicia el comando de guardar...\n");
+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;
+    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;
+        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;
+        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);        
+    } while (!full);
+    command.printf("fin de guardar...\n");
 }
 
-int main(){
-    init_servo();
+int main()
+{
     command.baud(9600);
-    home(); //llama a no_draw y va a 0,0
-   
+    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: guardar(); break; //FE
-            case CM_EJECUTAR: ejecutar(); break;  //FF
-            default: command.printf("Error de comando \n");break;      
+    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 FB FD 00 00 F0
-FC F0 FD 32 00 F0
-FD 32 32 F0 FD 00
-32 F0 FD 00 00 F0
-FB F0 FA F0 FF
-
-FE F0 FD 54 32 F0 FC F0 FD 32 00 F0 FD 32 32 F0 FD 00 32 F0 FD 00 00 F0 FB F0 FA F0 FF
-FE 12 34 12 34 12 34 12 34 12 34 12 34 12 34 12 34 12 34 12 34 12 34 12 34 12 FA FF
-
-#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
-
-    while(!full){
-        for(int i=1;i<=4;i++){
-            raw_data=command.getc();
-            command.printf("%x",raw_data);
-            if(raw_data==CM_STOP)
-                return;
-            if(i==1){
-                val=raw_data;
-            }else{
-                
-                val=val << 8;
-                val=val|raw_data;
-            }
-            command.printf(" %d",i);
-            command.printf(" %x\n",val);
-        }
-        mem_put(val);
-    }
-    //mem_get(&val); //&val es la dirección de la variable, no su contenido
-    //command.putc(val); //manda por puerto serial el contenido de "val" npi para qué
-
+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/
 */
\ No newline at end of file
diff -r 8e100835b017 -r 87a37f4163bd draw.cpp
--- a/draw.cpp	Wed Mar 14 05:17:52 2018 +0000
+++ b/draw.cpp	Wed Apr 25 02:12:19 2018 +0000
@@ -5,9 +5,9 @@
 
 
 
-PwmOut myServoX(PB_3);
-PwmOut myServoY(PB_4);
-PwmOut myServoZ(PB_5);
+PwmOut myServoX(D3);
+PwmOut myServoY(D4);
+PwmOut myServoZ(D5);
 
 uint8_t  posx_old=0;     // posición anterior del eje X
 uint8_t  posy_old=0;     // posición anterior del eje Y
@@ -21,9 +21,8 @@
 int coord2us(float coord)
 {
     if(0 <= coord <= MAXPOS)
-        return int(750+coord*1900/50);// u6
-    return 750;
-
+        return int(coord*30+500);// u6
+    return 500;
 }
 
 void sstime(uint8_t x, uint8_t y)
@@ -37,6 +36,7 @@
     
  }
  
+ 
 void draw(){
 myServoZ.pulsewidth_us(POSDRAW);
 wait_ms(ss_time*2);
diff -r 8e100835b017 -r 87a37f4163bd draw.h
--- a/draw.h	Wed Mar 14 05:17:52 2018 +0000
+++ b/draw.h	Wed Apr 25 02:12:19 2018 +0000
@@ -4,7 +4,7 @@
 #include "mbed.h"
 
 #define MAXPOS 50       // en milimetros
-#define POSDRAW 10   
+#define POSDRAW 30   
 
 
 void init_servo();
diff -r 8e100835b017 -r 87a37f4163bd stepmotor.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stepmotor.cpp	Wed Apr 25 02:12:19 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
diff -r 8e100835b017 -r 87a37f4163bd stepmotor.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stepmotor.h	Wed Apr 25 02:12:19 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