Step 12Steering, and other loose ends.
Outputting to the motors is done by setting a pulse width modulated (PWM) signal on the motor controller inputs. In Arduino land, this is done with analogWrite(). However, the default Arduino PWM frequency is way too low for motor control, so I added some direct register manipulation to force it to be 15.625kHz. Since the controllers are reversible, I use a small forest of if statements to figure out what to put on the DIR pins and whether or not to invert the output command.
One very important part of writing good control software is managing data types. You can see in the code that I often explicitly typecast to make sure I'm getting exactly the data type I want at every step of the calculation. Nothing is worse than having your controller freak out because a variable overflowed. For that reason, I also apply limiting at each step to ensure that the variables stay within appropriate ranges. For example, analogWrite() takes an integer value between 0 and 255, so I limit the outputs to this range at every step of the calculation.
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|






















































