-
Notifications
You must be signed in to change notification settings - Fork 0
/
int2.c
292 lines (231 loc) · 6.56 KB
/
int2.c
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
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define axis 3
#define joints 3
struct vec{
double axis_pos[axis];
double joint_pos[joints];
};
struct path{
struct vec pos;
struct path* next;
struct path* prev;
};
struct max_joint_steps{
double step[joints];
};
struct min_axis_steps{
double step[axis];
};
struct tool_offset{
double soffset[axis];
};
struct vec split(struct vec A, struct vec B, int steps){
struct vec C;
for(int i = 0; i < axis; i++){
C.axis_pos[i] = A.axis_pos[i] + (B.axis_pos[i] - A.axis_pos[i]) / steps;
}
return(C);
}
/*struct vec tfkin(struct vec A, struct tool_offset t){
struct vec a;
a.axis_pos[0] = A.axis_pos[0] + t.offset[0];
}*/
//struct vec fikin(struct vec A, struct tool_offset t){}
struct vec fkin(struct vec A){ // forward kin axis -> joints
struct vec a = A;
double tmp;
double jump;
// wire kin:
// A B
// \ /
// \ /
// C
//
// A = (0/0)
// B = (10/0)
// AC = joint[0]
// BC = joint[0]
//a.joint_pos[0] = sqrt(pow(A.axis_pos[0], 2) + pow(A.axis_pos[1], 2)); // AC = sqrt(x^2+y^2)
//a.joint_pos[1] = sqrt(pow(10 - A.axis_pos[0], 2) + pow(A.axis_pos[1], 2)); // BC = sqrt((10-x)^2+y^2)
// floppy kin
// rot disk = joint[0]
// lin head = joint[1] (radius)
tmp = atan2(A.axis_pos[1], A.axis_pos[0]);
a.joint_pos[0] = tmp;
a.joint_pos[1] = sqrt(pow(A.axis_pos[0], 2) + pow(A.axis_pos[1], 2));
/*for(int i = 0; i < joints; i++){
a.joint_pos[i] = A.axis_pos[i];
}*/
/*a.joint_pos[0] = A.axis_pos[0];
a.joint_pos[1] = A.axis_pos[1];
a.joint_pos[2] = A.axis_pos[2];
a.joint_pos[3] = A.axis_pos[3];
a.joint_pos[4] = A.axis_pos[4];
a.joint_pos[5] = A.axis_pos[5];*/
// cobra kin
double dest_xy = sqrt(A.axis_pos[0] * A.axis_pos[0] + A.axis_pos[1] * A.axis_pos[1]);
double dest_xyz = sqrt(dest_xy * dest_xy + (A.axis_pos[2] - 245) * (A.axis_pos[2] - 245));
double tjoint1 = atan2(A.axis_pos[2] - 245, dest_xy);
double tjoint2 = acos(dest_xyz / 2 / 190);
a.joint_pos[0] = atan2(A.axis_pos[1], A.axis_pos[0]);
a.joint_pos[1] = tjoint1 + 3.141526/2 - tjoint2 - 3.141526/4;
a.joint_pos[2] = tjoint2 - tjoint1;
return(a);
}
struct vec ikin(struct vec a){ // inverse kin joints -> axis
struct vec A = a;
// wire kin:
// A B
// \ /
// \ /
// C
//
// A = (0/0)
// B = (10/0)
// AC = joint[0]
// BC = joint[0]
A.axis_pos[0] = (pow(a.joint_pos[0], 2) - pow(a.joint_pos[1], 2) + pow(10, 2)) / (2 * 10);
A.axis_pos[1] = sqrt(pow(a.joint_pos[0], 2) - pow(A.axis_pos[0], 2));
// floppy kin
// rot disk = joint[0]
// lin head = joint[1] (radius)
A.axis_pos[0] = sqrt(pow(a.joint_pos[1], 2) / ( 1+ pow(tan(a.joint_pos[0]), 2)));
A.axis_pos[1] = tan(a.joint_pos[0]) * A.axis_pos[0];
/*for(int i = 0; i < axis; i++){
A.axis_pos[i] = a.joint_pos[i];
}*/
/*A.axis_pos[0] = a.joint_pos[0];
A.axis_pos[1] = a.joint_pos[1];
A.axis_pos[2] = a.joint_pos[2];
A.axis_pos[3] = a.joint_pos[3];
A.axis_pos[4] = a.joint_pos[4];
A.axis_pos[5] = a.joint_pos[5];*/
return(a);
}
void insert(struct path* A, struct vec B){
struct path* tmp = A->next;
A->next = (struct path *)malloc(sizeof(struct path));
A->next->pos = B;
A->next->next = tmp;
A->next->prev = A;
if(A->next->next){
A->next->next->prev = A->next;
}
}
int check_joint_steps(struct vec A, struct vec B, struct max_joint_steps max_j_s){
for(int i = 0; i < joints; i++){
if(fabs(A.joint_pos[i] - B.joint_pos[i]) > max_j_s.step[i]){
return(1);
}
}
return(0);
}
int check_axis_steps(struct vec A, struct vec B, struct min_axis_steps min_a_s){
for(int i = 0; i < axis; i++){
if(fabs(A.axis_pos[i] - B.axis_pos[i]) > min_a_s.step[i]){
return(1);
}
}
return(0);
}
int intp(struct path* A, struct max_joint_steps max_j_s, struct min_axis_steps min_a_s){
struct path* p = A;
int steps = 0;
int tmp = 0;
if(!A){
return(0);
}
p->pos = fkin(p->pos);
while(p->next){
p->next->pos = fkin(p->next->pos);
p = p->next;
}
p = A;
while(p->next){
for(int i = 0; i < axis; i++){
tmp = ceil(abs((p->next->pos.axis_pos[i] - p->pos.axis_pos[i]) / min_a_s.step[i]));
if(steps < tmp){
steps = tmp;
}
}
for(int i = 0; i < steps; i++){
insert(p, split(p->pos, p->next->pos, steps - i)); // split
p->next->pos = fkin(p->next->pos); // axis -> joints
p = p->next;
}
steps = 0;
p = p->next;
}
return(0);
}
int no_jump(struct path* AB){
double tmp;
while(AB && AB->next){
tmp = round((AB->next->pos.joint_pos[0] - AB->pos.joint_pos[0]) / M_PI);
AB->next->pos.joint_pos[0] -= tmp * M_PI;
AB = AB->next;
}
return(0);
}
int main(){
FILE *file;
//file = fopen("intp.txt","w+");
struct max_joint_steps max_j_s; // min axis res
max_j_s.step[0] = 1 / 180 * 3.141526; // 0.1 deg res -> rad
max_j_s.step[1] = 1 / 180 * 3.141526; // 0.1 mm res
max_j_s.step[2] = 1 / 180 * 3.141526; // 0.1 mm res
struct min_axis_steps min_a_s; // min joint res
min_a_s.step[0] = 1; // 0.1 mm res
min_a_s.step[1] = 1; // 0.1 mm res
min_a_s.step[2] = 1; // 0.1 mm res
struct vec A;
A.axis_pos[0] = 100;
A.axis_pos[1] = 0;
A.axis_pos[2] = 0;
struct vec B;
B.axis_pos[0] = 1;
B.axis_pos[1] = 0;
B.axis_pos[2] = 0;
struct path* AB = (struct path *)malloc(sizeof(struct path));
struct path* AB_head;
struct path* head = AB;
AB->next = 0;
AB->prev = 0;
AB->pos = A;
insert(AB, B);
//insert(AB, A);
int i = 0;
AB_head = AB;
while(AB){
i++;
AB = AB->next;
}
AB = AB_head;
double * joint_pos_0 = (double *) malloc(sizeof(double) * i);
double * joint_pos_1 = (double *) malloc(sizeof(double) * i);
double * joint_pos_2 = (double *) malloc(sizeof(double) * i);
i = 0;
while(AB){
joint_pos_0[i] = AB->pos.axis_pos[0];
joint_pos_1[i] = AB->pos.axis_pos[1];
joint_pos_2[i] = AB->pos.axis_pos[2];
i++;
AB = AB->next;
}
intp(AB, max_j_s, min_a_s);
//no_jump(AB);
/*while(AB){
//printf("Axis Pos (%f/%f)", AB->pos.axis_pos[0], AB->pos.axis_pos[1]);
//printf(" -> ");
//printf("joint Pos (%f/%f)", AB->pos.joint_pos[0], AB->pos.joint_pos[1]);
//printf("\n");
//fprintf(file, "%f %f %f %f\n", AB->pos.axis_pos[0], AB->pos.axis_pos[1], AB->pos.joint_pos[0], AB->pos.joint_pos[1]);
// plot with: gnuplot -e 'set multiplot layout 6,1; plot "intp.txt" using 0:1 title "axis[0]";plot "intp.txt" using 0:2 title "axis[1]";plot "intp.txt" using 0:3 title "joint[0]";plot "intp.txt" using 0:4 title "joint[1]";plot "intp.txt" using 1:2 title "axis"; plot "intp.txt" using 3:4 title "joints"' -p
fprintf(file, "%f %f %f %f %f %f\n", AB->pos.axis_pos[0], AB->pos.axis_pos[1], AB->pos.axis_pos[2], AB->pos.joint_pos[0], AB->pos.joint_pos[1], AB->pos.joint_pos[2]);
AB = AB->next;
}*/
//fclose(file);
return(0);
}