-
Notifications
You must be signed in to change notification settings - Fork 0
/
x-ray_diffraction.ino
209 lines (192 loc) · 4.66 KB
/
x-ray_diffraction.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
/*
* Version 1.1 last updated 29-July-2018
* https://github.com/RickyZiegahn/X-Ray_Diffraction
* Made for McGill University under D.H. Ryan
*/
#define pulse_high 10
#define pulse_rate 5
int detector = 4;
int d_dir = 7; //direction controller of detector motor
int d_loc = 0; //location of detector arm
int sample = 8;
int s_dir = 12; //direction controller of sample motor
int s_loc = 0; //location of sample arm
int pulse_low (pulse_rate - pulse_high); //The sum of the pulse length and lowtime is the length of one full cycle
int steps = 0;
int motor = 0;
int counter = 2; //pulses come into pin 2
int counts = 0;
int len = 1000;
double current_degree = 0;
double starting_degree = 0;
double ending_degree = 0;
double degree_increment = 0;
double factor = 400;
double constant = 0;
double degrees_to_steps(float deg) {
float temp_step = deg * factor + constant;
return temp_step;
}
double steps_to_degrees(int steps) {
float temp_degree = (steps - constant)/factor;
return temp_degree;
}
void wait_for_input() {
while(!Serial.available()) {
}
}
void confirm_flag() {
Serial.println(999999999);
}
void inc_count() {
counts += 1; //increments the number of counts detected each time there is a pulse
}
void accept_length() {
wait_for_input();
len = Serial.parseInt(); //reads length input
}
void accept_parameters() {
wait_for_input();
starting_degree = Serial.parseFloat();
wait_for_input();
ending_degree = Serial.parseFloat();
wait_for_input();
degree_increment = Serial.parseFloat();
wait_for_input();
len = Serial.parseInt();
}
void count() {
int t1 = millis();
int t2 = millis();
int dt = 0;
while (dt < len) {
t2 = millis();
dt = t2 - t1;
}
Serial.println(counts);
dt = 0;
}
void drive(int steps, int motor) {
choose_direction(steps, motor);
move_steps(steps, motor);
}
void choose_direction(int steps, int motor) {
if(steps > 0){
if(motor == 1){
digitalWrite(s_dir, HIGH);
}
if(motor == 2){
digitalWrite(d_dir, HIGH);
}
}
if(steps < 0){
if(motor == 1){
digitalWrite(s_dir, LOW);
}
if(motor == 2){
digitalWrite(d_dir, LOW);
}
}
}
void move_steps(int steps, int motor) {
if(motor == 1){
for(int i=1; i <= abs(steps); i++){
digitalWrite(sample, HIGH);
delayMicroseconds(pulse_high);
digitalWrite(sample, LOW);
delayMicroseconds(pulse_low);
}
}
if(motor == 2){
for(int i=1; i <= abs(steps); i++){
digitalWrite(detector, HIGH);
delayMicroseconds(pulse_high);
digitalWrite(detector, LOW);
delayMicroseconds(pulse_low);
}
}
}
void accept_drive() {
wait_for_input();
motor = Serial.parseInt(); //read motor input
wait_for_input();
steps = Serial.parseInt(); //read step input
drive(steps, motor);
confirm_flag();
}
void setup() {
pinMode(detector, OUTPUT);
pinMode(d_dir, OUTPUT);
pinMode(sample, OUTPUT);
pinMode(s_dir, OUTPUT);
pinMode(counter, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(counter), inc_count, RISING);
Serial.begin(9600);
}
void loop() {
wait_for_input();
int a = Serial.parseInt();
if(a == 1) {
accept_drive();
accept_drive();
d_loc = 0;
s_loc = 0;
wait_for_input();
int b = Serial.parseInt();
if (b == 1) {
accept_parameters();
//move to starting position
double fsteps = degrees_to_steps(starting_degree);
drive(round(fsteps/2), 1);
s_loc += round(fsteps/2);
drive(round(fsteps), 2);
d_loc += round(fsteps);
counts = 0;
count();
//proceed to the end
for (int i; i <= 999999999; i++) { //number is arbitrarily large
double wanted_degree = starting_degree + degree_increment * i;
double unrounded_steps = degrees_to_steps(wanted_degree);
int s_steps = round(unrounded_steps/2) - s_loc;
int d_steps = round(unrounded_steps) - d_loc;
drive(s_steps, 1);
s_loc += s_steps;
drive(d_steps, 2);
d_loc += d_steps;
counts = 0;
count();
if (wanted_degree < ending_degree) {
continue;
}
if (wanted_degree >= ending_degree) {
break;
}
}
}
else {
//return to beginning of loop
}
}
if(a == 2) {
wait_for_input();
int b = Serial.parseInt();
if (b == 1) { //drive() function from python
accept_drive();
}
if (b == 2) { //set_location() function from python
}
if (b == 3) { //goto() function from python
accept_drive();
}
if (b == 4) { // function from python
accept_length();
counts = 0;
count();
}
else {
//returns to beginning of loop
}
}
else {
}
}