jueves, 5 de marzo de 2026

codigo de bucle for cobot 7°

 Activity: Bringing the Robot Arm to Life!

You have been learning how the for loop makes our robot move smoothly instead of jumping suddenly. Please answer the following 5 questions in your notebook:

 

1.      The power of the loop: In your own words, what happens to the motor when we use a for loop with a delay(20) inside, instead of just telling it "go to 180 degrees"?

 

2.      Adjusting the speed: If you wanted the robot to move slower than a snail, which value in the code would you change, and why?

 

3.      Understanding limits: The robot currently moves from 0 to 180 degrees. What do you think would happen if we changed the condition i < 180 to i < 90?

 

4.      The brain of the operation: We use the PCA9685 board to control the motors. Why don't we need to plug each motor into a different digital pin on the Arduino (like pin 2, 3, or 4)?

 

5.      Time for the real test: Go to Wokwi, build a simple circuit with your Arduino, the PCA9685 board, and at least one servo motor. Copy and paste the code, run it, and see if the movement matches what you explained in point 1.

WOKWI CODE


#include <ESP32Servo.h>


// 1. Creamos los objetos para cada motor

Servo servoBase;

Servo servoHombro;

Servo servoCodo;


// 2. Definimos los pines PWM (asegúrate de que en Wokwi estén en estos pines)

const int PIN_BASE = 18;

const int PIN_HOMBRO = 19;

const int PIN_CODO = 21;


void setup() {

  // 3. Conectamos cada objeto a su pin físico

  servoBase.attach(PIN_BASE);

  servoHombro.attach(PIN_HOMBRO);

  servoCodo.attach(PIN_CODO);

  

  // Posición inicial de los tres motores

  servoBase.write(90);

  servoHombro.write(90);

  servoCodo.write(90);

}


void loop() {

  // Mover todos al mismo tiempo con un solo bucle FOR

  // Ejemplo: Mover todos desde 90 hasta 180

  for (int i = 90; i <= 180; i++) {

    servoBase.write(i);

    servoHombro.write(i);

    servoCodo.write(i);

    delay(20); 

  }

  

  delay(1000);

  

  // Regresar todos a 90

  for (int i = 180; i >= 90; i--) {

    servoBase.write(i);

    servoHombro.write(i);

    servoCodo.write(i);

    delay(20);

  }

  

  delay(1000);

}

ServomotorCable de Señal (Amarillo/Naranja)Cable Rojo (VCC)Cable Negro/Marrón (GND)
BasePin 185VGND
HombroPin 195VGND
CodoPin 215VGND

_______________________________________________________________________________

ARDUINO CODE


/*

   TITULO: codigo de bucle for cobot
   GRADO: Septimo
   
   OBJETIVO:
   Entender el bucle 'for' (para).
   En lugar de mover el motor de golpe, usaremos el 'for' para
   dar pequeños pasitos y lograr un movimiento suave y elegante.
*/

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver tarjeta = Adafruit_PWMServoDriver();

// --- CONFIGURACIÓN DE MOTORES ---
const int BASE = 0;
const int HOMBRO = 4;
const int CODO = 8;

// Calibración de servos (0 a 180 grados)
const int PULSO_MIN = 150;
const int PULSO_MAX = 600;

void setup() {
  Serial.begin(115200);
  tarjeta.begin();
  tarjeta.setPWMFreq(50);
  Serial.println(">>> ROBOT LISTO PARA MOVERSE SUAVEMENTE <<<");
 
  // Ponemos el robot en posición inicial (rápido)
  tarjeta.setPWM(BASE, 0, gradosAPulsos(90));
  tarjeta.setPWM(HOMBRO, 0, gradosAPulsos(90));
  delay(1000);
}

void loop() {
  // --- AQUI OCURRE LA MAGIA DEL BUCLE FOR ---
 
  Serial.println("1. Girando la base suavemente hacia la derecha...");
 
  // ANATOMÍA DEL BUCLE FOR:
  // 1. INICIO (int i = 90): Empezamos en 90 grados.
  // 2. CONDICIÓN (i > 0): Seguimos mientras 'i' sea mayor que 0.
  // 3. PASO (i--): Restamos 1 grado en cada vuelta (Vamos bajando 90, 89, 88...).
 
  for (int i = 90; i > 0; i--) {
    tarjeta.setPWM(BASE, 0, gradosAPulsos(i)); // Mover al grado 'i'
    delay(20); // Esperar 20 milisegundos (La velocidad del movimiento)
  }
 
  delay(1000); // Descanso

  Serial.println("2. Girando la base suavemente hacia la izquierda...");
 
  // AHORA AL REVÉS (SUMANDO):
  // 1. INICIO (int i = 0): Empezamos en 0.
  // 2. CONDICIÓN (i < 180): Seguimos mientras no lleguemos a 180.
  // 3. PASO (i++): Sumamos 1 grado en cada vuelta.
 
  for (int i = 0; i < 180; i++) {
    tarjeta.setPWM(BASE, 0, gradosAPulsos(i));
    delay(20); // Entre más grande este número, más lento se mueve el robot
  }

  delay(1000);
 
  Serial.println("3. Regresando al centro...");
  // De 180 bajamos hasta 90
  for (int i = 180; i >= 90; i--) {
    tarjeta.setPWM(BASE, 0, gradosAPulsos(i));
    delay(20);
  }
 
  delay(2000); // Pausa larga antes de repetir todo
}

// --- FUNCIÓN AYUDANTE (No te preocupes por esta parte aún) ---
// Convierte los grados (0-180) a los números raros que necesita la tarjeta
int gradosAPulsos(int grados) {
  return map(grados, 0, 180, PULSO_MIN, PULSO_MAX);
}


ComponentePin del ComponentePin en Arduino
PCA9685VCC5V
PCA9685GNDGND
PCA9685SCLA5
PCA9685SDAA4
ServosCable Rojo (V+)Fuente externa 5V-6V (+)
ServosCable Negro/Café (GND)Fuente externa 5V-6V (-)
ServosCable Naranja/Blanco (Señal)Pines 0, 4, 8 de la tarjeta

No hay comentarios:

Publicar un comentario