February 22nd, 2007

C#: Programming Made Fun

I realize I’m rather off the schedule I had planned. I blame the holiday weekend, my laziness, and Robocup. The international qualification deadline is at midnight on February 22nd. For those of you unaware of what the date is, that is in less than 24 hours (it’s 2:15am at the moment). That means it is somewhere close to crunch time, which means that I’ve been working. The new look for the RFC Cambridge site was rolled out tonight, and I’ve been helping with the team description paper (both writing sections, and proofreading it). With that out of the way, on to the actual topic of this post…

C# is one of the primary languages supported with the .NET Framework. C# is a strongly typed, managed language. This means that the language runtime is responsible for memory allocation and deallocation (garbage collection is the technical term). This saves a lot of hassles and helps to reduce (but not eliminate) memory leaks (memory that has been allocated, but is no longer pointed to, but not deleted). The .NET Framework is also important, as it provides C# with a powerful class library.

C# also has the capability of using essentially the entire power of C++ through something called Platform Invoke (or PInvoke for short). PInvoke allows you to call essentially arbitrary C++ (with a few requirements for how it is written) from within C#. This is hugely powerful, as you can access unmanaged memory, and do operations that are inherently more efficient in C++ than in C#. There are also a few features of C++ that aren’t available in C# (such as memory mapped files) which are useful.

Another powerful capability of C# is called reflection. Reflection is essentially the ability to examine and modify objects at runtime. This allows you to determine the capability of an object, and even to extend it programmatically, at run time, rather than at compile time. C# can also programmatically invoke a C# compiler (through the .NET framework) which can allow for things like dynamically compiled modules, and other interesting features. With Reflection, you can then discern properties of the runtime compiled code, and integrate it into an existing system. This allows for a theoretically powerful plugin system, which can at the same time be limited for security purposes. When compiling code at runtime you can specify the assemblies (code components more or less, some of these terms are almost another post in themselves). This can lead to some very interesting ideas (an XML Parsing system based solely on object structures, or an easily extensible editor are a couple that I’ve had), and allows for very powerful capabilities.

The last, and one of my favorite features of C# (well, more correctly Visual Studio) is the GUI (Graphical User Interface, ie what you see) designer. I originally began programming with Visual Basic (save the hate, I’ve moved on to “better” languages), and I always enjoyed the ease of building a GUI with Visual Basic, even if the language behind it was a little quirky. It was actually common to use Visual Basic to build a GUI, and use COM (Component Object Model, a rather nasty complicated Microsoft technology) to hook up C++ code for the actual program component. With C# you don’t have to go that far, as the GUI designer is part of C# as well.

This is getting long enough, even though there is a lot more to cover. I guess this will have to become a multiple part series (to be continued on Friday I suppose). I’ll close with a link to Visual C# Express Edition, which is a free fully featured C# IDE (Integrated Development Environment, I need a little extension to let me just link to wiki pages for these) for Windows. It’s fun to play with, and gives you most of the power of Visual Studio 2005 for free.

February 16th, 2007

RFC Cambridge Facelift

This isn’t what I intended to write about, but since it’s been my work this evening, I might as well. If you click here you can get a sneak preview of the new RFC Cambridge homepage. It’s a moderate redesign of the existing, but it converts everything to PHP (no more copy and paste and edit content!), with mod_rewrite hiding the ugly URLs. There are also a handful of new photos, the new sponsor rotor, and a bunch of other little tweaks (properly sized thumbnails make a big difference) that just make the page more webbish. It’s also almost all XHTML 1.0 Transitional compliant (I haven’t tested every single page, so I can’t quite say all, but all of the pages linked in the header are!). I think it looks a lot better (even though it’s very similar), and it appears that the rest of the team is happy.

Apparently, the way to get a website changed isn’t through drastic changes, but through small changes that everyone can agree on. I have yet to activate this as the actual full homepage, but that won’t take much (putting in one mod_rewrite rule). I just want to give it a bit of time for feedback from the team, and for anyone to find any links I haven’t updated to use the .rfc page (even though .htm will work, I don’t want to leave links floating). I still need to figure out if I want to add some breadcrumbs for navigation or anything, but I don’t think the site is complex enough to warrant it just yet. Maybe when we get a bit more of a network effect going.

February 14th, 2007

RFC Cambridge

RFC Cambridge is a Robocup team that is a joint effort between the MIT Competitive Robotics Club, and the Harvard College Engineering Society. We’re in our second year competing, with the international deadline coming up soon. For RFC, I’ve done a variety of different things. Last year, my largest single contribution was the refbox. Robocup uses a serial connection to send referee commands to each team. I developed a drop in component for our AI system that could read the commands from a serial port asynchronously. To avoid reinventing the wheel, it was composed of two subsystems. One was the serial reader, which read from the serial port, and placed any received values into a memory mapped file used as a buffer. The serial reader was in Managed C++, and used the Microsoft .NET serial reader class. Inside the actual AI was an unmanaged C++ component which read from the same memory mapped file buffer to read the commands.

Strictly speaking, the design was non-thread safe, and avoided issues mainly by having a guarantee that the input data could not exceed the size of the buffer, to cause a wrap-around race condition, and that each thread would never be in the same area. While not the best design, it was sufficient for our needs. Since the AI was reading the buffer every 33ms (or so), and there was no way possible that more than 255 commands would arrive in that 30ms, there was never an issue.

Another major unused contribution was a new pattern recognition system that was part of the vision module. This was to have been used to detect the location and orientation of each robot, using a “butterfly” pattern, rather than the much simpler two dot pattern that our team used. However, because of time constraints (it was written about 3 days before the competition, and fully integrated the day before the team left) it was not used at the international competition.

I also developed a small control system that allowed users to drive our robots with an Xbox 360 gamepad. This was useful for non-autonomous demos, as it allowed more effective control of the robot. It was done over the course of about two days, using C#, and a third party wrapper for the XInput technology with the Xbox 360 Gamepad for PC. It worked quite well, and was rather popular with people when we were doing demos at the MIT Activities Midway, as we could allow students to drive the robots, without any real training (one thumbstick to drive, the triggers to rotate), and it was fairly intuitive.

This year, I’ve been doing bits here and there, and will probably redevelop the 360 control system for our new platform (which is quite a change, but I’m not sure I can post about it just yet). Otherwise, I’ve mostly become the web guy. This is mostly because I know how to get at the servers (I didn’t make it complicated, that was someone else), and I’m one of the few people who has a lot of web development experience (4 years, including 2 years profesionally). This is the latest page I’ve done, which uses Lightbox, a rather neat little set of Javascript to display full size images as an overlay on the current page. I also redid the HTML for the main site, to replace a mess of tables from Dreamweaver with a CSS version of the same thing. It reduced the page size by about 50%, and also makes it much easier to read. I’m planning to port the existing content into some sort of script, so that changing one link doesn’t require editing every page, but that is a bit off yet (I need to sort out what the web server actually runs).