| Linux C Development |
Last Modified: 2007-03-23
This example is for all you Linux people that are exited about writing your own code to use the BrainStem who need some help setting up libraries, paths, and includes to use our open-source C Development structure while having the user's code in another location. The example is written in C and has only one line in the entire file that is specific to Linux (the line that defines the serial port). The rest uses all our cross-platform libraries to do the work of communicating with the BrainStem. In the example an SRF08 (or SRF10, SRF02 as they share the same I2C interface) sensor is queried for values and the values are printed on the screen.
The same pathing, library includes, and structure could be used for any of our library files or any that you write (and contribute!) This includes the Garcia robotics platform, etc.
The I2C bus can have many devices (including multiple BrainStem controllers) on it and each device has an address. Here we use an SRF08 Ultrasonic Ranger as our candidate device but this could easily be a compass, EEPROM, another Stem, or other I2C capable devices. In addition, all could be used in various combinations at the same time for a more complicated system.
A library routine for the SRF08 encapsulates most of the work and within that routine, the BrainStem packet of bytes requesting a sensor reading is assembled and shipped. The BrainStem hardware does the work of pushing the data across the bus to the sensor. The sensor then manages the readings and later, after a delay, the BrainStem library code reads back the sensor's newfound data and the example displays the data.
Instead of just printing the data, the C code in the example could be extended to stuff the data in to a database, filter it, send it along to some huge expensive software package like MatLab, LabView, or your (or perhaps your professor's) favorite religion.
The structure of the code is pretty simple. It first establishes connections to a couple of shared libraries that we will be using (aIO and aStem). These libraries manage the basic cross-platform I/O in one case and the packet structure and communication with the BrainStem over a link abstraction in the other case.
Once these are established, we use one to create a link abstraction from a serial port (again, this is the only platform specific call because of the serial port naming). This link abstraction is then used by the aStem library for communications with the BrainStem.
Next, we just loop a few times and read the data from the sensor printing the results as we go.
Finally, we clean up after ourselves and exit.
Using the Example
The example consists of three files. A readme.txt that details most of what you need to know to get started, a makefile that can be used as a template for other projects you might want to pursue, and the example.c file which is the actual code.
This example uses the Acroname C Development Download from the download center (linked on every website page in the jump bar under the logo). You need to download this and build it (instructions included in download) to do this example.
Here is the code you will need in addition to the C Development Download for Linux:
Things of Note
You may see errors on the first one or two readings. This is because the BrainStem needs constant heartbeat management to avoid dropping communications packets. Once the readings start happening, the heartbeat gets managed and the packets are no longer dropped. We manage this in the Garcia API using a seperate thread to manage the serial communications. This is way beyond the scope of this example but it is something to note.
You could substitute a Moto 1.0 for the GP 1.0 used in this example quite easily. You would only need to change the constant aMODULE in the C code to reflect the I2C address of the BrainStem module you are using.
The SRF08, SRF02, and SRF10 all use the same interface so they could all be used in this example.
We have libraries for many other devices we offer that sit on the I2C bus. You can readily use these (they are included in the C Development download under "brainstem/aCommon" and you can use them to model interactions with other I2C devices you may have that we don't have libraries for. If you do write up another routine, submit it and we will include it (with proper credit) in our downloads for the benefit of others. Remember, this is open-source!
| voice: 720-564-0373, email: email@example.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy |
© Copyright 1994-2012 Acroname, Inc., Boulder, Colorado. All rights reserved.