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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
| # include <stdlib.h>
# include <conio.h>
# include <stdio.h>
# include <math.h>
# include <graphics.h>
# include <conio.h>
# include <dos.h>
# include <tsani.h>
#include<assert.h>
template <class T>
class VECT
{
T *v;
int size;
public:
VECT(int s){ v=new T[s]; size=s;}
~VECT(){delete v;}
T& operator[](int i)
{
assert((i>=0 && i<size));
return v[i];
}
};
float pi=3.141593;
int N_max=1024*4;
VECT<unsigned int> datas(N_max);
VECT<float> U(N_max);
int read_data(void){
//zeroing
int j;
for(j=0;j<N_max; j++){
U[j]=0;
datas[j]=0;
}
int LXQ;
unsigned data,w11,w9_10,w1_2;
//step 1 >> reset LAM
cam_naf(17,0,10); //reset LAM
//step 2 >> discretization
data=0425;//step=timer
cam_i(17,3,16,&data);//limit register
cam_i(17,3,0,&data);//read answer
if(data&(1<<11)){//if wrong limits
printf("\nWrong limits!");
return(0);
}
//step 3 >> comutator
w11=0<<10;//one channel mode
w9_10=0<<8;//0 channel
w1_2=3;//10,24 Volt mode
data=w1_2|w9_10|w11;
cam_i(17,7,16,&data);//set comutator bite
//step4 >> start memory adress
data=0;
cam_i(17,2,16,&data);
//step5 >> RAM mode
data=1;
cam_i(17,1,16,&data);
//step6 >> "SINGLE" mode
data=4;
cam_i(17,1,16,&data);
//step7 >> wating for the start of period
unsigned prev_data=0;
while(1){ // while r1: 1->0
cam_i(11,0,0,&data);
if((data==0)&&(prev_data==1)){
//printf("\nwhile has braked");
break;
}
prev_data=data;
//printf("\nwhile is working");
}
//step8 >> АЦП start
data=1;
cam_i(17,5,16,&data);
//step9 >> wating for LAM
while(1){//while !LAM
LXQ=cam_naf(17,0,8);
if(LXQ&1) break;
}
//step10 >> reading mode
data=1;
cam_i(17,1,16,&data);
//step11 >> set start bite
data=0;
cam_i(17,2,16,&data);
//step12 >> reading
int i;
for(i=0;i<N_max;i++){
cam_i(17,0,0,&data);
datas[i]=data;
}
//step13 >> normalization
/*
for(i=0;i<21;i++){
printf("\ndata(%i)=%i",i,datas[i]);
} */
float k=10.24/2048;
for(i=0;i<N_max;i++){
U[i]=k*(datas[i]-2048.0);
}
/*moveto(300,300);
for(i=0;i<21;i++){
//printf("\ndata(%i)=%i",i,datas[i]);
lineto(100+i,U[i]*10);
}
getch();*/
return (1);
}
float ksi[3]={-0.6374,0.7641,-0.0993}, eta[3]={0.2966,0.1737,-0.939};
void norm_ksi_eta(void){
int i;
float l_ksi=sqrt(ksi[0]*ksi[0]+ksi[1]*ksi[1]+ksi[2]*ksi[2]);
float l_eta=sqrt(eta[0]*eta[0]+eta[1]*eta[1]+eta[2]*eta[2]);
for(i=0;i<=2;i++){
ksi[i]=ksi[i]/l_ksi;
eta[i]=eta[i]/l_eta;
}
}
//float r[3]={1,0,0},q[2]={0,0};
float scal(float *vector1, float *vector2){//скалярное произведение
return vector1[0]*vector2[0]+vector1[1]*vector2[1]+vector1[2]*vector2[2];
}
void get_q(float *vector_3d, float *vector_2d){//проектирование
vector_2d[0]=scal(vector_3d,ksi);
vector_2d[1]=scal(vector_3d,eta);
}
int x_center=350, y_center=280;
void line_3d(float x1,float y1,float z1,float x2, float y2, float z2){
float q1[2];
float q2[2];
float q1_3d[3]={x1,y1,z1};
float q2_3d[3]={x2,y2,z2};
get_q(q1_3d,q1);
get_q(q2_3d,q2);
line(x_center+q1[0],y_center+q1[1],x_center+q2[0],y_center+q2[1]);
}
int x_max=400, y_max=300, z_max=220;
void grid(void){
setcolor(WHITE);
gprintf(150,10);
setcolor(LIGHTCYAN);
gprintf(20,20,"Press \"x\" or \"y\" or \"z\" and then rotate the figure with keys \"->\" and \"<-\".");
gprintf(20,30,"Press \"m\" and then move the figure with keys \"->\", \"<-\", \"up\", \"down\".");
gprintf(20,getmaxy()-10,"\"p\":plot equation, \"q\":exit.");
gprintf(20,40,"You can also change the bar hight");
gprintf(20,50,"with keys \"up\" and \"down\".");
gprintf(getmaxx()-130,getmaxy()-10,"\"r\":read data");
setcolor(LIGHTRED);
line_3d(0,0,0,x_max,0,0);
line_3d(0,0,0,0,y_max,0);
line_3d(0,0,0,0,0,z_max);
line_3d(x_max,0,0,x_max-10,0,5);
line_3d(x_max,0,0,x_max-10,0,-5);
line_3d(0,y_max,0,0,y_max-10,5);
line_3d(0,y_max,0,0,y_max-10,-5);
line_3d(0,0,z_max,2.5,-2.5,z_max-10);
line_3d(0,0,z_max,-2.5,2.5,z_max-10);
setcolor(CYAN);
line_3d(x_max,0,0,x_max,y_max,0);
line_3d(x_max,y_max,0,0,y_max,0);
line_3d(x_max,0,0,x_max,0,z_max);
line_3d(x_max,0,z_max,0,0,z_max);
line_3d(0,y_max,0,0,y_max,z_max);
line_3d(0,y_max,z_max,0,0,z_max);
line_3d(x_max,y_max,0,x_max,y_max,z_max);
line_3d(x_max,y_max,z_max,x_max,0,z_max);
line_3d(x_max,y_max,z_max,0,y_max,z_max);
//line_3d();
setcolor(WHITE);
//x
line_3d(x_max,0,-5,x_max-5,0,-15);
line_3d(x_max-5,0,-5,x_max,0,-15);
//y
line_3d(0,y_max,-5,0,y_max-5,-15);
line_3d(0,y_max-5,-5,0,y_max-2.5,-10);
//z
line_3d(0,-10,z_max,0,-15,z_max);
line_3d(0,-10,z_max,0,-15,z_max-10);
line_3d(0,-15,z_max-10,0,-10,z_max-10);
}
char rotate(char axis){
char a=axis;
float step_fi=0.05,ksi_new[3]={0,0,0},eta_new[3]={0,0,0};
cleardevice();
grid();
if(axis=='x'){
while(1){
setcolor(WHITE);
//x
line_3d(x_max,0,-5,x_max-5,0,-15);
line_3d(x_max-5,0,-5,x_max,0,-15);
setcolor(BLACK);
//y
line_3d(0,y_max,-5,0,y_max-5,-15);
line_3d(0,y_max-5,-5,0,y_max-2.5,-10);
setcolor(BLACK);
//z
line_3d(0,-10,z_max,0,-15,z_max);
line_3d(0,-10,z_max,0,-15,z_max-10);
line_3d(0,-15,z_max-10,0,-10,z_max-10);
while(!kbhit()){}
a=getch();
if(a=='x'||a=='y'||a=='z') a=rotate(a);
if(a=='q'||a=='p'||a=='m'||a=='e') break;
if(a==75){// if ->
ksi_new[1]=ksi[1]*cos(step_fi)-ksi[2]*sin(step_fi);
ksi_new[2]=ksi[1]*sin(step_fi)+ksi[2]*cos(step_fi);
eta_new[1]=eta[1]*cos(step_fi)-eta[2]*sin(step_fi);
eta_new[2]=eta[1]*sin(step_fi)+eta[2]*cos(step_fi);
ksi[1]=ksi_new[1];
ksi[2]=ksi_new[2];
eta[1]=eta_new[1];
eta[2]=eta_new[2];
//norm_ksi_eta();
}
if(a==77){// if <-
step_fi=-step_fi;
ksi_new[1]=ksi[1]*cos(step_fi)-ksi[2]*sin(step_fi);
ksi_new[2]=ksi[1]*sin(step_fi)+ksi[2]*cos(step_fi);
eta_new[1]=eta[1]*cos(step_fi)-eta[2]*sin(step_fi);
eta_new[2]=eta[1]*sin(step_fi)+eta[2]*cos(step_fi);
ksi[1]=ksi_new[1];
ksi[2]=ksi_new[2];
eta[1]=eta_new[1];
eta[2]=eta_new[2];
step_fi=-step_fi;
//norm_ksi_eta();
}
cleardevice();
grid();
}
}
if(axis=='y'){
while(1){
setcolor(BLACK);
//x
line_3d(x_max,0,-5,x_max-5,0,-15);
line_3d(x_max-5,0,-5,x_max,0,-15);
setcolor(WHITE);
//y
line_3d(0,y_max,-5,0,y_max-5,-15);
line_3d(0,y_max-5,-5,0,y_max-2.5,-10);
setcolor(BLACK);
//z
line_3d(0,-10,z_max,0,-15,z_max);
line_3d(0,-10,z_max,0,-15,z_max-10);
line_3d(0,-15,z_max-10,0,-10,z_max-10);
while(!kbhit()){}
a=getch();
if(a=='x'||a=='y'||a=='z') a=rotate(a);
if(a=='q'||a=='p'||a=='m'||a=='e') break;
if(a==75){// if ->
ksi_new[2]=ksi[2]*cos(step_fi)-ksi[0]*sin(step_fi);
ksi_new[0]=ksi[2]*sin(step_fi)+ksi[0]*cos(step_fi);
eta_new[2]=eta[2]*cos(step_fi)-eta[0]*sin(step_fi);
eta_new[0]=eta[2]*sin(step_fi)+eta[0]*cos(step_fi);
ksi[2]=ksi_new[2];
ksi[0]=ksi_new[0];
eta[2]=eta_new[2];
eta[0]=eta_new[0];
}
if(a==77){// if <-
step_fi=-step_fi;
ksi_new[2]=ksi[2]*cos(step_fi)-ksi[0]*sin(step_fi);
ksi_new[0]=ksi[2]*sin(step_fi)+ksi[0]*cos(step_fi);
eta_new[2]=eta[2]*cos(step_fi)-eta[0]*sin(step_fi);
eta_new[0]=eta[2]*sin(step_fi)+eta[0]*cos(step_fi);
ksi[2]=ksi_new[2];
ksi[0]=ksi_new[0];
eta[2]=eta_new[2];
eta[0]=eta_new[0];
step_fi=-step_fi;
}
cleardevice();
grid();
}
}
if(axis=='z'){
while(1){
setcolor(BLACK);
//x
line_3d(x_max,0,-5,x_max-5,0,-15);
line_3d(x_max-5,0,-5,x_max,0,-15);
setcolor(BLACK);
//y
line_3d(0,y_max,-5,0,y_max-5,-15);
line_3d(0,y_max-5,-5,0,y_max-2.5,-10);
setcolor(WHITE);
//z
line_3d(0,-10,z_max,0,-15,z_max);
line_3d(0,-10,z_max,0,-15,z_max-10);
line_3d(0,-15,z_max-10,0,-10,z_max-10);
while(!kbhit()){}
a=getch();
if(a=='x'||a=='y'||a=='z') a=rotate(a);
if(a=='q'||a=='p'||a=='m'||a=='e') break;
if(a==75){// if ->
ksi_new[0]=ksi[0]*cos(step_fi)-ksi[1]*sin(step_fi);
ksi_new[1]=ksi[0]*sin(step_fi)+ksi[1]*cos(step_fi);
eta_new[0]=eta[0]*cos(step_fi)-eta[1]*sin(step_fi);
eta_new[1]=eta[0]*sin(step_fi)+eta[1]*cos(step_fi);
ksi[0]=ksi_new[0];
ksi[1]=ksi_new[1];
eta[0]=eta_new[0];
eta[1]=eta_new[1];
//norm_ksi_eta();
}
if(a==77){// if <-
step_fi=-step_fi;
ksi_new[0]=ksi[0]*cos(step_fi)-ksi[1]*sin(step_fi);
ksi_new[1]=ksi[0]*sin(step_fi)+ksi[1]*cos(step_fi);
eta_new[0]=eta[0]*cos(step_fi)-eta[1]*sin(step_fi);
eta_new[1]=eta[0]*sin(step_fi)+eta[1]*cos(step_fi);
ksi[0]=ksi_new[0];
ksi[1]=ksi_new[1];
eta[0]=eta_new[0];
eta[1]=eta_new[1];
step_fi=-step_fi;
//norm_ksi_eta();
}
cleardevice();
grid();
}
}
return a;
}
void plot_bar(float x0, float y0, float width_x, float width_y, float hight){
// setcolor(LIGHTGREEN);
line_3d(x0,y0,hight,x0+width_x,y0,hight);
line_3d(x0,y0,hight,x0,y0+width_y,hight);
line_3d(x0+width_x,y0,hight,x0+width_x,y0+width_y,hight);
line_3d(x0,y0+width_y,hight,x0+width_x,y0+width_y,hight);
setcolor(DARKGRAY);
line_3d(x0,y0,0,x0+width_x,y0,0);
line_3d(x0,y0,0,x0,y0+width_y,0);
line_3d(x0+width_x,y0,0,x0+width_x,y0+width_y,0);
line_3d(x0,y0+width_y,0,x0+width_x,y0+width_y,0);
line_3d(x0,y0,hight,x0,y0,0);
line_3d(x0,y0+width_y,hight,x0,y0+width_y,0);
line_3d(x0+width_x,y0,hight,x0+width_x,y0,0);
line_3d(x0+width_x,y0+width_y,hight,x0+width_x,y0+width_y,0);
}
char move_axis(void){
char a;
while(1){
while(!kbhit()){}
a=getch();
if(a=='x'||a=='y'||a=='z') a=rotate(a);
if(a=='q'||a=='p'||a=='e') break;
if(a==77) x_center=x_center+5;
if(a==75) x_center=x_center-5;
if(a==80) y_center=y_center+5;
if(a==72) y_center=y_center-5;
cleardevice();
//printf("\n x_center=%i",x_center);
grid();
}
return a;
}
float E_masshtab=600; //equation masshtab
float integral(int N1,int N2,int k){
int i;
double I_cos=0,I_sin=0,I=0;
float x;
for(i=N1;i<N2;i++){
x=2*((float)k)*pi*((float)i)/((float)N_max);
I_cos=I_cos+U[i]*cos(x);
}
I_cos=2*I_cos/(float)(N2-N1);
for(i=N1;i<N2;i++){
x=2*((float)k)*pi*((float)i)/((float)N_max);
I_sin=I_sin+U[i]*sin(x);
}
I_sin=2*I_sin/(float)(N2-N1);
I=sqrt(I_cos*I_cos+I_sin*I_sin);
return E_masshtab*I;
}
void plot(void){
int k=0,x0,y0,hight;
float delta_N=N_max,width_y=40,width_x=x_max,n;
double I=0;
I=integral(0,N_max,0); // CONSTANT GARMONIC
x0=0; //
y0=0; //
hight=I/5;
setcolor(WHITE); //
plot_bar(x0,y0,width_x,width_y,hight);//
k=1;
y0=width_y;
while(delta_N>=80){
for(n=0;n+delta_N-1<N_max;n=n+delta_N){
//i=(int)n;
I=integral(n,n+delta_N,k);
x0=x_max*(n/(float)N_max);
hight=I/5;
setcolor(CYAN);
if(k==1) setcolor(LIGHTGREEN);
if(k==2) setcolor(LIGHTGREEN);
if(k==4) setcolor(CYAN);
if(k==16) setcolor(LIGHTRED);
if(k==32) setcolor(LIGHTBLUE);//
if(k==64) setcolor(LIGHTRED);
if(k==128) setcolor(LIGHTRED);
plot_bar(x0,y0,width_x,width_y,hight);
}
delta_N=delta_N/2;
y0=y0+width_y;
k*=2;
width_x=x_max*(delta_N/(float)N_max);
}
int i;
float U_max=0;
for(i=0;i<N_max;i++){
if(U_max<fabs(U[i])) U_max=fabs(U[i]);
}
setcolor(LIGHTBLUE);
int x,z,x_next,z_next;
line_3d(0,0,z_max/2,x_max,0,z_max/2);
line_3d(x_max,0,z_max/2,x_max-10,0,z_max/2+3);
line_3d(x_max,0,z_max/2,x_max-10,0,z_max/2-3);
setcolor(YELLOW);
for(i=0;i<N_max-1;i++){
x=x_max*(float)i/(float)N_max;
z=z_max*(float)U[i]/U_max/2;
x_next=x_max*(float)(i+1)/(float)N_max;
z_next=z_max*(float)U[i+1]/U_max/2;
line_3d(x,0,z+z_max/2,x_next,0,z_next+z_max/2);
}
}
char equation(){
char a;
while(1){
while(!kbhit()){}
a=getch();
if(a=='x'||a=='y'||a=='z'||a=='m') return a;
if(a=='q'||a=='p') return a;
if(a==72){
E_masshtab=E_masshtab*2;
cleardevice();
grid();
plot();
}
if(a==80){
E_masshtab=E_masshtab/2;
cleardevice();
grid();
plot();
}
if(a=='r'){
read_data();
cleardevice();
grid();
plot();
}
}
}
void main(void){
norm_ksi_eta();
cam_crate(0);//define camac
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "*.bgi");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}
setcolor(WHITE);
gprintf(150,10);
// gprintf(60,10,"You can also change hight");
//read_data();
/* printf("\nksi[0]=%f ksi[1]=%f ksi[2]=%f; norm=%f\n",ksi[0],ksi[1],ksi[2],sqrt(ksi[0]*ksi[0]+ksi[1]*ksi[1]+ksi[2]*ksi[2]));
printf("\neta[0]=%f eta[1]=%f eta[2]=%f; norm=%f\n",eta[0],eta[1],eta[2],sqrt(eta[0]*eta[0]+eta[1]*eta[1]+eta[2]*eta[2]));
*/
int i;
float w=0;
for(i=0;i<N_max;i++)
/*if(i>=N_max/4&&i<=N_max/4+N_max/4){
U[i]=10*sin(16*2*pi*i/(float)N_max);
}else{
U[i]=0;
} */
if(1|| i<N_max/4+N_max/4 && i>N_max/4){
U[i]=sin( (2<<(int)(1+4*(i/(float)N_max)))*2*pi*i/(float)N_max);
}else{
U[i]=0;
}
grid();
plot();
char a='1';
while(a!='q'){
a=equation();
if(a=='x'||a=='y'||a=='z') a=rotate(a);
if(a=='m') a=move_axis();
if(a=='p') plot();
//printf("%i",a);
}
//getch();
closegraph();
/*
printf("\nx_center=%i, y_center=%i",x_center,y_center);
printf("\nksi[]={%f;%f;%f}",ksi[0],ksi[1],ksi[2]);
printf("\neta[]={%f;%f;%f}",eta[0],eta[1],eta[2]);
printf("\nPress any key.");
getch();
*/
} |