Desde 1994 en la Red. La pagina de los aficionados a la electronica, informatica y otras curiosidades de la vida. No dudes en visitarnos.
Ahora 0 visitas.| 3399408 Visitas (desde Dic. 2011), hoy: 333 Visitas 786 Pag. Vistas , ultimos 36 dias: 10108 Visitas. 29813 Pag. Vistas. Tu IP: 54.84.65.73
Que ando curioseando:
AutosuficienciaCosas de casaElectronicaEn InternetInformáticaMundo MisticoSin categoríaSociedadTe lo recomiendo

Arduino: Control de servo. Recordar posiciones.

Arduino.
Utilización de un servo como entrada y salida de datos con Arduino. Este codigo permite grabar una serie de posiciones de un servo mediante un pulsador y luego este, repetirá los movimientos almacenados.

/******************************************************************************************************
 *  Using a servo for input and output
 *
 * by Matt Mets
 * Created 9 Feb. 2008
 *
 * This demonstrates using the servo motor as an input device to record its own positions, which can
 * then be played back to animate whatever you are controlling.
 *
 * Servo control code thanks to:
 *    Servo control from an analog input
 *    http://itp.nyu.edu/physcomp/Labs/Servo
 *    by Tom Igoe
 *    additions by Carlyn Maw
 *    Created 28 Jan. 2006
 *    Updated 7 Jun. 2006
 *
 *  The minimum (minPulse) and maxiumum (maxPuluse) values
 *  will be different depending on your specific servo motor.
 *  Ideally, it should be between 1 and 2 milliseconds, but in practice,
 *  0.5 - 2.5 milliseconds works well for me.
 *  Try different values to see what numbers are best for you.
 *
 *  This program uses the millis() function to keep track of when the servo was
 *  last pulsed.  millis() produces an overflow error (i.e. generates a number
 *  that's too big to fit in a long variable) after about 5 days. if you're
 *  making a program that has to run for more than 5 days, you may need to
 *  account for this.
 ******************************************************************************************************/

/***** Variable Definitions ***************************************************************************/
int servoPin =        4;      // Servo motor control line
int analogPin =       1;      // Line from servo potentimeter
int recordButtonPin = 7;      // Record button
int playButtonPin =   10;     // Playback button
int statusPin =       13;     // Status LED

// Adjust these to fit your servo
int minPulse = 500;    // Minimum servo position (us)
int maxPulse = 2200;  // Maximum servo position (us)
int refreshTime = 20; // the time needed in between pulses (ms)

// Measure the minimum and maximum output voltages of the potentiometer and record them here.
// Note: These should be measured over the full range of the servos motion.
#define CALIBRATION_LOW_VOLTAGE   0.40  // Minimum potentiometer voltage (V)
#define CALIBRATION_HIGH_VOLTAGE  2.00  // Maximum potentiometer voltage (V)

// These are derived from the measured voltages above, are are computed during the setup routine
int inputOffset = 0;    // Input offset (ADC counts)
float inputScale = 0;   // Input scale (%)

#define MAX_POSITIONS 50           // Maximum number of positions that can be stored
int positionTable[MAX_POSITIONS];  // Table to hold each position
int positionCount = 0;             // Number of positions recorded

/***** Functions ************************************************************************************/
void setup()
{
  pinMode(analogPin, INPUT);
  pinMode(servoPin, INPUT);
  pinMode(recordButtonPin, INPUT);
  pinMode(playButtonPin, INPUT);
  pinMode(statusPin, OUTPUT);

  // Calculate the input offset and scale factors, based on hand-measured data
  // 204 = counts per volt
  inputOffset = CALIBRATION_LOW_VOLTAGE * 204;
  inputScale = 1024/((CALIBRATION_HIGH_VOLTAGE - CALIBRATION_LOW_VOLTAGE) * 204);

  // These are because I don't have a proper voltage rail on my proof of concept board, so I am using
  // pin 3 as a 5V source.  You probably don't need these.
  pinMode(3, OUTPUT);         // This pin powers some other pins so i dont have to wire...
  digitalWrite(3, HIGH);

  Serial.begin(9600);
}

// Sequence each recorded position
void doPlayBack()
{
  int position;
  long startTime;
  long lastPulse = 0;    // the time in milliseconds of the last pulse

  // play back each step for a second
  for(position = 0; position < positionCount; position++)
  {
    startTime = millis();
    while(millis() - startTime < 1000)
    {
      // pulse the servo again if rhe refresh time (20 ms) have passed:
      if (millis() - lastPulse >= refreshTime)
      {
        digitalWrite(servoPin, HIGH);   // Turn the motor on
        delayMicroseconds(positionTable[position]);       // Length of the pulse sets the motor position
        digitalWrite(servoPin, LOW);    // Turn the motor off
        lastPulse = millis();           // save the time of the last pulse
      }
    }
  }
}

void loop()
{
  int i;
  long analogValue;    // the value returned from the analog sensor

  // When the record button is pressed, try to record the current sensor position
  if(digitalRead(recordButtonPin) == LOW)
  {
    if(positionCount < MAX_POSITIONS)
    {
      // There is room to store the positions, so do so.
      analogValue = analogRead(analogPin);                    // Read the analog input
      Serial.println(analogValue);
      analogValue = (analogValue - inputOffset) * inputScale; // Scale the compressed input to a full range
      analogValue = analogValue*(maxPulse - minPulse)/1024 + minPulse;  // Precompute the pulse length

      positionTable[positionCount++] = analogValue;           // Store in the table
      Serial.println(analogValue);

      // Debounce the input button
      digitalWrite(statusPin, HIGH);
      while(digitalRead(recordButtonPin) == LOW)
        delay(200);
      digitalWrite(statusPin, LOW);
    } else {
      // There is no more room to record positions, so flash the LED in protest.
      for(i = 0; i < 5; i++)
      {
        digitalWrite(statusPin, HIGH);
        delay(100);
        digitalWrite(statusPin, LOW);
        delay(100);
      }
    }
  }

  // User wants to play back the sequence
  if(digitalRead(playButtonPin) == LOW) {
    digitalWrite(statusPin, HIGH);
    doPlayBack();
    digitalWrite(statusPin, LOW);
  }
}

Fuente: http://www.cibomahto.com/2008/02/thing-a-day-day-9-servo-as-input-device/

Escribe un comentario

Tu comentario