[SOLUCIONADO] ....Conoceis LIBRERIA para movimiento conjunto de varios SERVOS ?

Hola a todos

Como comento en el título, desearía mover dos o varios SERVOS al mismo tiempo, NO uno después de otro.
Conozco : Servo.h , y VarSpeed.h pero no realizan esa función.
Adjunto un sencillo código con dos Servos, que quisiera mover conjuntamente.

Gracias anticipadas !!!

Un saludo !!!

/*

    DOS (2)SERVOS GITHUB 

   https://github.com/netlabtoolkit/VarSpeedServo

*/

#include <VarSpeedServo.h>


VarSpeedServo Servo1;  // CREA OBJETOS
VarSpeedServo Servo2;

void setup()
{
  Serial.begin (9600) ;
  Serial.println ("  ") ;
  Serial.println ("================  A_SERVO_VarSpeed_4_github   \n");

  Servo1.attach(12); // Conexion de los servos
  Servo2.attach(8);
  pinMode (13, OUTPUT); // LED 13
}

void loop()
{
  digitalWrite (13, HIGH); // Enciende al PRINCIPIO DE MOVIMIENTOS

  int IZQ = 0;         // 0 GRADOS PREDETERMINADOS
  int DER = 180;       // >180

  int VELOC1 = 30;     // >20 VELOCIDADES PREDETERMINADAS
  int VELOC2 = 100;    // >100

  for (int i = 0; i < 2; i++) // Repite 2 veces la serie
  {
    Servo1.write(IZQ, VELOC1);  // servo1 > 0,vel:20
    Servo1.wait();   // espera a que termine el movimiento servo1

    Servo2.write(IZQ, VELOC1);  // servo2 > 0,vel:100
    Servo2.wait();   // espera a que termine el movimiento servo2

    Servo1.write(DER, VELOC1);  // servo1 > 180, vel:20
    Servo1.wait();

    Servo2.write(DER, VELOC1);  // servo1 > 0  , vel:20
    Servo2.wait();

    Servo2.write(IZQ, VELOC2);  // servo2 > 180, vel:100
    Servo2.wait();

    Servo1.write(IZQ, VELOC1);   // servo1 > 180, vel 20
    Servo1.wait();
  }
  digitalWrite (13, LOW); // Apaga al FIN DE MOVIMIENTOS

  delay (2000); // Reposo entre loops

  /*
                       // MARCADO PARA LOOP CONTINUO
    Servo1.detach(10);  // Desconecta del Pin  el servo
    Servo2.detach(8);
  */

} // LL-LOOP

