@@ -121,8 +121,11 @@ def straight(self, distance: float, speed: float = 0.5, timeout: float = None, m
121
121
rotationsToDo = distance / (self .wheel_diam * math .pi )
122
122
#print("rot:", rotationsToDo)
123
123
124
- # record current heading to maintain it
125
- heading = self .imu .get_yaw ()
124
+ if self .imu is not None :
125
+ # record current heading to maintain it
126
+ initial_heading = self .imu .get_yaw ()
127
+ else :
128
+ initial_heading = 0
126
129
127
130
while True :
128
131
@@ -139,7 +142,13 @@ def straight(self, distance: float, speed: float = 0.5, timeout: float = None, m
139
142
break
140
143
141
144
# calculate heading correction
142
- headingCorrection = secondary_controller .tick (self .imu .get_yaw () - heading )
145
+ if self .imu is not None :
146
+ # record current heading to maintain it
147
+ current_heading = self .imu .get_yaw ()
148
+ else :
149
+ current_heading = ((leftDelta - rightDelta )/ 2 )* 360 * self .wheel_diam / self .track_width
150
+
151
+ headingCorrection = secondary_controller .tick (current_heading - initial_heading )
143
152
144
153
self .set_effort (effort + headingCorrection , effort - headingCorrection )
145
154
@@ -155,7 +164,7 @@ def straight(self, distance: float, speed: float = 0.5, timeout: float = None, m
155
164
return time .time () < startTime + timeout
156
165
157
166
158
- def turn (self , turn_degrees : float , speed : float = 0.5 , timeout : float = None , main_controller : Controller = None , secondary_controller : Controller = None ) -> bool :
167
+ def turn (self , turn_degrees : float , speed : float = 0.5 , timeout : float = None , main_controller : Controller = None , secondary_controller : Controller = None , use_imu : bool = True ) -> bool :
159
168
"""
160
169
Turn the robot some relative heading given in turnDegrees, and exit function when the robot has reached that heading.
161
170
Speed is bounded from -1 (turn counterclockwise the relative heading at full speed) to 1 (turn clockwise the relative heading at full speed)
@@ -171,6 +180,7 @@ def turn(self, turn_degrees: float, speed: float = 0.5, timeout: float = None, m
171
180
: type main_controller: Controller
172
181
: param secondary_controller: The secondary controller, for maintaining position during the turn by controlling the encoder count difference
173
182
: type secondary_controller: Controller
183
+ : param use_imu: A boolean flag that changes if the main controller bases it's movement off of
174
184
: return: if the distance was reached before the timeout
175
185
: rtype: bool
176
186
"""
@@ -185,9 +195,9 @@ def turn(self, turn_degrees: float, speed: float = 0.5, timeout: float = None, m
185
195
186
196
if main_controller is None :
187
197
main_controller = PID (
188
- kp = .011 ,
198
+ kp = .015 ,
189
199
kd = 0.0012 ,
190
- minOutput = 0.20 ,
200
+ minOutput = 0.25 ,
191
201
maxOutput = speed ,
192
202
tolerance = 0.5 ,
193
203
toleranceCount = 3 ,
@@ -200,22 +210,30 @@ def turn(self, turn_degrees: float, speed: float = 0.5, timeout: float = None, m
200
210
kp = 0.002 ,
201
211
)
202
212
203
- turn_degrees += self .imu .get_yaw ()
213
+ if use_imu and (self .imu is not None ):
214
+ turn_degrees += self .imu .get_yaw ()
204
215
205
216
while True :
217
+
218
+ # calculate encoder correction to minimize drift
219
+ leftDelta = self .get_left_encoder_position () - startingLeft
220
+ rightDelta = self .get_right_encoder_position () - startingRight
221
+ encoderCorrection = secondary_controller .tick (leftDelta + rightDelta )
206
222
207
- # calculate turn speed from PID with delta heading
208
- turnError = turn_degrees - self .imu .get_yaw ()
223
+ if use_imu and (self .imu is not None ):
224
+ # calculate turn error (in degrees) from the imu
225
+ turnError = turn_degrees - self .imu .get_yaw ()
226
+ else :
227
+ # calculate turn error (in degrees) from the encoder counts
228
+ turnError = turn_degrees - ((rightDelta - leftDelta )/ 2 )* 360 * self .wheel_diam / self .track_width
229
+
230
+ # Pass the turn error to the main controller to get a turn speed
209
231
turnSpeed = main_controller .tick (turnError )
210
232
211
233
# exit if timeout or tolerance reached
212
234
if main_controller .is_done ():
213
235
break
214
236
215
- # calculate encoder correction to minimize drift
216
- left = self .get_left_encoder_position () - startingLeft
217
- right = self .get_right_encoder_position () - startingRight
218
- encoderCorrection = secondary_controller .tick (left + right )
219
237
220
238
self .set_effort (- turnSpeed - encoderCorrection , turnSpeed - encoderCorrection )
221
239
0 commit comments