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-01SolucionFuncionXY by
main.cpp@5:9d27f2f6b1dc, 2018-09-04 (annotated)
- Committer:
- andrescas
- Date:
- Tue Sep 04 01:34:37 2018 +0000
- Revision:
- 5:9d27f2f6b1dc
- Parent:
- 4:29a8b1df7fe2
servos
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| fabeltranm | 0:7ba873404776 | 1 | #include "mbed.h" |
| andrescas | 5:9d27f2f6b1dc | 2 | #define MAXPOS 180 // en milimetros |
| fabeltranm | 0:7ba873404776 | 3 | #define SS_TIME 100 // en microsegundos |
| fabeltranm | 3:fe8e265eaf6e | 4 | #define POSDRAW 10 |
| fabeltranm | 0:7ba873404776 | 5 | |
| andrescas | 5:9d27f2f6b1dc | 6 | PwmOut myServo1(PB_3); |
| andrescas | 5:9d27f2f6b1dc | 7 | PwmOut myServo2(PB_4); |
| andrescas | 5:9d27f2f6b1dc | 8 | PwmOut myServo3(PB_5); |
| andrescas | 5:9d27f2f6b1dc | 9 | PwmOut myServo4(PB_8); |
| andrescas | 5:9d27f2f6b1dc | 10 | PwmOut myServo5(PB_8); |
| andrescas | 5:9d27f2f6b1dc | 11 | PwmOut myServo6(PA_5); |
| andrescas | 5:9d27f2f6b1dc | 12 | PwmOut myServo7(PA_6); |
| andrescas | 5:9d27f2f6b1dc | 13 | PwmOut myServo8(PA_7); |
| fabeltranm | 0:7ba873404776 | 14 | |
| fabeltranm | 3:fe8e265eaf6e | 15 | int coord2us(float coord) |
| fabeltranm | 3:fe8e265eaf6e | 16 | { |
| fabeltranm | 3:fe8e265eaf6e | 17 | if(0 <= coord <= MAXPOS) |
| andrescas | 5:9d27f2f6b1dc | 18 | return int(750+coord*1900/180);// u6 |
| fabeltranm | 3:fe8e265eaf6e | 19 | return 750; |
| fabeltranm | 3:fe8e265eaf6e | 20 | |
| fabeltranm | 3:fe8e265eaf6e | 21 | } |
| andrescas | 5:9d27f2f6b1dc | 22 | void mov1(float A){ |
| andrescas | 5:9d27f2f6b1dc | 23 | int pulseA = coord2us(A); |
| andrescas | 5:9d27f2f6b1dc | 24 | myServo1.pulsewidth_us(pulseA); |
| andrescas | 5:9d27f2f6b1dc | 25 | wait_ms(SS_TIME); |
| andrescas | 5:9d27f2f6b1dc | 26 | } |
| andrescas | 5:9d27f2f6b1dc | 27 | void mov2(float b){ |
| andrescas | 5:9d27f2f6b1dc | 28 | int pulseb = coord2us(b); |
| andrescas | 5:9d27f2f6b1dc | 29 | myServo2.pulsewidth_us(pulseb); |
| andrescas | 5:9d27f2f6b1dc | 30 | wait_ms(SS_TIME); |
| fabeltranm | 2:878cb2697ab1 | 31 | } |
| andrescas | 5:9d27f2f6b1dc | 32 | void mov3(float c){ |
| andrescas | 5:9d27f2f6b1dc | 33 | int pulsec = coord2us(c); |
| andrescas | 5:9d27f2f6b1dc | 34 | myServo3.pulsewidth_us(pulsec); |
| andrescas | 5:9d27f2f6b1dc | 35 | wait_ms(SS_TIME); |
| andrescas | 5:9d27f2f6b1dc | 36 | } |
| andrescas | 5:9d27f2f6b1dc | 37 | void mov4(float d){ |
| andrescas | 5:9d27f2f6b1dc | 38 | int pulsed = coord2us(d); |
| andrescas | 5:9d27f2f6b1dc | 39 | myServo4.pulsewidth_us(pulsed); |
| andrescas | 5:9d27f2f6b1dc | 40 | wait_ms(SS_TIME); |
| fabeltranm | 2:878cb2697ab1 | 41 | } |
| andrescas | 5:9d27f2f6b1dc | 42 | void mov5(float e){ |
| andrescas | 5:9d27f2f6b1dc | 43 | int pulsee = coord2us(e); |
| andrescas | 5:9d27f2f6b1dc | 44 | myServo5.pulsewidth_us(pulsee); |
| andrescas | 5:9d27f2f6b1dc | 45 | wait_ms(SS_TIME); |
| andrescas | 5:9d27f2f6b1dc | 46 | } |
| andrescas | 5:9d27f2f6b1dc | 47 | void mov6(float f){ |
| andrescas | 5:9d27f2f6b1dc | 48 | int pulsef = coord2us(f); |
| andrescas | 5:9d27f2f6b1dc | 49 | myServo6.pulsewidth_us(pulsef); |
| fabeltranm | 0:7ba873404776 | 50 | wait_ms(SS_TIME); |
| andrescas | 5:9d27f2f6b1dc | 51 | } |
| andrescas | 5:9d27f2f6b1dc | 52 | void mov7(float g){ |
| andrescas | 5:9d27f2f6b1dc | 53 | int pulseg = coord2us(g); |
| andrescas | 5:9d27f2f6b1dc | 54 | myServo7.pulsewidth_us(pulseg); |
| andrescas | 5:9d27f2f6b1dc | 55 | wait_ms(SS_TIME); |
| fabeltranm | 0:7ba873404776 | 56 | } |
| andrescas | 5:9d27f2f6b1dc | 57 | void mov8(float h){ |
| andrescas | 5:9d27f2f6b1dc | 58 | int pulseh = coord2us(h); |
| andrescas | 5:9d27f2f6b1dc | 59 | myServo8.pulsewidth_us(pulseh); |
| andrescas | 5:9d27f2f6b1dc | 60 | wait_ms(SS_TIME); |
| andrescas | 5:9d27f2f6b1dc | 61 | } |
| fabeltranm | 3:fe8e265eaf6e | 62 | |
| fabeltranm | 0:7ba873404776 | 63 | int main() { |
| fabeltranm | 0:7ba873404776 | 64 | // configuracion de periodo |
| andrescas | 5:9d27f2f6b1dc | 65 | myServo1.period_ms(20); |
| andrescas | 5:9d27f2f6b1dc | 66 | myServo2.period_ms(20); |
| andrescas | 5:9d27f2f6b1dc | 67 | myServo3.period_ms(20); |
| andrescas | 5:9d27f2f6b1dc | 68 | myServo4.period_ms(20); |
| andrescas | 5:9d27f2f6b1dc | 69 | myServo5.period_ms(20); |
| andrescas | 5:9d27f2f6b1dc | 70 | myServo6.period_ms(20); |
| andrescas | 5:9d27f2f6b1dc | 71 | myServo7.period_ms(20); |
| andrescas | 5:9d27f2f6b1dc | 72 | myServo8.period_ms(20); |
| andrescas | 5:9d27f2f6b1dc | 73 | int pos=0; |
| andrescas | 5:9d27f2f6b1dc | 74 | int servmot=0; |
| fabeltranm | 0:7ba873404776 | 75 | while(1) |
| andrescas | 5:9d27f2f6b1dc | 76 | { //definir servo luego posicion |
| andrescas | 5:9d27f2f6b1dc | 77 | servmot=1; |
| andrescas | 5:9d27f2f6b1dc | 78 | pos=50; |
| fabeltranm | 0:7ba873404776 | 79 | wait(2); |
| andrescas | 5:9d27f2f6b1dc | 80 | if (servmot=1){ |
| andrescas | 5:9d27f2f6b1dc | 81 | mov1(pos);} |
| andrescas | 5:9d27f2f6b1dc | 82 | |
| andrescas | 5:9d27f2f6b1dc | 83 | if (servmot=2){ |
| andrescas | 5:9d27f2f6b1dc | 84 | mov2(pos);} |
| andrescas | 5:9d27f2f6b1dc | 85 | |
| andrescas | 5:9d27f2f6b1dc | 86 | if (servmot=3){ |
| andrescas | 5:9d27f2f6b1dc | 87 | mov3(pos);} |
| andrescas | 5:9d27f2f6b1dc | 88 | |
| andrescas | 5:9d27f2f6b1dc | 89 | if (servmot=4){ |
| andrescas | 5:9d27f2f6b1dc | 90 | mov4(pos);} |
| andrescas | 5:9d27f2f6b1dc | 91 | |
| andrescas | 5:9d27f2f6b1dc | 92 | if (servmot=5){ |
| andrescas | 5:9d27f2f6b1dc | 93 | mov5(pos);} |
| andrescas | 5:9d27f2f6b1dc | 94 | |
| andrescas | 5:9d27f2f6b1dc | 95 | if (servmot=6){ |
| andrescas | 5:9d27f2f6b1dc | 96 | mov6(pos);} |
| andrescas | 5:9d27f2f6b1dc | 97 | |
| andrescas | 5:9d27f2f6b1dc | 98 | if (servmot=7){ |
| andrescas | 5:9d27f2f6b1dc | 99 | mov7(pos);} |
| andrescas | 5:9d27f2f6b1dc | 100 | |
| andrescas | 5:9d27f2f6b1dc | 101 | if (servmot=8){ |
| andrescas | 5:9d27f2f6b1dc | 102 | mov8(pos);} |
| fabeltranm | 0:7ba873404776 | 103 | } |
| fabeltranm | 0:7ba873404776 | 104 | |
| andrescas | 5:9d27f2f6b1dc | 105 | } |
