Alex motors move at different speeds

If I have Alex move straight with robot.drive.forward(1) one of the motors moves faster than the other. How can I fix this?

Ah, one way is to measure the speeds of the two motors in real time, and slow down the fast motor. You might like to skim my project blog to get a rough idea of how (1) Use PWM to control the speed of a DC motor, (2) Count motor encoder output pulses to estimate the speed.

Or you can just roughly calibrate/adjust by trails and errors the PWM duty cycles of each of the two motors/wheels, and see the car is moving more or less in a straight line.

One problem is that not all DC motor controllers have independent PWM input for each of the two wheels in control. Usually only one PWM input is bot both wheels. In other words, the two wheels with (slightly) different specifications, might move at (slightly) different speeds even for same input PWM duty cycle.

The motor controller I am using is called TB6612FNG. It is not too different from the popular motor controller L298N.

You might see that what I am doing is very tedious and time consuming, and so it is hard for newbies with a faint heart. But once you learnt how to separately control the speeds of the two motors, you have become a ninja and you can move you car not just in a straight line, but also in a square, a circles etc.

The forward command is already using velocity measurements of the motors to try and keep them at the same speed. What must be happening here is that one motor is not able to reach the pre-programmed maximum speed which can be read with robot.drive.left_motor.max_speed - we haven’t added a function to change this value easily from the robot or drive classes but you could try this:

from pitop.pma import EncoderMotor, ForwardDirection

wheel_diameter = 0.073  # reducing this will reduce the maximum speed, default is 0.075
left_motor = EncoderMotor("M3", ForwardDirection.CLOCKWISE, wheel_diameter=wheel_diameter )
left_motor = EncoderMotor("M1", ForwardDirection.COUNTER_CLOCKWISE, wheel_diameter=wheel_diameter )

# overwrite the left and right motors with the newly created ones that have a lower max speed
robot.drive.left_motor = left_motor
robot.drive.right_motor = right_motor

I haven’t tested this but should be fairly easy to debug if there are any issues, let me know how you get on!