質問編集履歴

7

文書修正

2020/04/08 03:10

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -3,697 +3,3 @@
3
3
  これらの1~5のキーに入っている内容同士をenterキーを押す毎にシャッフルさせたいです。
4
4
 
5
5
  どのようにコードを書けば良いのでしょうか?
6
-
7
- ```#include <stdio.h>
8
-
9
- #include <stdlib.h>
10
-
11
- #include <time.h>
12
-
13
- #include <string.h>
14
-
15
- #include <math.h>
16
-
17
- #include <fbida.h>
18
-
19
- #include <fbiad.h>
20
-
21
- #include <sys/time.h>
22
-
23
- #include <unistd.h>
24
-
25
- #include "rt_wait.h"
26
-
27
- #include <termios.h>
28
-
29
- #include <fcntl.h>
30
-
31
- #include <pthread.h>
32
-
33
- #define filn 30
34
-
35
- double rt_cycle = 1.5;
36
-
37
- DABOARDSPEC Spec;
38
-
39
- DASMPLREQ SmplConfig;
40
-
41
- ADBOARDSPEC BoardSpec;
42
-
43
- ADSMPLREQ AdSmplConfig;
44
-
45
- ADSMPLCHREQ AdSmplChReq[7];
46
-
47
- unsigned short wData[7];
48
-
49
- char gmode = 'b';
50
-
51
- int g_end = 0;
52
-
53
-
54
-
55
- int kbhit(void)
56
-
57
- {
58
-
59
- struct termios oldt, newt;
60
-
61
- int ch;
62
-
63
- int oldf;
64
-
65
- tcgetattr(STDIN_FILENO, &oldt);
66
-
67
- newt = oldt;
68
-
69
- newt.c_lflag &= ~(ICANON | ECHO);
70
-
71
- tcsetattr(STDIN_FILENO, TCSANOW, &newt);
72
-
73
- oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
74
-
75
- fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
76
-
77
- ch = getchar();
78
-
79
- tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
80
-
81
- fcntl(STDIN_FILENO, F_SETFL, oldf);
82
-
83
- if (ch != EOF) {
84
-
85
- ungetc(ch, stdin);
86
-
87
- return 1;
88
-
89
- }
90
-
91
- return 0;
92
-
93
- }
94
-
95
- int main(void)
96
-
97
- {
98
-
99
- double ;
100
-
101
- double y;
102
-
103
- double freqv1;
104
-
105
- double freqv2;
106
-
107
- double freqe1;
108
-
109
- double freqe2;
110
-
111
- double ampv1;
112
-
113
- double ampv2;
114
-
115
- double ampe1;
116
-
117
- double ampe2;
118
-
119
- double phyv1, phyv2, phyv3;
120
-
121
- double phyvp1;
122
-
123
- double phyvp2;
124
-
125
- double ave_xp, ave_yp;
126
-
127
- double dx,dxa,dy,dya,dv;
128
-
129
- int ret, dnum, i, j, n;
130
-
131
- double ave_x, ave_y;
132
-
133
- char *fname = "data.txt";
134
-
135
- FILE *fp;
136
-
137
- unsigned short xData[2];
138
-
139
- unsigned short vData[2];
140
-
141
- DASMPLCHREQ DaSmplChReq[2];
142
-
143
- float x_history[filn], y_history[filn];
144
-
145
- double dData[12], daval = 0x0800;
146
-
147
- double wData_history0[filn], wData_history1[filn], wData_history2[filn], wData_history3[filn], wData_history4[filn], wData_history5[filn], wData_history6[filn], wData_history7[filn], wData_history8[filn], wData_history9[filn], wData_history10[filn], wData_history11[filn];
148
-
149
- pthread_t pth;
150
-
151
- int rtpth;
152
-
153
- unsigned long long rt_ticks = GetTick();
154
-
155
- void *thread_kbhit(void *thdata)
156
-
157
- {
158
-
159
- char c;
160
-
161
- struct thdata *priv = (struct thdata *)thdata;
162
-
163
-
164
-
165
- while(1){
166
-
167
- if( kbhit() ){
168
-
169
- c = getchar();
170
-
171
- }
172
-
173
- gmode = c;
174
-
175
- if( gmode == 'q' || g_end ){
176
-
177
-
178
-
179
- DaClose( dnum );
180
-
181
- AdClose(dnum);
182
-
183
- fclose(fp);
184
-
185
- return 0;
186
-
187
- }
188
-
189
- }
190
-
191
- return (void *) NULL;
192
-
193
- }
194
-
195
-
196
-
197
- system("clear");
198
-
199
- dnum = 1;
200
-
201
-
202
-
203
- ret = DaOpen(dnum);
204
-
205
- if(ret){
206
-
207
- printf("Open error: ret=%Xh\n", ret);
208
-
209
- exit(EXIT_FAILURE);
210
-
211
- }
212
-
213
-
214
-
215
- ret = AdOpen(dnum);
216
-
217
- if (ret) {
218
-
219
- printf("AdOpen error: ret=%Xh\n", ret);
220
-
221
- exit(EXIT_FAILURE);
222
-
223
- }
224
-
225
-
226
-
227
- ret = AdGetDeviceInfo(dnum, &BoardSpec);
228
-
229
- if(ret){
230
-
231
- printf("AdGetDeviceInfo error: ret=%Xh\n", ret);
232
-
233
- AdClose(dnum);
234
-
235
- exit(EXIT_FAILURE);
236
-
237
- }
238
-
239
-
240
-
241
- ret = DaGetDeviceInfo(dnum, &Spec);
242
-
243
- if(ret){
244
-
245
- printf("DaGetDeviceInfo error: ret=%Xh\n", ret);
246
-
247
- DaClose(dnum);
248
-
249
- exit(EXIT_FAILURE);
250
-
251
- }
252
-
253
-
254
-
255
- ret = DaGetSamplingConfig(dnum, &SmplConfig);
256
-
257
- if (ret) {
258
-
259
- printf("DaGetSamplingConfig error: ret=%Xh\n", ret);
260
-
261
- DaClose(dnum);
262
-
263
- exit(EXIT_FAILURE);
264
-
265
- }
266
-
267
-
268
-
269
- ret = AdGetSamplingConfig( dnum, &AdSmplConfig );
270
-
271
- if (ret) {
272
-
273
- printf("AdGetSamplingConfig error: ret=%Xh\n", ret);
274
-
275
- exit(EXIT_FAILURE);
276
-
277
- }
278
-
279
-
280
-
281
- DaSmplChReq[0].ulChNo = 1;
282
-
283
- DaSmplChReq[0].ulRange = SmplConfig.SmplChReq[0].ulRange;
284
-
285
- DaSmplChReq[1].ulChNo = 2;
286
-
287
- DaSmplChReq[1].ulRange = SmplConfig.SmplChReq[0].ulRange;
288
-
289
-
290
-
291
- AdSmplChReq[0].ulChNo = 1;
292
-
293
- AdSmplChReq[0].ulRange = AdSmplConfig.SmplChReq[0].ulRange;
294
-
295
- AdSmplChReq[1].ulChNo = 2;
296
-
297
- AdSmplChReq[1].ulRange = AdSmplConfig.SmplChReq[0].ulRange;
298
-
299
- AdSmplChReq[2].ulChNo = 3;
300
-
301
- AdSmplChReq[2].ulRange = AdSmplConfig.SmplChReq[0].ulRange;
302
-
303
- AdSmplChReq[3].ulChNo = 4;
304
-
305
- AdSmplChReq[3].ulRange = AdSmplConfig.SmplChReq[0].ulRange;
306
-
307
- AdSmplChReq[4].ulChNo = 5;
308
-
309
- AdSmplChReq[4].ulRange = AdSmplConfig.SmplChReq[0].ulRange;
310
-
311
- AdSmplChReq[5].ulChNo = 6;
312
-
313
- AdSmplChReq[5].ulRange = AdSmplConfig.SmplChReq[0].ulRange;
314
-
315
- AdSmplChReq[6].ulChNo = 7;
316
-
317
- AdSmplChReq[6].ulRange = AdSmplConfig.SmplChReq[0].ulRange;
318
-
319
- AdSmplChReq[7].ulChNo = 8;
320
-
321
- AdSmplChReq[7].ulRange = AdSmplConfig.SmplChReq[0].ulRange;
322
-
323
- fp = fopen("data.txt", "w");
324
-
325
- if( fp == NULL ){
326
-
327
- printf("fopen./n");
328
-
329
- return(-1);
330
-
331
- }
332
-
333
-
334
-
335
- double loadCellOffset[8] = { 0, 0, 0, 0, 0, 0, 0, 0};
336
-
337
- for( i=0; i < 10; i++ ){
338
-
339
- ret = AdInputAD(dnum, 8, AD_INPUT_SINGLE, AdSmplChReq, wData);
340
-
341
- if (ret) {
342
-
343
- AdClose(dnum);
344
-
345
- printf("AdInputAD error: ret=%Xh\n", ret);
346
-
347
- exit(EXIT_FAILURE);
348
-
349
- }
350
-
351
- loadCellOffset[0] += (double)wData[0]/10.0;
352
-
353
- loadCellOffset[1] += (double)wData[1]/10.0;
354
-
355
- loadCellOffset[2] += (double)wData[2]/10.0;
356
-
357
- loadCellOffset[3] += (double)wData[3]/10.0;
358
-
359
- loadCellOffset[4] += (double)wData[4]/10.0;
360
-
361
- loadCellOffset[5] += (double)wData[5]/10.0;
362
-
363
- loadCellOffset[6] += (double)wData[6]/10.0;
364
-
365
- loadCellOffset[7] += (double)wData[7]/10.0;
366
-
367
- }
368
-
369
- memset( x_history, 0, sizeof(x_history) );
370
-
371
- memset( y_history, 0, sizeof(y_history) );
372
-
373
- memset( wData_history0, 0, sizeof(wData_history0) );
374
-
375
- memset( wData_history1, 0, sizeof(wData_history1) );
376
-
377
- memset( wData_history2, 0, sizeof(wData_history2) );
378
-
379
- memset( wData_history3, 0, sizeof(wData_history3) );
380
-
381
- memset( wData_history4, 0, sizeof(wData_history4) );
382
-
383
- memset( wData_history5, 0, sizeof(wData_history5) );
384
-
385
- memset( wData_history6, 0, sizeof(wData_history6) );
386
-
387
- memset( wData_history7, 0, sizeof(wData_history7) );
388
-
389
- memset( wData_history8, 0, sizeof(wData_history8) );
390
-
391
- memset( wData_history9, 0, sizeof(wData_history9) );
392
-
393
- memset( wData_history10, 0, sizeof(wData_history10) );
394
-
395
- memset( wData_history11, 0, sizeof(wData_history11) );
396
-
397
-
398
-
399
- rtpth = pthread_create( &pth, NULL, thread_kbhit, (void *)NULL );
400
-
401
-
402
-
403
- for( i=0; i < (int)(3000.0/rt_cycle*1000.0); i++ ){
404
-
405
-
406
-
407
- ret = AdInputAD(dnum, 8, AD_INPUT_SINGLE, AdSmplChReq, wData);
408
-
409
- if (ret) {
410
-
411
- AdClose(dnum);
412
-
413
- printf("AdInputAD error: ret=%Xh\n", ret);
414
-
415
- exit(EXIT_FAILURE);
416
-
417
- }
418
-
419
-
420
-
421
- dData[0] = ((double)wData[0] - loadCellOffset[0]);
422
-
423
- dData[1] = ((double)wData[1] - loadCellOffset[1]);
424
-
425
- dData[2] = ((double)wData[2] - loadCellOffset[2]);
426
-
427
- dData[3] = ((double)wData[3] - loadCellOffset[3]);
428
-
429
- dData[4] = ((double)wData[4] - loadCellOffset[4]);
430
-
431
- dData[5] = ((double)wData[5] - loadCellOffset[5]);
432
-
433
- dData[6] = ((double)wData[6] - loadCellOffset[6]);
434
-
435
- dData[7] = ((double)wData[7] - loadCellOffset[7]);
436
-
437
- dData[8] = dData[0]-dData[1];
438
-
439
- dData[9] = dData[2]-dData[3];
440
-
441
- dData[10] = dData[4]-dData[5];
442
-
443
- dData[11] = dData[6]-dData[7];
444
-
445
-
446
-
447
- memmove(&(wData_history0[1]), wData_history0, (filn-1) * sizeof(float) );
448
-
449
- wData_history0[0] = dData[8];
450
-
451
-
452
-
453
- memmove(&(wData_history1[1]), wData_history1, (filn-1) * sizeof(float) );
454
-
455
- wData_history1[0] = dData[9];
456
-
457
-
458
-
459
- memmove(&(wData_history2[1]), wData_history2, (filn-1) * sizeof(float) );
460
-
461
- wData_history2[0] = dData[10];
462
-
463
-
464
-
465
- memmove(&(wData_history3[1]), wData_history3, (filn-1) * sizeof(float) );
466
-
467
- wData_history3[0] = dData[11];
468
-
469
-
470
-
471
- dData[8] = 0;
472
-
473
- dData[9] = 0;
474
-
475
- dData[10] = 0;
476
-
477
- dData[11] = 0;
478
-
479
-
480
-
481
- for( j = 0; j < filn; j++ ){
482
-
483
-
484
-
485
- dData[8] += wData_history0[j];
486
-
487
- dData[9] += wData_history1[j];
488
-
489
- dData[10] += wData_history2[j];
490
-
491
- dData[11] += wData_history3[j];
492
-
493
-
494
-
495
- }
496
-
497
- dData[8] = dData[8]/(double)filn;
498
-
499
- dData[9] = dData[9]/(double)filn;
500
-
501
- dData[10] = dData[10]/(double)filn;
502
-
503
- dData[11] = dData[11]/(double)filn;
504
-
505
-
506
-
507
- }
508
-
509
- memmove(&(x_history[1]), x_history, (filn-1) * sizeof(float) );
510
-
511
- x_history[0] = newvalue_x;
512
-
513
-
514
-
515
- memmove(&(y_history[1]), y_history, (filn-1) * sizeof(float) );
516
-
517
- y_history[0] = newvalue_y;
518
-
519
-
520
-
521
- for( j = 0, ave_x = 0; j < filn; j++ ){
522
-
523
- ave_x += x_history[j];
524
-
525
- ave_y += y_history[j];
526
-
527
- }
528
-
529
-
530
-
531
- ave_x = (ave_x / (double)filn) ;
532
-
533
- ave_y = (ave_y / (double)filn) ;
534
-
535
- dx = ave_x - ave_xp;
536
-
537
- dy = ave_y - ave_yp;
538
-
539
- dxa = fabs(dx);
540
-
541
- dya = fabs(dy);
542
-
543
- dv = sqrt(pow(dxa,2) + pow(dya,2));
544
-
545
- phyv1 = (dv / freqv1) + phyvp1;
546
-
547
- phyv2 = (dv / freqv2) + phyvp2;
548
-
549
- vData[1] = (ampv1* sin(2 * M_PI * phyv1) +ampv2* sin(2 * M_PI * phyv2)) / 10.0 * 0x8000 + 0x8000;
550
-
551
- vData[0] = sqrt(ampe1 * cos(2 * M_PI * phyv1) +ampe2 * cos(2 * M_PI * phyv2) + ampe1 + ampe2)/ 10.0 * 0x8000 + 0x8000;
552
-
553
-
554
-
555
- ret = DaOutputDA( dnum, 2, DaSmplChReq, vData );
556
-
557
- if (ret) {
558
-
559
- printf("DaOutputDA error: ret=%Xh\n", ret);
560
-
561
- DaClose(dnum);
562
-
563
- exit(EXIT_FAILURE);
564
-
565
- }
566
-
567
- ave_xp = ave_x;
568
-
569
- ave_yp = ave_y;
570
-
571
- phyvp1 = phyv1;
572
-
573
- phyvp2 = phyv2;
574
-
575
-
576
-
577
- rt_wait( &rt_ticks );
578
-
579
-
580
-
581
- switch (gmode) {
582
-
583
- case '5':
584
-
585
- freqv1 = 1.5;
586
-
587
- freqv2 = 1;
588
-
589
- ampv1=0.655;
590
-
591
- ampv2=0;
592
-
593
- ampe1=0;
594
-
595
- ampe2=y*ampe1;
596
-
597
- printf("%4.3f %4.3f %4.3f %4.3f \n", ampv1, ampv2, ampe1,ampe2);
598
-
599
- break;
600
-
601
- case '3':
602
-
603
- freqv1 = 2.5;
604
-
605
- freqv2 = 1;
606
-
607
- ampv1 =1.275;
608
-
609
- ampv2 =0;
610
-
611
- ampe1=0;
612
-
613
- ampe2=y*ampe1;
614
-
615
- printf("%4.3f %4.3f %4.3f %4.3f \n", ampv1, ampv2, ampe1,ampe2);
616
-
617
- break;
618
-
619
- case '4':
620
-
621
- freqv1 = 1.3;
622
-
623
- freqv2 = 1.9;
624
-
625
- x=5.33;
626
-
627
- y=5;
628
-
629
- ampv1=0.301;
630
-
631
- ampv2=x*ampv1;
632
-
633
- ampe1=0;
634
-
635
- ampe2=0;
636
-
637
- printf("%4.3f %4.3f %4.3f %4.3f \n", ampv1, ampv2, ampe1,ampe2);
638
-
639
- break;
640
-
641
- case '1':
642
-
643
- freqv1 = 1.3;
644
-
645
- freqv2 = 2.5;
646
-
647
- x=3.33;
648
-
649
- y=5.33;
650
-
651
- ampv1=0.393;
652
-
653
- ampv2=x*ampv1;
654
-
655
- ampe1=0;
656
-
657
- ampe2=y*ampe1;
658
-
659
- printf("%4.3f %4.3f %4.3f %4.3f \n", ampv1, ampv2, ampe1,ampe2);
660
-
661
- break;
662
-
663
- case '2':
664
-
665
- freqv1 = 1.9;
666
-
667
- freqv2 = 2.5;
668
-
669
- x=4;
670
-
671
- y=2.67;
672
-
673
- ampv1 =0.327;
674
-
675
- ampv2 =x*ampv1;
676
-
677
- ampe1=0;
678
-
679
- ampe2=y*ampe1;
680
-
681
- printf("%4.3f %4.3f %4.3f %4.3f \n", ampv1, ampv2, ampe1,ampe2);
682
-
683
- break;
684
-
685
- }
686
-
687
- }
688
-
689
- DaClose( dnum );
690
-
691
- AdClose(dnum);
692
-
693
- fclose(fp);
694
-
695
- return 0;
696
-
697
- }
698
-
699
- ```

