The problem I'm getting is that these motors don't spin at the same speed, causing my robot to go off centre, even if I give both motors a 255 PWM signal. Furthermore, both motors are quite temperamental i.e. sometimes they do work and sometimes (most of the time) they don't. Is there any way I can fix this accurately? I've tried giving one motor a slower pulse than the other (as one of the motors seems to be stronger) but, as I said, they're temperamental. Should I get stepper motors instead? The problem with that is, the chassis I've got accommodates only a particular size of motor/gearbox so it would be hard to use something other than what I have now.
If the kit is "cheap" (as in cheap manufacturing, not what you paid for it, I can't tell from the picture) then that is the way it is.
Possible error sources are loose wiring, bad joints (putting some resistance in one motors supply). You have used some transistor/mosfet , switched by the Arduino, and not driven the motors directly, right? One of them might be dodgy, and should be easy/cheap to replace.
You can fix tempramental by measuring how much the motors have turned (using an encoder) and then adjusting in the software. That would be fun/educational/challenging, depending on YOUR temprament. Otherwise, get a new chassis.
Firstly the major cause of difference will be mechanical friction. Perhaps
there is extra drag on one side due to misalignment or rubbing.
Check each motor separately direct from the battery and compare their
speed under no-load and also compare how hard they pull. If the difference
is already evident its nothing to do with the electronics, its the motors or
gearing. Try each motor in each direction - perhaps they are polarised
(designed to turn one direction only), in which case there may be a difference...
Compensate by having two different weighting factors for left and right
motors, so that PWM outputs are scaled differently.
Unfortunately, I've taken apart both motors and gearboxes to see if there's a visible difference but I can't really see any. The biggest thing is how temperamental the motors are at any PWM signal below around 120. Sometimes they start, sometimes they don't at those signals.
That's the variation in static friction I suspect - you can try boosting the PWM drive for a
fraction of a second upon starting to get them going, then drop the level down again to
the desired speed.
For more accurate mavigation you'd need encoders on the wheels or motors so provide
positional feedback.
chillingfox321:
Yes, I'm using a transistor to power the motors. I'm powering the motors and the transistors off the same supply - 6V - does that matter?
What transistors are you using, and what resistors do you have connected between the Arduino output pins and the transistor bases? Chances are that your transistors are providing different amounts of drive to the motors. For all but the smallest motors, you need to use mosfets, not transistors.
That platform does not appear to be of very high quality so variation between motors/gears/friction is probably unavoidable. However even with high quality components it would be an error in my opinion to ever have a sketch assume that two seperate motors will spin at the exact same speed given the same output command value. A feedback sensor signal from each motor/gear assembly is required so you can know the exact speed of each wheel is and adjust the two output commands for equal speed. Even then any slippage in tires will cause momentary deviations from straight ahead movement.