I have a bot with mecanum wheels and I have a move_distance function that moves the bot a certain distance left, right, forward or reverse. The issue is that if I run the function after another function, move_sideways which starts motors and sets them to move at a certain speed and direction, it doesn’t do anything. Running the move_distance function in the beginning of my code, before move_sideways, works as intended. Why would that be the case?
def move_sideways(speed, direction):
# Move straight in different directions
if direction == RIGHT:
front_left.set_velocity(speed, PERCENT)
front_right.set_velocity(-speed, PERCENT)
back_left.set_velocity(-speed, PERCENT)
back_right.set_velocity(speed, PERCENT)
elif direction == LEFT:
front_left.set_velocity(-speed, PERCENT)
front_right.set_velocity(speed, PERCENT)
back_left.set_velocity(speed, PERCENT)
back_right.set_velocity(-speed, PERCENT)
elif direction == FORWARD:
front_left.set_velocity(speed, PERCENT)
front_right.set_velocity(speed, PERCENT)
back_left.set_velocity(speed, PERCENT)
back_right.set_velocity(speed, PERCENT)
elif direction == REVERSE:
front_left.set_velocity(-speed, PERCENT)
front_right.set_velocity(-speed, PERCENT)
back_left.set_velocity(-speed, PERCENT)
back_right.set_velocity(-speed, PERCENT)
front_left.spin(FORWARD)
front_right.spin(FORWARD)
back_left.spin(FORWARD)
back_right.spin(FORWARD)
def move_distance(speed, direction, distance):
# Move straight in different directions for a specific distance
wheel_diameter_mm = 4 * 25.4 # 4-inch wheels converted to mm
wheel_circumference_mm = math.pi * wheel_diameter_mm
degrees_to_move = (distance / wheel_circumference_mm) * 360
if direction == RIGHT:
front_left.spin_for(FORWARD, degrees_to_move, DEGREES, wait=False)
front_right.spin_for(REVERSE, degrees_to_move, DEGREES, wait=False)
back_left.spin_for(FORWARD, degrees_to_move, DEGREES, wait=False)
back_right.spin_for(REVERSE, degrees_to_move, DEGREES)
elif direction == LEFT:
front_left.spin_for(REVERSE, degrees_to_move, DEGREES, wait=False)
front_right.spin_for(FORWARD, degrees_to_move, DEGREES, wait=False)
back_left.spin_for(REVERSE, degrees_to_move, DEGREES, wait=False)
back_right.spin_for(FORWARD, degrees_to_move, DEGREES)
elif direction == FORWARD:
front_left.spin_for(FORWARD, degrees_to_move, DEGREES, wait=False)
front_right.spin_for(FORWARD, degrees_to_move, DEGREES, wait=False)
back_left.spin_for(FORWARD, degrees_to_move, DEGREES, wait=False)
back_right.spin_for(FORWARD, degrees_to_move, DEGREES)
elif direction == REVERSE:
front_left.spin_for(REVERSE, degrees_to_move, DEGREES, wait=False)
front_right.spin_for(REVERSE, degrees_to_move, DEGREES, wait=False)
back_left.spin_for(REVERSE, degrees_to_move, DEGREES, wait=False)
back_right.spin_for(REVERSE, degrees_to_move, DEGREES)
def when_started1():
black_threshold = 2000 # Based on line tracker values
# Define motor speeds
forward_speed = 25 # Based on percent of motor speed
black_line = 0 # Number of black lines crossed
# Move arm up
grab_arm.set_velocity(60,PERCENT)
grab_arm.spin_for(FORWARD, 300, DEGREES)
while black_line < 2:
# Read line sensor values
right_sensor = line_tracker_c.value()
center_sensor = line_tracker_d.value()
left_sensor = line_tracker_b.value()
# Move to the right
move_sideways(forward_speed, RIGHT)
if center_sensor > black_threshold:
black_line += 1
if black_line != 2:
wait(1, SECONDS)
move_sideways(0, RIGHT)
while True:
Kp = 0.01 # Proportional control constant, higher = more sensitive
stop_distance_mm = 190 # Based on ultrasonic values
# Read line sensor values
right_sensor = line_tracker_c.value()
center_sensor = line_tracker_d.value()
left_sensor = line_tracker_b.value()
distance_mm = Range.distance(MM)
# Calculate the errors
error_left = left_sensor - black_threshold
error_right = right_sensor - black_threshold
# Calculate the control actions
# turn_speed_left = Kp * error_left
# turn_speed_right = Kp * error_right
if distance_mm < stop_distance_mm:
turn_in_direction(0, 0) # Stop all wheels
grab_claw.spin_for(REVERSE, 270, DEGREES, wait=False)
wait(2, SECONDS)
break
elif center_sensor > black_threshold: # Center sensor sees black (on the line)
# Move forward
move_sideways(forward_speed, FORWARD)
elif left_sensor > black_threshold or right_sensor > black_threshold:
# Calculate the control actions for turning while moving forward
turn_speed = Kp * (error_left if left_sensor > black_threshold else error_right)
# Move forward while turning
front_left.set_velocity(forward_speed - turn_speed, PERCENT)
front_right.set_velocity(forward_speed + turn_speed, PERCENT)
back_left.set_velocity(forward_speed - turn_speed, PERCENT)
back_right.set_velocity(forward_speed + turn_speed, PERCENT)
# Start the motors
front_left.spin(FORWARD)
front_right.spin(FORWARD)
back_left.spin(FORWARD)
back_right.spin(FORWARD)
move_distance(30, FORWARD, 500)
when_started1()