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

質問編集履歴

1

質問の変更

2020/01/20 08:53

投稿

1236
1236

スコア19

title CHANGED
@@ -1,1 +1,1 @@
1
- お絵かきチャット作ってます
1
+ arduinoで車走らせる
body CHANGED
@@ -1,16 +1,124 @@
1
1
  ### 前提・実現したいこと
2
- javaで簡単なお絵かきチャットを作りたいです。クライアントとサーバを使って、別々の端末から実行したお絵かきアプリで一緒に絵を描けるようにしたいです。クライアントとサーバについてはこちらのサイトを参考にしました。
2
+
3
- https://yoslab.net/netprog/index.php?%B1%E9%BD%AC3-2%20%A5%CD%A5%C3%A5%C8%A5%EF%A1%BC%A5%AF%C2%D0%C0%EF%A5%B2%A1%BC%A5%E0%A5%AF%A5%E9%A5%A4%A5%A2%A5%F3%A5%C8
3
+ arduino unoを用いて、車を走らせています。操作方法はスマホから音を出して、その周波数で直進や右折など制御しています。実際に走らせると動きがカクカクしてしまいます。滑らかに走らせるにはどうしたらよいでしょうか?原因はdelayの位置や値のような気がするのですが色々変えてみても改善しません。ハードの問題もあるかもしれませんが、プログラムで改善すべきところがあれば教えてください。
4
- こちらのサンプルコードに追加して、点と点を結んで線を引くという機能をつけたいのですがどうしたらいいでしょうか。プログラムに関しては大学で習っているのですが、苦手なので少しでも手を貸していただけるとありがたいです。
4
+
5
- ### 発生している問題・エラーメッセージ
5
+
6
- ```
7
- エラーメッセージ
8
- ```
9
6
  ### 該当のソースコード
7
+
10
- ```java
8
+ ```Arduino
9
+
10
+ #define motorA1 6
11
+ #define motorA2 7
12
+ #define motorB1 11
13
+ #define motorB2 12
14
+ #define pwm1 9
15
+ #define pwm2 10
16
+
17
+ #include "arduinoFFT.h"
18
+
19
+ #define SAMPLES 128 //Must be a power of 2
20
+ #define SAMPLING_FREQUENCY 10000 //Hz, must be less than 10000 due to ADC
21
+
22
+ arduinoFFT FFT = arduinoFFT();
23
+
24
+ unsigned int sampling_period_us;
25
+ unsigned long microseconds;
26
+
27
+ double vReal[SAMPLES];
28
+ double vImag[SAMPLES];
29
+
30
+ void setup() {
31
+ Serial.begin(115200);
32
+ pinMode(motorA1,OUTPUT);
33
+ pinMode(motorA2,OUTPUT);
34
+ pinMode(motorB1,OUTPUT);
35
+ pinMode(motorB2,OUTPUT);
36
+
37
+ sampling_period_us = round(1000000*(1.0/SAMPLING_FREQUENCY));
38
+ }
39
+
40
+ void loop() {
41
+
42
+ /*SAMPLING*/
43
+ for(int i=0; i<SAMPLES; i++)
44
+ {
45
+ microseconds = micros(); //Overflows after around 70 minutes!
46
+
47
+ vReal[i] = analogRead(0);
48
+ vImag[i] = 0;
49
+
50
+ while(micros() < (microseconds + sampling_period_us)){
51
+ }
52
+ }
53
+
54
+ /*FFT*/
55
+ FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
56
+ FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
57
+ FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
58
+ double frq = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
59
+
60
+ /*PRINT RESULTS*/
61
+ Serial.println(frq); //Print out what frequency is the most dominant.
62
+
63
+ for(int i=2; i<(SAMPLES/2); i++)
64
+ {
65
+ /*View all these three lines in serial terminal to see which frequencies has which amplitudes*/
66
+
67
+ //Serial.print((i * 1.0 * SAMPLING_FREQUENCY) / SAMPLES, 1);
68
+ //Serial.print(" ");
69
+ //Serial.println(vReal[i], 1); //View only this line in serial plotter to visualize the bins
70
+ }
71
+
72
+ //delay(100); //Repeat the process every second OR:
73
+ //while(1); //Run code once
74
+
75
+ if(frq < 2000){ //停止 2000以下
76
+ analogWrite(pwm1, 0);
77
+ digitalWrite(motorB1,LOW);
78
+ digitalWrite(motorB2,LOW);
79
+ analogWrite(pwm2, 0);
80
+ digitalWrite(motorA1,LOW);
81
+ digitalWrite(motorA2,LOW);
82
+ }
83
+
84
+ else if(2000<=frq && frq<2700){//直進 2000
85
+ digitalWrite(motorB2,LOW);
86
+ digitalWrite(motorB1,HIGH);
87
+ analogWrite(pwm1,255);
88
+ digitalWrite(motorA2,LOW);
89
+ digitalWrite(motorA1,HIGH);
90
+ analogWrite(pwm2, 255);
91
+ }
92
+
93
+ else if(2700 <= frq && frq < 3200){ //右折 2400
94
+ analogWrite(pwm1, 200);
95
+ digitalWrite(motorB1,HIGH);
96
+ digitalWrite(motorB2,LOW);
97
+ analogWrite(pwm2, 80);
98
+ digitalWrite(motorA1,HIGH);
99
+ digitalWrite(motorA2,LOW);
100
+ }
101
+
102
+ else if(3200 <= frq && frq < 3800){//左折 2800
103
+ analogWrite(pwm1, 80);
104
+ digitalWrite(motorB1,HIGH);
105
+ digitalWrite(motorB2,LOW);
106
+ analogWrite(pwm2, 200);
107
+ digitalWrite(motorA1,HIGH);
108
+ digitalWrite(motorA2,LOW);
109
+ }
110
+
111
+ else if(3800 <= frq ){//後退  3200
112
+ analogWrite(pwm1,100);
113
+ digitalWrite(motorB1,LOW);
114
+ digitalWrite(motorB2,HIGH);
115
+ analogWrite(pwm2, 100);
116
+ digitalWrite(motorA1,LOW);
117
+ digitalWrite(motorA2,HIGH);
118
+ }
119
+
11
- ソースコード
120
+ delay(100);
121
+ }
12
122
  ```
13
123
 
14
- ### 試したこと
15
- すでに授業でお絵かきアプリは作成しており、参考サイトのクライアントのコードとお絵かきアプリを合体させてみたのですが、片方で描いた絵がもう一方に反映されなかったり、お絵かきアプリが、ボタンで色を変えたり線や円、四角を描けるようになっていて複雑になりよくわからなくなってしまいました。
16
- ただ線を引けるようにするだけでいいでどのようにコドを追加したらいいか手助けをお願いします。
124
+ ### 補足情報(FW/ツールジョンなど)