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.| 3551840 Visitas (desde Dic. 2011), hoy: 209 Visitas 431 Pag. Vistas , ultimos 36 dias: 11169 Visitas. 45536 Pag. Vistas. Tu IP: 52.15.253.106
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.

1/******************************************************************************************************
2 *  Using a servo for input and output
3 *
4 * by Matt Mets
5 * Created 9 Feb. 2008
6 *
7 * This demonstrates using the servo motor as an input device to record its own positions, which can
8 * then be played back to animate whatever you are controlling.
9 *
10 * Servo control code thanks to:
11 *    Servo control from an analog input
13 *    by Tom Igoe
14 *    additions by Carlyn Maw
15 *    Created 28 Jan. 2006
16 *    Updated 7 Jun. 2006
17 *
18 *  The minimum (minPulse) and maxiumum (maxPuluse) values
19 *  will be different depending on your specific servo motor.
20 *  Ideally, it should be between 1 and 2 milliseconds, but in practice,
21 *  0.5 - 2.5 milliseconds works well for me.
22 *  Try different values to see what numbers are best for you.
23 *
24 *  This program uses the millis() function to keep track of when the servo was
25 *  last pulsed.  millis() produces an overflow error (i.e. generates a number
26 *  that's too big to fit in a long variable) after about 5 days. if you're
27 *  making a program that has to run for more than 5 days, you may need to
28 *  account for this.
29 ******************************************************************************************************/
30 
31/***** Variable Definitions ***************************************************************************/
32int servoPin =        4;      // Servo motor control line
33int analogPin =       1;      // Line from servo potentimeter
34int recordButtonPin = 7;      // Record button
35int playButtonPin =   10;     // Playback button
36int statusPin =       13;     // Status LED
37 
38// Adjust these to fit your servo
39int minPulse = 500;    // Minimum servo position (us)
40int maxPulse = 2200;  // Maximum servo position (us)
41int refreshTime = 20; // the time needed in between pulses (ms)
42 
43// Measure the minimum and maximum output voltages of the potentiometer and record them here.
44// Note: These should be measured over the full range of the servos motion.
45#define CALIBRATION_LOW_VOLTAGE   0.40  // Minimum potentiometer voltage (V)
46#define CALIBRATION_HIGH_VOLTAGE  2.00  // Maximum potentiometer voltage (V)
47 
48// These are derived from the measured voltages above, are are computed during the setup routine
49int inputOffset = 0;    // Input offset (ADC counts)
50float inputScale = 0;   // Input scale (%)
51 
52#define MAX_POSITIONS 50           // Maximum number of positions that can be stored
53int positionTable[MAX_POSITIONS];  // Table to hold each position
54int positionCount = 0;             // Number of positions recorded
55 
56/***** Functions ************************************************************************************/
57void setup()
58{
59  pinMode(analogPin, INPUT);
60  pinMode(servoPin, INPUT);
61  pinMode(recordButtonPin, INPUT);
62  pinMode(playButtonPin, INPUT);
63  pinMode(statusPin, OUTPUT);
64 
65  // Calculate the input offset and scale factors, based on hand-measured data
66  // 204 = counts per volt
67  inputOffset = CALIBRATION_LOW_VOLTAGE * 204;
68  inputScale = 1024/((CALIBRATION_HIGH_VOLTAGE - CALIBRATION_LOW_VOLTAGE) * 204);
69 
70  // These are because I don't have a proper voltage rail on my proof of concept board, so I am using
71  // pin 3 as a 5V source.  You probably don't need these.
72  pinMode(3, OUTPUT);         // This pin powers some other pins so i dont have to wire...
73  digitalWrite(3, HIGH);
74 
75  Serial.begin(9600);
76}
77 
78// Sequence each recorded position
79void doPlayBack()
80{
81  int position;
82  long startTime;
83  long lastPulse = 0;    // the time in milliseconds of the last pulse
84 
85  // play back each step for a second
86  for(position = 0; position < positionCount; position++)
87  {
88    startTime = millis();
89    while(millis() - startTime < 1000)
90    {
91      // pulse the servo again if rhe refresh time (20 ms) have passed:
92      if (millis() - lastPulse >= refreshTime)
93      {
94        digitalWrite(servoPin, HIGH);   // Turn the motor on
95        delayMicroseconds(positionTable[position]);       // Length of the pulse sets the motor position
96        digitalWrite(servoPin, LOW);    // Turn the motor off
97        lastPulse = millis();           // save the time of the last pulse
98      }
99    }
100  }
101}
102 
103void loop()
104{
105  int i;
106  long analogValue;    // the value returned from the analog sensor
107 
108  // When the record button is pressed, try to record the current sensor position
109  if(digitalRead(recordButtonPin) == LOW)
110  {
111    if(positionCount < MAX_POSITIONS)
112    {
113      // There is room to store the positions, so do so.
114      analogValue = analogRead(analogPin);                    // Read the analog input
115      Serial.println(analogValue);
116      analogValue = (analogValue - inputOffset) * inputScale; // Scale the compressed input to a full range
117      analogValue = analogValue*(maxPulse - minPulse)/1024 + minPulse;  // Precompute the pulse length
118 
119      positionTable[positionCount++] = analogValue;           // Store in the table
120      Serial.println(analogValue);
121 
122      // Debounce the input button
123      digitalWrite(statusPin, HIGH);
124      while(digitalRead(recordButtonPin) == LOW)
125        delay(200);
126      digitalWrite(statusPin, LOW);
127    } else {
128      // There is no more room to record positions, so flash the LED in protest.
129      for(i = 0; i < 5; i++)
130      {
131        digitalWrite(statusPin, HIGH);
132        delay(100);
133        digitalWrite(statusPin, LOW);
134        delay(100);
135      }
136    }
137  }
138 
139  // User wants to play back the sequence
140  if(digitalRead(playButtonPin) == LOW) {
141    digitalWrite(statusPin, HIGH);
142    doPlayBack();
143    digitalWrite(statusPin, LOW);
144  }
145}

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

Escribe un comentario

Tu comentario