回答編集履歴
3
修正
test
CHANGED
@@ -2,12 +2,16 @@
|
|
2
2
|
|
3
3
|
まず、Gyro.pyを含めたコードを示します。
|
4
4
|
|
5
|
-
Gyro.pyについて atan() ---> atan2() に変更しました。
|
6
|
-
|
7
|
-
|
5
|
+
変更1回目
|
8
6
|
|
9
7
|
GUI.pyとImage_Processing.pyについて img_src0関係を削除しました。
|
10
8
|
|
9
|
+
変更2回目
|
10
|
+
|
11
|
+
Gyro.pyに関しましては消させていただきます。(文字数確保のため)
|
12
|
+
|
13
|
+
Image_Processing.pyに関しまして、try-except文の追加、及びhsv変換前にロール角ピッチ角のログをとるようにしました。
|
14
|
+
|
11
15
|
なお、すべてのコードにおいて関係のなさそうなところを省略させていただきます。
|
12
16
|
|
13
17
|
```python
|
@@ -122,24 +126,8 @@
|
|
122
126
|
|
123
127
|
|
124
128
|
|
125
|
-
#サーボモータ(正面カメラ)のsetup
|
126
|
-
|
127
|
-
Servo_pin=18
|
128
|
-
|
129
|
-
GPIO.setmode(GPIO.BCM)
|
130
|
-
|
131
|
-
GPIO.setup(Servo_pin, GPIO.OUT)
|
132
|
-
|
133
|
-
self.Servo = GPIO.PWM(Servo_pin, 50)
|
134
|
-
|
135
|
-
self.Servo.start(0)
|
136
|
-
|
137
|
-
|
138
|
-
|
139
129
|
self.camera_set()
|
140
130
|
|
141
|
-
|
142
|
-
|
143
131
|
timer = QTimer(self.view1)
|
144
132
|
|
145
133
|
timer.timeout.connect(self.camera_set)
|
@@ -186,31 +174,7 @@
|
|
186
174
|
|
187
175
|
return
|
188
176
|
|
189
|
-
|
190
|
-
|
191
|
-
|
177
|
+
|
192
|
-
|
193
|
-
#角度からデューティ比を求める
|
194
|
-
|
195
|
-
duty = 2.5 + (12.0 - 2.5) * (angle + 90) / 180
|
196
|
-
|
197
|
-
#デューティ比を変更
|
198
|
-
|
199
|
-
self.Servo.ChangeDutyCycle(duty)
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
#ピッチ角分サーボを回転
|
204
|
-
|
205
|
-
servo_angle(round(self.Pitch))
|
206
|
-
|
207
|
-
#サーボモータをストップ
|
208
|
-
|
209
|
-
self.Servo.stop()
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
#画像回転
|
214
178
|
|
215
179
|
height = cv_img.shape[0]
|
216
180
|
|
@@ -318,16 +282,6 @@
|
|
318
282
|
|
319
283
|
|
320
284
|
|
321
|
-
data= open('/home/pi/Desktop/AutoMove/GyroData.txt','w') #open(ファイル名指定, どんな仕事をさせるか) 'a'→同じファイル名のファイルが存在していた時、既存ファイルに追記
|
322
|
-
|
323
|
-
# 'w'→同じファイル名のファイルが存在していた時、上書き保存
|
324
|
-
|
325
|
-
data.write( "ロール角:" + str(round(self.Roll)) + "、ピッチ角:" + str(round(self.Pitch)) + "\n") #文字列(str)p_vaiue3を書き込み+改行
|
326
|
-
|
327
|
-
data.close #ファイルを閉じる
|
328
|
-
|
329
|
-
|
330
|
-
|
331
285
|
variable, elapsed_time = Image_Processing.I_P(self.auto,self.img_src,self.Roll)
|
332
286
|
|
333
287
|
|
@@ -382,53 +336,71 @@
|
|
382
336
|
|
383
337
|
|
384
338
|
|
385
|
-
def I_P(auto,img_src,Roll):
|
339
|
+
def I_P(auto,img_src,Roll,Pitch):
|
340
|
+
|
386
|
-
|
341
|
+
try:
|
342
|
+
|
387
|
-
t1 = time.time()
|
343
|
+
t1 = time.time()
|
388
|
-
|
344
|
+
|
389
|
-
if auto == 0:
|
345
|
+
if auto == 0:
|
390
|
-
|
346
|
+
|
391
|
-
return
|
347
|
+
return
|
392
|
-
|
348
|
+
|
393
|
-
F=0
|
349
|
+
F=0
|
350
|
+
|
351
|
+
|
352
|
+
|
394
|
-
|
353
|
+
r=str(round(Roll))
|
354
|
+
|
395
|
-
|
355
|
+
p=str(round(Pitch))
|
396
|
-
|
356
|
+
|
357
|
+
|
358
|
+
|
397
|
-
img_src=cv2.resize(img_src,(180,60))
|
359
|
+
img_src=cv2.resize(img_src,(180,60))
|
398
|
-
|
360
|
+
|
399
|
-
img_src=img_src[20:60, 20-round(75*math.cos(math.radians(90-Roll))):140-round(75*math.cos(math.radians(90-Roll)))]
|
361
|
+
img_src=img_src[20:60, 20-round(75*math.cos(math.radians(90-Roll))):140-round(75*math.cos(math.radians(90-Roll)))]
|
362
|
+
|
363
|
+
|
364
|
+
|
400
|
-
|
365
|
+
WRIGHT=open('/home/pi/Desktop/PAR.txt','a')
|
366
|
+
|
367
|
+
WRIGHT=("P:"+p+"、R:"+r+"\n")
|
368
|
+
|
369
|
+
WRIGHT.close
|
370
|
+
|
371
|
+
|
372
|
+
|
401
|
-
#HSV変換
|
373
|
+
#HSV変換
|
402
|
-
|
374
|
+
|
403
|
-
hsv = cv2.cvtColor(img_src, cv2.COLOR_BGR2HSV)
|
375
|
+
hsv = cv2.cvtColor(img_src, cv2.COLOR_BGR2HSV)
|
404
|
-
|
376
|
+
|
405
|
-
GREEN_min = np.array([30, 64, 30])
|
377
|
+
GREEN_min = np.array([30, 64, 30])
|
406
|
-
|
378
|
+
|
407
|
-
GREEN_max = np.array([90, 255,255])
|
379
|
+
GREEN_max = np.array([90, 255,255])
|
408
|
-
|
380
|
+
|
409
|
-
img_mask = cv2.inRange(hsv,GREEN_min ,GREEN_max)
|
381
|
+
img_mask = cv2.inRange(hsv,GREEN_min ,GREEN_max)
|
410
382
|
|
411
383
|
|
412
384
|
|
413
|
-
neiborhood8 = np.array([[1, 1, 1],[1, 1, 1],[1, 1, 1]],np.uint8)
|
385
|
+
neiborhood8 = np.array([[1, 1, 1],[1, 1, 1],[1, 1, 1]],np.uint8)
|
414
|
-
|
386
|
+
|
415
|
-
img_erosion = cv2.erode(img_mask,neiborhood8,iterations=2)
|
387
|
+
img_erosion = cv2.erode(img_mask,neiborhood8,iterations=2)
|
416
|
-
|
388
|
+
|
417
|
-
img_erosion = cv2.erode(img_erosion,neiborhood8,iterations=2)
|
389
|
+
img_erosion = cv2.erode(img_erosion,neiborhood8,iterations=2)
|
418
|
-
|
390
|
+
|
419
|
-
img_dilation = cv2.dilate(img_erosion,neiborhood8,iterations=2)
|
391
|
+
img_dilation = cv2.dilate(img_erosion,neiborhood8,iterations=2)
|
420
|
-
|
392
|
+
|
421
|
-
img_dilation = cv2.dilate(img_dilation,neiborhood8,iterations=2)
|
393
|
+
img_dilation = cv2.dilate(img_dilation,neiborhood8,iterations=2)
|
422
|
-
|
394
|
+
|
423
|
-
img_dst = cv2.bitwise_and(img_src, img_src, mask=img_dilation)
|
395
|
+
img_dst = cv2.bitwise_and(img_src, img_src, mask=img_dilation)
|
424
|
-
|
396
|
+
|
425
|
-
img_dst2 = cv2.bitwise_and(img_src, img_src, mask=img_mask)
|
397
|
+
img_dst2 = cv2.bitwise_and(img_src, img_src, mask=img_mask)
|
426
|
-
|
427
|
-
|
428
|
-
|
398
|
+
|
399
|
+
|
400
|
+
|
429
|
-
#Value.pyで変数(variable)を計算
|
401
|
+
#Value.pyで変数(variable)を計算
|
430
|
-
|
402
|
+
|
431
|
-
row_L , row_R ,variable = Value.Get_MotorParameter(img_mask,1 ,F)
|
403
|
+
row_L , row_R ,variable = Value.Get_MotorParameter(img_mask,1 ,F)
|
432
404
|
|
433
405
|
|
434
406
|
|
@@ -436,160 +408,30 @@
|
|
436
408
|
|
437
409
|
|
438
410
|
|
439
|
-
t2 = time.time()
|
411
|
+
t2 = time.time()
|
440
|
-
|
412
|
+
|
441
|
-
elapsed_time = t2 - t1
|
413
|
+
elapsed_time = t2 - t1
|
442
|
-
|
443
|
-
|
444
|
-
|
414
|
+
|
415
|
+
|
416
|
+
|
445
|
-
return variable ,elapsed_time
|
417
|
+
return variable ,elapsed_time
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
except:
|
422
|
+
|
423
|
+
print("Error")
|
424
|
+
|
425
|
+
WRIGHT=open('/home/pi/Desktop/PAR.txt','a')
|
426
|
+
|
427
|
+
WRIGHT=("ERROR"+"\n")
|
428
|
+
|
429
|
+
WRIGHT.close
|
430
|
+
|
431
|
+
variable=0
|
432
|
+
|
433
|
+
elapsed_time=0
|
434
|
+
|
435
|
+
return variable ,elapsed_time
|
446
436
|
|
447
437
|
```
|
448
|
-
|
449
|
-
```python
|
450
|
-
|
451
|
-
#Gyro.py
|
452
|
-
|
453
|
-
import smbus
|
454
|
-
|
455
|
-
import math
|
456
|
-
|
457
|
-
from time import sleep
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
DEV_ADDR = 0x68
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
ACCEL_XOUT = 0x3b
|
466
|
-
|
467
|
-
ACCEL_YOUT = 0x3d
|
468
|
-
|
469
|
-
ACCEL_ZOUT = 0x3f
|
470
|
-
|
471
|
-
TEMP_OUT = 0x41
|
472
|
-
|
473
|
-
GYRO_XOUT = 0x43
|
474
|
-
|
475
|
-
GYRO_YOUT = 0x45
|
476
|
-
|
477
|
-
GYRO_ZOUT = 0x47
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
PWR_MGMT_1 = 0x6b
|
482
|
-
|
483
|
-
PWR_MGMT_2 = 0x6c
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
bus = smbus.SMBus(1)
|
488
|
-
|
489
|
-
bus.write_byte_data(DEV_ADDR, PWR_MGMT_1, 0)
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
def read_word(adr):
|
496
|
-
|
497
|
-
high = bus.read_byte_data(DEV_ADDR, adr)
|
498
|
-
|
499
|
-
low = bus.read_byte_data(DEV_ADDR, adr+1)
|
500
|
-
|
501
|
-
val = (high << 8) + low
|
502
|
-
|
503
|
-
return val
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
# Sensor data read
|
508
|
-
|
509
|
-
def read_word_sensor(adr):
|
510
|
-
|
511
|
-
val = read_word(adr)
|
512
|
-
|
513
|
-
if (val >= 0x8000): # minus
|
514
|
-
|
515
|
-
return -((65535 - val) + 1)
|
516
|
-
|
517
|
-
else: # plus
|
518
|
-
|
519
|
-
return val
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
def get_temp():
|
526
|
-
|
527
|
-
temp = read_word_sensor(TEMP_OUT)
|
528
|
-
|
529
|
-
x = temp / 340 + 36.53 # data sheet(register map)記載の計算式.
|
530
|
-
|
531
|
-
return x
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
def getGyro():
|
538
|
-
|
539
|
-
x = read_word_sensor(GYRO_XOUT)/ 131.0
|
540
|
-
|
541
|
-
y = read_word_sensor(GYRO_YOUT)/ 131.0
|
542
|
-
|
543
|
-
z = read_word_sensor(GYRO_ZOUT)/ 131.0
|
544
|
-
|
545
|
-
return [x, y, z]
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
def getAccel():
|
552
|
-
|
553
|
-
x = read_word_sensor(ACCEL_XOUT)/ 16384.0
|
554
|
-
|
555
|
-
y= read_word_sensor(ACCEL_YOUT)/ 16384.0
|
556
|
-
|
557
|
-
z= read_word_sensor(ACCEL_ZOUT)/ 16384.0
|
558
|
-
|
559
|
-
return [x, y, z]
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
def GyroSensor(Roll, Pitch):
|
570
|
-
|
571
|
-
ax, ay, az = getAccel()
|
572
|
-
|
573
|
-
gx, gy, gz = getGyro()
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
#print ('{0:4.3f}, {0:4.3f}, {0:4.3f}, {0:4.3f}, {0:4.3f}, {0:4.3f},' .format(gx, gy, gz, ax, ay, az))
|
578
|
-
|
579
|
-
Roll = math.atan2(ay,az) * 57.324 #57.324 = 360/2π であり、rad から ° に変換
|
580
|
-
|
581
|
-
Pitch = math.atan2(-ax , math.sqrt( ay* ay+ az*az ) ) * 57.324
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
#pitch = math.atan(-ax / (ay*math.sin(roll) + az*math.cos(roll)))
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
#print('{0:4.3f}, {0:4.3f},' .format(pitch, roll))
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
return Pitch, Roll
|
594
|
-
|
595
|
-
```
|
2
修正
test
CHANGED
@@ -576,9 +576,9 @@
|
|
576
576
|
|
577
577
|
#print ('{0:4.3f}, {0:4.3f}, {0:4.3f}, {0:4.3f}, {0:4.3f}, {0:4.3f},' .format(gx, gy, gz, ax, ay, az))
|
578
578
|
|
579
|
-
Roll = math.atan2(ay
|
579
|
+
Roll = math.atan2(ay,az) * 57.324 #57.324 = 360/2π であり、rad から ° に変換
|
580
|
-
|
580
|
+
|
581
|
-
Pitch = math.atan2(-ax
|
581
|
+
Pitch = math.atan2(-ax , math.sqrt( ay* ay+ az*az ) ) * 57.324
|
582
582
|
|
583
583
|
|
584
584
|
|
1
修正
test
CHANGED
@@ -280,7 +280,7 @@
|
|
280
280
|
|
281
281
|
motor.init_GPIO()
|
282
282
|
|
283
|
-
self.p1
|
283
|
+
self.pin1 , self.pin2 , self.pin3 , self.pin4 , self.pin5 , self.pin6 = motor.init_GPIO()
|
284
284
|
|
285
285
|
self.auto_move_set()
|
286
286
|
|
@@ -300,7 +300,7 @@
|
|
300
300
|
|
301
301
|
self.auto = 0
|
302
302
|
|
303
|
-
self.m
|
303
|
+
self.motor_S()
|
304
304
|
|
305
305
|
GPIO.cleanup()
|
306
306
|
|
@@ -334,7 +334,7 @@
|
|
334
334
|
|
335
335
|
#モーターを動かすプログラム。画像処理(Image_Processing.py)にて得られた変数(variable)を基にモータの出力を変更
|
336
336
|
|
337
|
-
self.Duty_R , self.Duty_L = motor.motor_
|
337
|
+
self.Duty_R , self.Duty_L = motor.motor_start(p_vaiue3,self.pin1 , self.pin2 , self.pin3 , self.pin4 , self.pin5 , self.pin6 )
|
338
338
|
|
339
339
|
|
340
340
|
|
@@ -390,11 +390,7 @@
|
|
390
390
|
|
391
391
|
return
|
392
392
|
|
393
|
-
F
|
393
|
+
F=0
|
394
|
-
|
395
|
-
row_mean_p = row_mean_p2 = h3 = 0
|
396
|
-
|
397
|
-
row_mean_p3 = 320
|
398
394
|
|
399
395
|
|
400
396
|
|
@@ -412,7 +408,7 @@
|
|
412
408
|
|
413
409
|
img_mask = cv2.inRange(hsv,GREEN_min ,GREEN_max)
|
414
410
|
|
415
|
-
|
411
|
+
|
416
412
|
|
417
413
|
neiborhood8 = np.array([[1, 1, 1],[1, 1, 1],[1, 1, 1]],np.uint8)
|
418
414
|
|
@@ -432,11 +428,11 @@
|
|
432
428
|
|
433
429
|
#Value.pyで変数(variable)を計算
|
434
430
|
|
435
|
-
row_
|
431
|
+
row_L , row_R ,variable = Value.Get_MotorParameter(img_mask,1 ,F)
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
|
440
436
|
|
441
437
|
|
442
438
|
|