Thought for the Dazed

I've had to give up that Distance Learning course as I was having trouble seeing the teacher.

Flickr
www.flickr.com
RobMiles' items Go to RobMiles' photostream
Twitter
C# Yellow Book

Search entire site
Wednesday
Feb102010

First Open Day of 2010

4345696377
Another satisfied customer wins a PSP in our Open Day draw from Mike. With a copy of FiFa you can even play as Hull City….

We had a bunch of people up to see us today. Thank you very much for coming folks, we hope you found the visit worth your journey. This is the time of year when we start to get busy, we have a bunch of open days over the next few weeks.

Wednesday
Feb102010

Processing Lots of Files in C#

4346439556

Elliot came to see me today with a need to process a whole bunch of files on a disk. I quite enjoy playing with code and so we spent a few minutes building a framework which would work through a directory tree and allow him to work on each file in turn. Then I thought it was worth blogging, and here we are.

Finding all the files in a directory

The first thing you want to do is find all the files in a directory. Suppose we put the path to the directory into a string:

string startPath = @"c:\users\Rob\Documents";

Note that I’ve used the special version of string literal with the @ in front. This is so my string can contain escape characters (in this case the backslash character) without them being interpreted as part of control sequences. I want to actually use backslash (\) without taking unwanted newlines (\n)

I can find all the files in that directory by using the Directory.GetFiles method, which is in the System.IO namespace. It returns an array of strings with all the filenames in it.

string [] filenames = Directory.GetFiles(startPath);
for (int i = 0; i < filenames.Length; i++)
{
   Console.WriteLine("File : " + filenames[i]);
}

This lump of C# will print out the names of all the files in the startPath directory. So now Elliot can work on each file in turn.

Finding all the Directories in a Directory

Unfortunately my lovely solution doesn’t actually do all that we want. It will pull out all the files in a directory, but we also want to work on the content of the directories in that directory too. It turns out that getting all the directories in a directory is actually very easy too. You use the Directory.GetDirectories method:

string [] directories =
          Directory.GetDirectories(startPath);
for (int i = 0; i < directories.Length; i++)
{
    Console.WriteLine("Directory : " + directories[i]);
}

This lump of C# will print out all the directories in the path that was supplied.

Processing a Whole Directory Tree

I can make a method which will process all the files in a directory tree. This could be version 1.0

static void ProcessFiles(string startPath)
{
   Console.WriteLine("Processing: " + startPath); 
   string [] filenames = Directory.GetFiles(startPath); 
   for (int i = 0; i < filenames.Length; i++)
   {
      // This is where we process the files themselves
      Console.WriteLine("Processing: " + filenames[i]); 
   }
}

I can use it by calling it with a path to work on:

ProcessFiles(@"c:\users\Rob\Documents");

This would work through all the files in my Documents directory. Now I need to improve the method to make it work through an entire directory tree. It turns out that this is really easy too. We can use recursion.

Recursive solutions appear when we define a solution in terms of itself. In this situation we say things like: “To process a directory we must process all the directories in it”.  From a programming perspective recursion is where a method calls itself.  We want to make ProcessFiles call itself for every directory in the start path.

static void ProcessFiles(string startPath)
{
  Console.WriteLine("Processing: " + startPath); 

  string [] directories = 
                  Directory.GetDirectories(startPath); 
  for (int i = 0; i < directories.Length; i++)
  {
    ProcessFiles(directories[i]);
  }

  string [] filenames = Directory.GetFiles(startPath); 
  for (int i = 0; i < filenames.Length; i++)
  { 
    Console.WriteLine("Processing : " + filenames[i]); 
  }
}

The clever, recursive, bit is in red. This uses the code we have already seen, gets a list of all the directory paths and then calls ProcessFiles (i.e. itself) to work on those. If you compile this method (remember to add using System.IO; to the top so that you can get hold of all these useful methods) you will find that it will print out all the files in all the directories.

Console Window Tip:  If you want to pause the listing as it whizzes past in the command window you can hold down CTRL and press S to stop the display, and CTRL+Q to resume it.

Tuesday
Feb092010

Best Laid Plans of Mice and Robots

4344787264
Picture posed by model.

So, I had this plan to make a line following robot. I’d even figured out how to do the lines. The idea was to print track segments on A4 pages, and then laminate the pages and lay them on the floor. The robot would go from page to page and it would all work.

That was the plan.

So I drew out some track segments in Photoshop Elements, printed them out and then laminated them. Then, and only then,  (and this is the stupid part) did I get round to testing my track design with the robot sensors. Turns out that the line sensors can see white paper really well. Really, really well. I get 99% reflection when I put the robot on the white parts of the paper. Only problem is, I also get 99% reflection when I put the robot on black parts. This is not a problem with the laminate,  it seems that, as far as the sensor is concerned, black is most definitely not black. Robot fashion shows must be awfully dull.

Anyhoo, my line following plans were in danger of not being plans any more. Finally I had a brainwave. Carpet seems to give me only around 20% reflection, even through plastic. So I’ve now made some track parts that just have transparent sections where the dark bits should be. You can just see how this works in the corner section above. The green LED is lit because the sensor at the far side of the robot is on the white part.

And now I’ve hit another snag. The robot runs rather roughshod over the path sections. I was hoping they would be heavy enough to stay put when the robot goes over them, but it seems that Jason is rather heavy footed, and messes them up. I’ve now got to find some velcro, or something that will stop them getting moved around as the robot travels over them.

That’s for tomorrow though, I’m off to bed now.

Monday
Feb082010

A “Bustlectomy” for Jason the Micro Framework Robot

4341034089

I did a bit of surgery on my Fez Micro Framework Robot yesterday. I took off his “bustle” at the back. I say his bustle, because my Micro-Framework robot is now called Jason, in honour of the JSON framework I’m working on to give him simple two way communication with a host machine.

The bustle was fine, but it hung out over the back a bit, and I wanted to make him (it?) a bit leaner and meaner. The new slim line Jason has all the sensors on his nose. He has a range finder right at the front and a pair of line followers underneath and coloured LEDs he can use to tell the world how he feels. At the moment he zooms around the living room nearly bouncing off things. I must admit it is great fun building him and writing programs to control what he does. You can write any number of desktop apps, but there is something very satisfying about seeing your code make the robot rush up to a wall, notice it, spin round and then vanish under the sofa. I’ll put up some more construction details later, when I’ve finished playing…

4341036295

Sunday
Feb072010

Kodu Rocks

Daisies

I must admit I was a bit underwhelmed when I first saw Kodu. At first glimpse I couldn’t see how it would help to teach people how to program.

It turns out that this is because it is not really a teaching tool as such.   The point that I seemed to miss was that the intention was to put people in touch with the experience of making a machine do a fairly complex task under their control. Rather than teaching programming, they were aiming to teach the joy of programming. Then, with a bit of luck, folks who find this fun will move into more formal ways of making this happen and turn to real coding.

I downloaded the free Kodu Technical Preview which runs on the PC (you can also get the program on Xbox Live for 400 credits) and had a play. It is great fun. In no time at all I had created a world and had my little creature running round after the ball and picking it up.  I want to have another go with this.

I can see this being one of those things you show your kids and then after a while they will grab the gamepad and kick you off the machine so that they can have a go at finding all the things you can do with the environment. At first I was comparing the system with Little Big Planet, which also offers a way you can build your own worlds, but I think Kodu is better in this respect. It is presented from the start as an environment where you create behaviours, rather than as a platform game you can add things to.

From a teaching perspective it is great in that it gets you thinking about a program as a sequence of actions and decisions, and that is fine by me.