Quote:
|
Originally Posted by Alan Anderson
The compiler we use has the irritating feature of choosing eight-bit arithmetic when adding eight-bit quantities. That often results in overflow and unexpected results. Adding a constant 2000 to the expression forces the compiler to do things using 16-bit arithmetic, and everything then works as it should.
The Limit_Mix() function subtracts 2000 from its result, neatly compensating for the extra 2000 in its input.
|
OK. I forgot about the 8 bit quirk. Adding the 2000 bias does circumvent this trap, but it's an awfully kludgy way to do it. I assert the following would be clearer: (Assuming that all the definitions remain eight bits each.)
Code:
pwm01 = pwm03 = Limit_Mix((int)PWM_in1 + PWM_in2 - 127); /* LEFT WHEELS */
pwm02 = pwm04 = Limit_Mix((int)PWM_in2 - PWM_in1 + 127); /* RIGHT WHEELS */