-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecode_v4.ino
330 lines (299 loc) · 12.4 KB
/
recode_v4.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*Filawinder Firmware rebuilt by Arnd Struve
* Hamburg, 26.02.2019
*
* Original firmware did not work for me, so I decided to rewrite it. Code length was reduced by half.
*
* Commands:
* 1) hold left and turn pot to set min
* 2) hold right and turn pot to set max
* 3) hold center and turn pot to set current position and direction
* 4) press left and right for more than 0.5s to start QTR calibration
*/
#include "EEPROMex.h"
#include "EEPROMVar.h"
#include <Servo.h>
#include <QTRSensors.h>
#include <PID_v1.h>
//PINS
#define qtrPin0 0 //Top
#define qtrPin1 1
#define qtrPin2 2
#define qtrPin3 3 //Bottom
#define hallPin 7
#define btnMinPin 3
#define btnMaxPin 8
#define btnSetPin 4
#define swAutoPin 2
#define potPin 6
#define motorPin 5
#define servoPin 6
#define ledPin 13
//debug options
bool debugMode = true; //Activated all kinds of debug messages in serial
bool debugMotorSpeed = false; //Current motor speed in manual and auto mode [%]
bool debugGuide = true;
bool debugInputs = false;
//Servo
int servoMinPos = EEPROM.readInt(70); //Right limit for filament guide
int servoMaxPos = EEPROM.readInt(80); //Left Limit for Filamnet guide
float servoCurrentPos = EEPROM.readInt(90); //read from current servo position
//float servoCurrentPos = 0;
bool servoCurrentDir = bool(EEPROM.readInt(100)); //Direction the guide is moving
float degPerRotation = 1.17; //Guide shift per registered rotation. 1.75mm --> 1.17 deg
int rotStatus = LOW; //HIGH, when hallSensor goes from high to low
//QTR
unsigned int lastRegisteredPos = 0; //store last non-zero line position
unsigned long lastRegisteredTime = millis(); // time when this non zero position was observed
int timeOut = 1000; //milli seconds before sustained zero position is valid
int cutOffMin = 400;
int cutOffMax = 2600;
unsigned int sensorValues[4];
//Motor
double Setpoint = 1500; //The value PID trys to maintain. The number controls the amount of tension on the spool.
double Input, Output; //PID variables
unsigned long timeToMinimumSpeed = 0;
int minimumMotorSpeed = 25; //does not turn under 25/255
int currentMotorSpeed = 0;
int motorTimeOut = 2500; //tolerance timeframe, before motor turns off completely
int revCount = 0;
QTRSensorsAnalog qtra((unsigned char[]) {qtrPin3, qtrPin2, qtrPin1, qtrPin0}, 4, 4, QTR_NO_EMITTER_PIN); //4 Sensors used, 4 Samples per Sensor, No emitter pin
PID pullPID(&Input, &Output, &Setpoint, 0.0020, 0, 0.001, REVERSE); //Specify the links and initial tuning parameters
Servo servo;
void setup() {
pinMode(qtrPin0, INPUT); //Set Inputs and Outputs
pinMode(qtrPin1, INPUT);
pinMode(qtrPin2, INPUT);
pinMode(qtrPin3, INPUT);
pinMode(hallPin, INPUT);
pinMode(btnMinPin, INPUT_PULLUP); //Using internal pullups
pinMode(btnMaxPin, INPUT_PULLUP);
pinMode(btnSetPin, INPUT_PULLUP);
pinMode(swAutoPin, INPUT_PULLUP);
pinMode(potPin, INPUT);
pinMode(motorPin, OUTPUT);
pinMode(servoPin, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(38400); //Serial for debugging
if(debugMode){Serial.println("GUIDE min: " + String(servoMinPos) + " max: " + String(servoMaxPos) + " current: " + String(servoCurrentPos) + " dir: " + String(servoCurrentDir));}
servo.attach(servoPin); //Init Servo
//servoCurrentPos = servo.read();
digitalWrite(motorPin, currentMotorSpeed); //Set Motorspeed to 0
initQTR(); //Get saved sensor values from EEPROM
pullPID.SetMode(AUTOMATIC); //turn the PID on
pullPID.SetControllerDirection(REVERSE);
pullPID.SetOutputLimits(-5, 5);
//pullPID.SetSampleTime(500);
}
void loop(){
while(debugInputs){testInputs();}
//Set Servo Minimum: btnMin and not btnMax
if (!digitalRead(btnMinPin) && digitalRead(btnMaxPin)){
delay(500);
while (!digitalRead(btnMinPin) && digitalRead(btnMaxPin)){
setMinGuidePosition();
}
}
//Set Servo Maximum: btnMax and not btnMin
if (!digitalRead(btnMaxPin) && digitalRead(btnMinPin)){
delay(500);
while (!digitalRead(btnMaxPin) && digitalRead(btnMinPin)){
setMaxGuidePosition();
}
}
//Set current servo position: Set and not btnMin or btnMax
while (!digitalRead(btnSetPin) && digitalRead(btnMinPin) && digitalRead(btnMaxPin)){setGuidePosition();}
//QTR Calibration: Min + Max
if (!digitalRead(btnMaxPin) && !digitalRead(btnMinPin)){
delay(500);
if (!digitalRead(btnMaxPin) && !digitalRead(btnMinPin)){ //still pressed?
calibrateQTR(); //Will read and store min and max values within the next 5s
}
}
//If none of the above is pressed execute guideControl() and motor control (in auto or manual mode)
guideControl();
//Manual mode: Auto off
if (digitalRead(swAutoPin)){
manualControl();
}
//Auto mode: Auto on
if (!digitalRead(swAutoPin)){
pullControl();
}
}
void pullControl(){
//unsigned int Input = getLinePos(); //Get line position from sensors
Input = getLinePos();
if (!pullPID.Compute()){ //Run the PID
return;
}
currentMotorSpeed = currentMotorSpeed + (int)(Output+0.5);
setMotorSpeed(currentMotorSpeed); //Set the spool speed to the PID result
if(debugMotorSpeed){Serial.println("Auto motor speed: " + String(currentMotorSpeed/2.55) + "%");}
}
void guideControl(){
//Revolution is registered
if (rotStatus == LOW && !digitalRead(hallPin)){
rotStatus = HIGH;
revCount++;
if(debugGuide){Serial.println("Rev: " + String(revCount));}
if (servoCurrentPos >= servoMinPos){ //Min Left about 130 (higher value than max)
servoCurrentDir = HIGH;
}
if (servoCurrentPos <= servoMaxPos){
servoCurrentDir = LOW;
}
if (servoCurrentDir) { //If the current direction of the guide is forward
servoCurrentPos = (servoCurrentPos - degPerRotation); //Move the guide +1.17 degree for 1.75mm filament
}
else{
servoCurrentPos = (servoCurrentPos + degPerRotation);
}
servo.write(int(servoCurrentPos));
if(debugGuide){
if(servoCurrentDir){Serial.println("pos: " + String(servoCurrentPos) + " dir: HIGH Limits: " + String(servoMinPos) + "/" + String(servoMaxPos));}
else{Serial.println("pos: " + String(servoCurrentPos) + " dir: LOW Limits: " + String(servoMinPos) + "/" + String(servoMaxPos));}
}
}
//reset trigger
if (rotStatus == HIGH && digitalRead(hallPin)){
rotStatus = LOW;
}
delay(100); //delay for not reading hall sensor "switch" twice
}
void manualControl(){
int currentMotorSpeed = analogRead(potPin); // reads the value of the potentiometer (value between 0 and 1023)
currentMotorSpeed = map(currentMotorSpeed, 0, 1023, 0, 255); // scale it to use it with the servo (value between 0 and 180)
analogWrite(motorPin, currentMotorSpeed); // sets the servo position according to the scaled value
if(debugMotorSpeed){Serial.println("Manual motor speed: " + String(currentMotorSpeed/2.55) + "%");} //Motor speed in percent
}
void setMotorSpeed(int motorSpeed){
//when motor speed is set lower than the minimal speed --> keep minimal speed for some time
if (motorSpeed < minimumMotorSpeed && (millis() - timeToMinimumSpeed < motorTimeOut)){
motorSpeed = minimumMotorSpeed;
}
else {
timeToMinimumSpeed = millis();
}
if (motorSpeed < 0){
motorSpeed = 0;
}
if (motorSpeed>255){
motorSpeed=255;
}
analogWrite(motorPin, motorSpeed); //Set the spool speed to the PID result
}
unsigned int getLinePos(){
qtra.readCalibrated(sensorValues);
unsigned int currentLinePosition = qtra.readLine(sensorValues, QTR_EMITTERS_OFF, 1);
//Ignore extreme values 0/3000, when the filament is not in the sensor frame for some time
if (currentLinePosition == 0 || currentLinePosition == 3000){
if (millis()-lastRegisteredTime <= timeOut || (cutOffMin < lastRegisteredPos && lastRegisteredPos < cutOffMax)){
currentLinePosition = lastRegisteredPos;
}
}
//Line within limits
else{
lastRegisteredPos = currentLinePosition;
lastRegisteredTime = millis();
}
return currentLinePosition;
}
void calibrateQTR(){
analogWrite(motorPin, 0);
Serial.println("Calibrating QTR sensors...");
qtra.resetCalibration();
delay(1000);
digitalWrite(ledPin, HIGH);
for (int i = 0; i < 300; i++) // make the calibration take about 7 seconds
{
qtra.calibrate(); // reads all sensors 10 times at 2500 us per read (i.e. ~25 ms per call)
}
for (int i = 0; i < 4; i++)
{
EEPROM.writeInt(i*2, qtra.calibratedMinimumOn[i]); //Write the minimum values into EEPROM. Each takes 2 slots, 0-1,2-3,4-5,6-7
EEPROM.writeInt((i*2)+10, qtra.calibratedMaximumOn[i]); //Write the minimum values into EEPROM. Each takes 2 slots, 10-11,12-13,14-15,16-17
Serial.println("sensor" + String(i) + " min/max: " + String(qtra.calibratedMinimumOn[i])+ "/" + String(qtra.calibratedMaximumOn[i]));
}
digitalWrite(ledPin, LOW);
Serial.println("Calibration done.");
for (int i = 0; i < 5; i++){ //blink 5 times
delay(300);
digitalWrite(ledPin, HIGH);
delay(700);
digitalWrite(ledPin, LOW);
}
}
void initQTR(){
//Read values from EEPROM
qtra.calibrate();
for (int i = 0; i < 4; i++){
qtra.calibratedMinimumOn[i] = EEPROM.readInt(i*2);
qtra.calibratedMaximumOn[i] = EEPROM.readInt((i*2)+10);
if(debugMode){Serial.println("sensor" + String(i) + " min/max: " + String(EEPROM.readInt(i*2)) + "/" + String(EEPROM.readInt(i*2+10)));}
}
if(debugMode){Serial.println("Sensor values restored from EEPROM.");}
}
void setGuidePosition(){
analogWrite(motorPin, 0);
int lastPosition = servoCurrentPos;
servoCurrentPos = potAverage(5); //Make the current position the max limit
servo.write(servoCurrentPos); //Move the guide to the knob position
if (lastPosition > servoCurrentPos){
servoCurrentDir = HIGH; //Direction Left-Right
}
if (lastPosition < servoCurrentPos){
servoCurrentDir = LOW; //Direction Right-Left
}
if(debugGuide && servoCurrentDir){Serial.println("servoCurrentPos: " + String(servoCurrentPos) + "\tservoCurrentDir: HIGH");}
if(debugGuide && !servoCurrentDir){Serial.println("servoCurrentPos: " + String(servoCurrentPos) + "\tservoCurrentDir: LOW");}
if(digitalRead(btnSetPin)){
EEPROM.writeInt(90, servoCurrentPos); //write to EEPROM when button is released
EEPROM.writeInt(100, servoCurrentDir); //write to EEPROM when button is released
if(debugGuide && servoCurrentDir){Serial.println("Writing servoCurrentPos " + String(servoCurrentPos) + " and servoCurrentDir: HIGH to EEPROM");}
if(debugGuide && !servoCurrentDir){Serial.println("Writing servoCurrentPos " + String(servoCurrentPos) + " and servoCurrentDir: LOW to EEPROM");}
}
}
void setMinGuidePosition(){
analogWrite(motorPin, 0);
servoMinPos = potAverage(5); //Make the current position the minlimit
servo.write(servoMinPos); //Move the guide to the knob position
if(debugGuide){Serial.println("Min Limit: " + String(servoMinPos));}
if(digitalRead(btnMinPin)){
EEPROM.writeInt(70, servoMinPos); //write to EEPROM when button is released
if(debugGuide){Serial.println("Writing servoMinPos " + String(servoMinPos) + " to EEPROM");}
}
}
void setMaxGuidePosition(){
analogWrite(motorPin, 0);
servoMaxPos = potAverage(5); //Make the current position the max limit
servo.write(servoMaxPos); //Move the guide to the knob position
if(debugGuide){Serial.println("Max Limit: " + String(servoMaxPos));}
if(digitalRead(btnMaxPin)){
EEPROM.writeInt(80, servoMaxPos); //write to EEPROM when button is released
if(debugGuide){Serial.println("Writing servoMaxPos " + String(servoMaxPos) + " to EEPROM");}
}
}
int potAverage(int potReadings){
//sum readings "potReadings"-times and divide by potReadings. Return mapped value.
int average = 0;
for (int i = 0; i < potReadings; i++){
average += analogRead(potPin);
delay(1); //delay in between reads for stability
}
average = int(average/potReadings);
average = map(average, 0, 1023, 180, 0);
return average;
}
//FOR DEBUGGING
void testInputs(){
Serial.print("Min: " + String(digitalRead(btnMinPin)) + "\t");
Serial.print("Max: " + String(digitalRead(btnMaxPin)) + "\t");
Serial.print("Set: " + String(digitalRead(btnSetPin)) + "\t");
Serial.print("Auto: " + String(digitalRead(swAutoPin)) + "\t");
Serial.print("Pot: " + String(analogRead(potPin)) + "\t");
Serial.print("S0: " + String(analogRead(qtrPin0)) + "\t");
Serial.print("S1: " + String(analogRead(qtrPin1)) + "\t");
Serial.print("S2: " + String(analogRead(qtrPin2)) + "\t");
Serial.print("S3: " + String(analogRead(qtrPin3)) + "\n");
delay(100);
}