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

Dependencies:   mbed

Fork of 01_Embebidos by Daniel Vizcaya

Committer:
Bethory
Date:
Wed Apr 25 02:12:19 2018 +0000
Revision:
6:87a37f4163bd
Parent:
3:3665eb7346e2
Ejercicio 2do corte

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bethory 3:3665eb7346e2 1
Bethory 3:3665eb7346e2 2 #include "draw.h"
Bethory 3:3665eb7346e2 3 #include "mbed.h"
Bethory 3:3665eb7346e2 4 #include "math.h"
Bethory 3:3665eb7346e2 5
Bethory 3:3665eb7346e2 6
Bethory 3:3665eb7346e2 7
Bethory 6:87a37f4163bd 8 PwmOut myServoX(D3);
Bethory 6:87a37f4163bd 9 PwmOut myServoY(D4);
Bethory 6:87a37f4163bd 10 PwmOut myServoZ(D5);
Bethory 3:3665eb7346e2 11
Bethory 3:3665eb7346e2 12 uint8_t posx_old=0; // posición anterior del eje X
Bethory 3:3665eb7346e2 13 uint8_t posy_old=0; // posición anterior del eje Y
Bethory 3:3665eb7346e2 14 uint8_t ss_time=100; // tiempo de espera para moverse 1 mm en microsegundos
Bethory 3:3665eb7346e2 15
Bethory 3:3665eb7346e2 16 void put_sstime(uint8_t vtime){
Bethory 3:3665eb7346e2 17 ss_time=vtime;
Bethory 3:3665eb7346e2 18
Bethory 3:3665eb7346e2 19 }
Bethory 3:3665eb7346e2 20
Bethory 3:3665eb7346e2 21 int coord2us(float coord)
Bethory 3:3665eb7346e2 22 {
Bethory 3:3665eb7346e2 23 if(0 <= coord <= MAXPOS)
Bethory 6:87a37f4163bd 24 return int(coord*30+500);// u6
Bethory 6:87a37f4163bd 25 return 500;
Bethory 3:3665eb7346e2 26 }
Bethory 3:3665eb7346e2 27
Bethory 3:3665eb7346e2 28 void sstime(uint8_t x, uint8_t y)
Bethory 3:3665eb7346e2 29 {
Bethory 3:3665eb7346e2 30 double dx=abs(x-posx_old);
Bethory 3:3665eb7346e2 31 double dy=abs(y-posy_old);
Bethory 3:3665eb7346e2 32 double dist= sqrt(dx*dx+dy*dy);
Bethory 3:3665eb7346e2 33 wait_ms((int)(ss_time*dist));
Bethory 3:3665eb7346e2 34 posx_old =x;
Bethory 3:3665eb7346e2 35 posy_old=y;
Bethory 3:3665eb7346e2 36
Bethory 3:3665eb7346e2 37 }
Bethory 3:3665eb7346e2 38
Bethory 6:87a37f4163bd 39
Bethory 3:3665eb7346e2 40 void draw(){
Bethory 3:3665eb7346e2 41 myServoZ.pulsewidth_us(POSDRAW);
Bethory 3:3665eb7346e2 42 wait_ms(ss_time*2);
Bethory 3:3665eb7346e2 43 }
Bethory 3:3665eb7346e2 44
Bethory 3:3665eb7346e2 45 void nodraw(){
Bethory 3:3665eb7346e2 46 myServoZ.pulsewidth_us(MAXPOS);
Bethory 3:3665eb7346e2 47 wait_ms(ss_time*2);
Bethory 3:3665eb7346e2 48 }
Bethory 3:3665eb7346e2 49
Bethory 3:3665eb7346e2 50
Bethory 3:3665eb7346e2 51 void vertex2d(uint8_t x, uint8_t y){
Bethory 3:3665eb7346e2 52
Bethory 3:3665eb7346e2 53 int pulseX = coord2us(x);
Bethory 3:3665eb7346e2 54 int pulseY = coord2us(y);
Bethory 3:3665eb7346e2 55
Bethory 3:3665eb7346e2 56 myServoX.pulsewidth_us(pulseX);
Bethory 3:3665eb7346e2 57 myServoY.pulsewidth_us(pulseY);
Bethory 3:3665eb7346e2 58
Bethory 3:3665eb7346e2 59 sstime(x,y);
Bethory 3:3665eb7346e2 60
Bethory 3:3665eb7346e2 61 }
Bethory 3:3665eb7346e2 62
Bethory 3:3665eb7346e2 63 void initdraw(float x, float y){
Bethory 3:3665eb7346e2 64 vertex2d (x,y);
Bethory 3:3665eb7346e2 65 draw();
Bethory 3:3665eb7346e2 66 }
Bethory 3:3665eb7346e2 67
Bethory 3:3665eb7346e2 68 void home(){
Bethory 3:3665eb7346e2 69 nodraw();
Bethory 3:3665eb7346e2 70 vertex2d(0 ,0);
Bethory 3:3665eb7346e2 71 }
Bethory 3:3665eb7346e2 72
Bethory 3:3665eb7346e2 73
Bethory 3:3665eb7346e2 74
Bethory 3:3665eb7346e2 75 void init_servo()
Bethory 3:3665eb7346e2 76 {
Bethory 3:3665eb7346e2 77 myServoX.period_ms(20);
Bethory 3:3665eb7346e2 78 myServoY.period_ms(20);
Bethory 3:3665eb7346e2 79 myServoZ.period_ms(20);
Bethory 3:3665eb7346e2 80 }