質問編集履歴

2

再追記

2018/07/06 07:38

投稿

KRoNe_9
KRoNe_9

スコア11

test CHANGED
File without changes
test CHANGED
@@ -441,3 +441,393 @@
441
441
 
442
442
 
443
443
  ```
444
+
445
+
446
+
447
+ 再追記(7/6)
448
+
449
+ 綺麗にまとまってきたので再追記します。
450
+
451
+ ```C++
452
+
453
+ #include "app.h"
454
+
455
+ #include <vector>
456
+
457
+ #include <math.h>
458
+
459
+
460
+
461
+ rgb_raw_t rgb_val;
462
+
463
+ int red=0;
464
+
465
+ int green=0;
466
+
467
+ int blue=0;
468
+
469
+ int gray=0;
470
+
471
+
472
+
473
+ using namespace std;
474
+
475
+ FILE *fp;
476
+
477
+
478
+
479
+ void main_task(intptr_t unused)
480
+
481
+ {
482
+
483
+ ev3_sensor_config(EV3_PORT_2,COLOR_SENSOR);
484
+
485
+ ev3_motor_config(EV3_PORT_B, LARGE_MOTOR);
486
+
487
+ ev3_motor_config(EV3_PORT_C, LARGE_MOTOR);
488
+
489
+
490
+
491
+ ev3_sta_cyc(EV3_CYC_10MS);
492
+
493
+ ev3_sta_cyc(EV3_CYC_50MS);
494
+
495
+ ev3_sta_cyc(EV3_CYC_100MS);
496
+
497
+
498
+
499
+
500
+
501
+ while (1) {
502
+
503
+ tslp_tsk(100);
504
+
505
+ }
506
+
507
+
508
+
509
+ ev3_stp_cyc(EV3_CYC_100MS);
510
+
511
+ ev3_stp_cyc(EV3_CYC_50MS);
512
+
513
+ ev3_stp_cyc(EV3_CYC_10MS);
514
+
515
+ ext_tsk();
516
+
517
+ }
518
+
519
+
520
+
521
+ void cyc_task_10ms(intptr_t exinf)
522
+
523
+ {
524
+
525
+ static int key;
526
+
527
+
528
+
529
+ static vector<int> go;
530
+
531
+ static vector<int> back;
532
+
533
+
534
+
535
+ switch(key)
536
+
537
+ {
538
+
539
+
540
+
541
+ // 直進(スキャン)
542
+
543
+ case 0:
544
+
545
+ for(;;){
546
+
547
+ ev3_color_sensor_get_rgb_raw(EV3_PORT_2,&rgb_val);
548
+
549
+ red=rgb_val.r;
550
+
551
+ green=rgb_val.g;
552
+
553
+ blue=rgb_val.b;
554
+
555
+
556
+
557
+ gray=(red*0.3)+(green*0.59)+(blue*0.11);
558
+
559
+
560
+
561
+ ev3_motor_set_power(EV3_PORT_B,20);
562
+
563
+ ev3_motor_set_power(EV3_PORT_C,20);
564
+
565
+ if(gray<30 && ev3_motor_get_counts(EV3_PORT_C) < 320){
566
+
567
+ go.push_back(1);
568
+
569
+ }else if(gray>=30 && ev3_motor_get_counts(EV3_PORT_C) < 320){
570
+
571
+ go.push_back(0);
572
+
573
+ }else{
574
+
575
+ ev3_motor_reset_counts(EV3_PORT_C);
576
+
577
+ key = 1;
578
+
579
+ break;
580
+
581
+ }
582
+
583
+ }
584
+
585
+ break;
586
+
587
+
588
+
589
+
590
+
591
+ //回転180
592
+
593
+ case 1:
594
+
595
+ if (ev3_motor_get_counts(EV3_PORT_C) > 530)
596
+
597
+ {
598
+
599
+ ev3_motor_stop(EV3_PORT_B, true);
600
+
601
+ ev3_motor_stop(EV3_PORT_C, true);
602
+
603
+ ev3_motor_reset_counts(EV3_PORT_B);
604
+
605
+ ev3_motor_reset_counts(EV3_PORT_C);
606
+
607
+ key = 2;
608
+
609
+ }else{
610
+
611
+ ev3_motor_set_power(EV3_PORT_C,15);
612
+
613
+ ev3_motor_stop(EV3_PORT_B,true);
614
+
615
+
616
+
617
+ double z ,l ;
618
+
619
+ int a,x;
620
+
621
+ z = go.size()/150;
622
+
623
+ fp = fopen("colorscan.txt","a");
624
+
625
+
626
+
627
+ for(static int k = 0 ; k < 150; k++){
628
+
629
+ l = k * z;
630
+
631
+ x = (int)floor(l);
632
+
633
+ a = go[x];
634
+
635
+ fprintf(fp,"%d",a);
636
+
637
+ }
638
+
639
+ fclose(fp);
640
+
641
+ }
642
+
643
+ break;
644
+
645
+
646
+
647
+ // バック
648
+
649
+ case 2:
650
+
651
+ ev3_motor_set_power(EV3_PORT_B,-20);
652
+
653
+ ev3_motor_set_power(EV3_PORT_C,-20);
654
+
655
+ if (ev3_motor_get_counts(EV3_PORT_B) < -100) {
656
+
657
+ ev3_motor_reset_counts(EV3_PORT_B);
658
+
659
+ key = 3;
660
+
661
+ }
662
+
663
+ break;
664
+
665
+
666
+
667
+ //直進(スキャン)
668
+
669
+ case 3:
670
+
671
+
672
+
673
+ for(;;){
674
+
675
+ ev3_color_sensor_get_rgb_raw(EV3_PORT_2,&rgb_val);
676
+
677
+ red=rgb_val.r;
678
+
679
+ green=rgb_val.g;
680
+
681
+ blue=rgb_val.b;
682
+
683
+
684
+
685
+ gray=(red*0.3)+(green*0.59)+(blue*0.11);
686
+
687
+
688
+
689
+ ev3_motor_set_power(EV3_PORT_B,20);
690
+
691
+ ev3_motor_set_power(EV3_PORT_C,20);
692
+
693
+ if(gray<30 && ev3_motor_get_counts(EV3_PORT_C) < 240){
694
+
695
+ back.push_back(1);
696
+
697
+ }else if(gray>=30 && ev3_motor_get_counts(EV3_PORT_C) < 240){
698
+
699
+ back.push_back(0);
700
+
701
+ }else{
702
+
703
+ ev3_motor_reset_counts(EV3_PORT_C);
704
+
705
+ key = 4;
706
+
707
+ break;
708
+
709
+ }
710
+
711
+ }
712
+
713
+ break;
714
+
715
+
716
+
717
+ // 停止
718
+
719
+ case 4:
720
+
721
+ ev3_motor_stop(EV3_PORT_B, true);
722
+
723
+ ev3_motor_stop(EV3_PORT_C, true);
724
+
725
+
726
+
727
+ double w ,n ;
728
+
729
+ int b,y;
730
+
731
+
732
+
733
+ w = back.size()/150;
734
+
735
+ fp = fopen("colorscan.txt","a");
736
+
737
+ for(static int j = 0 ; j < 150; j++){
738
+
739
+
740
+
741
+ n = j * w;
742
+
743
+ y = (int)floor(n);
744
+
745
+ b = back[y];
746
+
747
+ fprintf(fp,"%d",b);
748
+
749
+ }
750
+
751
+ fclose(fp);
752
+
753
+
754
+
755
+ fp = fopen("colorscan.txt","a");
756
+
757
+ for(static int i=0; i<1; i++){
758
+
759
+ fprintf(fp,"\n");
760
+
761
+ }
762
+
763
+ fclose(fp);
764
+
765
+
766
+
767
+ break;
768
+
769
+ ext_tsk();
770
+
771
+ }
772
+
773
+ }
774
+
775
+
776
+
777
+
778
+
779
+
780
+
781
+ void cyc_task_50ms(intptr_t exinf)
782
+
783
+ {
784
+
785
+ // 通信
786
+
787
+ ext_tsk();
788
+
789
+ }
790
+
791
+
792
+
793
+ void cyc_task_100ms(intptr_t exinf)
794
+
795
+ {
796
+
797
+ // 表示
798
+
799
+ ext_tsk();
800
+
801
+ }
802
+
803
+
804
+
805
+ void ev3_cyc_10ms(intptr_t exinf)
806
+
807
+ {
808
+
809
+ act_tsk(CYC_TASK_10MS);
810
+
811
+ }
812
+
813
+
814
+
815
+ void ev3_cyc_50ms(intptr_t exinf)
816
+
817
+ {
818
+
819
+ act_tsk(CYC_TASK_50MS);
820
+
821
+ }
822
+
823
+
824
+
825
+ void ev3_cyc_100ms(intptr_t exinf)
826
+
827
+ {
828
+
829
+ act_tsk(CYC_TASK_100MS);
830
+
831
+ }
832
+
833
+ ```

1

追記

2018/07/06 07:38

投稿

KRoNe_9
KRoNe_9

スコア11

test CHANGED
File without changes
test CHANGED
@@ -21,3 +21,423 @@
21
21
  ということです。
22
22
 
23
23
  よろしくお願いします。
24
+
25
+
26
+
27
+ 追記====================
28
+
29
+ コメントくださったみなさんありがとうございます。
30
+
31
+ よく考えてみれば等差数列で切り下げれば問題ありませんでした。
32
+
33
+ 質問の時から少し状況、段階が進んだので追記します。現在EV3のカラーセンサを用いて読み取ったデータをテキストファイルに書き込もうとしています。
34
+
35
+ 読み取ったデータをベクター配列に格納して、配列から読み取ったものをテキストファイルに書き込もうとしているのですが思ったようにうまくいきませんcase1から抜け出せない状態です。
36
+
37
+ とても汚いプログラムですがどこがおかしいや、ここはこうすべきなどを教えていただけると幸いです。
38
+
39
+ ```c++
40
+
41
+ #include "app.h"
42
+
43
+ #include <vector>
44
+
45
+ #include <math.h>
46
+
47
+
48
+
49
+ rgb_raw_t rgb_val;
50
+
51
+ int red=0;
52
+
53
+ int green=0;
54
+
55
+ int blue=0;
56
+
57
+ int gray=0;
58
+
59
+
60
+
61
+
62
+
63
+ using namespace std;
64
+
65
+ FILE *fp;
66
+
67
+
68
+
69
+ vector<int> go;
70
+
71
+ vector<int> back;
72
+
73
+
74
+
75
+ void main_task(intptr_t unused)
76
+
77
+ {
78
+
79
+ ev3_sensor_config(EV3_PORT_2,COLOR_SENSOR);
80
+
81
+ ev3_motor_config(EV3_PORT_B, LARGE_MOTOR);
82
+
83
+ ev3_motor_config(EV3_PORT_C, LARGE_MOTOR);
84
+
85
+
86
+
87
+ ev3_sta_cyc(EV3_CYC_10MS);
88
+
89
+ ev3_sta_cyc(EV3_CYC_50MS);
90
+
91
+ ev3_sta_cyc(EV3_CYC_100MS);
92
+
93
+
94
+
95
+
96
+
97
+ while (1) {
98
+
99
+ tslp_tsk(100);
100
+
101
+ }
102
+
103
+
104
+
105
+ ev3_stp_cyc(EV3_CYC_100MS);
106
+
107
+ ev3_stp_cyc(EV3_CYC_50MS);
108
+
109
+ ev3_stp_cyc(EV3_CYC_10MS);
110
+
111
+ ext_tsk();
112
+
113
+ }
114
+
115
+
116
+
117
+ void cyc_task_10ms(intptr_t exinf)
118
+
119
+ {
120
+
121
+ static int key;
122
+
123
+
124
+
125
+
126
+
127
+ switch(key)
128
+
129
+ {
130
+
131
+
132
+
133
+ // 直進(スキャン)
134
+
135
+ case 0:
136
+
137
+
138
+
139
+ if (ev3_motor_get_counts(EV3_PORT_C) > 300) {
140
+
141
+ ev3_motor_stop(EV3_PORT_B, true);
142
+
143
+ ev3_motor_stop(EV3_PORT_C, true);
144
+
145
+ ev3_motor_reset_counts(EV3_PORT_C);
146
+
147
+ key = 1;
148
+
149
+ }else{
150
+
151
+ ev3_color_sensor_get_rgb_raw(EV3_PORT_2,&rgb_val);
152
+
153
+ red=rgb_val.r;
154
+
155
+ green=rgb_val.g;
156
+
157
+ blue=rgb_val.b;
158
+
159
+ gray=(red*0.3)+(green*0.59)+(blue*0.11);
160
+
161
+
162
+
163
+ ev3_motor_set_power(EV3_PORT_B,20);
164
+
165
+ ev3_motor_set_power(EV3_PORT_C,20);
166
+
167
+
168
+
169
+ if(gray<50){
170
+
171
+ go.push_back(1);
172
+
173
+ }else{
174
+
175
+ go.push_back(0);
176
+
177
+ }
178
+
179
+ }
180
+
181
+
182
+
183
+ break;
184
+
185
+
186
+
187
+
188
+
189
+ //回転180
190
+
191
+ case 1:
192
+
193
+
194
+
195
+ if (ev3_motor_get_counts(EV3_PORT_C) > 520)
196
+
197
+ {
198
+
199
+ ev3_motor_stop(EV3_PORT_B, true);
200
+
201
+ ev3_motor_stop(EV3_PORT_C, true);
202
+
203
+ ev3_motor_reset_counts(EV3_PORT_B);
204
+
205
+ ev3_motor_reset_counts(EV3_PORT_C);
206
+
207
+ key = 2;
208
+
209
+ }else{
210
+
211
+ ev3_motor_set_power(EV3_PORT_C,15);
212
+
213
+ ev3_motor_stop(EV3_PORT_B,true);
214
+
215
+
216
+
217
+ double x ,z ,l ;
218
+
219
+ int a;
220
+
221
+ z = go.size()/150;
222
+
223
+
224
+
225
+ for(static int k = 0 ; k < 150; k++){
226
+
227
+ fp = fopen("colorscan.txt","a");
228
+
229
+ l = k * z;
230
+
231
+ x = floor(l);
232
+
233
+ a = go[x];
234
+
235
+ fprintf(fp,"%d",a);
236
+
237
+ fclose(fp);
238
+
239
+ }
240
+
241
+ }
242
+
243
+ break;
244
+
245
+
246
+
247
+ // バック
248
+
249
+ case 2:
250
+
251
+ ev3_motor_set_power(EV3_PORT_B,-20);
252
+
253
+ ev3_motor_set_power(EV3_PORT_C,-20);
254
+
255
+ if (ev3_motor_get_counts(EV3_PORT_B) < -100) {
256
+
257
+ ev3_motor_reset_counts(EV3_PORT_B);
258
+
259
+ key = 3;
260
+
261
+ }
262
+
263
+ break;
264
+
265
+
266
+
267
+ //直進(スキャン)
268
+
269
+ case 3:
270
+
271
+
272
+
273
+ if (ev3_motor_get_counts(EV3_PORT_C) > 300) {
274
+
275
+ ev3_motor_reset_counts(EV3_PORT_C);
276
+
277
+ key = 4;
278
+
279
+ }else{
280
+
281
+ ev3_motor_set_power(EV3_PORT_B,20);
282
+
283
+ ev3_motor_set_power(EV3_PORT_C,20);
284
+
285
+
286
+
287
+ ev3_color_sensor_get_rgb_raw(EV3_PORT_2,&rgb_val);
288
+
289
+ red=rgb_val.r;
290
+
291
+ green=rgb_val.g;
292
+
293
+ blue=rgb_val.b;
294
+
295
+
296
+
297
+ gray=(red*0.3)+(green*0.59)+(blue*0.11);
298
+
299
+
300
+
301
+ if(gray<50){
302
+
303
+ back.push_back(1);
304
+
305
+ }else{
306
+
307
+ back.push_back(0);
308
+
309
+ }
310
+
311
+ }
312
+
313
+ break;
314
+
315
+
316
+
317
+ // 停止
318
+
319
+ case 4:
320
+
321
+
322
+
323
+ ev3_motor_stop(EV3_PORT_B, true);
324
+
325
+ ev3_motor_stop(EV3_PORT_C, true);
326
+
327
+
328
+
329
+ double y , w ,n ;
330
+
331
+ int b;
332
+
333
+
334
+
335
+ w = back.size()/150;
336
+
337
+
338
+
339
+ for(static int j = 0 ; j < 150; j++){
340
+
341
+ fp = fopen("colorscan.txt","a");
342
+
343
+ n = j * w;
344
+
345
+ y = floor(n);
346
+
347
+ b = back[y];
348
+
349
+ fprintf(fp,"%d",b);
350
+
351
+ fclose(fp);
352
+
353
+ }
354
+
355
+
356
+
357
+ for(static int i=0; i<1; i++){
358
+
359
+ fp = fopen("colorscan.txt","a");
360
+
361
+ fprintf(fp,"%d",88);
362
+
363
+ fclose(fp);
364
+
365
+ }
366
+
367
+
368
+
369
+ break;
370
+
371
+ ext_tsk();
372
+
373
+
374
+
375
+ }
376
+
377
+ }
378
+
379
+
380
+
381
+
382
+
383
+
384
+
385
+ void cyc_task_50ms(intptr_t exinf)
386
+
387
+ {
388
+
389
+ // 通信
390
+
391
+ ext_tsk();
392
+
393
+ }
394
+
395
+
396
+
397
+ void cyc_task_100ms(intptr_t exinf)
398
+
399
+ {
400
+
401
+ // 表示
402
+
403
+ ext_tsk();
404
+
405
+ }
406
+
407
+
408
+
409
+ void ev3_cyc_10ms(intptr_t exinf)
410
+
411
+ {
412
+
413
+ act_tsk(CYC_TASK_10MS);
414
+
415
+ }
416
+
417
+
418
+
419
+ void ev3_cyc_50ms(intptr_t exinf)
420
+
421
+ {
422
+
423
+ act_tsk(CYC_TASK_50MS);
424
+
425
+ }
426
+
427
+
428
+
429
+ void ev3_cyc_100ms(intptr_t exinf)
430
+
431
+ {
432
+
433
+ act_tsk(CYC_TASK_100MS);
434
+
435
+ }
436
+
437
+
438
+
439
+
440
+
441
+
442
+
443
+ ```