6

ソースコードを変更しました。

2020/04/08 03:10

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- C言語: switch文の中身をシャッフルするにはどうすればいでしょうか。
1
+ C言語: switch文の中身をシャッフルするにはどうすればいでしょうか。
test CHANGED
@@ -1,25 +1,9 @@
1
- (a b c d )=(1,2,3,4),(4,2,5,7),(8,1,9,5),(9,2,3,1)
1
+ 以下のコードのswitch文についてですが、現状例えば1のキーを押すと必ず同じ値が読み込出力されます。
2
-
3
- のように4パターンの(a,b,c,d)を定めます。
2
+
4
-
5
- これらの値を用いて、
6
-
7
- x=4a+3b+2c+d
8
-
9
- を計算させたいです。
10
-
11
- 現在はswitch文を用いて4パターンそれぞれをキーボードの1,2,3,4のキーを押すとそれぞれの文字に数字が代入され、計算されるようなコードになっていますが、enterキーを一回押すと、それぞれのキーから出力される4パターンの値を1回ランダムにシャッフルされるようなコードにしたいです。(a,b,c,d)のグループ内の値は常に固定で、④つのグループ同士でシャッフルを行いたいです。
12
-
13
-
14
-
15
- 現在構造体を作成し、それらを配列に入てシャッフルしようと試みてが、上手くいきません
3
+ れらの1~5のキーに入いる内容同士をenterキーを押す毎にシャッフルさせたす。
16
-
17
-
18
4
 
