ferney alberto beltran molina
/
00_LAB_COMMAND_SERVO
mover servomotore por telecomandos
main.cpp@5:115f329c72ab, 2019-02-25 (annotated)
- Committer:
- fabeltranm
- Date:
- Mon Feb 25 12:31:53 2019 +0000
- Revision:
- 5:115f329c72ab
- Parent:
- 4:136a0aeb7d51
- Child:
- 6:24f37d9e0c57
add help
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fabeltranm | 0:a9181441db9b | 1 | #include "mbed.h" |
fabeltranm | 0:a9181441db9b | 2 | |
fabeltranm | 0:a9181441db9b | 3 | /***************************************************************************** |
fabeltranm | 5:115f329c72ab | 4 | generar un programa que controle por el puerto serial el grado de 4 servo motores. |
fabeltranm | 5:115f329c72ab | 5 | por medio de la comunicacion serial el comando es |
fabeltranm | 0:a9181441db9b | 6 | |
fabeltranm | 0:a9181441db9b | 7 | | | | | |
fabeltranm | 2:fda902bed8c6 | 8 | | INITCMD | N_motor | N_grados | |
fabeltranm | 0:a9181441db9b | 9 | | 0xff | 0x01- 0x04 | 0x00 - 0xb4 | |
fabeltranm | 0:a9181441db9b | 10 | |
fabeltranm | 0:a9181441db9b | 11 | para enviar los comandos usar el programa Coolterm |
fabeltranm | 5:115f329c72ab | 12 | |
fabeltranm | 5:115f329c72ab | 13 | # para el servo motor se debe modificar el ciclo util del PWM SEGUN: |
fabeltranm | 5:115f329c72ab | 14 | # ________ ________ |
fabeltranm | 5:115f329c72ab | 15 | # ___| |____________| |___________ |
fabeltranm | 5:115f329c72ab | 16 | # <-width-> |
fabeltranm | 5:115f329c72ab | 17 | # <-------period 20ms---> |
fabeltranm | 5:115f329c72ab | 18 | # period = 20 ms |
fabeltranm | 5:115f329c72ab | 19 | # width = 1000us y 2000us |
fabeltranm | 5:115f329c72ab | 20 | # 1000us para 0 grados |
fabeltranm | 5:115f329c72ab | 21 | # 2000us para 180 grados |
fabeltranm | 5:115f329c72ab | 22 | # |
fabeltranm | 5:115f329c72ab | 23 | |
fabeltranm | 0:a9181441db9b | 24 | *****************************************************************************/ |
fabeltranm | 0:a9181441db9b | 25 | |
fabeltranm | 0:a9181441db9b | 26 | |
fabeltranm | 0:a9181441db9b | 27 | Serial command(USBTX, USBRX); |
fabeltranm | 0:a9181441db9b | 28 | PwmOut myservo1(PB_4); |
fabeltranm | 0:a9181441db9b | 29 | /*INGRESE L A CONFIGURACION DE LOS MOTORES*/ |
fabeltranm | 0:a9181441db9b | 30 | |
fabeltranm | 2:fda902bed8c6 | 31 | #define INITCMD 0xFF |
fabeltranm | 3:ed8f41c75a80 | 32 | #define DEGREES_MAX 180 |
fabeltranm | 5:115f329c72ab | 33 | |
fabeltranm | 1:b473a0ad2b83 | 34 | // definición de las variables globales |
fabeltranm | 0:a9181441db9b | 35 | |
fabeltranm | 5:115f329c72ab | 36 | uint8_t N_motor; // variable almacena el número de motor en leer_datos() |
fabeltranm | 5:115f329c72ab | 37 | uint8_t N_grados; // varable almacena los grados que se mueve el motor en leer_datos() |
fabeltranm | 0:a9181441db9b | 38 | |
fabeltranm | 0:a9181441db9b | 39 | |
fabeltranm | 3:ed8f41c75a80 | 40 | // definición de las funciones |
fabeltranm | 0:a9181441db9b | 41 | void setup_uart(); |
fabeltranm | 1:b473a0ad2b83 | 42 | void setup_servo(); |
fabeltranm | 4:136a0aeb7d51 | 43 | |
fabeltranm | 1:b473a0ad2b83 | 44 | void mover_servo(uint8_t motor, uint8_t gradss); |
fabeltranm | 4:136a0aeb7d51 | 45 | uint32_t degrees2usec(uint8_t grados){ |
fabeltranm | 4:136a0aeb7d51 | 46 | |
fabeltranm | 1:b473a0ad2b83 | 47 | void leer_datos(); |
fabeltranm | 0:a9181441db9b | 48 | |
fabeltranm | 4:136a0aeb7d51 | 49 | |
fabeltranm | 4:136a0aeb7d51 | 50 | |
fabeltranm | 0:a9181441db9b | 51 | int main() { |
fabeltranm | 1:b473a0ad2b83 | 52 | |
fabeltranm | 1:b473a0ad2b83 | 53 | setup_uart(); |
fabeltranm | 1:b473a0ad2b83 | 54 | setup_servo(); |
fabeltranm | 4:136a0aeb7d51 | 55 | //command.printf("inicio de programa"); |
fabeltranm | 1:b473a0ad2b83 | 56 | while(1){ |
fabeltranm | 1:b473a0ad2b83 | 57 | leer_datos(); |
fabeltranm | 2:fda902bed8c6 | 58 | mover_servo(N_motor, N_grados); |
fabeltranm | 4:136a0aeb7d51 | 59 | } |
fabeltranm | 1:b473a0ad2b83 | 60 | } |
fabeltranm | 1:b473a0ad2b83 | 61 | |
fabeltranm | 1:b473a0ad2b83 | 62 | |
fabeltranm | 1:b473a0ad2b83 | 63 | |
fabeltranm | 1:b473a0ad2b83 | 64 | void setup_uart(){ |
fabeltranm | 1:b473a0ad2b83 | 65 | command.baud(115200); |
fabeltranm | 1:b473a0ad2b83 | 66 | } |
fabeltranm | 1:b473a0ad2b83 | 67 | |
fabeltranm | 1:b473a0ad2b83 | 68 | void setup_servo(){ |
fabeltranm | 0:a9181441db9b | 69 | myservo1.period_ms(20); |
fabeltranm | 1:b473a0ad2b83 | 70 | myservo1.pulsewidth_us(1000); |
fabeltranm | 1:b473a0ad2b83 | 71 | } |
fabeltranm | 1:b473a0ad2b83 | 72 | |
fabeltranm | 1:b473a0ad2b83 | 73 | void leer_datos(){ |
fabeltranm | 2:fda902bed8c6 | 74 | while(command.getc()!= INITCMD); |
fabeltranm | 2:fda902bed8c6 | 75 | N_motor=command.getc(); |
fabeltranm | 2:fda902bed8c6 | 76 | N_grados=command.getc(); |
fabeltranm | 0:a9181441db9b | 77 | |
fabeltranm | 1:b473a0ad2b83 | 78 | } |
fabeltranm | 1:b473a0ad2b83 | 79 | |
fabeltranm | 1:b473a0ad2b83 | 80 | |
fabeltranm | 4:136a0aeb7d51 | 81 | |
fabeltranm | 4:136a0aeb7d51 | 82 | |
fabeltranm | 4:136a0aeb7d51 | 83 | void mover_servo(uint8_t motor, uint8_t grados){ |
fabeltranm | 4:136a0aeb7d51 | 84 | |
fabeltranm | 4:136a0aeb7d51 | 85 | uint32_t dpulse=0; |
fabeltranm | 4:136a0aeb7d51 | 86 | |
fabeltranm | 4:136a0aeb7d51 | 87 | /* complementar el código necesario |
fabeltranm | 4:136a0aeb7d51 | 88 | tip: deben pasar grados a microsegundo */ |
fabeltranm | 4:136a0aeb7d51 | 89 | |
fabeltranm | 4:136a0aeb7d51 | 90 | |
fabeltranm | 4:136a0aeb7d51 | 91 | // dpulse=degrees2usec(grados); |
fabeltranm | 4:136a0aeb7d51 | 92 | // myservo1.pulsewidth_us(dpulse); |
fabeltranm | 4:136a0aeb7d51 | 93 | |
fabeltranm | 4:136a0aeb7d51 | 94 | } |
fabeltranm | 4:136a0aeb7d51 | 95 | |
fabeltranm | 4:136a0aeb7d51 | 96 | |
fabeltranm | 3:ed8f41c75a80 | 97 | uint32_t degrees2usec(uint8_t grados){ |
fabeltranm | 3:ed8f41c75a80 | 98 | // Retorno el valor en usegundos, donde |
fabeltranm | 3:ed8f41c75a80 | 99 | // y − y1 = m(x − x1 ) |
fabeltranm | 3:ed8f41c75a80 | 100 | if(grados <= DEGREES_MAX) |
fabeltranm | 3:ed8f41c75a80 | 101 | return int(750+grados*1900/180);// u6 |
fabeltranm | 3:ed8f41c75a80 | 102 | return 750; |
fabeltranm | 3:ed8f41c75a80 | 103 | |
fabeltranm | 4:136a0aeb7d51 | 104 | } |