Difference between revisions of "Bots-hitechnic-compass"

From Earlham CS Department
Jump to navigation Jump to search
(HiTechnic compass)
Line 1: Line 1:
 
Back to [https://wiki.cs.earlham.edu/index.php/Robotics-2010 Robotics Main Page]
 
Back to [https://wiki.cs.earlham.edu/index.php/Robotics-2010 Robotics Main Page]
 
----
 
----
=== HiTechnic compass ===
+
== HiTechnic compass ==
 +
[http://cs.earlham.edu/~leemasa/robotics/hicompass.py hicompass.py]
 +
 
 
* Detailed design and usage information for the HiTechnic [http://www.hitechnic.com/cgi-bin/commerce.cgi?preadd=action&key=NMC1034 compass sensor]
 
* Detailed design and usage information for the HiTechnic [http://www.hitechnic.com/cgi-bin/commerce.cgi?preadd=action&key=NMC1034 compass sensor]
* Because of the way the registers are mapped you can use the <code>UltrasonicSensor</code> class to work with this compass. The value returned by <code>get_sample()</code> is 1/2 the actual heading in degrees, that is you multiply the returned value by 2 to calculate the heading.
+
** Note: The description of how heading is stored in the sensor is incorrect. 0x44 points to the low byte and 0x45 points to the high bit. In other words, the formula for getting heading is (0x44) + 255*(0x45)
* Calibration is rarely necessary and shouldn't be an issue for us in the near-term.
+
 
* Example code:
+
 
#!/usr/bin/env python
+
=== Functions ===
# This example uses the "plain" UltraSonic (i.e. digital) sensor interface
+
Expect more functions in the near future
# with the HiTechnic compass sensor. The compass_heading() function
+
*CompassSensor(bot, port) - declares a HiTechnic compass object
# attempts to do the conversion to degrees (but omits the adder register
+
*get_manufacturer() - returns the eight-character string in the "manufacturer" block of the device (should be HiTechnc)
# as documented by HiTechnic).
+
*get_sample() - returns the heading measurement of the sensor in degrees clockwise from due North.
+
**Although bus errors wit the hicompass are relatively rare, get_sample() automatically tries again if it fails to get a heading.
import time
+
*calibrate_mode() - sets the compass to calibrate mode to correct for magnetic interference.
import nxt.locator
+
**see Calibrating the Compass below
from nxt.sensor import *
+
*measure_mode() - sets the compass to measure mode. The compass is in this mode by default
+
 
  def id_nxt(b):
+
=== Examples ===
name, host, signal_strength, user_flash = b.get_device_info()
+
Plug your compass into port four and place the following code somewhere in a program that already works with your robot
print 'NXT brick name: %s' % name
+
 
print 'Host address: %s' % host
+
  bot = (whatever variable you use to refer to your bot)
print 'Bluetooth signal strength: %s' % signal_strength
+
compass = hicompass.CompassSensor(bot,PORT_4)
print 'Free user flash: %s' % user_flash
+
print compass.get_manufacturer()
             
+
heading = compass.get_sample()
def compass_heading(b, p):
+
  while heading > 10:
# Because of limitations in the UltraSonic interface it returns 1/2 the
+
        heading = compass.get_sample()
# actual heading.
+
        print 'Heading: ', heading
return UltrasonicSensor(b, p).get_sample() * 2
+
 
   
+
This will tell the sensor to collect headings until it is within ten degrees East of due North.
print 'Looking for brick 1 ...'
+
 
sock = nxt.locator.find_one_brick(host='00:16:53:06:ED:B8', name='NXT')
+
=== Calibrating the Compass ===
+
#Assemble a mobile robot that can drive in a circle
if sock:
+
#*The size and precision of the circle is unimportant as long as the robot can make a full revolution
print 'Found brick 1'
+
#Set your compass sensor to calibrate mode and instruct your robot to drive in a circle
else:
+
#*The robot should make between one and a half and two revolutions in about twenty seconds and stop
print 'Timed-out, no NXT bricks found, exiting'
+
#Be sure to set your compass sensor back to measure mode when you are done!
exit
+
 
+
=== Notes ===
if sock:
+
The compass sensor is a very sensitive device. Here are some tips to make sure you get accurate readings
print 'Connecting to the brick ...'
+
*Keep the sensor level
bot = sock.connect()
+
*Make sure the sensor is steady. The more it bounces around the less accurate your readings will be
 
+
*Keep the sensor at least four inches from your NXT brick and six from your motors. They generate magnetic fields that will result in inaccurate readings.
if bot:
 
print 'Connected to the brick'
 
else:
 
print 'Timed-out connecting to brick 1, exiting'
 
exit
 
 
if bot:
 
id_nxt(bot)
 
 
while 1:
 
try:
 
print 'Compass heading: ', compass_heading(bot, PORT_4)
 
time.sleep(1)
 
continue
 
except:
 
time.sleep(1)
 
continue
 

Revision as of 10:36, 8 March 2010

Back to Robotics Main Page


HiTechnic compass

hicompass.py

  • Detailed design and usage information for the HiTechnic compass sensor
    • Note: The description of how heading is stored in the sensor is incorrect. 0x44 points to the low byte and 0x45 points to the high bit. In other words, the formula for getting heading is (0x44) + 255*(0x45)


Functions

Expect more functions in the near future

  • CompassSensor(bot, port) - declares a HiTechnic compass object
  • get_manufacturer() - returns the eight-character string in the "manufacturer" block of the device (should be HiTechnc)
  • get_sample() - returns the heading measurement of the sensor in degrees clockwise from due North.
    • Although bus errors wit the hicompass are relatively rare, get_sample() automatically tries again if it fails to get a heading.
  • calibrate_mode() - sets the compass to calibrate mode to correct for magnetic interference.
    • see Calibrating the Compass below
  • measure_mode() - sets the compass to measure mode. The compass is in this mode by default

Examples

Plug your compass into port four and place the following code somewhere in a program that already works with your robot

bot = (whatever variable you use to refer to your bot)
compass = hicompass.CompassSensor(bot,PORT_4)
print compass.get_manufacturer()
heading = compass.get_sample()
while heading > 10:
        heading = compass.get_sample()
        print 'Heading: ', heading

This will tell the sensor to collect headings until it is within ten degrees East of due North.

Calibrating the Compass

  1. Assemble a mobile robot that can drive in a circle
    • The size and precision of the circle is unimportant as long as the robot can make a full revolution
  2. Set your compass sensor to calibrate mode and instruct your robot to drive in a circle
    • The robot should make between one and a half and two revolutions in about twenty seconds and stop
  3. Be sure to set your compass sensor back to measure mode when you are done!

Notes

The compass sensor is a very sensitive device. Here are some tips to make sure you get accurate readings

  • Keep the sensor level
  • Make sure the sensor is steady. The more it bounces around the less accurate your readings will be
  • Keep the sensor at least four inches from your NXT brick and six from your motors. They generate magnetic fields that will result in inaccurate readings.