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
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.
10
* Servo control code thanks to:
11
* Servo control from an analog input
14
* additions by Carlyn Maw
15
* Created 28 Jan. 2006
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.
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
29
******************************************************************************************************/
31
/***** Variable Definitions ***************************************************************************/
32
int servoPin = 4; // Servo motor control line
33
int analogPin = 1; // Line from servo potentimeter
34
int recordButtonPin = 7; // Record button
35
int playButtonPin = 10; // Playback button
36
int statusPin = 13; // Status LED
38
// Adjust these to fit your servo
39
int minPulse = 500; // Minimum servo position (us)
40
int maxPulse = 2200; // Maximum servo position (us)
41
int refreshTime = 20; // the time needed in between pulses (ms)
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)
48
// These are derived from the measured voltages above, are are computed during the setup routine
49
int inputOffset = 0; // Input offset (ADC counts)
50
float inputScale = 0; // Input scale (%)
52
#define MAX_POSITIONS 50 // Maximum number of positions that can be stored
53
int positionTable[MAX_POSITIONS]; // Table to hold each position
54
int positionCount = 0; // Number of positions recorded
56
/***** Functions ************************************************************************************/
59
pinMode(analogPin, INPUT);
60
pinMode(servoPin, INPUT);
61
pinMode(recordButtonPin, INPUT);
62
pinMode(playButtonPin, INPUT);
63
pinMode(statusPin, OUTPUT);
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);
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);
78
// Sequence each recorded position
83
long lastPulse = 0; // the time in milliseconds of the last pulse
85
// play back each step for a second
86
for(position = 0; position < positionCount; position++)
89
while(millis() - startTime < 1000)
91
// pulse the servo again if rhe refresh time (20 ms) have passed:
92
if (millis() - lastPulse >= refreshTime)
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
106
long analogValue; // the value returned from the analog sensor
108
// When the record button is pressed, try to record the current sensor position
109
if(digitalRead(recordButtonPin) == LOW)
111
if(positionCount < MAX_POSITIONS)
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
119
positionTable[positionCount++] = analogValue; // Store in the table
120
Serial.println(analogValue);
122
// Debounce the input button
123
digitalWrite(statusPin, HIGH);
124
while(digitalRead(recordButtonPin) == LOW)
126
digitalWrite(statusPin, LOW);
128
// There is no more room to record positions, so flash the LED in protest.
129
for(i = 0; i < 5; i++)
131
digitalWrite(statusPin, HIGH);
133
digitalWrite(statusPin, LOW);
139
// User wants to play back the sequence
140
if(digitalRead(playButtonPin) == LOW) {
141
digitalWrite(statusPin, HIGH);
143
digitalWrite(statusPin, LOW);
Fuente: http://www.cibomahto.com/2008/02/thing-a-day-day-9-servo-as-input-device/
12.415 Veces leído 0 comentarios