Showing posts with label Real Studio. Show all posts
Showing posts with label Real Studio. Show all posts

Tuesday, May 1, 2012

More about how to use GPSKit for Real Studio

While I was completing the GPSKit documentation, I felt like I may share the some bits of it on this blog. I hope you'll find them useful and feel free to leave a comment if you wish to.
Just as a reminder, GPSKit 1.0beta2 is downloadable here.

The design of GPSKit.

GPSKit is based on a separation of tasks design. They are 4 layers of them:

  1. The first layer is the hardware connection. It gets the data from the GPS device and spit out valid chunks of data 'tokenized' according to the protocol specifications. It's the zdGPSDataProvider interface.
  2. The second layer is the data validator and parser. It receives the tokens from the zdGPSDataProvider, check their validity and turn each of them into an object that represents the GPS raw data in a more human understandable way. It's the zdGPSDataListener interface .
  3. The third layer is a GPS fix factory. It uses the objects instances provided by the zdGPSDataListener to create a zdGPSFix instance and make it available to the entire application. it's the zdGPSFixProvider interface.
  4. As in any I/O protocol, errors may happen, so there is a fourth layer that will handle these errors and perform the actions needed in such case. It's the zdGPSErrorHandler interface.

In order to know where to send the data, the zdGPSDataListener needs to say hello, 'to register' itself to the zdGPSDataProvider. As the zdGPSFixProvider is meant to be implemented within the same class as the zdGPSDataListener, there is no need to register it. However, it isn't mandatory for the zdGPSDataListener to implement the zdGPSFixProvider as some protocols may not provide geolocation information.

Currently, only the NMEA 00183 protocol via a serial port is available in GPSKit. For this format, the zdGPSDataProvider layer is implemented by the zdNMEASerial class. The zdGPSDataListener and the zdGPSFixProvider are both implemented by the zdNMEAParser. The zdGPSErrorHandler is implemented by the zdGPSDeviceConnection.

Getting started.

If you need to get the geolocation data provided by the GPS device connected to a serial port, you'll have to create a data provider, a data listener that is also a fix provider, register the listener to the provider, store the references to all these class instances and finally open the connection. Fortunately, zdGPSDeviceConnection is here to automate this process. It implements a method with the following syntax:

  Sub BuildSerialConnection( _
        inProtocol As zdGPSDeviceConnection.Protocol, _
        inSerialPort As SerialPort)

Where inProtocol is one of the value of the zdGPSDeviceConnection.Protocol ('UserImplemented' and 'NMEA0183' are the only currently available values), and inSerialPort is the SerialPort instance the GPS device is connected to. It will handle the serial settings depending on the protocol specifications (e.g. 4800 bauds, 8N1 for the NMEA 0183).
Note: The 'UserImplemented' protocol is currently used for internal testing purpose only. This may change in a future release.

So to build and open a connection, proceed as following:

  Dim theConnection As New zdGPSDeviceConnection

  theConnection.BuildSerialConnection( _
        zdGPSDeviceConnection.Protocol.NMEA0183, _
        System.SerialPort( 0 ) )

  If theConnection.Open Then
    MsgBox "Connection is open"
  Else
    MsgBow "theConnection failed to open"
  End If

Of course, you'll have to adapt the code depending on the serial port you are using and set your GPS device interface type to 'NMEA 0183'.

If the connection is successful, you can access the

  zdGPSDeviceConnection.NMEAParser.GetLatestGPSFix() As zdGPSFix

to get the geolocation data. Don't forget to test for 'Nil' before using the returned reference.

Something you should be aware of.

The BuildSerialConnection() method will raise a zdGPSKitException if the passed serial port reference is 'Nil' or if the chosen protocol is 'UserImplemented'. In both cases, the Message property content explains, in english, why the exception has been raised.

Why using a more complicated design when a single class handling all the data processing would have been simpler?

While the format of data may be the same (e.g. NMEA 00183), the type of hardware connection may be different depending on the GPS device you're using (e.g. serial port, USB, even TCP/IP). So to avoid duplicate code for each connection type, a multiple layering with interfaces was chosen. Actually, the first reason was to allow the testing of the code by using a file that simulates the input of serial data. A little application, developed with Real Studio, was used to record the data coming from the GPS device along with the timing information to a binary file while driving a car. Back to the IDE, A class implementing the zdGPSDataProvider interface was used to replay the flow of data with the same timing. The application and the replay class are not included in the current release because they really can use some refactoring, but may be in another release...

