-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutputLegoMindstormsNXT.py
188 lines (157 loc) · 6.47 KB
/
OutputLegoMindstormsNXT.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
125
126
127
128
129
130
131
132
133
134
135
136
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
from ObjectMotion import *
import time
import logging
from Helpers import *
import nxt, thread
from OutputRobotBase import *
class LegoNXT(BluetoothRobotBase):
speed_default = 75
speed_max = 100
speed_min = 5
def __init__(self, bluetooth_address):
super(LegoNXT,self).__init__(bluetooth_address)
self._name = 'Lego Mindstorms NXT Brick'
self._brick = None
self._hardware_motion_type = MTYPE_UNDEFINED
self._motor_a = None
self._motor_b = None
self._motor_c = None
self._motor_a_speed_last = 0
self._motor_b_speed_last = 0
self._motor_c_speed_last = 0
def connect(self, hardware_motion_type = MTYPE_TANKSTEER):
connection_attempt_count = 0
connection_retry = False
self._supported_motion_types.append(hardware_motion_type)
while connection_attempt_count < LegoNXT.maximum_retry_count:
self.report_connection_attempt()
try:
connection_attempt_count += 1
self._brick = nxt.locator.find_one_brick(host=self._bluetooth_address, debug=True, method=nxt.locator.Method(usb=False, bluetooth=True, fantomusb=False))
self._connection_success = True
except Exception as ex:
self.report_connection_fail_retry(connection_attempt_count, LegoNXT.maximum_retry_count)
time.sleep(2)
connection_retry = True
pass
if not connection_retry:
# exit loop
break
# end loop
if connection_retry:
# looks like it all went bad
self._connection_success = False
raise RobotNotConnected('Bluetooth connection failed.', self)
self.report_connection_success()
self._hardware_motion_type = hardware_motion_type
self._motor_a = nxt.Motor(self._brick, nxt.PORT_A)
self._motor_b = nxt.Motor(self._brick, nxt.PORT_B)
self._motor_c = nxt.Motor(self._brick, nxt.PORT_C)
self._motor_a_speed_last = 0
self._motor_b_speed_last = 0
self._motor_c_speed_last = 0
def disconnect(self):
if self._connection_success:
self._brick = None
def stop(self):
if self._connection_success:
self.output_motion(ObjectMotion(MTYPE_UNDEFINED, DDIR_STATIONARY))
def output_motion(self, object_motion):
# if object_motion.motion_type != MTYPE_TANKSTEER:
# raise RobotBadMotionInstruction("")
direction = object_motion.direction
speed = object_motion.speed
nxtspeed = LegoNXT.speed_default
if speed == -1:
nxtspeed = LegoNXT.speed_default
else:
if (speed > 0) and (speed<=1):
nxtspeed = int(speed * LegoNXT.speed_max)
if nxtspeed < LegoNXT.speed_min:
nxtspeed = LegoNXT.speed_min
elif nxtspeed > LegoNXT.speed_max:
nxtspeed = LegoNXT.speed_max
motor_speed_a = 0
motor_speed_b = 0
if direction == DDIR_AHEADLEFT:
motor_speed_a = 50
motor_speed_b = 100
elif direction == DDIR_AHEADRIGHT:
motor_speed_a = 100
motor_speed_b = 50
elif direction == DDIR_AHEAD:
motor_speed_a = 100
motor_speed_b = 100
elif direction == DDIR_LEFT:
motor_speed_a = -100
motor_speed_b = 100
elif direction == DDIR_RIGHT:
motor_speed_a = 100
motor_speed_b = -100
elif direction == DDIR_REVERSELEFT:
motor_speed_a = -50
motor_speed_b = -100
elif direction == DDIR_REVERSERIGHT:
motor_speed_a = -100
motor_speed_b = -50
elif direction == DDIR_REVERSE:
motor_speed_a = -100
motor_speed_b = -100
elif direction == DDIR_STATIONARY:
motor_speed_a = 0
motor_speed_b = 0
motor_speed_a = int(motor_speed_a * (float(nxtspeed) / float(LegoNXT.speed_max)))
motor_speed_b = int(motor_speed_b * (float(nxtspeed) / float(LegoNXT.speed_max)))
# if (self._motor_a_speed_last != motor_speed_a) or (self._motor_b_speed_last != motor_speed_b):
# logging.debug(self.name + " output_motion a:" + str(motor_speed_a) + " b:" + str(motor_speed_b))
degrees = 45
if motor_speed_a != 0:
if self._motor_a_speed_last != motor_speed_a:
self.__turn_motor(self._motor_a, motor_speed_a, degrees)
else:
self.__stop_motor(self._motor_a, None)
if motor_speed_b != 0:
if self._motor_b_speed_last != motor_speed_b:
self.__turn_motor(self._motor_b, motor_speed_b, degrees)
else:
self.__stop_motor(self._motor_b, None)
self._motor_a_speed_last = motor_speed_a
self._motor_b_speed_last = motor_speed_b
def __turn_motor(self, motor, power, degrees):
# if(power!=0) and (degrees!=0):
# motor.weak_turn(power, degrees)
logging.debug(self.name + ' Motor: ' + str(motor.port) + " Power:" + str(power) + " Degrees:" + str(degrees))
if(power != 0) and (degrees != 0):
motor.run(power)
def __stop_motor(self, motor, *args):
motor.idle()
def test_motors(self):
self.__turn_motor(self._motor_a,100,360)
time.sleep(1)
self.__stop_motor(self._motor_a)
time.sleep(1)
self.__turn_motor(self._motor_a,-100,360)
time.sleep(1)
self.__stop_motor(self._motor_a)
time.sleep(1)
self.__turn_motor(self._motor_b,100,360)
time.sleep(1)
self.__stop_motor(self._motor_b)
time.sleep(1)
self.__turn_motor(self._motor_b,-100,360)
time.sleep(1)
self.__stop_motor(self._motor_b)
time.sleep(1)
def test_motors_quick(self):
self.__turn_motor(self._motor_a,100,30)
time.sleep(1)
self.__stop_motor(self._motor_a)
self.__turn_motor(self._motor_a,-100,30)
time.sleep(1)
self.__stop_motor(self._motor_a)
self.__turn_motor(self._motor_b,100,30)
time.sleep(1)
self.__stop_motor(self._motor_b)
self.__turn_motor(self._motor_b,-100,30)
time.sleep(1)
self.__stop_motor(self._motor_b)