Andres Guio / Mbed 2 deprecated Segundo_corte

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
DANIELOM1916
Date:
Tue Apr 24 02:22:47 2018 +0000
Commit message:
guio

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
main.h 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
memory.cpp Show annotated file Show diff for this revision Revisions of this file
memory.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/draw.cpp	Tue Apr 24 02:22:47 2018 +0000
@@ -0,0 +1,70 @@
+
+#include "draw.h"
+#include "mbed.h"
+#include "math.h"
+
+PwmOut myServoZ(PB_3);
+PwmOut myServoX(PB_4);
+PwmOut myServoY(PB_10);
+
+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=50;     // 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 direcmov(uint8_t x, uint8_t y) //calculo de movimiento en diagonal desde el punto anterior
+{
+    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 vertex2d(uint8_t x, uint8_t y){
+
+    int pulseX = coord2us(x);
+    int pulseY = coord2us(y);
+    
+    myServoX.pulsewidth_us(pulseX);
+    myServoY.pulsewidth_us(pulseY);
+   
+    direcmov(x,y); 
+}
+
+void draw(){
+myServoZ.pulsewidth_us(coord2us(POSDRAW));
+wait_ms(ss_time*10);
+}
+
+void nodraw(){
+myServoZ.pulsewidth_us(coord2us(POSNODRAW));
+wait_ms(ss_time*10);
+}
+
+void initdraw(float x, float y){  //punto inicial de dibujo
+    vertex2d (x,y);
+    draw();
+}
+
+void home(){     // posicion de reposo
+    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 02:22:47 2018 +0000
@@ -0,0 +1,19 @@
+#include "mbed.h"
+
+#ifndef DRAW_H   
+#define DRAW_H  
+
+#define MAXPOS 50       // en milimetros
+#define POSDRAW 50  
+#define POSNODRAW 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 02:22:47 2018 +0000
@@ -0,0 +1,171 @@
+#include "mbed.h"
+#include "main.h"
+
+
+void trasladar (uint8_t ,uint8_t);
+void cuadrante ();
+
+/*
+El sitema tiene tres estados:
+1. Standby: estado que indica que esta en espera de un telecomando (Ejecutar 0 guardar)
+2. Drawing: estado en donde el piccolo eejecuta las ordenes giardadas en el array 
+            de memora hasta encontrar el comando CM_STOP 
+3. Saving:  estado donde el sistema recibe lso datos y lso almacena en memoria acorde 
+            a los comandos de 0xfd, oxfc, 0xfb,0cfa, se sake de este modo cuado se recibe
+            el comando CM_STOP
+            
+todo telecomando debe finalizar con el dato CM_END
+*/
+
+stepmotor smotor1(D7,D8,D9,D10);
+
+Serial command(USBTX, USBRX);
+
+int main() {
+    init_servo();
+    init_serial();
+    draw();
+    nodraw();
+    home();
+    debug_m("inicio \n");
+    uint32_t read_cc;
+    while(1)
+    {
+        read_cc=read_command();
+        switch (read_cc) {
+            case  CM_EXECUTING: 
+                  //  cuadrante(); //eliminar esta funcion y ubicarlo dentro de la funcion drawing().
+                    drawing(); 
+                    break;
+            case  CM_SAVING: saving(); break;
+            
+            default: debug_m("error de comando. \nSe espera  0xFEF0 o 0xFFF0 \n");break ;      
+        }
+    }
+    
+}
+
+
+uint32_t read_command()  //memoria dinamica
+{
+   /* retorna los byte recibidos concatenados en un entero, se reciben maximo 4 bytes,
+    recibe hasta que encuetra  un fin de comando "CM_END".
+    Ejemplo: para el comando drawing se 
+    espera un byte,  por lo que al recibir 0xFF y 0xF0 la funcióm  retorna el 0xff
+    siempre y cuando se reciba el fin de dato  F0 de lo contrario retorna un cero
+    para el caso del comando vertex2d se espera  recibir 3 bytes, 1 del comando 
+    y dos bytes para x y y, se retorna la concatenación de los tres bytes  siempre y
+    cuando el cuarto byte sea CM_END de lo contrario retorna un cero 
+    */
+    
+    uint32_t val=0;
+    uint8_t  cnt=0;
+    
+    char endc=command.getc();
+    
+    while(endc != CM_END && cnt <4) {
+    if(endc!=CM_END)   
+        val=((val<<8) +endc);
+     endc=command.getc();
+     cnt++;
+    }   
+    if(endc==CM_END)   
+        return val;
+    return 0;   //al retornar 0 indica que no se recibe el comando  
+}
+void init_serial()
+{
+    command.baud(9600);    
+}
+
+void drawing(){
+    /* la funcion se ejecuta siempre y cuando exista datos validos para leer de 
+    memoria */ 
+    debug_m("se esta ejecutando el dibujo... \n");       
+     
+    uint8_t error=0;
+    MEM_TYPE dato;
+
+    tail_reset();  //borra la cabeza de memoria
+
+    while(error==0){
+        error = mem_get(&dato);
+        if (error==0) {
+            switch (dato) {
+            case  CM_DRAW: 
+                    debug_m("-> Baja Z\n");
+                    draw();
+                    break ;     
+            case  CM_NODRAW: 
+                    debug_m("-> Sube Z\n");
+                    nodraw();
+                    break ; 
+                     
+            default: 
+                int y      = (uint8_t)(dato);
+                int x      = (uint8_t)(dato>>8);
+                char ncomm  = (uint8_t)(dato>>16);
+                
+                 
+                if (ncomm == CM_VERTEX2D) {
+                    debug_m("-> Mover piccolo x a %d y y a %d \n",x,x, y);
+                    vertex2d(x,y);
+                }
+                
+                if (ncomm == CM_MOVER) {
+                    debug_m("-> cuadrante %d y sentido %d \n",x,x, y);
+                    trasladar (x,y);                
+                }    
+                break;   
+             }
+         }        
+    }  
+    
+    debug_m("fin del comando ..\n");    
+    
+}
+
+
+void saving(){
+    debug_m("se inicia el comando guardar..\n");    
+    
+    MEM_TYPE dato=0;
+    mem_free();
+    while(dato !=CM_STOP){
+        dato = read_command();
+        if (dato !=CM_STOP)
+               mem_put(dato);
+    }  
+    debug_m("fin del comando guardar..\n");    
+    
+}
+
+void trasladar(uint8_t cuadrante, uint8_t sentido){   
+
+        uint32_t speed=1500;
+        int i=0;
+        
+        int a = cuadrante;
+        int cw = sentido;
+     
+        while(a>i)
+        {
+         smotor1.set_speed(speed);
+    
+       
+         command.printf("velocidad del motor:  %d, %f rpm, CW=%d  \n",smotor1.get_speed(), (60/((smotor1.get_speed()*4.096)/1000)),cw);
+        
+         
+         smotor1.step(4096,cw);   
+         
+                 
+         wait(1);
+         i++;  
+       }   
+}
+
+void debug_m(char *s , ... ){
+    #if DEBUG
+    command.printf(s);
+    #endif  
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Tue Apr 24 02:22:47 2018 +0000
@@ -0,0 +1,65 @@
+#ifndef MAIN_H   
+#define MAIN_H  
+
+#include "draw.h"
+#include "memory.h"
+#include "stepmotor.h"
+
+//******************************************************************************
+// Desarrollado por ferney beltran fbeltran@ecci.edu.co y modificado para uso 
+// estudiantil respetando derechos de autor por estudiantes de ing. mecatronica.
+//******************************************************************************
+
+//*****************************************************************************
+//  COMANDOS
+//  |POS_0|POS_1|POS_2| POS_3 | 
+//  | #C  |  a  |  b  |   c   |
+//
+// #C       ->  Indica el comando FF, FE, ...
+// a        ->  es el fin de comando F0 en otros casos es el valor de la coord X
+//b         ->  Coordenada Y
+// c        ->   fin de comando F0
+
+// Nota: El fin  de comando no se almacena 
+//******************************************************************************
+
+/*
+prueba 1
+ff f0 
+
+prueba 2
+fe f0 fd 00 00 f0 fc f0 fd 00 32 f0 fd 32 32 f0 fd 32 00 f0 fd 00 00 f0 fb f0  fa f0
+ff f0
+
+prueba 3
+fe f0 fd 00 00 f0 fc f0 fd 00 0A f0 fd 32 00 f0 fd 32 14 f0 fd 00 14 f0 fd 00 1E f0 fd 32 1E f0 fd 32 28 f0 fd 00 28 f0 fa f0
+ff f0
+
+
+
+*/
+
+#define DEBUG 1
+
+
+void debug_m(char *s , ... );
+uint32_t read_command();
+void init_serial();
+void drawing();
+void saving();
+
+
+
+
+/********************* PARAMETROS PARA DEFINIR  EL  COMMANDO ******************/
+
+#define CM_EXECUTING  0xff
+#define CM_SAVING   0xfe
+#define CM_VERTEX2D 0xfd
+#define CM_DRAW 0xfc
+#define CM_NODRAW 0xfb
+#define CM_STOP 0xfa
+#define CM_END 0xf0
+#define CM_MOVER 0xf9
+
+#endif //  MAIN_H 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Apr 24 02:22:47 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/994bdf8177cb
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/memory.cpp	Tue Apr 24 02:22:47 2018 +0000
@@ -0,0 +1,43 @@
+#include "mbed.h"
+#include "memory.h"
+
+int mem_head = 0;  //cabeza de memoria
+int mem_tail = 0;  //cabeza de cola
+uint8_t full = 0;
+
+MEM_TYPE buffer[MEM_SIZE]; // reserve memoria tamaño 10
+
+void tail_reset()  // reinicio el valor de la cola en 0
+{
+ mem_tail=0;
+}
+
+void mem_free()  // reinicio el valor de la cabeza y full a 0
+{
+ mem_head=0;
+ full=0;
+}
+
+uint8_t mem_put(MEM_TYPE data) // poner dato en memoria "Escribir dato"
+{
+     if (full)
+        return 1;
+    buffer[mem_head] = data; // envio dato a grabar 
+    mem_head += 1;           // aumente la cabeza en 1
+    if (mem_head == MEM_SIZE)  // envio error memoria llena si cabeza es igual a 10(tamaño total de la memoria)
+        full =1;
+    return 0;
+}
+
+uint8_t mem_get(MEM_TYPE* data) // leer dato en memoria
+{
+    if (mem_head == 0) // envio error no hay nada en memoria
+        return 1; 
+    if (mem_head == mem_tail) // envio error memoria llena
+        return 1; 
+    
+    *data = buffer[mem_tail];  // apuntador selecciona memoria a leer
+    mem_tail += 1;            //
+  
+    return 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/memory.h	Tue Apr 24 02:22:47 2018 +0000
@@ -0,0 +1,18 @@
+#ifndef MEMORY_ARRAY_H   
+#define MEMORY_ARRAY_H  
+
+#include "mbed.h"
+
+#define MEM_SIZE 10
+#define MEM_TYPE uint32_t
+
+
+
+void tail_reset();
+void  mem_free();
+uint8_t mem_put(MEM_TYPE data);
+uint8_t mem_get(MEM_TYPE* data);
+
+
+
+#endif //  MEMORY_H 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stepmotor.cpp	Tue Apr 24 02:22:47 2018 +0000
@@ -0,0 +1,54 @@
+#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 02:22:47 2018 +0000
@@ -0,0 +1,24 @@
+#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