Wallrus: Firmware

The component that actually allows us to control Wallrus’s motors and spray paint actuator is the PIC microcontroller. The microcontroller is responsible for keeping track of the state of the system, controlling the speeds that the motors run at, controlling the spray can actuator, and sending and receiving data from the host PC.

To implement this functionality, we began by basing our code on the Microchip C18 version of Brad Minch’s Lab 4 USB firmware for the PIC18F2455. We defined a number of USB vendor requests to get the current state of the system and set a desired state. The USB communication was actually quite straightforward: we send the desired state from the PC using an eight-byte DATA_OUT token and decode it on the PIC. When responding to a request to get the device’s current actual state, we simply send the state back to the PC using the eight-byte buffer available in responses to requests.

Control of the motors and solenoids is built around the PIC’s Timer0 overflow interrupt. We set that interrupt to fire at about 10 KHz. Each time it fires, we check to see whether either stepper motor’s position is scheduled to be changed. If it does, we run a function that determines the proper direction and the next state of that motor’s two coils. We also check to make sure that that motor is not at the end of its belt, which we determine by looking at the value of a limit switch we mounted to the bottom of the motor housing. In the interrupt handler function, we also determine the proper state of the spray paint actuator solenoids based on whether the carriage is moving and whether it’s scheduled to be on.

There is no real user interaction directly with the PIC: it receives all its commands directly from the host computer, and does no geometric calculations on its own. We designed the system in this way in the interests of simplicity and practicality: while it may have been possible to implement all the geometric functions we needed on the microcontroller, it would have to connect to a computer to receive path data anyway. Making the PIC’s firmware as simple as possible has paid off, as it has allowed us to refine our algorithms on the host computer without having to do much reprogramming and debugging of the microcontroller at all.