3
3
SLIGHT_TIME = 0.1
4
4
TURN_TIME = 0.75
5
5
6
+ class IMUProtocol (asyncio .Protocol ):
7
+ def __init__ (self , up ):
8
+ self .up = up
9
+
10
+ def data_received (self , data ):
11
+ text = data .decode ().strip ().split ()[- 1 ]
12
+ rgbc = tuple ([int (i ) for i in text .split ("," )])
13
+ self .up ._received (rgbc )
14
+
15
+ class IMU :
16
+ def __init__ (self ):
17
+ self .loop = asyncio .get_event_loop ()
18
+ self .data = (0 ,0 ,0 )
19
+ self .initial = False
20
+ self ._mark = 0
21
+ self .loop .run_until_complete (self .connect ())
22
+
23
+ @asyncio .coroutine
24
+ def connect (self ):
25
+ yield from self .loop .connect_read_pipe (
26
+ lambda up = self : IMUProtocol (up ), open ("/var/run/euler0" ))
27
+
28
+ def _received (self , euler ):
29
+ self .data = euler
30
+ self .heading = euler [0 ]
31
+ if not initial :
32
+ self .initial = euler
33
+
34
+ def mark (self ):
35
+ self ._mark = self .heading
36
+
37
+ @property
38
+ def from_start (self ):
39
+ return self .heading - self .initial [0 ]
40
+ @property
41
+ def from_mark (self ):
42
+ return self .heading - self ._mark
43
+
6
44
class DriveMotors :
7
45
def __init__ (self , left = 0 , right = 1 ):
8
46
self .loop = asyncio .get_event_loop ()
9
47
self ._left = open ("/var/run/motor{}" .format (left ),"w" )
10
48
self ._right = open ("/var/run/motor{}" .format (right ),"w" )
11
49
self .__left = 0
12
50
self .__right = 0
51
+ self .imu = IMU ()
13
52
self .loop .run_until_complete (self .connect ())
14
53
self .__handle = None
15
54
@@ -25,7 +64,7 @@ def left(self):
25
64
@left .setter
26
65
def left (self , val ):
27
66
self .cancel ()
28
- val = val * 0.999
67
+ # val = val * 0.999
29
68
self .__left = val
30
69
#if val == 0:
31
70
#traceback.print_stack()
@@ -38,50 +77,40 @@ def right(self):
38
77
@right .setter
39
78
def right (self , val ):
40
79
self .cancel ()
41
- val = val * 0.9
80
+ # val = val * 0.9
42
81
self .__right = val
43
82
self ._right .write ("{}\n " .format (val ).encode ())
44
83
45
84
def cancel (self ):
46
85
if self .__handle : self .__handle .cancel ()
47
86
87
+ def __set (self , right , left , t = None ):
88
+ self .imu .mark ()
89
+ self .right = right
90
+ self .left = left
91
+ if t :
92
+ self .__handle = self .loop .call_later (t , self .forward )
93
+
48
94
def stop (self ):
49
- self .right = 0
50
- self .left = 0
95
+ self .__set (0 , 0 )
51
96
52
97
def forward (self ):
53
- self .right = - 1
54
- self .left = 1
98
+ self .__set (- 1 , 1 )
55
99
56
100
def backward (self , t = None ):
57
- self .right = 1
58
- self .left = - 1
59
- if t :
60
- self .__handle = self .loop .call_later (t , self .forward )
101
+ self .__set (1 , - 1 , t )
61
102
62
103
def turnright (self , t = TURN_TIME ):
63
- self .right = 1
64
- self .left = 1
65
- if t :
66
- self .__handle = self .loop .call_later (t , self .forward )
104
+ self .__set (1 , 1 , t )
67
105
68
106
def turnleft (self , t = TURN_TIME ):
69
- self .right = - 1
70
- self .left = - 1
71
- if t :
72
- self .__handle = self .loop .call_later (t , self .forward )
107
+ self .__set (- 1 , - 1 , t )
73
108
74
109
def slightright (self , t = SLIGHT_TIME ):
75
110
print ("Slight right" )
76
- self .right = - 0.09
77
- self .left = 1
78
- if t :
79
- self .__handle = self .loop .call_later (t , self .forward )
111
+ self .__set (- 0.09 , 1 , t )
80
112
81
113
def slightleft (self , t = SLIGHT_TIME ):
82
114
print ("Slight left" )
83
- self .right = - 1
84
- self .left = 0.09
85
- if t :
86
- self .__handle = self .loop .call_later (t , self .forward )
115
+ self .__set (- 1 , 0.09 , t )
87
116
0 commit comments