ferney alberto beltran molina
/
00_LAB_COMMAND_SERVO
mover servomotore por telecomandos
main.cpp@3:ed8f41c75a80, 2019-02-22 (annotated)
- Committer:
- fabeltranm
- Date:
- Fri Feb 22 18:30:06 2019 +0000
- Revision:
- 3:ed8f41c75a80
- Parent:
- 2:fda902bed8c6
- Child:
- 4:136a0aeb7d51
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 | 1:b473a0ad2b83 | 30 | void mover_servo(uint8_t motor, uint8_t gradss); |
fabeltranm | 1:b473a0ad2b83 | 31 | void leer_datos(); |
fabeltranm | 0:a9181441db9b | 32 | |
fabeltranm | 0:a9181441db9b | 33 | int main() { |
fabeltranm | 1:b473a0ad2b83 | 34 | |
fabeltranm | 1:b473a0ad2b83 | 35 | setup_uart(); |
fabeltranm | 1:b473a0ad2b83 | 36 | setup_servo(); |
fabeltranm | 1:b473a0ad2b83 | 37 | while(1){ |
fabeltranm | 1:b473a0ad2b83 | 38 | leer_datos(); |
fabeltranm | 2:fda902bed8c6 | 39 | mover_servo(N_motor, N_grados); |
fabeltranm | 1:b473a0ad2b83 | 40 | } |
fabeltranm | 1:b473a0ad2b83 | 41 | } |
fabeltranm | 1:b473a0ad2b83 | 42 | |
fabeltranm | 1:b473a0ad2b83 | 43 | |
fabeltranm | 1:b473a0ad2b83 | 44 | |
fabeltranm | 1:b473a0ad2b83 | 45 | void setup_uart(){ |
fabeltranm | 1:b473a0ad2b83 | 46 | command.baud(115200); |
fabeltranm | 1:b473a0ad2b83 | 47 | } |
fabeltranm | 1:b473a0ad2b83 | 48 | |
fabeltranm | 1:b473a0ad2b83 | 49 | void setup_servo(){ |
fabeltranm | 0:a9181441db9b | 50 | myservo1.period_ms(20); |
fabeltranm | 1:b473a0ad2b83 | 51 | myservo1.pulsewidth_us(1000); |
fabeltranm | 1:b473a0ad2b83 | 52 | } |
fabeltranm | 1:b473a0ad2b83 | 53 | |
fabeltranm | 1:b473a0ad2b83 | 54 | void leer_datos(){ |
fabeltranm | 2:fda902bed8c6 | 55 | while(command.getc()!= INITCMD); |
fabeltranm | 2:fda902bed8c6 | 56 | N_motor=command.getc(); |
fabeltranm | 2:fda902bed8c6 | 57 | N_grados=command.getc(); |
fabeltranm | 0:a9181441db9b | 58 | |
fabeltranm | 1:b473a0ad2b83 | 59 | } |
fabeltranm | 1:b473a0ad2b83 | 60 | |
fabeltranm | 1:b473a0ad2b83 | 61 | |
fabeltranm | 3:ed8f41c75a80 | 62 | uint32_t degrees2usec(uint8_t grados){ |
fabeltranm | 3:ed8f41c75a80 | 63 | // Retorno el valor en usegundos, donde |
fabeltranm | 3:ed8f41c75a80 | 64 | // y − y1 = m(x − x1 ) |
fabeltranm | 3:ed8f41c75a80 | 65 | if(grados <= DEGREES_MAX) |
fabeltranm | 3:ed8f41c75a80 | 66 | return int(750+grados*1900/180);// u6 |
fabeltranm | 3:ed8f41c75a80 | 67 | return 750; |
fabeltranm | 3:ed8f41c75a80 | 68 | |
fabeltranm | 3:ed8f41c75a80 | 69 | } |
fabeltranm | 1:b473a0ad2b83 | 70 | |
fabeltranm | 1:b473a0ad2b83 | 71 | void mover_servo(uint8_t motor, uint8_t grados){ |
fabeltranm | 1:b473a0ad2b83 | 72 | |
fabeltranm | 1:b473a0ad2b83 | 73 | /* |
fabeltranm | 3:ed8f41c75a80 | 74 | complementar el códogo necesario |
fabeltranm | 1:b473a0ad2b83 | 75 | tip: deben pasar grados a microsegundo |
fabeltranm | 3:ed8f41c75a80 | 76 | se genera in ejemplo con un servo |
fabeltranm | 1:b473a0ad2b83 | 77 | |
fabeltranm | 1:b473a0ad2b83 | 78 | */ |
fabeltranm | 3:ed8f41c75a80 | 79 | uint32_t dpulse=degrees2usec(grados); |
fabeltranm | 3:ed8f41c75a80 | 80 | //dpulse= degrees2sec(grados) |
fabeltranm | 0:a9181441db9b | 81 | myservo1.pulsewidth_us(dpulse); |
fabeltranm | 1:b473a0ad2b83 | 82 | |
fabeltranm | 0:a9181441db9b | 83 | } |
fabeltranm | 3:ed8f41c75a80 | 84 | |
fabeltranm | 3:ed8f41c75a80 | 85 |