質問編集履歴
1
質問の変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
arduinoで車を走らせる
|
test
CHANGED
@@ -1,31 +1,247 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
|
3
|
+
|
4
|
-
|
4
|
+
|
5
|
-
|
5
|
+
arduino unoを用いて、車を走らせています。操作方法はスマホから音を出して、その周波数で直進や右折など制御しています。実際に走らせると動きがカクカクしてしまいます。滑らかに走らせるにはどうしたらよいでしょうか?原因はdelayの位置や値のような気がするのですが色々変えてみても改善しません。ハードの問題もあるかもしれませんが、プログラムで改善すべきところがあれば教えてください。
|
6
|
-
|
7
|
-
|
6
|
+
|
8
|
-
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
|
9
|
-
###
|
11
|
+
### 該当のソースコード
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
```Arduino
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
#define motorA1 6
|
20
|
+
|
21
|
+
#define motorA2 7
|
22
|
+
|
23
|
+
#define motorB1 11
|
24
|
+
|
25
|
+
#define motorB2 12
|
26
|
+
|
27
|
+
#define pwm1 9
|
28
|
+
|
29
|
+
#define pwm2 10
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
#include "arduinoFFT.h"
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
#define SAMPLES 128 //Must be a power of 2
|
38
|
+
|
39
|
+
#define SAMPLING_FREQUENCY 10000 //Hz, must be less than 10000 due to ADC
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
arduinoFFT FFT = arduinoFFT();
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
unsigned int sampling_period_us;
|
48
|
+
|
49
|
+
unsigned long microseconds;
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
double vReal[SAMPLES];
|
54
|
+
|
55
|
+
double vImag[SAMPLES];
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
void setup() {
|
60
|
+
|
61
|
+
Serial.begin(115200);
|
62
|
+
|
63
|
+
pinMode(motorA1,OUTPUT);
|
64
|
+
|
65
|
+
pinMode(motorA2,OUTPUT);
|
66
|
+
|
67
|
+
pinMode(motorB1,OUTPUT);
|
68
|
+
|
69
|
+
pinMode(motorB2,OUTPUT);
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
sampling_period_us = round(1000000*(1.0/SAMPLING_FREQUENCY));
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
void loop() {
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
/*SAMPLING*/
|
84
|
+
|
85
|
+
for(int i=0; i<SAMPLES; i++)
|
86
|
+
|
87
|
+
{
|
88
|
+
|
89
|
+
microseconds = micros(); //Overflows after around 70 minutes!
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
vReal[i] = analogRead(0);
|
94
|
+
|
95
|
+
vImag[i] = 0;
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
while(micros() < (microseconds + sampling_period_us)){
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
/*FFT*/
|
108
|
+
|
109
|
+
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
|
110
|
+
|
111
|
+
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
|
112
|
+
|
113
|
+
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
|
114
|
+
|
115
|
+
double frq = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
/*PRINT RESULTS*/
|
120
|
+
|
121
|
+
Serial.println(frq); //Print out what frequency is the most dominant.
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
for(int i=2; i<(SAMPLES/2); i++)
|
126
|
+
|
127
|
+
{
|
128
|
+
|
129
|
+
/*View all these three lines in serial terminal to see which frequencies has which amplitudes*/
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
//Serial.print((i * 1.0 * SAMPLING_FREQUENCY) / SAMPLES, 1);
|
134
|
+
|
135
|
+
//Serial.print(" ");
|
136
|
+
|
137
|
+
//Serial.println(vReal[i], 1); //View only this line in serial plotter to visualize the bins
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
//delay(100); //Repeat the process every second OR:
|
144
|
+
|
145
|
+
//while(1); //Run code once
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
if(frq < 2000){ //停止 2000以下
|
150
|
+
|
151
|
+
analogWrite(pwm1, 0);
|
152
|
+
|
153
|
+
digitalWrite(motorB1,LOW);
|
154
|
+
|
155
|
+
digitalWrite(motorB2,LOW);
|
156
|
+
|
157
|
+
analogWrite(pwm2, 0);
|
158
|
+
|
159
|
+
digitalWrite(motorA1,LOW);
|
160
|
+
|
161
|
+
digitalWrite(motorA2,LOW);
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
else if(2000<=frq && frq<2700){//直進 2000
|
168
|
+
|
169
|
+
digitalWrite(motorB2,LOW);
|
170
|
+
|
171
|
+
digitalWrite(motorB1,HIGH);
|
172
|
+
|
173
|
+
analogWrite(pwm1,255);
|
174
|
+
|
175
|
+
digitalWrite(motorA2,LOW);
|
176
|
+
|
177
|
+
digitalWrite(motorA1,HIGH);
|
178
|
+
|
179
|
+
analogWrite(pwm2, 255);
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
else if(2700 <= frq && frq < 3200){ //右折 2400
|
186
|
+
|
187
|
+
analogWrite(pwm1, 200);
|
188
|
+
|
189
|
+
digitalWrite(motorB1,HIGH);
|
190
|
+
|
191
|
+
digitalWrite(motorB2,LOW);
|
192
|
+
|
193
|
+
analogWrite(pwm2, 80);
|
194
|
+
|
195
|
+
digitalWrite(motorA1,HIGH);
|
196
|
+
|
197
|
+
digitalWrite(motorA2,LOW);
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
else if(3200 <= frq && frq < 3800){//左折 2800
|
204
|
+
|
205
|
+
analogWrite(pwm1, 80);
|
206
|
+
|
207
|
+
digitalWrite(motorB1,HIGH);
|
208
|
+
|
209
|
+
digitalWrite(motorB2,LOW);
|
210
|
+
|
211
|
+
analogWrite(pwm2, 200);
|
212
|
+
|
213
|
+
digitalWrite(motorA1,HIGH);
|
214
|
+
|
215
|
+
digitalWrite(motorA2,LOW);
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
else if(3800 <= frq ){//後退 3200
|
222
|
+
|
223
|
+
analogWrite(pwm1,100);
|
224
|
+
|
225
|
+
digitalWrite(motorB1,LOW);
|
226
|
+
|
227
|
+
digitalWrite(motorB2,HIGH);
|
228
|
+
|
229
|
+
analogWrite(pwm2, 100);
|
230
|
+
|
231
|
+
digitalWrite(motorA1,LOW);
|
232
|
+
|
233
|
+
digitalWrite(motorA2,HIGH);
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
delay(100);
|
240
|
+
|
241
|
+
}
|
10
242
|
|
11
243
|
```
|
12
244
|
|
13
|
-
|
245
|
+
|
14
|
-
|
15
|
-
|
246
|
+
|
16
|
-
|
17
|
-
###
|
247
|
+
### 補足情報(FW/ツールのバージョンなど)
|
18
|
-
|
19
|
-
```java
|
20
|
-
|
21
|
-
ソースコード
|
22
|
-
|
23
|
-
```
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
### 試したこと
|
28
|
-
|
29
|
-
すでに授業でお絵かきアプリは作成しており、参考サイトのクライアントのコードとお絵かきアプリを合体させてみたのですが、片方で描いた絵がもう一方に反映されなかったり、お絵かきアプリが、ボタンで色を変えたり線や円、四角を描けるようになっていて複雑になりよくわからなくなってしまいました。
|
30
|
-
|
31
|
-
ただ線を引けるようにするだけでいいのでどのようにコードを追加したらいいか手助けをお願いします。
|