Project Description
This is a library that offers a .NET API to control all current Directed Perception and FLIR Pan/Tilt units through the serial port.

Announcement

I certainly wasn't expecting 160+ downloads in the first week of posting it. I'd appreciate it if you could leave a note explaining who you are in the Discussions page, I'm just curious.

Anyway, more importantly I'm currently working on Release 3 of the Bham.Ptu library. This adds ASCII command support, as well as a few other things. I've also reorganised a few member functions so Release 3 will be a breaking change, but hopefully not too big a problem to re-integrate.

I have posted my Qt 4.6 "PTLib" class library too. It isn't as complete or as tested as the C# version, but it should serve as a good starting point.

Solution Contents

The solution is developed in C# 3.0 (though it is 100% compatible with the .NET Framework 2.0) and is comprised of three projects:

Bham.Ptu - A class library.
Bham.Ptu.UI - A WinForms GUI that can be used to control the speed and position of the PTU, it also provides for rudiminentary joystick support.
Bham.Ptu.Console - A simple console application that runs through some quick tests. Largely obsolete.

There is also PTLib, a C++ library that makes heavy use of Qt and QSerialDevice. It is a separate download.

Using the library

When consuming the Bham.Ptu library you only need to use the PTUnit class. The PTConnection class is used internally by PTUnit, though you're free to use it if you need to send custom commands down the pipe.

Except where noted, all methods of the PTUnit class are non-blocking (that is, the PTU is expected to return a status code immediately) and should take no longer than 10ms to execute. The class is designed around a 1:1 mapping between methods and PTU operations. I opted for methods rather than properties because of their volatile nature and because the methods have side-effects. Feel free to wrap up PTUnit with a property-laden class if you wish.

If you want to use PTUnit with a PropertyGrid (as shown in Bham.Ptu.UI) then make use of the PTUnitState class, which stores a snapshot of the current state of the unit (though everything is read-only).

Some minor changes will need to be made to the PTConnection class if you're running this code on big-endian systems (i.e. Mono on PowerPC or ARM/RISC).

Code Sample

C#

if( PTUnit.GetFirmwareInfo("COM1") == null ) throw new ApplicationException("Device is not attached to COM1");

using(PTUnit unit = new PTUnit("COM1")) {
	
	unit.SetPanDesiredPosition( 0 );
	unit.AwaitCompletion();
	unit.SetPanDesiredPosition( 500 );
	unit.AwaitCompletion();
	unit.SetPanDesiredPosition( 0 );
	unit.AwaitCompletion();
}


VB

If PTUnit.GetFirmwareInfo("COM1") = Nothing Then Throw New ApplicationException("Device is not attached to COM1")

Using unit As New PTUnit("COM1")
	
	unit.SetPanDesiredPosition( 0 )
	unit.AwaitCompletion()
	unit.SetPanDesiredPosition( 500 )
	unit.AwaitCompletion()
	unit.SetPanDesiredPosition( 0 )
	unit.AwaitCompletion()
	
End Using

C++ / Qt

PTUnit* unit = new PTUnit( deviceName );

unit->setDesiredPositionAbs(Axis::Pan, 0);
unit->awaitCompletion();
unit->setDesiredPositionAbs(Axis::Pan, 500);
unit->awaitCompletion();
unit->setDesiredPositionAbs(Axis::Pan, 0);
unit->awaitCompletion();

delete unit;

Last edited Oct 20, 2010 at 10:25 AM by W3bbo, version 9