Repository navigation
Expand file tree
/
Copy path1-logic-test.cpp
More file actions
333 lines (264 loc) · 7.5 KB
/
Copy path1-logic-test.cpp
File metadata and controls
333 lines (264 loc) · 7.5 KB
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
331
332
333
#include <iostream>
#include <fstream>
#include <chrono>
#include <thread>
#include <cmath>
#include <wiringPi.h>
#include <ctime>
using namespace std::chrono;
using namespace std::this_thread;
using namespace std;
bool history_list_initialized_flag = 0;
string history_file_dir;
struct Node{
string timestamp;
string command;
Node* next;
Node(string new_timestamp, string new_command){
timestamp = new_timestamp;
command = new_command;
}
};
Node* head;
void display(Node* head){
cout<<"--------"<<endl;
Node* current = head;
while (current!=nullptr){
cout <<'['<< current->timestamp<< " : " << current->command<<']' << " ";
current = current->next;
}
cout<<endl;
cout<<"--------"<<endl;
}
void append(Node*& head, string timestamp, string command){
Node* current = head;
Node* new_node = new Node(timestamp, command);
if (head == nullptr){
head = new_node;
return;
}
while (current->next!=nullptr){
current=current->next;
}
current->next = new_node;
}
int save_history(string history_file_dir, string command, string arg){
if (history_list_initialized_flag == 0){
time_t timestamp = time(NULL);
string ts = to_string(timestamp);
Node* head = new Node(ts, "START");
history_list_initialized_flag = 1;
}
time_t timestamp = time(NULL);
string ts = to_string(timestamp);
string command_arg = command+" "+arg;
append(head, ts, command_arg);
display(head);
if (command == "end"){
cout<<"Writing history to: "<<history_file_dir<<endl;
fstream history_file(history_file_dir);
if (not history_file.is_open()) {
cerr << "ERROR couldn't open "<<history_file_dir<<endl;
return 1;
}
Node* current = head;
while (current != nullptr) {
history_file << current->timestamp << " " << current->command<< endl;
current = current->next;
}
history_file.close();
cout<<"History saved succesfully"<<endl;
history_file.close();
}
return 0;
}
const string config_path = "motor.conf";
//PIN DEFINITIONS
// !!! CAUTION !!!
// If the program is interrupted or suspended and the
// pins are in HIGH state the engine coils may overheat.
//
//naming convention:
//hm → horizontal motor
//vm → vertical motor
//
// wiringPi physical
// ↓ ↓
const int hm1 = 29; //40
const int hm2 = 28; //38
const int hm3 = 27; //36
const int hm4 = 26; //32
//--------------- ;
const int vm1 = 25; //37
const int vm2 = 24; //35
const int vm3 = 23; //33
const int vm4 = 22; //31
//(remember to connect GND);
const int step_delay = 10; //step delay at full speed
const double ramp_amount = 20; //acceleration
const double ramp_percent = 0.20; //defines at what point of the command we finish
//and start acceleration.
void ramp(double progress){
//cout<<progress<<endl;
int round_step_delay;
if(progress < ramp_percent){
round_step_delay = floorf(step_delay - progress * ramp_amount);
sleep_for(milliseconds(round_step_delay));
cout << "0-20%" << round_step_delay << endl;
} else if(progress > 1-ramp_percent){
round_step_delay = floorf(step_delay + progress * ramp_amount);
sleep_for(milliseconds(round_step_delay));
cout << "80-100%" << round_step_delay << endl;
}
else{
round_step_delay = floorf(step_delay - ramp_amount);
sleep_for(milliseconds(step_delay));
cout <<"20%-80%" << round_step_delay << endl;
}
}
//takes a command and moves the engines
//returns 0 if executed succefully
int run_command(string command, string arg){
//initialize the pins
pinMode(hm1, OUTPUT);
pinMode(hm2, OUTPUT);
pinMode(hm3, OUTPUT);
pinMode(hm4, OUTPUT);
pinMode(vm1, OUTPUT);
pinMode(vm2, OUTPUT);
pinMode(vm3, OUTPUT);
pinMode(vm4, OUTPUT);
digitalWrite(hm1, LOW);
digitalWrite(hm2, LOW);
digitalWrite(hm3, LOW);
digitalWrite(hm4, LOW);
digitalWrite(vm1, LOW);
digitalWrite(vm2, LOW);
digitalWrite(vm3, LOW);
digitalWrite(vm4, LOW);
int intarg = stoi(arg);
cout<<"executing: "<<command<<" "<<intarg<<endl;
if(command == "sleep"){
sleep_for(milliseconds(intarg));
}
int clock_sequence[][4] = {
{1, 0, 0, 1},
{0, 0, 1, 1},
{0, 1, 1, 0},
{1, 1, 0, 0},
};
int counter_clock_sequence[][4] = {
{1, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 1, 1},
{1, 0, 0, 1},
};
if (command == "up") {
int phase = 0;
double progress = 0; // 0-1 value representing command execution progress
for (int i = 0; i < intarg; i++) {
progress = double(i)/double(intarg);
digitalWrite(hm1, clock_sequence[phase][0]);
digitalWrite(hm2, clock_sequence[phase][1]);
digitalWrite(hm3, clock_sequence[phase][2]);
digitalWrite(hm4, clock_sequence[phase][3]);
phase = (phase + 1) % 4;
ramp(progress);
}
}
if (command == "down") {
int phase = 0;
for (int i = 0; i < intarg; i++) {
digitalWrite(hm1, counter_clock_sequence[phase][0]);
digitalWrite(hm2, counter_clock_sequence[phase][1]);
digitalWrite(hm3, counter_clock_sequence[phase][2]);
digitalWrite(hm4, counter_clock_sequence[phase][3]);
phase = (phase + 1) % 4;
sleep_for(milliseconds(step_delay));
}
}
if(command == "right"){
int phase = 0;
for (int i = 0; i < intarg; i++) {
digitalWrite(vm1, clock_sequence[phase][0]);
digitalWrite(vm2, clock_sequence[phase][1]);
digitalWrite(vm3, clock_sequence[phase][2]);
digitalWrite(vm4, clock_sequence[phase][3]);
phase = (phase + 1) % 4;
sleep_for(milliseconds(step_delay));
}
}
if(command == "left"){
int phase = 0;
for (int i = 0; i < intarg; i++) {
digitalWrite(vm1, counter_clock_sequence[phase][0]);
digitalWrite(vm2, counter_clock_sequence[phase][1]);
digitalWrite(vm3, counter_clock_sequence[phase][2]);
digitalWrite(vm4, counter_clock_sequence[phase][3]);
phase = (phase + 1) % 4;
sleep_for(milliseconds(step_delay));
}
}
//cleanup to stop coils from overheating / energy waste
digitalWrite(hm1, LOW);
digitalWrite(hm2, LOW);
digitalWrite(hm3, LOW);
digitalWrite(hm4, LOW);
digitalWrite(vm1, LOW);
digitalWrite(vm2, LOW);
digitalWrite(vm3, LOW);
digitalWrite(vm4, LOW);
save_history(history_file_dir, command, to_string(intarg));
return 0;
}
int execute_command_list(string commands_file_dir){
fstream commands_file(commands_file_dir);
if (not commands_file.is_open()) {
cerr << "ERROR couldn't open "<<config_path<<endl;
return 1;
}
if(step_delay < 0){
cerr<<"ERROR step delay has to be a positive integer"<<endl;
return 1;
}
if(step_delay < 5){
cout<<"WARNING low step delay, this may cause unexpected behavior"<<endl;
}
string command, arg;
while((commands_file>>command)and(commands_file>>arg)){
run_command(command, arg);
}
commands_file.close();
return 0;
}
int main(){
cout<<"using config from: "<<config_path<<endl;
fstream config_file(config_path);
if (not config_file.is_open()) {
cerr << "ERROR couldn't open "<<config_path<<endl;
return 1;
}
if (wiringPiSetup() == -1){
cerr << "ERROR wiringPi setup failed" <<endl;
return 1;
}
string commands_file_dir, engine_1_name, engine_2_name;
string engine_1_dpfs, engine_2_dpfs;
config_file >> commands_file_dir;
config_file >> commands_file_dir;
config_file >> history_file_dir;
config_file >> history_file_dir;
config_file >> engine_1_name;
config_file >> engine_1_name;
config_file >> engine_1_dpfs;
config_file >> engine_1_dpfs;
config_file >> engine_2_name;
config_file >> engine_2_name;
config_file >> engine_2_dpfs;
config_file >> engine_2_dpfs;
cout<<"----------------"<<endl;
cout<<"running commands from: "<<commands_file_dir<<endl;
execute_command_list(commands_file_dir);
config_file.close();
return 0;
}