ferney alberto beltran molina
/
00_LAB_COMMAND_SERVO
mover servomotore por telecomandos
main.cpp@1:b473a0ad2b83, 2019-02-22 (annotated)
- Committer:
- fabeltranm
- Date:
- Fri Feb 22 13:10:24 2019 +0000
- Revision:
- 1:b473a0ad2b83
- Parent:
- 0:a9181441db9b
- Child:
- 2:fda902bed8c6
okey
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 | 0:a9181441db9b | 8 | | init_commd | 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 | 0:a9181441db9b | 19 | |
fabeltranm | 0:a9181441db9b | 20 | |
fabeltranm | 1:b473a0ad2b83 | 21 | // definición de las variables globales |
fabeltranm | 0:a9181441db9b | 22 | |
fabeltranm | 0:a9181441db9b | 23 | uint8_t nm; // almacena el número de motor |
fabeltranm | 0:a9181441db9b | 24 | uint8_t ng; // almacena los grados que se mueve el motor |
fabeltranm | 0:a9181441db9b | 25 | |
fabeltranm | 0:a9181441db9b | 26 | |
fabeltranm | 0:a9181441db9b | 27 | void setup_uart(); |
fabeltranm | 1:b473a0ad2b83 | 28 | void setup_servo(); |
fabeltranm | 1:b473a0ad2b83 | 29 | void mover_servo(uint8_t motor, uint8_t gradss); |
fabeltranm | 1:b473a0ad2b83 | 30 | void leer_datos(); |
fabeltranm | 0:a9181441db9b | 31 | |
fabeltranm | 0:a9181441db9b | 32 | int main() { |
fabeltranm | 1:b473a0ad2b83 | 33 | |
fabeltranm | 1:b473a0ad2b83 | 34 | setup_uart(); |
fabeltranm | 1:b473a0ad2b83 | 35 | setup_servo(); |
fabeltranm | 1:b473a0ad2b83 | 36 | while(1){ |
fabeltranm | 1:b473a0ad2b83 | 37 | leer_datos(); |
fabeltranm | 1:b473a0ad2b83 | 38 | mover_servo(nm, ng); |
fabeltranm | 1:b473a0ad2b83 | 39 | } |
fabeltranm | 1:b473a0ad2b83 | 40 | } |
fabeltranm | 1:b473a0ad2b83 | 41 | |
fabeltranm | 1:b473a0ad2b83 | 42 | |
fabeltranm | 1:b473a0ad2b83 | 43 | |
fabeltranm | 1:b473a0ad2b83 | 44 | void setup_uart(){ |
fabeltranm | 1:b473a0ad2b83 | 45 | command.baud(115200); |
fabeltranm | 1:b473a0ad2b83 | 46 | } |
fabeltranm | 1:b473a0ad2b83 | 47 | |
fabeltranm | 1:b473a0ad2b83 | 48 | void setup_servo(){ |
fabeltranm | 0:a9181441db9b | 49 | myservo1.period_ms(20); |
fabeltranm | 1:b473a0ad2b83 | 50 | myservo1.pulsewidth_us(1000); |
fabeltranm | 1:b473a0ad2b83 | 51 | } |
fabeltranm | 1:b473a0ad2b83 | 52 | |
fabeltranm | 1:b473a0ad2b83 | 53 | void leer_datos(){ |
fabeltranm | 1:b473a0ad2b83 | 54 | while(command.getc()!= 0xff); |
fabeltranm | 1:b473a0ad2b83 | 55 | nm=command.getc(); |
fabeltranm | 1:b473a0ad2b83 | 56 | ng=command.getc(); |
fabeltranm | 0:a9181441db9b | 57 | |
fabeltranm | 1:b473a0ad2b83 | 58 | } |
fabeltranm | 1:b473a0ad2b83 | 59 | |
fabeltranm | 1:b473a0ad2b83 | 60 | |
fabeltranm | 1:b473a0ad2b83 | 61 | |
fabeltranm | 1:b473a0ad2b83 | 62 | void mover_servo(uint8_t motor, uint8_t grados){ |
fabeltranm | 1:b473a0ad2b83 | 63 | |
fabeltranm | 1:b473a0ad2b83 | 64 | /* |
fabeltranm | 1:b473a0ad2b83 | 65 | complementar el códogo necesarios |
fabeltranm | 1:b473a0ad2b83 | 66 | tip: deben pasar grados a microsegundo |
fabeltranm | 1:b473a0ad2b83 | 67 | |
fabeltranm | 1:b473a0ad2b83 | 68 | */ |
fabeltranm | 1:b473a0ad2b83 | 69 | uint32_t dpulse=0; |
fabeltranm | 0:a9181441db9b | 70 | myservo1.pulsewidth_us(dpulse); |
fabeltranm | 1:b473a0ad2b83 | 71 | |
fabeltranm | 0:a9181441db9b | 72 | } |