How GPSKit was developed.

Two Garmin GPS receivers are used to develop GPSKit: A GPS 72 and an eTrex H. A Nokia Navigator 6110 cell phone was also used via Bluetooth as well as some serial data from a Garmin Rhino 130 and a GPS III previously recorded to binary files.

The code was written and tested with Real Studio 2009r4 ( Enterprise edition ) on a MacBook 13". A HP nw9400 laptop and a Acer Aspire One ZG5 notebook are used to test on Windows XP. The Acer notebook is equipped with A 32GB SSD disk and is mostly used to record the GPS serial data when driving a car.

One more thing...

Doing something else than driving when your vehicle is moving is very dangerous. Always park your vehicle safely before using a GPS device, a computer, a cell phone or any other kind of stuff that needs at least one of your hands and draws your attention away from driving or riding.

Friday, April 27, 2012

GPSKit 1.0b2: The first public release for Real Studio

After being away for a while, I'm back again with some hot news: The first public beta release of GPSKit for Real Studio is now available for you to test it. A zip archive containing the demo app and a draft of the the documentation is available here.

The set of classes comes within a demo app and with a sketchy documentation that will be completed along the beta testing period. All  the classes are encrypted and their use is time limited: 10 minutes for a debug build and 5 minutes in a compiled app. An unlocking scheme will be implemented before the final candidate stage. The selling price hasn't been fixed yet.

To open a connection to your GPS device, create a new zdGPSDeviceConnection, pick a protocol from the zdGPSDeviceConnection.Protocol enumeration ( currently limited to NMEA 0183 ) and a reference to the SerialPort your GPS device is connected to. Then call the zdGPSDeviceConnection.BuildSerialConnection() method. You can see an example of how it works in the TestWindow.ConnectButton.Action event.

If you find a bug, a typo or think of a missing feature, feel free to create a report here. A subscription is required.
I'd be glad to hear any thought, critic or advice you may have about GPSKit, so don't hesitate to post a comment below.

The two top items on the GPSKit's to-do list are:
  1. Improve and complete the documentation to make it really useful.
  2. Implement the text output protocol.
They will be the main focus of the GPSKit v1.0b3 release. Stay tuned!

Tuesday, April 17, 2012

After a desert trip...

As I was in the South Moroccan Sahara desert for the last ten days to work with the TV production crew of the "Marathon des Sables" race, I had to temporarily pause the GPSKit development. But I found the opportunity to record some GPS NMEA data stream that should prove useful to test GPSKit in a more "real life" way.
I also got some pictures of our roaming technical setup:

  • 4 cameras using the Panasonic P2 cards to record video.
  • An Avid LANShare video server with 4 TB of storage.
  • An in & out media station
  • three video editing "suits"
  • A Satellite broadcasting station
I will make an entire post about it in a few days.

Tuesday, March 20, 2012

