teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

再追記

2018/07/06 07:38

投稿

KRoNe_9
KRoNe_9

スコア11

title CHANGED
File without changes
body CHANGED
@@ -219,4 +219,199 @@
219
219
 
220
220
 
221
221
 
222
+ ```
223
+
224
+ 再追記(7/6)
225
+ 綺麗にまとまってきたので再追記します。
226
+ ```C++
227
+ #include "app.h"
228
+ #include <vector>
229
+ #include <math.h>
230
+
231
+ rgb_raw_t rgb_val;
232
+ int red=0;
233
+ int green=0;
234
+ int blue=0;
235
+ int gray=0;
236
+
237
+ using namespace std;
238
+ FILE *fp;
239
+
240
+ void main_task(intptr_t unused)
241
+ {
242
+ ev3_sensor_config(EV3_PORT_2,COLOR_SENSOR);
243
+ ev3_motor_config(EV3_PORT_B, LARGE_MOTOR);
244
+ ev3_motor_config(EV3_PORT_C, LARGE_MOTOR);
245
+
246
+ ev3_sta_cyc(EV3_CYC_10MS);
247
+ ev3_sta_cyc(EV3_CYC_50MS);
248
+ ev3_sta_cyc(EV3_CYC_100MS);
249
+
250
+
251
+ while (1) {
252
+ tslp_tsk(100);
253
+ }
254
+
255
+ ev3_stp_cyc(EV3_CYC_100MS);
256
+ ev3_stp_cyc(EV3_CYC_50MS);
257
+ ev3_stp_cyc(EV3_CYC_10MS);
258
+ ext_tsk();
259
+ }
260
+
261
+ void cyc_task_10ms(intptr_t exinf)
262
+ {
263
+ static int key;
264
+
265
+ static vector<int> go;
266
+ static vector<int> back;
267
+
268
+ switch(key)
269
+ {
270
+
271
+ // 直進(スキャン)
272
+ case 0:
273
+ for(;;){
274
+ ev3_color_sensor_get_rgb_raw(EV3_PORT_2,&rgb_val);
275
+ red=rgb_val.r;
276
+ green=rgb_val.g;
277
+ blue=rgb_val.b;
278
+
279
+ gray=(red*0.3)+(green*0.59)+(blue*0.11);
280
+
281
+ ev3_motor_set_power(EV3_PORT_B,20);
282
+ ev3_motor_set_power(EV3_PORT_C,20);
283
+ if(gray<30 && ev3_motor_get_counts(EV3_PORT_C) < 320){
284
+ go.push_back(1);
285
+ }else if(gray>=30 && ev3_motor_get_counts(EV3_PORT_C) < 320){
286
+ go.push_back(0);
287
+ }else{
288
+ ev3_motor_reset_counts(EV3_PORT_C);
289
+ key = 1;
290
+ break;
291
+ }
292
+ }
293
+ break;
294
+
295
+
296
+ //回転180
297
+ case 1:
298
+ if (ev3_motor_get_counts(EV3_PORT_C) > 530)
299
+ {
300
+ ev3_motor_stop(EV3_PORT_B, true);
301
+ ev3_motor_stop(EV3_PORT_C, true);
302
+ ev3_motor_reset_counts(EV3_PORT_B);
303
+ ev3_motor_reset_counts(EV3_PORT_C);
304
+ key = 2;
305
+ }else{
306
+ ev3_motor_set_power(EV3_PORT_C,15);
307
+ ev3_motor_stop(EV3_PORT_B,true);
308
+
309
+ double z ,l ;
310
+ int a,x;
311
+ z = go.size()/150;
312
+ fp = fopen("colorscan.txt","a");
313
+
314
+ for(static int k = 0 ; k < 150; k++){
315
+ l = k * z;
316
+ x = (int)floor(l);
317
+ a = go[x];
318
+ fprintf(fp,"%d",a);
319
+ }
320
+ fclose(fp);
321
+ }
322
+ break;
323
+
324
+ // バック
325
+ case 2:
326
+ ev3_motor_set_power(EV3_PORT_B,-20);
327
+ ev3_motor_set_power(EV3_PORT_C,-20);
328
+ if (ev3_motor_get_counts(EV3_PORT_B) < -100) {
329
+ ev3_motor_reset_counts(EV3_PORT_B);
330
+ key = 3;
331
+ }
332
+ break;
333
+
334
+ //直進(スキャン)
335
+ case 3:
336
+
337
+ for(;;){
338
+ ev3_color_sensor_get_rgb_raw(EV3_PORT_2,&rgb_val);
339
+ red=rgb_val.r;
340
+ green=rgb_val.g;
341
+ blue=rgb_val.b;
342
+
343
+ gray=(red*0.3)+(green*0.59)+(blue*0.11);
344
+
345
+ ev3_motor_set_power(EV3_PORT_B,20);
346
+ ev3_motor_set_power(EV3_PORT_C,20);
347
+ if(gray<30 && ev3_motor_get_counts(EV3_PORT_C) < 240){
348
+ back.push_back(1);
349
+ }else if(gray>=30 && ev3_motor_get_counts(EV3_PORT_C) < 240){
350
+ back.push_back(0);
351
+ }else{
352
+ ev3_motor_reset_counts(EV3_PORT_C);
353
+ key = 4;
354
+ break;
355
+ }
356
+ }
357
+ break;
358
+
359
+ // 停止
360
+ case 4:
361
+ ev3_motor_stop(EV3_PORT_B, true);
362
+ ev3_motor_stop(EV3_PORT_C, true);
363
+
364
+ double w ,n ;
365
+ int b,y;
366
+
367
+ w = back.size()/150;
368
+ fp = fopen("colorscan.txt","a");
369
+ for(static int j = 0 ; j < 150; j++){
370
+
371
+ n = j * w;
372
+ y = (int)floor(n);
373
+ b = back[y];
374
+ fprintf(fp,"%d",b);
375
+ }
376
+ fclose(fp);
377
+
378
+ fp = fopen("colorscan.txt","a");
379
+ for(static int i=0; i<1; i++){
380
+ fprintf(fp,"\n");
381
+ }
382
+ fclose(fp);
383
+
384
+ break;
385
+ ext_tsk();
386
+ }
387
+ }
388
+
389
+
390
+
391
+ void cyc_task_50ms(intptr_t exinf)
392
+ {
393
+ // 通信
394
+ ext_tsk();
395
+ }
396
+
397
+ void cyc_task_100ms(intptr_t exinf)
398
+ {
399
+ // 表示
400
+ ext_tsk();
401
+ }
402
+
403
+ void ev3_cyc_10ms(intptr_t exinf)
404
+ {
405
+ act_tsk(CYC_TASK_10MS);
406
+ }
407
+
408
+ void ev3_cyc_50ms(intptr_t exinf)
409
+ {
410
+ act_tsk(CYC_TASK_50MS);
411
+ }
412
+
413
+ void ev3_cyc_100ms(intptr_t exinf)
414
+ {
415
+ act_tsk(CYC_TASK_100MS);
416
+ }
222
417
  ```

1

追記

2018/07/06 07:38

投稿

KRoNe_9
KRoNe_9

スコア11

title CHANGED
File without changes
body CHANGED
@@ -9,4 +9,214 @@
9
9
  ここからほぼ等間隔に150文字抽出し別のテキストファイルに書き込みたい
10
10
 
11
11
  ということです。
12
- よろしくお願いします。
12
+ よろしくお願いします。
13
+
14
+ 追記====================
15
+ コメントくださったみなさんありがとうございます。
16
+ よく考えてみれば等差数列で切り下げれば問題ありませんでした。
17
+ 質問の時から少し状況、段階が進んだので追記します。現在EV3のカラーセンサを用いて読み取ったデータをテキストファイルに書き込もうとしています。
18
+ 読み取ったデータをベクター配列に格納して、配列から読み取ったものをテキストファイルに書き込もうとしているのですが思ったようにうまくいきませんcase1から抜け出せない状態です。
19
+ とても汚いプログラムですがどこがおかしいや、ここはこうすべきなどを教えていただけると幸いです。
20
+ ```c++
21
+ #include "app.h"
22
+ #include <vector>
23
+ #include <math.h>
24
+
25
+ rgb_raw_t rgb_val;
26
+ int red=0;
27
+ int green=0;
28
+ int blue=0;
29
+ int gray=0;
30
+
31
+
32
+ using namespace std;
33
+ FILE *fp;
34
+
35
+ vector<int> go;
36
+ vector<int> back;
37
+
38
+ void main_task(intptr_t unused)
39
+ {
40
+ ev3_sensor_config(EV3_PORT_2,COLOR_SENSOR);
41
+ ev3_motor_config(EV3_PORT_B, LARGE_MOTOR);
42
+ ev3_motor_config(EV3_PORT_C, LARGE_MOTOR);
43
+
44
+ ev3_sta_cyc(EV3_CYC_10MS);
45
+ ev3_sta_cyc(EV3_CYC_50MS);
46
+ ev3_sta_cyc(EV3_CYC_100MS);
47
+
48
+
49
+ while (1) {
50
+ tslp_tsk(100);
51
+ }
52
+
53
+ ev3_stp_cyc(EV3_CYC_100MS);
54
+ ev3_stp_cyc(EV3_CYC_50MS);
55
+ ev3_stp_cyc(EV3_CYC_10MS);
56
+ ext_tsk();
57
+ }
58
+
59
+ void cyc_task_10ms(intptr_t exinf)
60
+ {
61
+ static int key;
62
+
63
+
64
+ switch(key)
65
+ {
66
+
67
+ // 直進(スキャン)
68
+ case 0:
69
+
70
+ if (ev3_motor_get_counts(EV3_PORT_C) > 300) {
71
+ ev3_motor_stop(EV3_PORT_B, true);
72
+ ev3_motor_stop(EV3_PORT_C, true);
73
+ ev3_motor_reset_counts(EV3_PORT_C);
74
+ key = 1;
75
+ }else{
76
+ ev3_color_sensor_get_rgb_raw(EV3_PORT_2,&rgb_val);
77
+ red=rgb_val.r;
78
+ green=rgb_val.g;
79
+ blue=rgb_val.b;
80
+ gray=(red*0.3)+(green*0.59)+(blue*0.11);
81
+
82
+ ev3_motor_set_power(EV3_PORT_B,20);
83
+ ev3_motor_set_power(EV3_PORT_C,20);
84
+
85
+ if(gray<50){
86
+ go.push_back(1);
87
+ }else{
88
+ go.push_back(0);
89
+ }
90
+ }
91
+
92
+ break;
93
+
94
+
95
+ //回転180
96
+ case 1:
97
+
98
+ if (ev3_motor_get_counts(EV3_PORT_C) > 520)
99
+ {
100
+ ev3_motor_stop(EV3_PORT_B, true);
101
+ ev3_motor_stop(EV3_PORT_C, true);
102
+ ev3_motor_reset_counts(EV3_PORT_B);
103
+ ev3_motor_reset_counts(EV3_PORT_C);
104
+ key = 2;
105
+ }else{
106
+ ev3_motor_set_power(EV3_PORT_C,15);
107
+ ev3_motor_stop(EV3_PORT_B,true);
108
+
109
+ double x ,z ,l ;
110
+ int a;
111
+ z = go.size()/150;
112
+
113
+ for(static int k = 0 ; k < 150; k++){
114
+ fp = fopen("colorscan.txt","a");
115
+ l = k * z;
116
+ x = floor(l);
117
+ a = go[x];
118
+ fprintf(fp,"%d",a);
119
+ fclose(fp);
120
+ }
121
+ }
122
+ break;
123
+
124
+ // バック
125
+ case 2:
126
+ ev3_motor_set_power(EV3_PORT_B,-20);
127
+ ev3_motor_set_power(EV3_PORT_C,-20);
128
+ if (ev3_motor_get_counts(EV3_PORT_B) < -100) {
129
+ ev3_motor_reset_counts(EV3_PORT_B);
130
+ key = 3;
131
+ }
132
+ break;
133
+
134
+ //直進(スキャン)
135
+ case 3:
136
+
137
+ if (ev3_motor_get_counts(EV3_PORT_C) > 300) {
138
+ ev3_motor_reset_counts(EV3_PORT_C);
139
+ key = 4;
140
+ }else{
141
+ ev3_motor_set_power(EV3_PORT_B,20);
142
+ ev3_motor_set_power(EV3_PORT_C,20);
143
+
144
+ ev3_color_sensor_get_rgb_raw(EV3_PORT_2,&rgb_val);
145
+ red=rgb_val.r;
146
+ green=rgb_val.g;
147
+ blue=rgb_val.b;
148
+
149
+ gray=(red*0.3)+(green*0.59)+(blue*0.11);
150
+
151
+ if(gray<50){
152
+ back.push_back(1);
153
+ }else{
154
+ back.push_back(0);
155
+ }
156
+ }
157
+ break;
158
+
159
+ // 停止
160
+ case 4:
161
+
162
+ ev3_motor_stop(EV3_PORT_B, true);
163
+ ev3_motor_stop(EV3_PORT_C, true);
164
+
165
+ double y , w ,n ;
166
+ int b;
167
+
168
+ w = back.size()/150;
169
+
170
+ for(static int j = 0 ; j < 150; j++){
171
+ fp = fopen("colorscan.txt","a");
172
+ n = j * w;
173
+ y = floor(n);
174
+ b = back[y];
175
+ fprintf(fp,"%d",b);
176
+ fclose(fp);
177
+ }
178
+
179
+ for(static int i=0; i<1; i++){
180
+ fp = fopen("colorscan.txt","a");
181
+ fprintf(fp,"%d",88);
182
+ fclose(fp);
183
+ }
184
+
185
+ break;
186
+ ext_tsk();
187
+
188
+ }
189
+ }
190
+
191
+
192
+
193
+ void cyc_task_50ms(intptr_t exinf)
194
+ {
195
+ // 通信
196
+ ext_tsk();
197
+ }
198
+
199
+ void cyc_task_100ms(intptr_t exinf)
200
+ {
201
+ // 表示
202
+ ext_tsk();
203
+ }
204
+
205
+ void ev3_cyc_10ms(intptr_t exinf)
206
+ {
207
+ act_tsk(CYC_TASK_10MS);
208
+ }
209
+
210
+ void ev3_cyc_50ms(intptr_t exinf)
211
+ {
212
+ act_tsk(CYC_TASK_50MS);
213
+ }
214
+
215
+ void ev3_cyc_100ms(intptr_t exinf)
216
+ {
217
+ act_tsk(CYC_TASK_100MS);
218
+ }
219
+
220
+
221
+
222
+ ```