//***********************  FIN  ***************************
/*
   
   TRADUCCION (ver original al final)
   
   VarSpeedServo: Clase para manipular servomotores conectados a Arduino.

   Un servo se activa creando una instancia de la clase Servo conectando
   el pin deseado por el método attach(). Los servos se pulsan en segundo
   plano usando el valor escrito más recientemente usando el método
   write()
 
   Los METODOS son:

   attach(pin ): conecta un servomotor a un pin digital E/S.

   attach(pin, min, max  ): se conecta a un pin configurando los valores
   mínimo y máximo en microsegundos.
   El mínimo predeterminado es 544 y el máximo es 2400.

   write(value) : establece el ángulo del servo en grados.
  (el valor que no es válido como angulo, se trata como pulso
   en microsegundos)

   write(value, speed):- posición: 0º- 180º.
                       - velocidad: 1-255 .

   write(valor, velocidad, espera): espera es un valor booleano que,
   si es TRUE, hace que la llamada a la función se bloquee
   hasta que se complete el movimiento INICIADO.

   writeMicroseconds() - Establece el ancho del pulso del servo en microsegundos

   read(): obtiene el último ancho de pulso del servo escrito como
   un ángulo entre 0 y 180.

   readMicroseconds() - Obtiene el último ancho de pulso del servo escrito
   en microsegundos. (era read_us() en la primera versión)

   attached(): devuelve verdadero si hay un servo conectado.

   detach() - Desconecta servos conectados en su pin de E/S.

   slowmove(valor, velocidad): lo mismo que escribir(valor, velocidad),
   conservado por compatibilidad con la versión de Korman.

   stop() - detiene el servo en la posición actual

   sequencePlay(secuencia, secuenciaPosiciones); // reproduce una secuencia
   en bucle comenzando en la posición 0

   sequencePlay(secuencia, posiciones de secuencia, loop,
   startPosition); // reproduce la secuencia con el número de
   posiciones,  loop: si es TRUE: startPosition.

   sequenceStop()(); // detener la secuencia en la posición actual

   wait();           // espera a que termine el movimiento

   isMoving()     // devuelve verdadero si el servo aún se está moviendo
   
   ***********************************************
   
  
   
   ORIGINAL
   
   VarSpeedServo - Class for manipulating servo motors connected to Arduino pins. Methods:
 * * The methods are:

   attach(pin )  - Attaches a servo motor to an i/o pin.
   
   attach(pin, min, max  ) - Attaches to a pin setting min and max values in microseconds
  
   default min is 544, max is 2400

   write(value)     - Sets the servo angle in degrees.  (invalid angle that is valid as pulse in microseconds is treated as microseconds)
   
   write(value, speed) - speed varies the speed of the move to new position 0=full speed, 1-255 slower to faster
   
   write(value, speed, wait) - wait is a boolean that, if true, causes the function call to block until move is complete

   writeMicroseconds() - Sets the servo pulse width in microseconds
   
   read()      - Gets the last written servo pulse width as an angle between 0 and 180.
   
   readMicroseconds()  - Gets the last written servo pulse width in microseconds. (was read_us() in first release)
   
   attached()  - Returns true if there is a servo attached.
   
   detach()    - Stops an attached servos from pulsing its i/o pin.

   slowmove(value, speed) - The same as write(value, speed), retained for compatibility with Korman's version

   stop() - stops the servo at the current position

   sequencePlay(sequence, sequencePositions); // play a looping sequence starting at position 0
  
   sequencePlay(sequence, sequencePositions, loop, startPosition); // play sequence with number of positions, loop if true, start at position
   
   sequenceStop(); // stop sequence at current position
  
   wait(); // wait for movement to finish
   
   isMoving()  // return true if servo is still moving

   
 *******************************************************
*/

Podeis comprobar al final los METODOS de esta libreria.

No creo que haya alguna pero si te las ingenias puedes hacer que parezca que se mueven simultáneamente.
Mira

Una breve explicación en

1 Like

Hola @MaximoEsfuerzo

Eres un pozo de ciencia !!!!!

Muy buen truco !!!

Ha funcionado a la primera ( solo cambiando en mi caso los dos pins de attachs)

Un gran saludo !!! ..:champagne:... :cocktail:.. :cocktail:

Adjunto el código COPIADO del SIMULADOR para comodidad de los que vengan mas tarde a consultar.

/*
 * 
 * 
 *      DOS SERVOS (FORO) 
 *     
 * 
 * 
 */

 #include <Servo.h>

Servo servo_h;
Servo servo_v;

int pos[6][2] = {{45, 45}, {90,180}, {135,135},{0,180},{90,90},{0,0}};

void mover_servos(int hpos, int vpos, unsigned long s) {
  static int oldhpos;
  static int oldvpos;
  Serial.println("h: " + String(oldhpos) + " ->" + String(hpos));
  Serial.println("v: " + String(oldvpos) + " ->" + String(vpos));
  Serial.println("s: " + String(s));
  Serial.println();

  int hdif = hpos - oldhpos;
  int hdir = hdif < 0 ? -1 : 1;
  hdif = abs(hdif);
  int vdif = vpos - oldvpos;
  int vdir = vdif < 0 ? -1 : 1;
  vdif = abs(vdif);
  int pasos = vdif >= hdif ? vdif : hdif;

  for (int i = 0 ; i <= pasos; i++) {
    servo_h.write(oldhpos + hdif / (float)pasos * hdir * i );
    delay(s);
    servo_v.write(oldvpos + vdif / (float)pasos * vdir * i );
    delay(s);
  }
  oldhpos = hpos;
  oldvpos = vpos;
}


void setup() 
{
  Serial.begin(115200);
  servo_h.attach(12);
  servo_v.attach(8);
  servo_h.write(0);
  servo_v.write(0);
  delay(500);
  mover_servos(180, 180, 1);
  delay(100);
  mover_servos(0, 0, 1);
  delay(1000);
}

void loop()
{
  static int x;
  mover_servos(pos[x][0],pos[x][1], random(1, 5));
  x++;
  x %= 6;
  delay(1000);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.