ferney alberto beltran molina
/
00_LAB_COMMAND_SERVO
mover servomotore por telecomandos
main.cpp@4:136a0aeb7d51, 2019-02-22 (annotated)
- Committer:
- fabeltranm
- Date:
- Fri Feb 22 18:44:32 2019 +0000
- Revision:
- 4:136a0aeb7d51
- Parent:
- 3:ed8f41c75a80
- Child:
- 5:115f329c72ab
ok
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 | 0:a9181441db9b | 4 | generar un programa que controle por el puerto serial el grado de 4 servo motores |
fabeltranm | 0:a9181441db9b | 5 | por medio de la comunicacion serial donde el comando sea |
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 | 0:a9181441db9b | 12 | *****************************************************************************/ |
fabeltranm | 0:a9181441db9b | 13 | |
fabeltranm | 0:a9181441db9b | 14 | |
fabeltranm | 0:a9181441db9b | 15 | Serial command(USBTX, USBRX); |
fabeltranm | 0:a9181441db9b | 16 | PwmOut myservo1(PB_4); |
fabeltranm | 0:a9181441db9b | 17 | /*INGRESE L A CONFIGURACION DE LOS MOTORES*/ |
fabeltranm | 0:a9181441db9b | 18 | |
fabeltranm | 2:fda902bed8c6 | 19 | #define INITCMD 0xFF |
fabeltranm | 3:ed8f41c75a80 | 20 | #define DEGREES_MAX 180 |
fabeltranm | 1:b473a0ad2b83 | 21 | // definición de las variables globales |
fabeltranm | 0:a9181441db9b | 22 | |
fabeltranm | 2:fda902bed8c6 | 23 | uint8_t N_motor; // almacena el número de motor |
fabeltranm | 2:fda902bed8c6 | 24 | uint8_t N_grados; // almacena los grados que se mueve el motor |
fabeltranm | 0:a9181441db9b | 25 | |
fabeltranm | 0:a9181441db9b | 26 | |
fabeltranm | 3:ed8f41c75a80 | 27 | // definición de las funciones |
fabeltranm | 0:a9181441db9b | 28 | void setup_uart(); |
fabeltranm | 1:b473a0ad2b83 | 29 | void setup_servo(); |
fabeltranm | 4:136a0aeb7d51 | 30 | |
fabeltranm | 1:b473a0ad2b83 | 31 | void mover_servo(uint8_t motor, uint8_t gradss); |
fabeltranm | 4:136a0aeb7d51 | 32 | uint32_t degrees2usec(uint8_t grados){ |
fabeltranm | 4:136a0aeb7d51 | 33 | |
fabeltranm | 1:b473a0ad2b83 | 34 | void leer_datos(); |
fabeltranm | 0:a9181441db9b | 35 | |
fabeltranm | 4:136a0aeb7d51 | 36 | |
fabeltranm | 4:136a0aeb7d51 | 37 | |
fabeltranm | 0:a9181441db9b | 38 | int main() { |
fabeltranm | 1:b473a0ad2b83 | 39 | |
fabeltranm | 1:b473a0ad2b83 | 40 | setup_uart(); |
fabeltranm | 1:b473a0ad2b83 | 41 | setup_servo(); |
fabeltranm | 4:136a0aeb7d51 | 42 | //command.printf("inicio de programa"); |
fabeltranm | 1:b473a0ad2b83 | 43 | while(1){ |
fabeltranm | 1:b473a0ad2b83 | 44 | leer_datos(); |
fabeltranm | 2:fda902bed8c6 | 45 | mover_servo(N_motor, N_grados); |
fabeltranm | 4:136a0aeb7d51 | 46 | } |
fabeltranm | 1:b473a0ad2b83 | 47 | } |
fabeltranm | 1:b473a0ad2b83 | 48 | |
fabeltranm | 1:b473a0ad2b83 | 49 | |
fabeltranm | 1:b473a0ad2b83 | 50 | |
fabeltranm | 1:b473a0ad2b83 | 51 | void setup_uart(){ |
fabeltranm | 1:b473a0ad2b83 | 52 | command.baud(115200); |
fabeltranm | 1:b473a0ad2b83 | 53 | } |
fabeltranm | 1:b473a0ad2b83 | 54 | |
fabeltranm | 1:b473a0ad2b83 | 55 | void setup_servo(){ |
fabeltranm | 0:a9181441db9b | 56 | myservo1.period_ms(20); |
fabeltranm | 1:b473a0ad2b83 | 57 | myservo1.pulsewidth_us(1000); |
fabeltranm | 1:b473a0ad2b83 | 58 | } |
fabeltranm | 1:b473a0ad2b83 | 59 | |
fabeltranm | 1:b473a0ad2b83 | 60 | void leer_datos(){ |
fabeltranm | 2:fda902bed8c6 | 61 | while(command.getc()!= INITCMD); |
fabeltranm | 2:fda902bed8c6 | 62 | N_motor=command.getc(); |
fabeltranm | 2:fda902bed8c6 | 63 | N_grados=command.getc(); |
fabeltranm | 0:a9181441db9b | 64 | |
fabeltranm | 1:b473a0ad2b83 | 65 | } |
fabeltranm | 1:b473a0ad2b83 | 66 | |
fabeltranm | 1:b473a0ad2b83 | 67 | |
fabeltranm | 4:136a0aeb7d51 | 68 | |
fabeltranm | 4:136a0aeb7d51 | 69 | |
fabeltranm | 4:136a0aeb7d51 | 70 | void mover_servo(uint8_t motor, uint8_t grados){ |
fabeltranm | 4:136a0aeb7d51 | 71 | |
fabeltranm | 4:136a0aeb7d51 | 72 | uint32_t dpulse=0; |
fabeltranm | 4:136a0aeb7d51 | 73 | |
fabeltranm | 4:136a0aeb7d51 | 74 | /* complementar el código necesario |
fabeltranm | 4:136a0aeb7d51 | 75 | tip: deben pasar grados a microsegundo */ |
fabeltranm | 4:136a0aeb7d51 | 76 | |
fabeltranm | 4:136a0aeb7d51 | 77 | |
fabeltranm | 4:136a0aeb7d51 | 78 | // dpulse=degrees2usec(grados); |
fabeltranm | 4:136a0aeb7d51 | 79 | // myservo1.pulsewidth_us(dpulse); |
fabeltranm | 4:136a0aeb7d51 | 80 | |
fabeltranm | 4:136a0aeb7d51 | 81 | } |
fabeltranm | 4:136a0aeb7d51 | 82 | |
fabeltranm | 4:136a0aeb7d51 | 83 | |
fabeltranm | 3:ed8f41c75a80 | 84 | uint32_t degrees2usec(uint8_t grados){ |
fabeltranm | 3:ed8f41c75a80 | 85 | // Retorno el valor en usegundos, donde |
fabeltranm | 3:ed8f41c75a80 | 86 | // y − y1 = m(x − x1 ) |
fabeltranm | 3:ed8f41c75a80 | 87 | if(grados <= DEGREES_MAX) |
fabeltranm | 3:ed8f41c75a80 | 88 | return int(750+grados*1900/180);// u6 |
fabeltranm | 3:ed8f41c75a80 | 89 | return 750; |
fabeltranm | 3:ed8f41c75a80 | 90 | |
fabeltranm | 4:136a0aeb7d51 | 91 | } |