-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForce_Bench.ino
201 lines (179 loc) · 5.75 KB
/
Force_Bench.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
#include "config.h"
#include "console_helper_functions.h"
#include "sauter_helper_functions.h"
#include "motor_helper_functions.h"
float x;
float force;
bool sign;
void setup() {
// start serial connection to computer
Serial.begin(9600);
// start serial connection to Sauter
Serial1.begin(9600);
// set connection to Gecko driver
pinMode(pin_direction, OUTPUT);
pinMode(pin_step, OUTPUT);
delay(100);
}
void loop() {
x = 0.0;
force = 0.0;
// flush data from console
while(Serial.available() ) {Serial.read();}
int option = 0;
while(option < 1 or option > 6){
Serial.println("----------------------------------------------------");
Serial.println("Force bench cycle test. Choose option:");
Serial.println("1) open.");
Serial.println("2) open, then close.");
Serial.println("3) close.");
Serial.println("4) close, then open");
Serial.println("5) manual");
Serial.println("6) config");
//Serial.println("7) change other options");
option = get_int();
}
while(Serial.available() ) {Serial.read();} // read everything coming from sauter
while(Serial1.available() ) {Serial1.read();} // read everything coming from sauter
//
// Configurations menu option selected
//
if (option == 6){
change_config();
//
// Manual mode selected
// Manually enter desired position
//
} else if(option == 5){
Serial.println("----------------------------------------------------");
Serial.println("Manual mode selected");
Serial.println("Measure height and enter it: ");
x = get_float();
force = get_sauter_force(n_force_measures,BUFFER_SIZE);
print_data(x, force);
float target;
bool stop = false;
bool init = false;
int direction, steps;
while(~stop){
Serial.println("Desired position (0 to stop, \'negative\' to go to initial position): ");
target = get_float();
if (target == 0)
break;
else if (target < 0){
steps = steps_initial;
target = -target;
init = true;
} else {
steps = steps_test;
init = false;
}
direction = target < x? HIGH : LOW;
while(abs(x - target) > 2*millimeters_per_step*steps){
make_steps(direction,steps,pin_direction,pin_step,pulse_period, pulse_dutycycle);
x = x - 2*(direction-0.5)*millimeters_per_step*steps;
//Serial.print("dist: "); Serial.print(abs(x - target)); Serial.print(" ");
if(init)
force = get_sauter_force(1,BUFFER_SIZE);
else
force = get_sauter_force(n_force_measures,BUFFER_SIZE);
print_data(x, force);
if(abs(force) >= FORCE_MAX) {
stop = true;
Serial.println("Force over limits, stopping test...");
break;
}
}
}
//
// Auto mode selected
// Test all the way until force limit is achieved
//
} else {
int direction_odd = (option == 1 || option == 2)? HIGH : LOW;
int direction_even = HIGH - direction_odd;
int direction = (option == 1 || option == 2)? HIGH : LOW;
bool stop = false;
//print_dir(direction);
// Stage 1
char proceed = 'a';
while(proceed != 'y' && proceed != 'n'){
Serial.println("Go to initial position (y/n)?");
proceed = get_char();
//Serial.println(proceed);
}
if(proceed == 'y'){
Serial.println("Going to initial position...");
while(abs(force) < FORCE_LIM){
make_steps(direction,steps_initial,pin_direction,pin_step,pulse_period, pulse_dutycycle);
force = get_sauter_force(1,BUFFER_SIZE);
}
sign = force > 0;
}
// Stage 2:
int i = 0;
if(!stop){
Serial.println("Starting test phase 1...");
direction = switch_dir(direction);
//print_dir(direction);
Serial.println("Measure height and enter it: ");
x = get_float();
while(abs(force) < FORCE_LIM || i*millimeters_per_step < X_MIN){
if(abs(force) >= FORCE_MAX) {
stop = true;
Serial.println("Force over limits, stopping test...");
break;
}
force = get_sauter_force(n_force_measures,BUFFER_SIZE);
//x = height_initial + 2*(direction_odd-0.5)*i*millimeters_per_step;
x = x - 2*(direction-0.5)*millimeters_per_step*steps_test;
sign = force > 0;
print_data(x, force);
make_steps(direction,steps_test,pin_direction,pin_step,pulse_period, pulse_dutycycle);
i += 1;
}
}
// Stage 3:
if(!stop && (option == 2 || option == 4)){
Serial.println("Starting test phase 2...");
direction = switch_dir(direction);
//print_dir(direction);
if(wait_between_stages) wait_for_enter();
int steps = i;
for(i = 0; i < steps; i++){
force = get_sauter_force(n_force_measures,BUFFER_SIZE);
x = x - 2*(direction-0.5)*millimeters_per_step*steps_test;
sign = force > 0;
print_data(x, force);
make_steps(direction,steps_test,pin_direction,pin_step,pulse_period, pulse_dutycycle);
if(abs(force) >= FORCE_MAX) {
stop = true;
Serial.println("Force over limits, stopping test...");
break;
}
}
// Print last one
print_data(x, force);
}
// Stage 4: Go to low force position
if(!stop){
Serial.println("Stopping...");
direction = switch_dir(direction);
//print_dir(direction);
while(abs(force) > FORCE_ZERO){
force = get_sauter_force(n_force_measures,BUFFER_SIZE);
make_steps(direction,steps_final,pin_direction,pin_step,pulse_period, pulse_dutycycle);
if(abs(force) >= FORCE_MAX){
break;
}
}
}
}
}
int switch_dir(int value){
if (value == HIGH){
return LOW;
} else if (value == LOW){
return HIGH;
}
}