albeiro jojoa / Mbed 2 deprecated martes1303

Dependencies:   mbed

Fork of 01-02EjercicioComunicacionSerial by ferney alberto beltran molina

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #include "draw.h"
00004 #define CM_EJECUTAR 0xff
00005 #define CM_GUARDAR 0xfe
00006 #define CM_VERTEX2D 0xfd
00007 #define CM_DRAW 0xfc
00008 #define CM_NODRAW 0xfb
00009 #define CM_STOP 0xfa
00010 #define CM_END 0xf0
00011 #define MEM_TYPE uint32_t
00012 #define MAXPOS 50       
00013 #define POSDRAW 10 
00014 #define ss_time 100 
00015 #include "math.h"
00016 
00017 
00018 
00019 
00020 void ejecutar(){
00021     command.printf("se esta ejecutando el dibujo..."); 
00022      
00023   
00024     PwmOut myServoX(PB_3);
00025     PwmOut myServoY(PB_4);
00026     PwmOut myServoZ(PB_5);
00027    
00028     
00029     uint32_t  posx_old=0;     // posición anterior del eje X
00030     uint32_t  posy_old=0;     // posición anterior del eje Y
00031     uint32_t  ss_time=100;     // tiempo  de espera para moverse 1 mm en microsegundos 
00032     
00033     void  put_sstime(uint32_t vtime)
00034     {
00035         s_time = vtime;
00036     }
00037     
00038     int coord2us(float coord);
00039     {
00040     if(0 <= coord <= MAXPOS)
00041     return int(750+coord*1900/50);// u6
00042     return 750;
00043     }
00044  
00045    void sstime(uint32_t x, uint32_t y)
00046     {
00047         double dx=abs(x-posx_old);
00048         double dy=abs(y-posy_old);
00049         double dist= sqrt(dx*dx+dy*dy);
00050         wait_ms((int)(ss_time*dist));
00051         posx_old =x;
00052         posy_old=y;    
00053     }
00054  
00055     void vertex2d(uint32_t x, uint32_t y)
00056     { 
00057     int pulseX = coord2us(x);
00058     int pulseY = coord2us(y);
00059     
00060         myServoX.pulsewidth_us(pulseX);
00061         myServoY.pulsewidth_us(pulseY);
00062         sstime(x,y); 
00063     }
00064    
00065    void  home () 
00066    {
00067         nodraw ();
00068         vertex2d ( 0,0 );
00069     }    
00070     
00071     void  draw () ;
00072     {
00073         myServoZ.pulsewidth_us (POSDRAW);
00074         wait_ms (ss_time * 2 );
00075     }
00076  
00077     void  nodraw () ;
00078     {
00079         myServoZ.pulsewidth_us (MAXPOS);
00080         wait_ms (ss_time * 2 );
00081     }
00082     void  initdraw ( float  x,  fLoat  y) ;
00083     {
00084         vertex2d (x, y);
00085         dibujar();
00086     } 
00087     void  init_servo ();
00088     {
00089         myServoX.period_ms ( 20 );
00090         myServoY.period_ms ( 20 );
00091          myServoZ.period_ms ( 20 );
00092     }
00093 
00094  }
00095  
00096 
00097 
00098 
00099 
00100 
00101 
00102 
00103 
00104 
00105 
00106 void guardar(){
00107     command.printf("se inicia el comado de guardar..");    
00108     // ubicar acà el codigo
00109     int mem_head = 0;
00110 int mem_tail = 0;
00111 uint32_t full = 0;
00112  
00113 MEM_TYPE buffer[MEM_SIZE];
00114 Serial command(USBTX, USBRX); 
00115  
00116 uint32_t mem_free()
00117 {
00118  mem_head=0;
00119  full=0;
00120 }
00121  
00122 uint32_t mem_put(MEM_TYPE data)
00123 {
00124  
00125     if (full)
00126         return 1;
00127     buffer[mem_head] = data;
00128     mem_head += 1;
00129     if (mem_head == MEM_SIZE)
00130         full =1;
00131     return 0;
00132 }
00133 uint32_t mem_get(MEM_TYPE* data)
00134 {
00135     if (mem_head == 0)
00136         return 1; 
00137     if (mem_head == mem_tail)
00138         return 1; 
00139     
00140  
00141     *data = buffer[mem_tail];
00142     mem_tail += 1;
00143   
00144     return 0;
00145 }
00146  
00147 int main(){
00148     
00149     mem_put(command.getc());
00150     mem_put(command.getc());
00151     mem_put(command.getc());
00152     mem_put(command.getc());
00153     MEM_TYPE val;
00154     mem_get(&val);
00155     command.putc(val);
00156     }
00157 }
00158 
00159 
00160 
00161 
00162 
00163 
00164 
00165 void init_serial()
00166 {
00167     Serial command(USBTX, USBRX);
00168     command.baud(9600);    
00169     pc.format(8,none,1);
00170 }
00171 int main() {
00172     init_servo();
00173     init_serial();
00174     home();
00175     char read_cc;
00176     while(1)
00177     {
00178         switch (read_cc) {
00179             case  CM_EJECUTAR: ejecutar(); break;
00180             case  CM_GUARDAR: guardar(); break;
00181             default: command.printf("error de comando");break ;      
00182         }
00183     }
00184 }