19
5
  どのようにコードを書けば良いのでしょうか?
20
6
 
21
- ソースコードは以下のようになっております。
22
-
23
7
  ```#include <stdio.h>
24
8
 
25
9
  #include <stdlib.h>
@@ -34,172 +18,682 @@
34
18
 
35
19
  #include <fbiad.h>
36
20
 
21
+ #include <sys/time.h>
22
+
23
+ #include <unistd.h>
24
+
25
+ #include "rt_wait.h"
26
+
27
+ #include <termios.h>
28
+
29
+ #include <fcntl.h>
30
+
31
+ #include <pthread.h>
32
+
33
+ #define filn 30
34
+
35
+ double rt_cycle = 1.5;
36
+
37
+ DABOARDSPEC Spec;
38
+
39
+ DASMPLREQ SmplConfig;
40
+
41
+ ADBOARDSPEC BoardSpec;
42
+
43
+ ADSMPLREQ AdSmplConfig;
44
+
45
+ ADSMPLCHREQ AdSmplChReq[7];
46
+
47
+ unsigned short wData[7];
48
+
49
+ char gmode = 'b';
50
+
51
+ int g_end = 0;
52
+
53
+
54
+
55
+ int kbhit(void)
56
+
57
+ {
58
+
59
+ struct termios oldt, newt;
60
+
61
+ int ch;
62
+
63
+ int oldf;
64
+
65
+ tcgetattr(STDIN_FILENO, &oldt);
66
+
67
+ newt = oldt;
68
+
69
+ newt.c_lflag &= ~(ICANON | ECHO);
70
+
71
+ tcsetattr(STDIN_FILENO, TCSANOW, &newt);
72
+
73
+ oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
74
+
75
+ fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
76
+
77
+ ch = getchar();
78
+
79
+ tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
80
+
81
+ fcntl(STDIN_FILENO, F_SETFL, oldf);
82
+
83
+ if (ch != EOF) {
84
+
85
+ ungetc(ch, stdin);
86
+
87
+ return 1;
88
+
89
+ }
90
+
91
+ return 0;
92
+
93
+ }
94
+
37
95
  int main(void)
38
96
 
39
97
  {
40
98
 
41
- double a =1;
42
-
43
- double b =1;
44
-
45
- double c =1;
46
-
47
- double d =1;
48
-
49
- double e =1;
50
-
51
-
52
-
53
-
99
+ double ;
100
+
101
+ double y;
102
+
103
+ double freqv1;
104
+
105
+ double freqv2;
106
+
107
+ double freqe1;
108
+
109
+ double freqe2;
110
+
111
+ double ampv1;
112
+
113
+ double ampv2;
114
+
115
+ double ampe1;
116
+
117
+ double ampe2;
118
+
119
+ double phyv1, phyv2, phyv3;
120
+
121
+ double phyvp1;
122
+
123
+ double phyvp2;
124
+
125
+ double ave_xp, ave_yp;
126
+
127
+ double dx,dxa,dy,dya,dv;
128
+
129
+ int ret, dnum, i, j, n;
130
+
131
+ double ave_x, ave_y;
132
+
133
+ char *fname = "data.txt";
134
+
135
+ FILE *fp;
136
+
137
+ unsigned short xData[2];
138
+
139
+ unsigned short vData[2];
140
+
141
+ DASMPLCHREQ DaSmplChReq[2];
142
+
143
+ float x_history[filn], y_history[filn];
144
+
145
+ double dData[12], daval = 0x0800;
146
+
147
+ double wData_history0[filn], wData_history1[filn], wData_history2[filn], wData_history3[filn], wData_history4[filn], wData_history5[filn], wData_history6[filn], wData_history7[filn], wData_history8[filn], wData_history9[filn], wData_history10[filn], wData_history11[filn];
148
+
149
+ pthread_t pth;
150
+
151
+ int rtpth;
152
+
153
+ unsigned long long rt_ticks = GetTick();
54
154
 
55
155
  void *thread_kbhit(void *thdata)
56
156
 
57
157
  {
58
158
 
59
- char c;
159
+ char c;
60
-
61
-
62
-
160
+
63
- struct thdata *priv = (struct thdata *)thdata;
161
+ struct thdata *priv = (struct thdata *)thdata;
64
-
65
-
66
-
162
+
163
+
164
+
67
- while(1){
165
+ while(1){
68
-
166
+
69
- if( kbhit() ){
167
+ if( kbhit() ){
70
-
168
+
71
- c = getchar();
169
+ c = getchar();
170
+
171
+ }
172
+
173
+ gmode = c;
174
+
175
+ if( gmode == 'q' || g_end ){
176
+
177
+
178
+
179
+ DaClose( dnum );
180
+
181
+ AdClose(dnum);
182
+
183
+ fclose(fp);
184
+
185
+ return 0;
186
+
187
+ }
188
+
189
+ }
190
+
191
+ return (void *) NULL;
72
192
 
73
193
  }
74
194
 
75
- gmode = c;
76
-
77
- if( gmode == 'q' || g_end ){
78
-
79
-
80
-
81
- return 0;
195
+
196
+
197
+ system("clear");
198
+
199
+ dnum = 1;
200
+
201
+
202
+
203
+ ret = DaOpen(dnum);
204
+
205
+ if(ret){
206
+
207
+ printf("Open error: ret=%Xh\n", ret);
208
+
209
+ exit(EXIT_FAILURE);
210
+
211
+ }
212
+
213
+
214
+
215
+ ret = AdOpen(dnum);
216
+
217
+ if (ret) {
218
+
219
+ printf("AdOpen error: ret=%Xh\n", ret);
220
+
221
+ exit(EXIT_FAILURE);
222
+
223
+ }
224
+
225
+
226
+
227
+ ret = AdGetDeviceInfo(dnum, &BoardSpec);
228
+
229
+ if(ret){
230
+
231
+ printf("AdGetDeviceInfo error: ret=%Xh\n", ret);
232
+
233
+ AdClose(dnum);
234
+
235
+ exit(EXIT_FAILURE);
236
+
237
+ }
238
+
239
+
240
+
241
+ ret = DaGetDeviceInfo(dnum, &Spec);
242
+
243
+ if(ret){
244
+
245
+ printf("DaGetDeviceInfo error: ret=%Xh\n", ret);
246
+
247
+ DaClose(dnum);
248
+
249
+ exit(EXIT_FAILURE);
250
+
251
+ }
252
+
253
+
254
+
255
+ ret = DaGetSamplingConfig(dnum, &SmplConfig);
256
+
257
+ if (ret) {
258
+
259
+ printf("DaGetSamplingConfig error: ret=%Xh\n", ret);
260
+
261
+ DaClose(dnum);
262
+
263
+ exit(EXIT_FAILURE);
264
+
265
+ }
266
+
267
+
268
+
269
+ ret = AdGetSamplingConfig( dnum, &AdSmplConfig );
270
+
271
+ if (ret) {
272
+
273
+ printf("AdGetSamplingConfig error: ret=%Xh\n", ret);
274
+
275
+ exit(EXIT_FAILURE);
276
+
277
+ }
278
+
279
+
280
+
281
+ DaSmplChReq[0].ulChNo = 1;
282
+
283
+ DaSmplChReq[0].ulRange = SmplConfig.SmplChReq[0].ulRange;
284
+
285
+ DaSmplChReq[1].ulChNo = 2;
286
+
287
+ DaSmplChReq[1].ulRange = SmplConfig.SmplChReq[0].ulRange;
288
+
289
+
290
+
291
+ AdSmplChReq[0].ulChNo = 1;
292
+
293
+ AdSmplChReq[0].ulRange = AdSmplConfig.SmplChReq[0].ulRange;
294
+
295
+ AdSmplChReq[1].ulChNo = 2;
296
+
297
+ AdSmplChReq[1].ulRange = AdSmplConfig.SmplChReq[0].ulRange;
298
+
299
+ AdSmplChReq[2].ulChNo = 3;
300
+
301
+ AdSmplChReq[2].ulRange = AdSmplConfig.SmplChReq[0].ulRange;
302
+
303
+ AdSmplChReq[3].ulChNo = 4;
304
+
305
+ AdSmplChReq[3].ulRange = AdSmplConfig.SmplChReq[0].ulRange;
306
+
307
+ AdSmplChReq[4].ulChNo = 5;
308
+
309
+ AdSmplChReq[4].ulRange = AdSmplConfig.SmplChReq[0].ulRange;
310
+
311
+ AdSmplChReq[5].ulChNo = 6;
312
+
313
+ AdSmplChReq[5].ulRange = AdSmplConfig.SmplChReq[0].ulRange;
314
+
315
+ AdSmplChReq[6].ulChNo = 7;
316
+
317
+ AdSmplChReq[6].ulRange = AdSmplConfig.SmplChReq[0].ulRange;
318
+
319
+ AdSmplChReq[7].ulChNo = 8;
320
+
321
+ AdSmplChReq[7].ulRange = AdSmplConfig.SmplChReq[0].ulRange;
322
+
323
+ fp = fopen("data.txt", "w");
324
+
325
+ if( fp == NULL ){
326
+
327
+ printf("fopen./n");
328
+
329
+ return(-1);
330
+
331
+ }
332
+
333
+
334
+
335
+ double loadCellOffset[8] = { 0, 0, 0, 0, 0, 0, 0, 0};
336
+
337
+ for( i=0; i < 10; i++ ){
338
+
339
+ ret = AdInputAD(dnum, 8, AD_INPUT_SINGLE, AdSmplChReq, wData);
340
+
341
+ if (ret) {
342
+
343
+ AdClose(dnum);
344
+
345
+ printf("AdInputAD error: ret=%Xh\n", ret);
346
+
347
+ exit(EXIT_FAILURE);
348
+
349
+ }
350
+
351
+ loadCellOffset[0] += (double)wData[0]/10.0;
352
+
353
+ loadCellOffset[1] += (double)wData[1]/10.0;
354
+
355
+ loadCellOffset[2] += (double)wData[2]/10.0;
356
+
357
+ loadCellOffset[3] += (double)wData[3]/10.0;
358
+
359
+ loadCellOffset[4] += (double)wData[4]/10.0;
360
+
361
+ loadCellOffset[5] += (double)wData[5]/10.0;
362
+
363
+ loadCellOffset[6] += (double)wData[6]/10.0;
364
+
365
+ loadCellOffset[7] += (double)wData[7]/10.0;
366
+
367
+ }
368
+
369
+ memset( x_history, 0, sizeof(x_history) );
370
+
371
+ memset( y_history, 0, sizeof(y_history) );
372
+
373
+ memset( wData_history0, 0, sizeof(wData_history0) );
374
+
375
+ memset( wData_history1, 0, sizeof(wData_history1) );
376
+
377
+ memset( wData_history2, 0, sizeof(wData_history2) );
378
+
379
+ memset( wData_history3, 0, sizeof(wData_history3) );
380
+
381
+ memset( wData_history4, 0, sizeof(wData_history4) );
382
+
383
+ memset( wData_history5, 0, sizeof(wData_history5) );
384
+
385
+ memset( wData_history6, 0, sizeof(wData_history6) );
386
+
387
+ memset( wData_history7, 0, sizeof(wData_history7) );
388
+
389
+ memset( wData_history8, 0, sizeof(wData_history8) );
390
+
391
+ memset( wData_history9, 0, sizeof(wData_history9) );
392
+
393
+ memset( wData_history10, 0, sizeof(wData_history10) );
394
+
395
+ memset( wData_history11, 0, sizeof(wData_history11) );
396
+
397
+
398
+
399
+ rtpth = pthread_create( &pth, NULL, thread_kbhit, (void *)NULL );
400
+
401
+
402
+
403
+ for( i=0; i < (int)(3000.0/rt_cycle*1000.0); i++ ){
404
+
405
+
406
+
407
+ ret = AdInputAD(dnum, 8, AD_INPUT_SINGLE, AdSmplChReq, wData);
408
+
409
+ if (ret) {
410
+
411
+ AdClose(dnum);
412
+
413
+ printf("AdInputAD error: ret=%Xh\n", ret);
414
+
415
+ exit(EXIT_FAILURE);
416
+
417
+ }
418
+
419
+
420
+
421
+ dData[0] = ((double)wData[0] - loadCellOffset[0]);
422
+
423
+ dData[1] = ((double)wData[1] - loadCellOffset[1]);
424
+
425
+ dData[2] = ((double)wData[2] - loadCellOffset[2]);
426
+
427
+ dData[3] = ((double)wData[3] - loadCellOffset[3]);
428
+
429
+ dData[4] = ((double)wData[4] - loadCellOffset[4]);
430
+
431
+ dData[5] = ((double)wData[5] - loadCellOffset[5]);
432
+
433
+ dData[6] = ((double)wData[6] - loadCellOffset[6]);
434
+
435
+ dData[7] = ((double)wData[7] - loadCellOffset[7]);
436
+
437
+ dData[8] = dData[0]-dData[1];
438
+
439
+ dData[9] = dData[2]-dData[3];
440
+
441
+ dData[10] = dData[4]-dData[5];
442
+
443
+ dData[11] = dData[6]-dData[7];
444
+
445
+
446
+
447
+ memmove(&(wData_history0[1]), wData_history0, (filn-1) * sizeof(float) );
448
+
449
+ wData_history0[0] = dData[8];
450
+
451
+
452
+
453
+ memmove(&(wData_history1[1]), wData_history1, (filn-1) * sizeof(float) );
454
+
455
+ wData_history1[0] = dData[9];
456
+
457
+
458
+
459
+ memmove(&(wData_history2[1]), wData_history2, (filn-1) * sizeof(float) );
460
+
461
+ wData_history2[0] = dData[10];
462
+
463
+
464
+
465
+ memmove(&(wData_history3[1]), wData_history3, (filn-1) * sizeof(float) );
466
+
467
+ wData_history3[0] = dData[11];
468
+
469
+
470
+
471
+ dData[8] = 0;
472
+
473
+ dData[9] = 0;
474
+
475
+ dData[10] = 0;
476
+
477
+ dData[11] = 0;
478
+
479
+
480
+
481
+ for( j = 0; j < filn; j++ ){
482
+
483
+
484
+
485
+ dData[8] += wData_history0[j];
486
+
487
+ dData[9] += wData_history1[j];
488
+
489
+ dData[10] += wData_history2[j];
490
+
491
+ dData[11] += wData_history3[j];
492
+
493
+
494
+
495
+ }
496
+
497
+ dData[8] = dData[8]/(double)filn;
498
+
499
+ dData[9] = dData[9]/(double)filn;
500
+
501
+ dData[10] = dData[10]/(double)filn;
502
+
503
+ dData[11] = dData[11]/(double)filn;
504
+
505
+
506
+
507
+ }
508
+
509
+ memmove(&(x_history[1]), x_history, (filn-1) * sizeof(float) );
510
+
511
+ x_history[0] = newvalue_x;
512
+
513
+
514
+
515
+ memmove(&(y_history[1]), y_history, (filn-1) * sizeof(float) );
516
+
517
+ y_history[0] = newvalue_y;
518
+
519
+
520
+
521
+ for( j = 0, ave_x = 0; j < filn; j++ ){
522
+
523
+ ave_x += x_history[j];
524
+
525
+ ave_y += y_history[j];
526
+
527
+ }
528
+
529
+
530
+
531
+ ave_x = (ave_x / (double)filn) ;
532
+
533
+ ave_y = (ave_y / (double)filn) ;
534
+
535
+ dx = ave_x - ave_xp;
536
+
537
+ dy = ave_y - ave_yp;
538
+
539
+ dxa = fabs(dx);
540
+
541
+ dya = fabs(dy);
542
+
543
+ dv = sqrt(pow(dxa,2) + pow(dya,2));
544
+
545
+ phyv1 = (dv / freqv1) + phyvp1;
546
+
547
+ phyv2 = (dv / freqv2) + phyvp2;
548
+
549
+ vData[1] = (ampv1* sin(2 * M_PI * phyv1) +ampv2* sin(2 * M_PI * phyv2)) / 10.0 * 0x8000 + 0x8000;
550
+
551
+ vData[0] = sqrt(ampe1 * cos(2 * M_PI * phyv1) +ampe2 * cos(2 * M_PI * phyv2) + ampe1 + ampe2)/ 10.0 * 0x8000 + 0x8000;
552
+
553
+
554
+
555
+ ret = DaOutputDA( dnum, 2, DaSmplChReq, vData );
556
+
557
+ if (ret) {
558
+
559
+ printf("DaOutputDA error: ret=%Xh\n", ret);
560
+
561
+ DaClose(dnum);
562
+
563
+ exit(EXIT_FAILURE);
564
+
565
+ }
566
+
567
+ ave_xp = ave_x;
568
+
569
+ ave_yp = ave_y;
570
+
571
+ phyvp1 = phyv1;
572
+
573
+ phyvp2 = phyv2;
574
+
575
+
576
+
577
+ rt_wait( &rt_ticks );
578
+
579
+
580
+
581
+ switch (gmode) {
582
+
583
+ case '5':
584
+
585
+ freqv1 = 1.5;
586
+
587
+ freqv2 = 1;
588
+
589
+ ampv1=0.655;
590
+
591
+ ampv2=0;
592
+
593
+ ampe1=0;
594
+
595
+ ampe2=y*ampe1;
596
+
597
+ printf("%4.3f %4.3f %4.3f %4.3f \n", ampv1, ampv2, ampe1,ampe2);
598
+
599
+ break;
600
+
601
+ case '3':
602
+
603
+ freqv1 = 2.5;
604
+
605
+ freqv2 = 1;
606
+
607
+ ampv1 =1.275;
608
+
609
+ ampv2 =0;
610
+
611
+ ampe1=0;
612
+
613
+ ampe2=y*ampe1;
614
+
615
+ printf("%4.3f %4.3f %4.3f %4.3f \n", ampv1, ampv2, ampe1,ampe2);
616
+
617
+ break;
618
+
619
+ case '4':
620
+
621
+ freqv1 = 1.3;
622
+
623
+ freqv2 = 1.9;
624
+
625
+ x=5.33;
626
+
627
+ y=5;
628
+
629
+ ampv1=0.301;
630
+
631
+ ampv2=x*ampv1;
632
+
633
+ ampe1=0;
634
+
635
+ ampe2=0;
636
+
637
+ printf("%4.3f %4.3f %4.3f %4.3f \n", ampv1, ampv2, ampe1,ampe2);
638
+
639
+ break;
640
+
641
+ case '1':
642
+
643
+ freqv1 = 1.3;
644
+
645
+ freqv2 = 2.5;
646
+
647
+ x=3.33;
648
+
649
+ y=5.33;
650
+
651
+ ampv1=0.393;
652
+
653
+ ampv2=x*ampv1;
654
+
655
+ ampe1=0;
656
+
657
+ ampe2=y*ampe1;
658
+
659
+ printf("%4.3f %4.3f %4.3f %4.3f \n", ampv1, ampv2, ampe1,ampe2);
660
+
661
+ break;
662
+
663
+ case '2':
664
+
665
+ freqv1 = 1.9;
666
+
667
+ freqv2 = 2.5;
668
+
669
+ x=4;
670
+
671
+ y=2.67;
672
+
673
+ ampv1 =0.327;
674
+
675
+ ampv2 =x*ampv1;
676
+
677
+ ampe1=0;
678
+
679
+ ampe2=y*ampe1;
680
+
681
+ printf("%4.3f %4.3f %4.3f %4.3f \n", ampv1, ampv2, ampe1,ampe2);
682
+
683
+ break;
684
+
685
+ }
686
+
687
+ }
688
+
689
+ DaClose( dnum );
690
+
691
+ AdClose(dnum);
692
+
693
+ fclose(fp);
694
+
695
+ return 0;
82
696
 
83
697
  }
84
698
 
85
- }
86
-
87
-
88
-
89
- return (void *) NULL;
90
-
91
- }
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
- z=4*a+3*b+2*c*d;
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
- switch (gmode) {
110
-
111
-
112
-
113
- case '1':
114
-
115
-
116
-
117
- a = 1; /
118
-
119
- b = 2;
120
-
121
- c = 3;
122
-
123
- d = 4;
124
-
125
- printf("%4.3f", z);
126
-
127
- break;
128
-
129
-
130
-
131
-
132
-
133
- case '2':
134
-
135
-
136
-
137
- a = 4; /
138
-
139
- b = 2;
140
-
141
- c = 5;
142
-
143
- d = 7;
144
-
145
- printf("%4.3f", z);
146
-
147
- break
148
-
149
-
150
-
151
- case '3':
152
-
153
-
154
-
155
- a = 8; /
156
-
157
- b = 1;
158
-
159
- c = 9;
160
-
161
- d = 5;
162
-
163
- printf("%4.3f", z);
164
-
165
- break;
166
-
167
-
168
-
169
- case '4':
170
-
171
-
172
-
173
- a = 9; /
174
-
175
- b = 2;
176
-
177
- c = 3;
178
-
179
- d = 1;
180
-
181
- printf("%4.3f", z);
182
-
183
- break;
184
-
185
- }
186
-
187
-
188
-
189
- }
190
-
191
- // Close the device.
192
-
193
- DaClose( dnum );
194
-
195
- AdClose(dnum);
196
-
197
- fclose(fp);
198
-
199
- return 0;
200
-
201
- }
202
-
203
- コード
204
-
205
699
  ```

