A HiTechnic compass, RobotC, and 100 lines of code…
Saturday, September 2nd, 2006There’s been a lot of great coverage on HiTechnic’s new compass sensor - the first product offered as the result of the company’s collaboration with the LEGO Group. But since the product has not yet officially hit the store shelves, an NXT-G block is not available from HiTechnic to read the compass bearing. The workaround thus far has been to use the standard NXT-G ultrasonic sensor block to retrieve a single byte (at 0×42) representing half the bearing and multiplying that value by two. Unfortunately, this means that we lose some precision in our measurement.
To see HiTechnic’s compass truly shine, I decided to create a simple test program in RobotC to communicate with it via the I2C protocol and concurrently read the first byte at 0×42 and the odd/even byte at 0×43. The result is the full bearing range from 0-359 degrees.
For every value retrieved at 0×42 (0-179), there is a value of 0 and 1 at 0×43. Thus, for the even byte, the bearing equals the value in 0×42 multiplied by two. For the odd byte, the bearing equals the value in 0×42 multiplied by two then one added to the result. Cool? Cool!
See the source code. The program reads the bearing from the compass and outputs it to the LCD on the intelligent brick. To use in a more sophisticated robotics application, the ReadBearing() function can be dropped right into its source code - provided the ports, addresses, and sensor type are defined. Enjoy!
Posted in hardware, development | No Comments »