Vanesa Lara Cruz / Mbed 2 deprecated serial-protocol-V2

Dependencies:   mbed

Fork of 02_LAB_serial_protocol by ferney alberto beltran molina

draw.cpp

Committer:
fabeltranm
Date:
2017-10-12
Revision:
14:124051c4524a
Parent:
13:4226825e2060
Child:
15:92cfae44483c

File content as of revision 14:124051c4524a:


#include "draw.h"
#include "mbed.h"
#include "math.h"

DigitalOut led(LED1);

PwmOut myServoX(PB_3);
PwmOut myServoY(PB_4);
PwmOut myServoZ(PB_5);

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;
    
}
void led_on(uint16_t ts, uint16_t tms )
{

                led=1;  
                wait(ts);wait_ms(tms);
                led=0;   
                 
}

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(){
myServoZ.pulsewidth_us(POSDRAW);
wait_ms(ss_time*2);
}

void nodraw(){
myServoZ.pulsewidth_us(MAXPOS);
wait_ms(ss_time*2);
}


void vertex2d(uint8_t x, uint8_t y){

    int pulseX = coord2us(x);
    int pulseY = coord2us(y);
    
    myServoX.pulsewidth_us(pulseX);
    myServoY.pulsewidth_us(pulseY);
   
    wait_ms(ss_time); //TODO: CALCULAR EL TIEMPO DE DONDE  VIENE 

}

void initdraw(float x, float y){
    vertex2d (x,y);
    draw();
}

void home(){
    nodraw();
    vertex2d(0 ,0);
}    



void line(float xi, float yi, float xf,  float yf)
{   
    float xp; float yp;
    float m= (yf-yi)/(xf-xi);
    float b=yf-m*xf;
    
    initdraw(xi,yi);
    for (xp =xi;xp<=xf;xp+=RSTEP)
        {
         yp =m*xp+b;
         vertex2d(xp,yp);
        }
    nodraw();
 
}


void rectangle(float x, float y, float width,float height) {
    
    initdraw(x,y);
    
    vertex2d(x+width, y);
    vertex2d(x+width, y+height);
    vertex2d(x, y+height);
    vertex2d(x, y);
    
    nodraw();
 
}

void circle(float x, float y, float radio)
{
 initdraw(x+radio,y);
 // coloque codigo aca 
 nodraw();

}