質問編集履歴
1
誤字の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,8 +4,6 @@
|
|
4
4
|
|
5
5
|
Unityで3Dオブジェクトを操作してy軸周りの回転量(横回転)を読み取り、動いた角度(現在は最終的な角度の値)をArduinoに送信してArduinoで制御するモータを回転させる、という動作を実現したいです。
|
6
6
|
|
7
|
-
(例えば(0.0, 0.0, 0.0, 0.1)→(0.0, 0.4, 0.0, 0.9)になった場合40°回転)
|
8
|
-
|
9
7
|
UnityとArduinoの通信は現在シリアル通信で行っています。
|
10
8
|
|
11
9
|
|
@@ -134,378 +132,118 @@
|
|
134
132
|
|
135
133
|
```
|
136
134
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
//
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
//
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
135
|
+
Arduinoのコード
|
136
|
+
|
137
|
+
int PUL=13; //Pin para la señal de pulso
|
138
|
+
|
139
|
+
int DIR=12; //define Direction pin
|
140
|
+
|
141
|
+
int EN=11; //define Enable Pin
|
142
|
+
|
143
|
+
void setup() {
|
144
|
+
|
145
|
+
Serial.begin(9600);
|
146
|
+
|
147
|
+
pinMode (PUL, OUTPUT);
|
148
|
+
|
149
|
+
pinMode (DIR, OUTPUT);
|
150
|
+
|
151
|
+
pinMode (EN, OUTPUT);
|
152
|
+
|
153
|
+
digitalWrite(EN,HIGH);
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
int i;
|
162
|
+
|
163
|
+
double angle = i * 0.225; //現在角度
|
164
|
+
|
165
|
+
String input;
|
166
|
+
|
167
|
+
int val = 0;
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
void loop() {
|
172
|
+
|
173
|
+
// put your main code here, to run repeatedly:
|
174
|
+
|
175
|
+
if (Serial.available() > 0) { // シリアル通信でデータが送られてくるまで待つ。
|
176
|
+
|
177
|
+
String line; // 受信文字列
|
178
|
+
|
179
|
+
int line_len; // 受信文字列の長さ
|
180
|
+
|
181
|
+
int led_duty; // LEDのPWM制御のデューティ比
|
182
|
+
|
183
|
+
line = Serial.readStringUntil('\n');
|
184
|
+
|
185
|
+
line_len = line.length();
|
186
|
+
|
187
|
+
if ((line_len >= 1) && (line_len <= 3)) {
|
188
|
+
|
189
|
+
val = line.toInt();
|
190
|
+
|
191
|
+
//Serial.print("LED Duty: ");
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
//input = Serial.read();
|
198
|
+
|
199
|
+
Serial.println(val); //角度表示
|
200
|
+
|
201
|
+
//val = input.toInt();
|
202
|
+
|
203
|
+
if(val >= 0) {
|
204
|
+
|
205
|
+
digitalWrite(DIR,LOW); //回転方向決定
|
206
|
+
|
207
|
+
}else{
|
208
|
+
|
209
|
+
digitalWrite(DIR,HIGH); //回転方向決定
|
210
|
+
|
211
|
+
val = val * -1;
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
for (int i=0; i<int(val/0.225); i++)
|
200
218
|
|
201
219
|
{
|
202
220
|
|
221
|
+
digitalWrite(PUL,HIGH); //パルス送信on
|
222
|
+
|
223
|
+
delayMicroseconds(400); //0.4秒待機
|
224
|
+
|
225
|
+
digitalWrite(PUL,LOW); //パルス送信off
|
226
|
+
|
227
|
+
delayMicroseconds(400); //0.4秒待機
|
228
|
+
|
229
|
+
double angle = i * 0.225;
|
230
|
+
|
231
|
+
Serial.print("angle:");
|
232
|
+
|
203
|
-
|
233
|
+
Serial.println(angle);
|
204
|
-
|
205
|
-
_message = null;
|
206
|
-
|
207
|
-
return tmp;
|
208
234
|
|
209
235
|
}
|
210
236
|
|
237
|
+
}else {
|
238
|
+
|
211
|
-
|
239
|
+
digitalWrite(PUL,LOW);
|
212
|
-
|
240
|
+
|
213
|
-
|
241
|
+
}
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
/// <summary>
|
218
|
-
|
219
|
-
/// メッセージを受け取ったときに発行するイベント
|
220
|
-
|
221
|
-
/// </summary>
|
222
|
-
|
223
|
-
public System.Action<string> onMessageCallback;
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
/// <summary>
|
228
|
-
|
229
|
-
/// コンストラクタ
|
230
|
-
|
231
|
-
/// </summary>
|
232
|
-
|
233
|
-
public SerialPortWrapper(string portName, int baudRate, int timeoutTime = 3000, Parity parity = Parity.None,
|
234
|
-
|
235
|
-
int dataBits = 8, StopBits stopBits = StopBits.One)
|
236
|
-
|
237
|
-
{
|
238
|
-
|
239
|
-
if (Init(portName, baudRate, timeoutTime, parity, dataBits, stopBits) == false)
|
240
|
-
|
241
|
-
{
|
242
|
-
|
243
|
-
Debug.LogError("Fail init.");
|
244
|
-
|
245
|
-
return;
|
246
|
-
|
247
|
-
}
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
_serialThread = new Thread(Update);
|
252
|
-
|
253
|
-
IsThreadRunning = true;
|
254
|
-
|
255
|
-
_serialThread.Start();
|
256
|
-
|
257
|
-
}
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
/// <summary>
|
262
|
-
|
263
|
-
/// 使い終わったら必ず呼んでください
|
264
|
-
|
265
|
-
/// </summary>
|
266
|
-
|
267
|
-
public void KillThread()
|
268
|
-
|
269
|
-
{
|
270
|
-
|
271
|
-
IsThreadRunning = false;
|
272
|
-
|
273
|
-
if (_serialThread != null)
|
274
|
-
|
275
|
-
{
|
276
|
-
|
277
|
-
_serialThread.Abort();
|
278
|
-
|
279
|
-
_serialPort.Close();
|
280
|
-
|
281
|
-
}
|
282
|
-
|
283
|
-
}
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
/// <summary>
|
288
|
-
|
289
|
-
/// 書き込み
|
290
|
-
|
291
|
-
/// </summary>
|
292
|
-
|
293
|
-
public void Write(string v)
|
294
|
-
|
295
|
-
{
|
296
|
-
|
297
|
-
if (_serialPort != null)
|
298
|
-
|
299
|
-
_serialPort.WriteLine(v);
|
300
|
-
|
301
|
-
}
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
/// <summary>
|
306
|
-
|
307
|
-
/// 初期化処理
|
308
|
-
|
309
|
-
/// </summary>
|
310
|
-
|
311
|
-
bool Init(string portName, int baudRate, int timeoutTime, Parity parity, int dataBits, StopBits stopBits)
|
312
|
-
|
313
|
-
{
|
314
|
-
|
315
|
-
_serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
|
316
|
-
|
317
|
-
try
|
318
|
-
|
319
|
-
{
|
320
|
-
|
321
|
-
_serialPort.Open();
|
322
|
-
|
323
|
-
_serialPort.DtrEnable = true;
|
324
|
-
|
325
|
-
_serialPort.RtsEnable = true;
|
326
|
-
|
327
|
-
_serialPort.DiscardInBuffer();
|
328
|
-
|
329
|
-
_serialPort.ReadTimeout = timeoutTime;
|
330
|
-
|
331
|
-
return true;
|
332
|
-
|
333
|
-
}
|
334
|
-
|
335
|
-
catch (Exception e)
|
336
|
-
|
337
|
-
{
|
338
|
-
|
339
|
-
Debug.LogError("Init : " + e.Message);
|
340
|
-
|
341
|
-
_serialPort.Close();
|
342
|
-
|
343
|
-
_serialPort = null;
|
344
|
-
|
345
|
-
return false;
|
346
|
-
|
347
|
-
}
|
348
|
-
|
349
|
-
}
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
void Update()
|
354
|
-
|
355
|
-
{
|
356
|
-
|
357
|
-
while (IsThreadRunning)
|
358
|
-
|
359
|
-
{
|
360
|
-
|
361
|
-
try
|
362
|
-
|
363
|
-
{
|
364
|
-
|
365
|
-
Message = _serialPort.ReadLine();
|
366
|
-
|
367
|
-
if (onMessageCallback != null)
|
368
|
-
|
369
|
-
{
|
370
|
-
|
371
|
-
onMessageCallback(Message);
|
372
|
-
|
373
|
-
}
|
374
|
-
|
375
|
-
}
|
376
|
-
|
377
|
-
catch (TimeoutException e)
|
378
|
-
|
379
|
-
{
|
380
|
-
|
381
|
-
// Debug.LogWarning(e.Message);
|
382
|
-
|
383
|
-
Message = null;
|
384
|
-
|
385
|
-
}
|
386
|
-
|
387
|
-
}
|
388
|
-
|
389
|
-
}
|
390
242
|
|
391
243
|
}
|
392
244
|
|
393
245
|
```
|
394
246
|
|
395
|
-
```
|
396
|
-
|
397
|
-
Arduinoのコード
|
398
|
-
|
399
|
-
int PUL=13; //Pin para la señal de pulso
|
400
|
-
|
401
|
-
int DIR=12; //define Direction pin
|
402
|
-
|
403
|
-
int EN=11; //define Enable Pin
|
404
|
-
|
405
|
-
void setup() {
|
406
|
-
|
407
|
-
Serial.begin(9600);
|
408
|
-
|
409
|
-
pinMode (PUL, OUTPUT);
|
410
|
-
|
411
|
-
pinMode (DIR, OUTPUT);
|
412
|
-
|
413
|
-
pinMode (EN, OUTPUT);
|
414
|
-
|
415
|
-
digitalWrite(EN,HIGH);
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
}
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
int i;
|
424
|
-
|
425
|
-
double angle = i * 0.225; //現在角度
|
426
|
-
|
427
|
-
String input;
|
428
|
-
|
429
|
-
int val = 0;
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
void loop() {
|
434
|
-
|
435
|
-
// put your main code here, to run repeatedly:
|
436
|
-
|
437
|
-
if (Serial.available() > 0) { // シリアル通信でデータが送られてくるまで待つ。
|
438
|
-
|
439
|
-
String line; // 受信文字列
|
440
|
-
|
441
|
-
int line_len; // 受信文字列の長さ
|
442
|
-
|
443
|
-
int led_duty; // LEDのPWM制御のデューティ比
|
444
|
-
|
445
|
-
line = Serial.readStringUntil('\n');
|
446
|
-
|
447
|
-
line_len = line.length();
|
448
|
-
|
449
|
-
if ((line_len >= 1) && (line_len <= 3)) {
|
450
|
-
|
451
|
-
val = line.toInt();
|
452
|
-
|
453
|
-
//Serial.print("LED Duty: ");
|
454
|
-
|
455
|
-
}
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
//input = Serial.read();
|
460
|
-
|
461
|
-
Serial.println(val); //角度表示
|
462
|
-
|
463
|
-
//val = input.toInt();
|
464
|
-
|
465
|
-
if(val >= 0) {
|
466
|
-
|
467
|
-
digitalWrite(DIR,LOW); //回転方向決定
|
468
|
-
|
469
|
-
}else{
|
470
|
-
|
471
|
-
digitalWrite(DIR,HIGH); //回転方向決定
|
472
|
-
|
473
|
-
val = val * -1;
|
474
|
-
|
475
|
-
}
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
for (int i=0; i<int(val/0.225); i++)
|
480
|
-
|
481
|
-
{
|
482
|
-
|
483
|
-
digitalWrite(PUL,HIGH); //パルス送信on
|
484
|
-
|
485
|
-
delayMicroseconds(400); //0.4秒待機
|
486
|
-
|
487
|
-
digitalWrite(PUL,LOW); //パルス送信off
|
488
|
-
|
489
|
-
delayMicroseconds(400); //0.4秒待機
|
490
|
-
|
491
|
-
double angle = i * 0.225;
|
492
|
-
|
493
|
-
Serial.print("angle:");
|
494
|
-
|
495
|
-
Serial.println(angle);
|
496
|
-
|
497
|
-
}
|
498
|
-
|
499
|
-
}else {
|
500
|
-
|
501
|
-
digitalWrite(PUL,LOW);
|
502
|
-
|
503
|
-
}
|
504
|
-
|
505
|
-
}
|
506
|
-
|
507
|
-
```
|
508
|
-
|
509
247
|
### 試したこと
|
510
248
|
|
511
249
|
|
@@ -518,4 +256,4 @@
|
|
518
256
|
|
519
257
|
### 補足情報(FW/ツールのバージョンなど)
|
520
258
|
|
521
|
-
UnityのバージョンはUnity2019.4.15f.1、ArduinoはArduinoIDE
|
259
|
+
UnityのバージョンはUnity2019.4.15f.1、Arduino UnoはArduinoIDEで制御しています。
|