Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Homepage
- include "mbed.h"
/*************** generar un programa que controle por el puerto serial el grado de 4 servo motores. por medio de la comunicacion serial el comando es
INITCMD | N_motor | N_grados |
0xff | 0x01- 0x04 | 0x00 - 0xb4 |
***************/ /*CONSTANTES*/
- define INITCMD 0xFF
- define DEGREES_MAX 180 /*TELECOMANDO*/
- define AUTOMATIC 0x01
- define PINZA 0x02
- define MANUAL 0x03
- define VELOCIDAD 0x04 /*PARAMETRO*/
- define HOME 0x01
- define MATERIAL 0x02
- define CELDA1 0x03
- define CELDA2 0x04
- define CELDA3 0x05
- define CELDA4 0x06
- define ABRIR_PINZA 0x01
- define CERRAR_PINZA 0x02 /*MOTORES MANUAL*/
- define MOTOR1 0x03
- define MOTOR2 0x04
- define MOTOR3 0x05
- define MOTOR4 0x06
/*VARIABLE RECIBE COMANDOS*/ Serial command(USBTX, USBRX); unsigned char comando; unsigned char parametro;
/*CONFIGURACIÓN SERVOS*/
PwmOut myservo1(PB_4);d5 BASE
PwmOut myservo2(PB_10);d6 IZQUIERDO
PwmOut myservo3(PA_8);d7 DERECHO
PwmOut myservo4(PA_9);d8 PINZA
/*VARIABLES GLOBALES*/ uint8_t N_motor; variable almacena el número de motor en leer_datos() uint8_t N_grados; varable almacena los grados que se mueve el motor en leer_datos()
/*DEFINICIÓN FUNCIONES*/ void setup_uart(); void setup_servo(); void info(char *s, ... ); void mover_servo(uint8_t motor, uint8_t gradss); uint32_t degrees2usec(uint8_t grados); void motor_velocidad(uint8_t motor, float valor);
void home(); void material(); void celda1(); void celda2(); void celda3(); void celda4(); void abrir_pinza(); void cerrar_pinza();
/*INICIO DE PROGRAMA*/ int main() { info("inicio \n"); setup_uart(); setup_servo();
while(1) { while(command.getc()!= INITCMD); comando = command.getc(); Comandos Automaticos if(comando == 0x01) { parametro= command.getc(); switch (parametro) { case HOME: home(); break; case MATERIAL: material(); break; case CELDA1: celda1(); break; case CELDA2: celda2(); break; case CELDA3: celda3(); break; case CELDA4: celda4(); break; default: info("No se encontro comando valido.Posiciones Automaticas \n"); break ; } Abre o Cierra Pinza } else if(comando == 0x02) { parametro= command.getc(); switch (parametro) { case ABRIR_PINZA: abrir_pinza(); break; case CERRAR_PINZA: cerrar_pinza(); break; default: info("No se encontro comando valido.Abrir o Cerrar Pinza \n"); break ; } Motor Manual } else if(comando == 0x03) { N_grados=command.getc(); mover_servo(MOTOR1,N_grados);
} else if(comando == 0x04) { N_grados=command.getc(); mover_servo(MOTOR2,N_grados);
} else if(comando == 0x05) { N_grados=command.getc(); mover_servo(MOTOR3,N_grados);
} else if(comando == 0x06) { N_grados=command.getc(); mover_servo(MOTOR4,N_grados);
Control de Velocidad } else if(comando == 0x07) { for(float i=0; i<1; i=i+0.01f) { increase speed from 0 to 1 in 5 seconds (50ms*100). motor_velocidad(1,i); wait(0.05f); } }
}Fin While(1) } Fin main
/*CONFIGURA VELOCIDAD DE TRAMSMISIÓN*/ void setup_uart() { command.baud(9600); }
/*CONFIGURACIÓN DE SERVOS*/ void setup_servo() { myservo1.period_ms(20); myservo2.period_ms(20); myservo3.period_ms(20); myservo4.period_ms(20); }
/*CONVERTIR GRADOS A SEGUNDOS*/ uint32_t degrees2usec(uint8_t grados) { Retorna el valor en microsegundos, donde y − y1 = m(x − x1 ) y= b + mx
if(grados <= DEGREES_MAX) return int(750+grados*1900/180); u6 return 750;
}
/*MOVER SERVO MANUALMENTE*/ void mover_servo(uint8_t motor, uint8_t grados) {
uint32_t dpulse=0; dpulse=degrees2usec(grados);
switch (motor) { case MOTOR1: myservo1.pulsewidth_us(dpulse); break; case MOTOR2: myservo2.pulsewidth_us(dpulse); break; case MOTOR3: myservo3.pulsewidth_us(dpulse); break; case MOTOR4: myservo4.pulsewidth_us(dpulse); break; default: info("No se encontro comando valido. Codigo Motor Manual \n"); break ; }fin switch }fin mover_servo
void info(char *s, ... ) { command.printf(s); }
void home() { myservo1.pulsewidth_us(600); wait(3.0); myservo2.pulsewidth_us(600); wait(3.0); myservo3.pulsewidth_us(600); wait(3.0); myservo4.pulsewidth_us(600); wait(3.0); }
void material() { myservo1.pulsewidth_us(1000); wait(3.0); myservo2.pulsewidth_us(600); wait(3.0); myservo3.pulsewidth_us(600); wait(3.0); myservo4.pulsewidth_us(600); wait(3.0); }
void celda1() { myservo1.pulsewidth_us(700); wait(3.0); myservo2.pulsewidth_us(600); wait(3.0); myservo3.pulsewidth_us(600); wait(3.0); myservo4.pulsewidth_us(600); wait(3.0); }
void celda2() { myservo1.pulsewidth_us(800); wait(3.0); myservo2.pulsewidth_us(600); wait(3.0); myservo3.pulsewidth_us(600); wait(3.0); myservo4.pulsewidth_us(600); wait(3.0); }
void celda3() { myservo1.pulsewidth_us(900); wait(3.0); myservo2.pulsewidth_us(600); wait(3.0); myservo3.pulsewidth_us(600); wait(3.0); myservo4.pulsewidth_us(600); wait(3.0); }
void celda4() { myservo1.pulsewidth_us(1000); wait(3.0); myservo2.pulsewidth_us(600); wait(3.0); myservo3.pulsewidth_us(600); wait(3.0); myservo4.pulsewidth_us(600); wait(3.0); }
void abrir_pinza() { myservo4.pulsewidth_us(900); wait(3.0); }
void cerrar_pinza() { myservo4.pulsewidth_us(600); wait(3.0); }
void motor_velocidad(uint8_t motor, float valor) {
switch (motor) { case MOTOR1: myservo1.write(valor); break; case MOTOR2: myservo2.write(valor); break; case MOTOR3: myservo3.write(valor); break; case MOTOR4: myservo4.write(valor); break; default: info("No se encontro comando valido. Codigo Motor Velocidad \n"); break ; }fin switch }