5

誤字修正

2020/04/07 03:03

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  を計算させたいです。
10
10
 
11
- 現在はswitch文を用いて4パターンそれぞれをキーボードの1,2,3,4のキーを押すとそれぞれの文字に数字が代入され、計算されるようなコードになっていますが、enterキーを一回押すと、それぞれのキーから出力される4パターンの値を1回ランダムにシャッフルされるようなコードにしたいです。
11
+ 現在はswitch文を用いて4パターンそれぞれをキーボードの1,2,3,4のキーを押すとそれぞれの文字に数字が代入され、計算されるようなコードになっていますが、enterキーを一回押すと、それぞれのキーから出力される4パターンの値を1回ランダムにシャッフルされるようなコードにしたいです。(a,b,c,d)のグループ内の値は常に固定で、④つのグループ同士でシャッフルを行いたいです。
12
12
 
13
13
 
14
14
 

4

誤字修正

2020/04/06 13:24

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
- まず、(a b c d e)=(1,3,3,6,5),(2,5,7,8,4),(5,2,8,4,5),(1,8,5,4,5)
1
+ まず、(a b c d )=(1,2,3,4),(4,2,5,7),(8,1,9,5),(9,2,3,1)
2
-
2
+
3
- のように4パターンの(a,b,c,d,e)を定めます。
3
+ のように4パターンの(a,b,c,d)を定めます。
4
4
 
