単体試験用
Dependencies: mbed
main.cpp@0:b0c47a96f7cd, 2017-12-08 (annotated)
- Committer:
- emcu
- Date:
- Fri Dec 08 11:15:39 2017 +0000
- Revision:
- 0:b0c47a96f7cd
- Child:
- 1:94062f1bb223
BY: www.emcu.eu; Date: 08-12-2017; Ver. 1.0; Example for Servo Motor: MG996R ; This example was tested on NUCLEO-F401RE; If you use a terminal emulator (9600,8,1) on your PC ; you also see the commands sends do RC motor.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emcu | 0:b0c47a96f7cd | 1 | /* |
emcu | 0:b0c47a96f7cd | 2 | |
emcu | 0:b0c47a96f7cd | 3 | BY: www.emcu.eu |
emcu | 0:b0c47a96f7cd | 4 | Date: 08-12-2017 |
emcu | 0:b0c47a96f7cd | 5 | Ver. 1.0 |
emcu | 0:b0c47a96f7cd | 6 | Example for Servo Motor: MG996R |
emcu | 0:b0c47a96f7cd | 7 | This example was tested on NUCLEO-F401RE |
emcu | 0:b0c47a96f7cd | 8 | If you use a terminal emulator (9600,8,1) on your PC |
emcu | 0:b0c47a96f7cd | 9 | you also see the commands sends do RC motor. |
emcu | 0:b0c47a96f7cd | 10 | |
emcu | 0:b0c47a96f7cd | 11 | --------------------------------------------------------------------------- |
emcu | 0:b0c47a96f7cd | 12 | |
emcu | 0:b0c47a96f7cd | 13 | Servo Motor: |
emcu | 0:b0c47a96f7cd | 14 | MG996R - http://www.electronicoscaldas.com/datasheet/MG996R_Tower-Pro.pdf |
emcu | 0:b0c47a96f7cd | 15 | |
emcu | 0:b0c47a96f7cd | 16 | RED - + Vcc from 4,5 to 7,2V |
emcu | 0:b0c47a96f7cd | 17 | BLACK - GND |
emcu | 0:b0c47a96f7cd | 18 | ORANGE - PWM |
emcu | 0:b0c47a96f7cd | 19 | |
emcu | 0:b0c47a96f7cd | 20 | Duty - example: 1.9 (mS) |
emcu | 0:b0c47a96f7cd | 21 | \ / |
emcu | 0:b0c47a96f7cd | 22 | \/ |
emcu | 0:b0c47a96f7cd | 23 | __ |
emcu | 0:b0c47a96f7cd | 24 | __| |_____________ |
emcu | 0:b0c47a96f7cd | 25 | ... 20mS...... |
emcu | 0:b0c47a96f7cd | 26 | |
emcu | 0:b0c47a96f7cd | 27 | For rotate to right use value 1 for Duty |
emcu | 0:b0c47a96f7cd | 28 | For rotate to left use value 5 for Duty |
emcu | 0:b0c47a96f7cd | 29 | |
emcu | 0:b0c47a96f7cd | 30 | Vedere queste spiegazioni: |
emcu | 0:b0c47a96f7cd | 31 | http://www.adrirobot.it/servotester/il_servomotore.htm |
emcu | 0:b0c47a96f7cd | 32 | |
emcu | 0:b0c47a96f7cd | 33 | |
emcu | 0:b0c47a96f7cd | 34 | |
emcu | 0:b0c47a96f7cd | 35 | NOTE: |
emcu | 0:b0c47a96f7cd | 36 | |
emcu | 0:b0c47a96f7cd | 37 | For rotate to Right (DESTRA) use this command: |
emcu | 0:b0c47a96f7cd | 38 | Move(Right); |
emcu | 0:b0c47a96f7cd | 39 | For rotate to Left (SINISTRA) use this command: |
emcu | 0:b0c47a96f7cd | 40 | Move(Left); |
emcu | 0:b0c47a96f7cd | 41 | |
emcu | 0:b0c47a96f7cd | 42 | You also has the possibility to send a command like below: |
emcu | 0:b0c47a96f7cd | 43 | Move(1.5): |
emcu | 0:b0c47a96f7cd | 44 | |
emcu | 0:b0c47a96f7cd | 45 | */ |
emcu | 0:b0c47a96f7cd | 46 | |
emcu | 0:b0c47a96f7cd | 47 | #include "mbed.h" |
emcu | 0:b0c47a96f7cd | 48 | |
emcu | 0:b0c47a96f7cd | 49 | DigitalOut Drive(PA_10); |
emcu | 0:b0c47a96f7cd | 50 | |
emcu | 0:b0c47a96f7cd | 51 | #define PWMoff 20 // Waiting for end PWD |
emcu | 0:b0c47a96f7cd | 52 | #define Right 1 // Move to Right -> DESTRA or DX |
emcu | 0:b0c47a96f7cd | 53 | #define Left 5 // Move to LEFT <- SINISTRA or SX |
emcu | 0:b0c47a96f7cd | 54 | #define DLY 1 |
emcu | 0:b0c47a96f7cd | 55 | |
emcu | 0:b0c47a96f7cd | 56 | void Move(double); |
emcu | 0:b0c47a96f7cd | 57 | |
emcu | 0:b0c47a96f7cd | 58 | double p=0; |
emcu | 0:b0c47a96f7cd | 59 | |
emcu | 0:b0c47a96f7cd | 60 | |
emcu | 0:b0c47a96f7cd | 61 | int main() |
emcu | 0:b0c47a96f7cd | 62 | { |
emcu | 0:b0c47a96f7cd | 63 | int n=0; |
emcu | 0:b0c47a96f7cd | 64 | |
emcu | 0:b0c47a96f7cd | 65 | printf("By: www.emcu.eu - Ver.1.0 \n\r"); |
emcu | 0:b0c47a96f7cd | 66 | |
emcu | 0:b0c47a96f7cd | 67 | printf("Rotate all to -> \n\r"); |
emcu | 0:b0c47a96f7cd | 68 | // Sposta tutto a DX |
emcu | 0:b0c47a96f7cd | 69 | for(n=0; n<20; n++) |
emcu | 0:b0c47a96f7cd | 70 | Move(Right); |
emcu | 0:b0c47a96f7cd | 71 | |
emcu | 0:b0c47a96f7cd | 72 | wait(DLY); |
emcu | 0:b0c47a96f7cd | 73 | |
emcu | 0:b0c47a96f7cd | 74 | printf("Rotate all to <- \n\r"); |
emcu | 0:b0c47a96f7cd | 75 | // Sposta tutto a SX |
emcu | 0:b0c47a96f7cd | 76 | for(n=0; n<20; n++) |
emcu | 0:b0c47a96f7cd | 77 | Move(Left); |
emcu | 0:b0c47a96f7cd | 78 | |
emcu | 0:b0c47a96f7cd | 79 | wait(DLY); |
emcu | 0:b0c47a96f7cd | 80 | |
emcu | 0:b0c47a96f7cd | 81 | printf("Move to 10 times to ->\n\r"); |
emcu | 0:b0c47a96f7cd | 82 | for(n=0; n<10; n++) |
emcu | 0:b0c47a96f7cd | 83 | Move(Right); |
emcu | 0:b0c47a96f7cd | 84 | |
emcu | 0:b0c47a96f7cd | 85 | wait(DLY); |
emcu | 0:b0c47a96f7cd | 86 | |
emcu | 0:b0c47a96f7cd | 87 | printf("END \n\r\n\r"); |
emcu | 0:b0c47a96f7cd | 88 | } |
emcu | 0:b0c47a96f7cd | 89 | |
emcu | 0:b0c47a96f7cd | 90 | |
emcu | 0:b0c47a96f7cd | 91 | void Move(double ON) |
emcu | 0:b0c47a96f7cd | 92 | { |
emcu | 0:b0c47a96f7cd | 93 | |
emcu | 0:b0c47a96f7cd | 94 | Drive=1; |
emcu | 0:b0c47a96f7cd | 95 | wait_ms(ON); |
emcu | 0:b0c47a96f7cd | 96 | Drive=0; |
emcu | 0:b0c47a96f7cd | 97 | wait_ms(20); |
emcu | 0:b0c47a96f7cd | 98 | |
emcu | 0:b0c47a96f7cd | 99 | } |
emcu | 0:b0c47a96f7cd | 100 | |
emcu | 0:b0c47a96f7cd | 101 | |
emcu | 0:b0c47a96f7cd | 102 | |
emcu | 0:b0c47a96f7cd | 103 | /* |
emcu | 0:b0c47a96f7cd | 104 | |
emcu | 0:b0c47a96f7cd | 105 | // Sposta tutto a DX - Move all to RIGHT |
emcu | 0:b0c47a96f7cd | 106 | for(n=0; n<200; n++) |
emcu | 0:b0c47a96f7cd | 107 | Move(Right); |
emcu | 0:b0c47a96f7cd | 108 | |
emcu | 0:b0c47a96f7cd | 109 | wait(DLY); |
emcu | 0:b0c47a96f7cd | 110 | |
emcu | 0:b0c47a96f7cd | 111 | // Sposta tutto a SX - Move all to LEFT |
emcu | 0:b0c47a96f7cd | 112 | for(n=0; n<200; n++) |
emcu | 0:b0c47a96f7cd | 113 | Move(Left); |
emcu | 0:b0c47a96f7cd | 114 | |
emcu | 0:b0c47a96f7cd | 115 | wait(DLY); |
emcu | 0:b0c47a96f7cd | 116 | |
emcu | 0:b0c47a96f7cd | 117 | */ |
emcu | 0:b0c47a96f7cd | 118 | |
emcu | 0:b0c47a96f7cd | 119 | /* |
emcu | 0:b0c47a96f7cd | 120 | p=0.4; |
emcu | 0:b0c47a96f7cd | 121 | myservo = p; |
emcu | 0:b0c47a96f7cd | 122 | printf("P = %.2f \n\r", p); |
emcu | 0:b0c47a96f7cd | 123 | wait(0.2); |
emcu | 0:b0c47a96f7cd | 124 | */ |