For the past year, I have been volunteering as a mentor for a robotics team at a nearby high school. This team has focused on building robots for the FIRST Robotics Competition for several years. The competition runs every year from January to April. For the first six weeks, each participating team builds a robot, roughly the size of a dishwasher, to compete in a game against robots from other teams. The game changes every year and can range from fast-paced basketball-like matches to precision-intensive construction challenges.
The latter game type was the one that I had the pleasure of working on with the team for the first time. For the six weeks of build time plus extra hours spent between our competitions, I helped the students and other mentors develop the electronics and software that made up the control system. Since this was a project targeted for high-school students, a lot of that involved taking pre-built modules provided by approved vendors and fitting them together into a system that allowed a pair of human operators to drive the robot.
Though the process of constructing this control system was relatively easy to manage, my small control systems group and I struggled with a problem that I have encountered before in previous projects: a lack of prototyping and testing techniques. The control system is firmly centered around a large, clunky, expensive controller board called the roboRIO of which our team had only two: one for the robot and one for backup. Destruction or loss of either one of these boards would be a huge blow. In addition, the software utilities and development tool suite were limited to Windows, which in my opinion is a terrible platform for learning how to program. These are only a couple of the factors that I believe resulted in our group having to wait weeks until the robot's hardware was finished before the software could be properly tested. All through the build season I yearned to break free of this suffocating environment.
After the robot was completed and the team returned from the competitions, I eventually found the time to ponder an alternative development system that would be suitable for prototyping robot code as well as teaching new students the basics of programming. I started with one basic requirement: that code written for the normal system should be able to run unchanged on the new system. That meant re-implementing the main control system C++ API, WPILib. An additional constraint that I adopted was that the robot's hardware should be relatively cheap and easy to modify and expand. After considering a number of educational robotic kits, I settled on the RedBot by SparkFun. The reasons for this choice include the platform's huge popularity, a plethora of plug-and-play sensors and actuators, Arduino compatibility, and affordability. The fact that it's design files have been open-sourced is another significant benefit.
After nearly three months of sporadic work, I'm finally ready to present an initial status report. Most of the time has been spent on the new implementation of WPILib. It is currently developed only for Linux, though a Windows port is at the top of the to-do list. At this point, only a small fraction of WPILib's interfaces are available for use, including digital output and input and driving capabilities. A lot of work went into the underlying architecture that runs the user's program on a PC base-station while communicating with the robot via a minimal serial protocol. The robot itself runs a simple control loop written in the Arduino language that gets packets from the base-station, pokes the necessary hardware, and spits packets back. More technical details on this architecture as well as plans for future development will soon follow in another post.
Monday, June 15, 2015
Thursday, January 1, 2015
Building an Instruction Set Simulator to Unit Test Assembly Code
In my last post, I described a basic technique for using the AVR simulator Simulavr to perform unit testing on assembly code. The code under test along with a minimal entry function were assembled by avr-gcc and loaded into Simulavr. The tests themselves were written in Tcl since Simulavr features an appropriate interface. I prefer to write unit tests in higher-level languages, anyways. Other cool features that Simulavr offers are a GDB interface to step through troublesome functions and cycle-level accuracy.
However, I didn't feel like that method could be used for much besides the very basic use cases I demonstrated in my examples. Simulavr only simulates a subset of AVR devices and its Tcl interface is not documented very well. In addition, I found Tcl's built-in unit testing framework awkward to use. These reasons plus a bit of Not Invented Here Syndrome led me to try putting together my own simulator. Because this project's primary purpose is to facilitate basic assembly code testing, cycle-level accuracy is not necessary. Leaving that out greatly simplifies the implementation.
I chose Python as the primary language because I am already familiar with it and it has a pretty good unit testing framework in the unittest package. Its object-oriented programming features also make it easy to build the simulator, which is currently made up of the following modules:
- System: Encapsulates the registers, program counter, and stack of a device.
- Instructions: Each instruction has its own class which derives from a base class that contains its location within the program. The classes each have their own unique execution methods that operate on a given instance of a system.
- Program: Contains a collection of instruction objects and map of labels to addresses. There are also functions here to build the instruction objects given an assembly code listing.
An implementation of this basic simulator along with some example assembly code, unit tests, and helper scripts are present here. The test-driven development flow used can be described as follows:
- Write some tests in a Python class that is derived from 'unittest.TestCase'.
- Implement the desired functionality in a new assembly function.
- Assemble the function into an unlinked object.
- Dump the contents of the object into a simple ASCII-based format that contains only labels and instructions in hexadecimal (this is accomplished with avr-objdump and an Awk script).
- Parse the listing and build a Program object.
- Build a System object and initialize it so that the given function runs a certain way.
- Use the System object to run the Program.
- Analyze the System object to determine if the function ran as expected. If it did, the unit test passes.
The example demonstrates the above flow with a function that adds the values in two registers and stores the result in a third register. The given unit test initializes the System's registers with two known values and compares the value of the third register to the expected sum to determine success.
The framework was relatively quick and easy to code up, but only a handful of instructions have been implemented so far. In addition, there is no ability to perform branching or subroutine calls. After those are taken care of, I want to build some more advanced testing functionality, like a method for mocking out specified subroutines. And, of course, this framework should be ported to and tested with different architectures, like MSP430.
Subscribe to:
Posts (Atom)