5
5
  これらの値を用いて、
6
6
 

3

誤字修正

2020/04/06 13:19

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  を計算させたいです。
10
10
 
11
- 現在はswitch文を用いて4パターンそれぞれをキーボードの1,2,3,4を押すとそれぞれの文字に数字が代入され、計算されるようなコードになっていますが、enterを一回押すと、それぞれのセルから出力される値をランダムに1回入替えるようなコードにしたいです。
11
+ 現在はswitch文を用いて4パターンそれぞれをキーボードの1,2,3,4のキーを押すとそれぞれの文字に数字が代入され、計算されるようなコードになっていますが、enterキーを一回押すと、それぞれのキーから出力される4パターンの値を1回ランダムにシャッフルされるようなコードにしたいです。
12
12
 
13
13
 
14
14
 

2

コード修正

2020/04/06 12:58

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -20,11 +20,7 @@
20
20
 
21
21
  ソースコードは以下のようになっております。
22
22
 
23
-
24
-
25
-
26
-
27
- #include <stdio.h>
23
+ ```#include <stdio.h>
28
24
 
29
25
  #include <stdlib.h>
30
26
 
@@ -203,3 +199,7 @@
203
199
  return 0;
204
200
 
205
201
  }
202
+
203
+ コード
204
+
205
+ ```

