API in c


Can anybody direct me to an API for the UltraBorg in 'c' for Raspbian?

Thanks in advance, and apologies if I missed it off the menu somewhere.

piborg's picture

Unfortunately we do not, but I can point you in the right direction for making one.

There is some C code in our Arduino example for the UltraBorg:
UltraBorg Arduino library and example sketch
This could be adapted to run on Raspbian reasonably easily.

What you will need to do is replace the calls to the Wire library with calls to the standard Linux I2C driver.
These can be included with :

#include <linux/i2c-dev.h>
#include <linux/i2c.h>

The functions you will need to use are:

  • open to access the I2C device for talking
  • ioctl to set the I2C address of the UltraBorg
  • write to send data to the UltraBorg
  • read to get data from the UltraBorg

As a reference we do have a C library for XLoBorg here: XLoBorg C library
This uses all of the calls mentioned above to communicate with a board via the I2C device driver.

Thanks for the advise. I have looked at the two API's you suggest. However the time for this project is not very long, and my C skills are not quite up to the standard of completing this task in the time I have.

All I need is a function to set servo #1 to min/max extent, on default address. May I ask if anybody might have a minimal code example just to complete this function?



piborg's picture

Unfortunately we do not have any examples already coded.

I have put together a crude C example which should work, but i do not have time to test it this week.
I would recommend testing the UltraBorg is working using the standard Python examples first, then see if you can get the C example to work.


I have expanded your example into a larger set of commands for reading/writing to the servo's and USM. This does not contain the calibration functions, but may be useful to any members wanting a very simple c API to UltraBorg.

piborg's picture

That is great, I am glad to hear the example proved useful.

I will point anyone else who asks about a C library to your version :)

Compiling on a RPi 2 gives persistant errors. Looks like 'sys/ioctl.h' is missing (even the folder include/sys seems to lack ). Besides a lot of warnings (all on data types changes), there are also 2 errors on redefining the structs in i2cdev.h.

I tried to rewrite this using wiringPi but I'm lackking to much insight in how the board commands and messages work, what is exactly returned, etc. I feel at ease in Arduino and Python, but this is to heavy for me to get it figgered out.

I really would like to use a c/cpp lib: I've build a balancing bot with core functionality on Arduino and high level control on RPi. I've used 2 ultraborgs for reading 8 HC-SR04's (input for an obstacle avoiding algorithm). The Python interpreter turns out to be to slow for image (openCV) and sensor handling. So I would like to switch to Cpp, but am stuck with python by the ultraborg. Very gratefull if anyone could help or hint just to port the usm-functions to C++ !!

piborg's picture

That is a bit troubling, sys/ioctl.h is a fairly essential header for file based I/O in Linux, most low-level examples would need to use it.

This is just a hunch, but I would guess that the C libraries and their various bits are not fully installed. If this is the case it may well explain some of the other odd errors as well.

This is the command I would usually run on a Debian based install, so it will probably work on Raspbian as well:

sudo apt-get install build-essential

ThnX for helping! I get: "Build-essential is already the newest version". reinstall of libc6-dev also makes no differance. ioctl.h is avilable in (/usr/include/) asm-generic, arm-linux-gnueabihf/sys, arm-linux-gnuabihf/asm, linux/hdcl, linux/mmc and linux. I'm using the latest Raspbian Jessie. Made a fresh install on another sd-card, but the /usr/include/sys folder still lacks. ;-(

piborg's picture

Had another hunt, but I cannot see why this would be occurring.

If I had to guess I recon the file at /usr/include/arm-linux-gnueabi/sys/ioctl.h is the right one as it seems to belong to the libc6-dev package on standard Debian.

I would try replacing:

#include <sys/ioctl.h>


#include <arm-linux-gnueabi/sys/ioctl.h>

and see what happens.

My best guess here is that /usr/include/arm-linux-gnueabi is supposed to be on the system include path, but for some reason it is missing.

ThnX again. Really appreciated! I had the same hunt, but that didn't work either. Also tried apt-get upgrade gcc (takes some time), and apt-get -- reinstall install libstdc6++-4.6-dev. Looks like I touched something somewhere for now I'm back at my former problem with UltraBorg.py : both the adresses are found, but return FF as id iso 36. So I'll bake a new sd-build tomorrow and start thinking on how to fix 8x a voltage devider on the balancer. The programming of the Usm's in c++ won't take as much time as I spent on the UltraBorgs up to now. Might I stumble on a solution for this topic sometime, I will add it to this blog.

Subscribe to Comments for &quot;API in c&quot;