Refactoring systematically changes or simplifies internal code without changing externally observable behavior. For robot movement, it consolidates repeated motor-control logic while preserving function names and arguments. section 3.10
A practical refactoring pass for robot movement:
- Find repeated logic, such as setting both motor throttles, waiting, and stopping the motors. section 3.10
- Centralize that logic in a movement function accepting duration, speed, and left- and right-motor factors. section 3.10
- Keep readable operations such as forward as small wrappers that pass arguments to the centralized implementation. section 3.10
- Save the reusable implementation as commonmotor.py and import it for later robot controls. section 3.10
Listing 3.7 commonmotor.py: Consolidating common logic into a common function
#!/usr/bin/env python3
from adafruit_crickit import crickit
import time
import os
DC_ADJUST_R = float(os.environ.get('ROBO_DC_ADJUST_R', '1'))
DC_ADJUST_L = float(os.environ.get('ROBO_DC_ADJUST_L', '1'))
ADJUST = dict(R=DC_ADJUST_R, L=DC_ADJUST_L)
MOTOR = dict(R=crickit.dc_motor_1, L=crickit.dc_motor_2)
THROTTLE_SPEED = {0: 0, 1: 0.5, 2: 0.7, 3: 0.9}
def set_throttle(name, speed, factor=1):
MOTOR[name].throttle = THROTTLE_SPEED[speed] * ADJUST[name] * factor
def movement(duration=0.2, speed=3, factor_r=1, factor_l=1):
set_throttle('R', speed, factor_r)
set_throttle('L', speed, factor_l)
time.sleep(duration)
set_throttle('R', 0)
set_throttle('L', 0)
def forward(duration=0.2, speed=3):
movement(duration, speed)
def backward(duration=0.2, speed=3):
movement(duration, speed, factor_r=-1, factor_l=-1)
def right(duration=0.2, speed=3):
movement(duration, speed, factor_r=0.5)
def left(duration=0.2, speed=3):
movement(duration, speed, factor_l=0.5)
def spin_right(duration=0.2, speed=3):
movement(duration, speed, factor_r=-1, factor_l=1)
def spin_left(duration=0.2, speed=3):
movement(duration, speed, factor_r=1, factor_l=-1)
Shows a complete Python motor library that centralizes throttle conversion, motor adjustment, timed movement, and directional wrappers. section 3.10
Consolidating robot movement
Before refactoring, each movement operation repeats throttle, timing, and stopping code. Afterward, movement(duration, speed, factor_r, factor_l) performs the shared work, while forward(duration, speed) delegates to it without changing how callers invoke the function. section 3.10





















