/
demoSequentialCenter.py
125 lines (93 loc) · 5.02 KB
/
demoSequentialCenter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#asebamedulla "ser:device=/dev/ttyACM0" & python vision-controlled-thymio/demoCenter.py
import thymio
import emd
import io
from time import clock
from time import sleep
import picamera
import picamera.array
import numpy as np
import scipy.misc
import os
import _raspClient
import _div
if __name__ == '__main__':
thymio.init()
#sock = _raspClient.initSocket('192.168.86.98')
speed = 100
#timeMeasure = 0.01
#timeTurn = 0.01
with picamera.PiCamera() as camera:
camera.resolution = (50,50)
camera.framerate = 60
camera.hflip = True
camera.vflip = True
with picamera.array.PiYUVArray(camera) as stream:
camera.capture(stream, format="yuv")
stream.truncate(0)
while True:
#_div.clear()
#FW PHASE
thymio.setBothSaveWaitUntilReadyAndReturnTime(speed, speed)
t0 = clock()
# while thymio.getLeft() != speed or thymio.getRight() != speed:
# print thymio.getLeft()
# print thymio.getRight()
camera.capture(stream, format="yuv", use_video_port = True)
image1 = emd.getReceptiveFields(stream.array, nOCellsX = 50, nOCellsY = 50)
stream.truncate(0)
#sleep(timeMeasure)
camera.capture(stream, format="yuv", use_video_port = True)
image2 = emd.getReceptiveFields(stream.array, nOCellsX = 50, nOCellsY = 50)
stream.truncate(0)
motionX = emd.getMotionX(image1, image2)
#TURN PHASE
emdsLeft = (-motionX[0:motionX.shape[0],0:motionX.shape[1]/2]).clip(min = 0)
emdsRight = motionX[0:motionX.shape[0],motionX.shape[1]/2:motionX.shape[1]].clip(min = 0)
meanLeft = np.mean(emdsLeft)
meanRight = np.mean(emdsRight)
adjustmentProportionLeft = meanLeft/(meanLeft+meanRight)
adjustmentProportionRight = meanRight/(meanLeft+meanRight)
t1 = clock()
thymio.setBothSaveWaitUntilReadyAndReturnTime(speed * adjustmentProportionLeft * 2, speed * adjustmentProportionRight * 2)
sleep(t1 -t0)
_div.clear()
print (t1-t0)
#sleep(timeTurn)
# groundSensors = thymio.getGroundSensorR()
# if groundSensors[0] < 100 or groundSensors[1] < 100:
# print("Warning Thymio is about to fall from the table")
# thymio.setRight(0)
# thymio.setLeft(0)
# else:
# print("left: %.3f, right: %.3f" % (meanMovementLeft,meanMovementRight))
# wantedAdjustmentProportionLeft = meanLeft/(meanLeft+meanRight)
# wantedAdjustmentProportionRight = meanRight/(meanLeft+meanRight)
# print "end"
# break
# currentSpeedLeft = thymio.getLeft()
# currentSpeedRight = thymio.getRight()
# currentSpeedSum = currentSpeedLeft + currentSpeedRight
# if(currentSpeedSum != 0):
# lastAdjustmentProportionLeft = currentSpeedLeft/currentSpeedSum
# lastAdjustmentProportionRight = currentSpeedRight/currentSpeedSum
# else:
# lastAdjustmentProportionLeft = 0.5
# lastAdjustmentProportionRight = 0.5
# # actualAdjustmentProportionLeft = (wantedAdjustmentProportionLeft + lastAdjustmentProportionLeft) / 2
# # actualAdjustmentProportionRight = (wantedAdjustmentProportionRight + lastAdjustmentProportionRight) / 2
# actualAdjustmentProportionLeft = 0.5 + wantedAdjustmentProportionLeft - lastAdjustmentProportionLeft
# actualAdjustmentProportionRight = 0.5 + wantedAdjustmentProportionRight - lastAdjustmentProportionRight
# # leftSpeed = baseSpeed + (turnSpeed * (meanLeft/(meanLeft+meanRight)))**1.5
# # rightSpeed = baseSpeed + (turnSpeed * (meanRight/(meanLeft+meanRight)))**1.5
# # thymio.setLeft(leftSpeed)
# # thymio.setRight(rightSpeed)
# thymio.setLeft(baseSpeed * actualAdjustmentProportionLeft * 2)
# thymio.setRight(baseSpeed * actualAdjustmentProportionRight * 2)
# #print motionX
# #network.SetVariable("thymio-II", "motor.left.target", [speed*meanLeft])
# #network.SetVariable("thymio-II", "motor.right.target", [speed*meanRight])
# # network.SetVariable("thymio-II", "motor.left.target", [0])
# # network.SetVariable("thymio-II", "motor.right.target", [0])
# #_raspClient.sendImage(np.hstack((movementLeft,movementRight)), sock)
# stream.truncate(0)