Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of 01-01EjercicioFuncionXY by
draw.cpp@3:3665eb7346e2, 2018-03-12 (annotated)
- Committer:
- Bethory
- Date:
- Mon Mar 12 04:21:09 2018 +0000
- Revision:
- 3:3665eb7346e2
- Child:
- 6:87a37f4163bd
Ah? vamos
Who changed what in which revision?
| User | Revision | Line number | New 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 | 3:3665eb7346e2 | 8 | PwmOut myServoX(PB_3); |
| Bethory | 3:3665eb7346e2 | 9 | PwmOut myServoY(PB_4); |
| Bethory | 3:3665eb7346e2 | 10 | PwmOut myServoZ(PB_5); |
| 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 | 3:3665eb7346e2 | 24 | return int(750+coord*1900/50);// u6 |
| Bethory | 3:3665eb7346e2 | 25 | return 750; |
| Bethory | 3:3665eb7346e2 | 26 | |
| Bethory | 3:3665eb7346e2 | 27 | } |
| Bethory | 3:3665eb7346e2 | 28 | |
| Bethory | 3:3665eb7346e2 | 29 | void sstime(uint8_t x, uint8_t y) |
| Bethory | 3:3665eb7346e2 | 30 | { |
| Bethory | 3:3665eb7346e2 | 31 | double dx=abs(x-posx_old); |
| Bethory | 3:3665eb7346e2 | 32 | double dy=abs(y-posy_old); |
| Bethory | 3:3665eb7346e2 | 33 | double dist= sqrt(dx*dx+dy*dy); |
| Bethory | 3:3665eb7346e2 | 34 | wait_ms((int)(ss_time*dist)); |
| Bethory | 3:3665eb7346e2 | 35 | posx_old =x; |
| Bethory | 3:3665eb7346e2 | 36 | posy_old=y; |
| Bethory | 3:3665eb7346e2 | 37 | |
| Bethory | 3:3665eb7346e2 | 38 | } |
| Bethory | 3:3665eb7346e2 | 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 | } |
