-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfocusController.ino
216 lines (177 loc) · 5.65 KB
/
focusController.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
// Define driver output pins
int engPin = 7;
int dirPin = 6;
int clkPin = 5;
void setup() {
pinMode(clkPin, OUTPUT);
pinMode(engPin, OUTPUT);
pinMode(dirPin, OUTPUT);
Serial.begin(9600);
// Wait for Serial connection to initialize before proceeding
while (!Serial) ;
Serial.println("Input 1 to Turn LED on and 2 to turn off");
}
void loop() {
if (Serial.available())
{
int state = Serial.parseInt();
switch (state) {
case 1:
Serial.println("Move Forward");
digitalWrite(engPin, LOW); // Motor is enabled when eng = 0
digitalWrite(dirPin, LOW);
moveMotor(clkPin, 250);
break;
case 2:
Serial.println("Move Backward");
digitalWrite(engPin, LOW);
digitalWrite(dirPin, HIGH);
moveMotor(clkPin, 250);
break;
case 3:
Serial.println("Motor Off");
digitalWrite(engPin, HIGH);
break;
case 4:
Serial.println("Basic Test");
digitalWrite(engPin, LOW);
digitalWrite(dirPin, HIGH);
int period = 25;
for (int i = 0; i < 100; i++)
{
digitalWrite(clkPin, HIGH);
delay(period);
digitalWrite(clkPin, LOW);
delay(period);
}
break;
default:
// statements
break;
}
if (state == 4)
{
Serial.println("Basic Test");
digitalWrite(engPin, LOW);
digitalWrite(dirPin, HIGH);
int period = 25;
for (int i = 0; i < 10; i++)
{
digitalWrite(clkPin, HIGH);
delay(period);
digitalWrite(clkPin, LOW);
delay(period);
}
}
}
}
void testMotor(int clkPin)
{
int testPeriod = 500;
int numSpin = 2;
for (int i = 0; i < numSpin; i++)
{
digitalWrite(clkPin, HIGH);
delay(testPeriod);
digitalWrite(clkPin, LOW);
delay(testPeriod);
}
}
void moveMotor(int clkPin, int targetNumSteps) // Linear ramp to stop speed then
{
int slowPeriod = 20; // 20 Hz, could be changed to whatever
int fastPeriod = 10; // 500 Hz (Measured clock speed at MRO was 770 Hz)
int accelTime = slowPeriod - fastPeriod; //Number of steps to reach max clk speed, in the case of linear ramp it is just slow - fast
int numSteps= 0;
// Start Motor
// Non linear example: for ( int i = slowPeriod, i>= fastPeriod; i = int(i/1.33) )
//Linear Ramp:
int clkPeriod = slowPeriod;
while(clkPeriod >= fastPeriod && numSteps < targetNumSteps - accelTime)
{
digitalWrite(clkPin, HIGH);
delay(clkPeriod);
digitalWrite(clkPin, LOW);
delay(clkPeriod);
Serial.print("Accelerating, Clkperiod = "); Serial.println(clkPeriod);
numSteps++;
clkPeriod--;
}
//Stay at max speed until we are ready to decelerate
while (numSteps < targetNumSteps - accelTime)
{
digitalWrite(clkPin, HIGH);
delay(fastPeriod);
digitalWrite(clkPin, LOW);
delay(fastPeriod);
Serial.print("Max speed, Clkperiod = "); Serial.println(clkPeriod);
numSteps++;
}
//Linear ramp down to slowPeriod
clkPeriod = fastPeriod;
while(clkPeriod < slowPeriod && numSteps < targetNumSteps)
{
digitalWrite(clkPin, HIGH);
delay(clkPeriod);
digitalWrite(clkPin, LOW);
delay(clkPeriod);
Serial.print("Deccelerating, Clkperiod = "); Serial.println(clkPeriod);
Serial.println(clkPeriod);
numSteps++;
clkPeriod++;
}
Serial.println("Motor has stopped...hopefully ._.");
}
void moveMotorSmooth(int motorPin)
{
float freq = 150; // Initial frequency of the clock signal (2 Hz)
float targetFreq = 3080; // Target frequency of the clock signal (770 Hz)
float k = 0.05; // Rate of frequency change
unsigned long prevMillis = 0; // Used to time frequency changes
unsigned long interval = 1; // Interval for frequency updates (in milliseconds)
bool moveCancelled = false;
while(!moveCancelled)
{
// Calculate the current frequency using asymptotic formula from chat gpt
// Asymptotic:
//freq = freq - (freq - targetFreq) / (1 + exp(-k * ((millis() / 1000.0) - 1000)));
freq++;
// Update the frequency every 'interval' milliseconds
unsigned long currentMillis = millis();
if (currentMillis - prevMillis >= interval) {
prevMillis = currentMillis;
// Update the tone frequency
tone(motorPin, freq); // Generate the clock signal with the calculated frequency
}
// Optionally print the current frequency to the Serial Monitor
Serial.println(freq);
//If the frequency reaches or exceeds the target, stop generating the clock signal
if (freq >= targetFreq - 1) {
//noTone(motorPin); // Stop the clock signal when target is reached
Serial.println("Target frequency reached.");
moveCancelled = true;
}
}
delay(800);
while(freq > 0)
{
// Calculate the current frequency using asymptotic formula from chat gpt
// Asymptotic: freq = freq - (freq - targetFreq) / (1 + exp(-k * (millis() / 1000.0 - 1000)));
freq--;
// Update the frequency every 'interval' milliseconds
unsigned long currentMillis = millis();
if (currentMillis - prevMillis >= interval) {
prevMillis = currentMillis;
// Update the tone frequency
tone(motorPin, freq); // Generate the clock signal with the calculated frequency
}
// Optionally print the current frequency to the Serial Monitor
Serial.println(freq);
// If the frequency reaches or exceeds the target, stop generating the clock signal
if (freq <= 150) {
freq = 0;
noTone(motorPin); // Stop the clock signal when target is reached
Serial.println("Target frequency reached.");
}
}
}