Embedded GroupBox on Windows ( Feedback #4946 )

While developing the GPSKit Demo application, I've been bitten by a cosmetic issue on Windows XP. It appears that a GroupBox embedded in another GroupBox will have its text displayed incorrectly on this platform. This is known as issue #4946. It has been reported since REALbasic 2008r4.1.

I'm currently developing with Real Studio 2009r4, but I suspect it's still present in the latest version as the report isn't closed. I managed to found a workaround for this issue but I can't report anything in Feedback due to an expired license. So I decided to share it here. The workaround is as simple as putting the following three lines of code in the Open event of the GroupBox

  #if TargetWin32 then
    Me.Parent = Nil
  #endif


And let's create a simple test project ( available here ) and see the result:


Obviously, The 'Orphan GroupBox' is the one that has been... orphaned in its Open event. Of course there is a bad side to this: you're losing the benefits of embedded controls: e.g. setting only the parent as invisible in order to hide all its content. But I'd rather do extra coding on my side than releasing a ugly and incomplete interface for the user.

So, having this fixed in GPSKit Demo, you can now find the version 1.0a2 build for Windows here and for Mac OS X ( universal build ) here. Do not forget to register on our bug tracker system if you're willing to report the issue(s) you've discovered.

Monday, March 19, 2012

State of the GPS Kit

After a restless race full of legal formalities, I manage to find some time to work on the GPSKit project. And I'm pleased to announce that GPSKit and its demo project are now entering the alpha stage of the development.

As you can see on the picture on the left, the demo is a simple project but it's meant to demonstrate the basic use of GPSKit and one of the provided controls. Actually, there is only one control implemented for now: the GPS constellation canvas which is a representation of the position in the  sky of the satellites that the GPS device antennae can 'see'. There will be another control which will be a representation of the Signal to Noise ( S/N ) ratio for each of these satellites. The protocol popup menu is disabled since the NMEA0183 protocol is the only one supported for now. If you click on the 'Disconnect' button or no new data is sent for more than 10 seconds, the data text color is set to red to warn you that the currently displayed fix is obsolete. As soon as new data arrive, the text display is set to black again.

If you're bold enough to test this first alpha build, at your own risk, you will find it here as a zip archive ( Only Universal Mac OS X for the moment... ). You'll need to connect a GPS device to your Mac so the system can see it as a serial port. Then set the protocol to NMEA0183 on your GPS device configuration menu and launch GPSKit Demo. Select the serial port in the upper left popup menu and click 'Connect'. after a few secondes you should be able to see the fix data and the constellation of satellites.

If you encounter any problem, I'd be happy to hear about it. You can use our online bug tracker available at this address: http://bugs.zaatardigital.com/. You need to register before reporting an issue or request a feature. Note that it hasn't been tested on Mac OS X 10.7.x AKA Lion... yet!

The next stage will be the release of the source code for the demo project and the encrypted classes, modules and controls that are part of GPSKit and the documentation to help you implementing GPS localization in your Real Studio project. There is still a lot of work to do, so I'm going back to the IDE right now.

Update: The issue on Windows is now fixed. See this post to download new builds.

Monday, March 5, 2012

Don't break on all exceptions

In a recent post on Real Software's blog, Paul Lefebvre wrote about some useful pragma commands. But there was one he didn't mention that I've found to be useful in many cases and which was introduced in the 2009r2 release:

#pragma BreakOnExceptions On | Off | Default

When I'm working on a project in the IDE, I always leave the 'Break On Exceptions' menu item checked. I'm the kind of programmer whom is afraid of missing a special and random exception condition running a project in the debugger. I just can't help it, even if it proved useful only once or twice since I'm using Real Studio ( REALbasic 2.1 actually ). The 'dark' side is that each time an exception is raised, the debugger kicks in. Even if this exception condition is expected and properly handled.

Let's see an example with a simple CreateFile() function that takes a FolderItem as parameter and returns a boolean that is True if the creation was successful, and False if it wasn't:

Function CreateFile(inFile As FolderItem) As Boolean

  // Create the binary stream
  Dim theStream As BinaryStream
  Try
    theStream = BinaryStream.Create( theFile )
  Catch theIOException As IOException
    // Do some logging
    System.DebugLog CurrentMethodName _
        + ": Failed to create " + theFile.ShellPath
    // Inform the caller that it failed
    Return False
  End Try
  // Write to the stream
  theStream.Write "Some data"

  // test for write error
  // [...]

  // Close the file.
  theStream.Close
  // the creation was successful
  Return True

End Function


If you're testing a batch process of dozens or hundreds of files for which some exceptions can occur, you don't want the debugger to kick in each time. So I'm doing this:

Function CreateFile(inFile As FolderItem) As Boolean

  // Create the binary stream
  Dim theStream As BinaryStream

  #pragma BreakOnExceptions Off '<--- added a pragma

  Try
    theStream = BinaryStream.Create( theFile )
  Catch theIOException As IOException
    // Do some logging
    System.DebugLog CurrentMethodName _
        + ": Failed to create " + theFile.ShellPath
    // Inform the caller that it failed
    Return False
  End Try

  #pragma BreakOnExceptions Default '<--- added a pragma

  // Write to the stream
  theStream.Write "Some data"

  // test for write error
  // [...]

  // Close the file.
  theStream.Close
  // the creation was successful
  Return True

End Function


With these pragma commands, the debugger won't show up whenever an IOException is raised during the BinaryStream creation attempt. And if you need to know about the file paths that failed to be created, you can list them in Mac OS X console, Windows' debugger or Linux's StdErr . You can even log the IOException.ErrorNumber property's value for more information on why it failed.

Now, by using this pragma wherever the code properly handles exceptions, you can concentrate on real unexpected exception conditions.

Friday, February 24, 2012

GPSKit For Real Studio

I was hoping to release the first beta tomorrow ( Saturday, 25th ) but the refactoring took longer than I thought due to an unexpected hardware issue. Tonight, all the GPSKit stuff will be ready. Unfortunately, the demo project still needs a heavy cleaning as do the documentation. So, the first beta is expected to be released later next week.

Photo by Asley Pomeroy - 2011
Let's have a sneak peek of what it can do for you:

Currently, GPSKit handles the NMEA0183 protocol via a RS-232 link. But the Text Out and the Garmin protocol are planned to be part of the final release and will be added during the beta test period.

If all you need for your Real Studio project is localization support, GPSKit provides a 'fix' in real time. In the GPS terminology, a fix is a set of data including the latitude, longitude, ground speed, altitude & vertical velocity ( in 3D fix mode ), heading, time stamp and data about the accuracy of the fix. This fix is generally refreshed once per second by the GPS device.

If your application needs more data, e.g. the satellites used for the fix, navigation information, alarm when approaching a waypoint, data about the waypoint, etc... it can access any of the NMEA0183 sentences provided by the GPS device in the form of a class and, of course in real time. Some models designed for boats even provide data about the autopilot... To spare you the time of learning all the NMEA0183 sentences, GPSKit provides classes to handle the most common sentences as object with real name properties, easier to understand than parameter indexes.

If you have any question, comment or suggestion, you're welcome.

Saturday, February 18, 2012

Playing With Helper Apps

Part 1: The Command Line Parameters.

For one of our Real Studio application, we choosed to create a helper app to handle serial communications and data parsing as a background process. In order to setup the Serial and the IPC path needed by the helper app, we decided that we will use command line parameters.

I wrote a few helper applications in the past, but I never have to use command line parameters as the IPC path they'll used was always known before compiling and that the setup parameters were send to the helper by using IPC. As I wasn't used to deal with command line parameters, I created a simple test app exposing the content of the Args() array in the ConsoleApplication.Run event and allowing me to play with it in a Terminal window.

The code is as following:

Function Run(args() as String) As Integer
  Print "--"
  Print "ArgsTester was launched with the following arguments:"
  Print " "
  // Print the content of Args()
  For i As Integer = 0 to Args.Ubound
    Print " Args( " + Format( i, "#00" ) + " ) = """ + args( i ) + """"
  Next
  Print " "
  Print "We're done."
  Return 0
End Function

That's it. I called the application 'ArgsTester' and build it.

After a few tests I noticed that, accordingly to Real Studio's documentation, the first element of the array always contains the path to the application. But, and that's not documented, the last element is always empty. There is a bug report about that: Feedback #15573. Feel free to add yourself. Also, it doesn't seem to  happen on Windows.

Now, the helper app needs some data from the main app in order to initialize itself:
  • A serial port and the settings for it.
  • A communication channel to talk with main app
  • The kind of protocol it should expect from the serial device
For the serial port setup, it'll use the '-s' ( or '--serial' ) followed by a comma separated value that will hold the serial parameter in this particular order:

SerialPortSystemIndex,Baud,BitNumber,StopBit,Parity[,CTS][,DTR][,XON]

The communication channel will be provided by an IPCSocket, so the options tag will be '-i' ( or '--ipc' ) and the value will be a Unix-style path to be used with the IPCSocket.Path property. The protocol will be set with the option tag '-p' ( or '--protocol' ) followed by the name of the protocol.

Let's try this with ArgsTester with the following command line:

argstester -s 0,6,3,0,0 -c /var/tmp/ipc.test.channel -p myProtocol

and see the result:


Pretty easy to handle from here, isn't it?

One last thing... In order to get rid of the last empty element on Mac OS X you can just do:

  #If TargetMacOS Then
    Call args.Pop
  #endif


In the ConsoleApplication.Run event before parsing the arguments.

To be continued...