Package nxt :: Module compass

Source Code for Module nxt.compass

 1  # nxt.compass module -- Classes to read Mindsensors Compass sensors 
 2  # Copyright (C) 2006  Douglas P Lau 
 3  # Copyright (C) 2009  Marcus Wanner 
 4  # 
 5  # This program is free software: you can redistribute it and/or modify 
 6  # it under the terms of the GNU General Public License as published by 
 7  # the Free Software Foundation, either version 3 of the License, or 
 8  # (at your option) any later version. 
 9  # 
10  # This program is distributed in the hope that it will be useful, 
11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
13  # GNU General Public License for more details. 
14   
15  import sensor 
16  from time import sleep 
17   
18 -class Command(object):
19 'Namespace for enumeration compass sensor commands' 20 # NOTE: just a namespace (enumeration) 21 AUTO_TRIG_ON = (0x41, 0x02) 22 AUTO_TRIG_OFF = (0x53, 0x01) 23 MAP_HEADING_BYTE = 0x42 # map heading to 0-255 range 24 MAP_HEADING_INTEGER = 0x49 # map heading to 0-36000 range 25 SAMPLING_50_HZ = 0x45 # set sampling frequency to 50 Hz 26 SAMPLING_60_HZ = 0x55 # set sampling frequency to 60 Hz 27 SET_ADPA_MODE_ON = 0x4E # set ADPA mode on 28 SET_ADPA_MODE_OFF = 0x4F # set ADPA mode off 29 BEGIN_CALIBRATION = 0x43 # begin calibration 30 DONE_CALIBRATION = 0x44 # done with calibration 31 LOAD_USER_CALIBRATION = 0x4C # load user calibration value
32 33 # I2C addresses for a Mindsensors CMPS-Nx compass sensor 34 I2C_ADDRESS_CMPS_NX = { 35 0x41: ('command', 1, True), 36 0x42: ('heading_lsb', 1, False), 37 0x43: ('heading_msb', 1, False), 38 0x44: ('x_offset_lsb', 1, True), 39 0x45: ('x_offset_msb', 1, True), 40 0x46: ('y_offset_lsb', 1, True), 41 0x47: ('y_offset_msb', 1, True), 42 0x48: ('x_range_lsb', 1, True), 43 0x49: ('x_range_msb', 1, True), 44 0x4A: ('y_range_lsb', 1, True), 45 0x4B: ('y_range_msb', 1, True), 46 0x4C: ('x_raw_lsb', 1, True), 47 0x4D: ('x_raw_msb', 1, True), 48 0x4E: ('y_raw_lsb', 1, True), 49 0x4F: ('y_raw_msb', 1, True), 50 } 51
52 -class _MetaCMPS_Nx(sensor._Meta):
53 'Metaclass which adds accessor methods for CMPS-Nx I2C addresses' 54
55 - def __init__(cls, name, bases, dict):
56 super(_MetaCMPS_Nx, cls).__init__(name, bases, dict) 57 for address in I2C_ADDRESS_CMPS_NX: 58 name, n_bytes, set_method = I2C_ADDRESS_CMPS_NX[address] 59 q = sensor._make_query(address, n_bytes) 60 setattr(cls, 'get_' + name, q) 61 if set_method: 62 c = sensor._make_command(address) 63 setattr(cls, 'set_' + name, c)
64
65 -class CompassSensor(sensor.DigitalSensor):
66 67 __metaclass__ = _MetaCMPS_Nx 68
69 - def __init__(self, brick, port):
70 super(CompassSensor, self).__init__(brick, port) 71 self.sensor_type = Type.LOW_SPEED_9V 72 self.mode = Mode.RAW 73 self.set_input_mode() 74 sleep(0.1) # Give I2C time to initialize
75 76 CompassSensor.get_sample = CompassSensor.get_heading_lsb 77