1

ソースコード追記しました。

2020/04/06 12:22

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -17,3 +17,189 @@
17
17
 
18
18
 
19
19
  どのようにコードを書けば良いのでしょうか?
20
+
21
+ ソースコードは以下のようになっております。
22
+
23
+
24
+
25
+
26
+
27
+ #include <stdio.h>
28
+
29
+ #include <stdlib.h>
30
+
31
+ #include <time.h>
32
+
33
+ #include <string.h>
34
+
35
+ #include <math.h>
36
+
37
+ #include <fbida.h>
38
+
39
+ #include <fbiad.h>
40
+
41
+ int main(void)
42
+
43
+ {
44
+
45
+ double a =1;
46
+
47
+ double b =1;
48
+
49
+ double c =1;
50
+
51
+ double d =1;
52
+
53
+ double e =1;
54
+
55
+
56
+
57
+
58
+
59
+ void *thread_kbhit(void *thdata)
60
+
61
+ {
62
+
63
+ char c;
64
+
65
+
66
+
67
+ struct thdata *priv = (struct thdata *)thdata;
68
+
69
+
70
+
71
+ while(1){
72
+
73
+ if( kbhit() ){
74
+
75
+ c = getchar();
76
+
77
+ }
78
+
79
+ gmode = c;
80
+
81
+ if( gmode == 'q' || g_end ){
82
+
83
+
84
+
85
+ return 0;
86
+
87
+ }
88
+
89
+ }
90
+
91
+
92
+
93
+ return (void *) NULL;
94
+
95
+ }
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+ z=4*a+3*b+2*c*d;
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+ switch (gmode) {
114
+
115
+
116
+
117
+ case '1':
118
+
119
+
120
+
121
+ a = 1; /
122
+
123
+ b = 2;
124
+
125
+ c = 3;
126
+
127
+ d = 4;
128
+
129
+ printf("%4.3f", z);
130
+
131
+ break;
132
+
133
+
134
+
135
+
136
+
137
+ case '2':
138
+
139
+
140
+
141
+ a = 4; /
142
+
143
+ b = 2;
144
+
145
+ c = 5;
146
+
147
+ d = 7;
148
+
149
+ printf("%4.3f", z);
150
+
151
+ break
152
+
153
+
154
+
155
+ case '3':
156
+
157
+
158
+
159
+ a = 8; /
160
+
161
+ b = 1;
162
+
163
+ c = 9;
164
+
165
+ d = 5;
166
+
167
+ printf("%4.3f", z);
168
+
169
+ break;
170
+
171
+
172
+
173
+ case '4':
174
+
175
+
176
+
177
+ a = 9; /
178
+
179
+ b = 2;
180
+
181
+ c = 3;
182
+
183
+ d = 1;
184
+
185
+ printf("%4.3f", z);
186
+
187
+ break;
188
+
189
+ }
190
+
191
+
192
+
193
+ }
194
+
195
+ // Close the device.
196
+
197
+ DaClose( dnum );
198
+
199
+ AdClose(dnum);
200
+
201
+ fclose(fp);
202
+
203
+ return 0;
204
+
205
+ }