Skip to content

Commit 4cb3e6d

Browse files
committed
Made the IMU actually optional for differential drive, use_imu parameter for turns
1 parent d0efce6 commit 4cb3e6d

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

XRPLib/differential_drive.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,11 @@ def straight(self, distance: float, speed: float = 0.5, timeout: float = None, m
121121
rotationsToDo = distance / (self.wheel_diam * math.pi)
122122
#print("rot:", rotationsToDo)
123123

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
126129

127130
while True:
128131

@@ -139,7 +142,13 @@ def straight(self, distance: float, speed: float = 0.5, timeout: float = None, m
139142
break
140143

141144
# 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)
143152

144153
self.set_effort(effort + headingCorrection, effort - headingCorrection)
145154

@@ -155,7 +164,7 @@ def straight(self, distance: float, speed: float = 0.5, timeout: float = None, m
155164
return time.time() < startTime+timeout
156165

157166

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:
159168
"""
160169
Turn the robot some relative heading given in turnDegrees, and exit function when the robot has reached that heading.
161170
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
171180
: type main_controller: Controller
172181
: param secondary_controller: The secondary controller, for maintaining position during the turn by controlling the encoder count difference
173182
: type secondary_controller: Controller
183+
: param use_imu: A boolean flag that changes if the main controller bases it's movement off of
174184
: return: if the distance was reached before the timeout
175185
: rtype: bool
176186
"""
@@ -185,9 +195,9 @@ def turn(self, turn_degrees: float, speed: float = 0.5, timeout: float = None, m
185195

186196
if main_controller is None:
187197
main_controller = PID(
188-
kp = .011,
198+
kp = .015,
189199
kd = 0.0012,
190-
minOutput = 0.20,
200+
minOutput = 0.25,
191201
maxOutput = speed,
192202
tolerance = 0.5,
193203
toleranceCount = 3,
@@ -200,22 +210,30 @@ def turn(self, turn_degrees: float, speed: float = 0.5, timeout: float = None, m
200210
kp = 0.002,
201211
)
202212

203-
turn_degrees += self.imu.get_yaw()
213+
if use_imu and (self.imu is not None):
214+
turn_degrees += self.imu.get_yaw()
204215

205216
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)
206222

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
209231
turnSpeed = main_controller.tick(turnError)
210232

211233
# exit if timeout or tolerance reached
212234
if main_controller.is_done():
213235
break
214236

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)
219237

220238
self.set_effort(-turnSpeed - encoderCorrection, turnSpeed - encoderCorrection)
221239

XRPLib/encoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from micropython import const
33

44
class Encoder:
5-
def __init__(self, aPin:int, bPin:int, ticks_per_revolution:int = 145):
5+
def __init__(self, aPin:int, bPin:int, ticks_per_revolution:int = 146.25):
66
# TODO: Look into PIO implementation as to not take CPU time
77
self.currentPosition = 0
88
self.ticks_per_rev = ticks_per_revolution

0 commit comments

Comments
 (0)