HiTechnic Sensors

Work in progress…

class nxt.sensor.hitechnic.Compass(brick, port, check_compatible=True)

Hitechnic compass sensor.

I2C_ADDRESS = {'adder': (67, 'B'), 'factory_scale_divisor': (19, 'B'), 'factory_scale_factor': (18, 'B'), 'heading': (66, 'B'), 'mode': (65, 'B'), 'product_id': (8, '8s'), 'sensor_type': (16, '8s'), 'version': (0, '8s')}
class Modes
MEASUREMENT = 0
CALIBRATION = 67
CALIBRATION_FAILED = 2
get_heading()

Returns heading from North in degrees.

get_sample()

Returns heading from North in degrees.

get_relative_heading(target=0)
is_in_range(minval, maxval)

This deserves a little explanation: if max > min, it’s straightforward, but if min > max, it switches the values of max and min and returns true if heading is NOT between the new max and min

get_mode()
set_mode(mode)
compatible_sensors = [<nxt.sensor.digital._SCompatibility object>, <nxt.sensor.digital._SCompatibility object>]
class nxt.sensor.hitechnic.Accelerometer(brick, port, check_compatible=True)

Object for Accelerometer sensors. Thanks to Paulo Vieira.

I2C_ADDRESS = {'all_data': (66, '3b3B'), 'factory_scale_divisor': (19, 'B'), 'factory_scale_factor': (18, 'B'), 'product_id': (8, '8s'), 'sensor_type': (16, '8s'), 'version': (0, '8s'), 'x_axis_high': (66, 'b'), 'xyz_short': (66, '3b'), 'y_axis_high': (67, 'b'), 'z_axis_high': (68, 'b')}
class Acceleration(x, y, z)
get_acceleration()

Returns the acceleration along x, y, z axes. 200 => 1g.

get_sample()

Returns the acceleration along x, y, z axes. 200 => 1g.

compatible_sensors = [<nxt.sensor.digital._SCompatibility object>, <nxt.sensor.digital._SCompatibility object>]
class nxt.sensor.hitechnic.IRReceiver(brick, port, check_compatible=True)

Object for HiTechnic IRReceiver sensors for use with LEGO Power Functions IR Remotes. Coded to HiTechnic’s specs for the sensor but not tested. Please report whether this worked for you or not!

I2C_ADDRESS = {'all_data': (66, '8b'), 'factory_scale_divisor': (19, 'B'), 'factory_scale_factor': (18, 'B'), 'm1A': (66, 'b'), 'm1B': (67, 'b'), 'm2A': (68, 'b'), 'm2B': (69, 'b'), 'm3A': (70, 'b'), 'm3B': (71, 'b'), 'm4A': (72, 'b'), 'm4B': (73, 'b'), 'product_id': (8, '8s'), 'sensor_type': (16, '8s'), 'version': (0, '8s')}
class SpeedReading(m1A, m1B, m2A, m2B, m3A, m3B, m4A, m4B)
get_speeds()

Returns the motor speeds for motors A and B on channels 1-4. Values are -128, -100, -86, -72, -58, -44, -30, -16, 0, 16, 30, 44, 58, 72, 86 and 100. -128 specifies motor brake mode. Note that no motors are actually being controlled here!

get_sample()

Returns the motor speeds for motors A and B on channels 1-4. Values are -128, -100, -86, -72, -58, -44, -30, -16, 0, 16, 30, 44, 58, 72, 86 and 100. -128 specifies motor brake mode. Note that no motors are actually being controlled here!

compatible_sensors = [<nxt.sensor.digital._SCompatibility object>, <nxt.sensor.digital._SCompatibility object>]
class nxt.sensor.hitechnic.IRSeekerv2(brick, port, check_compatible=True)

Object for HiTechnic IRSeeker sensors. Coded to HiTechnic’s specs for the sensor but not tested. Please report whether this worked for you or not!

I2C_ADDRESS = {'AC_direction': (73, 'B'), 'AC_sensor_1': (74, 'B'), 'AC_sensor_2': (75, 'B'), 'AC_sensor_3': (76, 'B'), 'AC_sensor_4': (77, 'B'), 'AC_sensor_5': (78, 'B'), 'DC_direction': (66, 'B'), 'DC_sensor_1': (67, 'B'), 'DC_sensor_2': (68, 'B'), 'DC_sensor_3': (69, 'B'), 'DC_sensor_4': (70, 'B'), 'DC_sensor_5': (71, 'B'), 'DC_sensor_mean': (72, 'B'), 'all_AC': (73, '6B'), 'all_DC': (66, '7B'), 'dspmode': (65, 'B'), 'factory_scale_divisor': (19, 'B'), 'factory_scale_factor': (18, 'B'), 'product_id': (8, '8s'), 'sensor_type': (16, '8s'), 'version': (0, '8s')}
I2C_DEV = 16
class DSPModes
AC_DSP_1200Hz = 0
AC_DSP_600Hz = 1
class DCData(direction, sensor_1, sensor_2, sensor_3, sensor_4, sensor_5, sensor_mean)
class ACData(direction, sensor_1, sensor_2, sensor_3, sensor_4, sensor_5)
get_dc_values()

Returns the unmodulated (DC) values.

get_ac_values()

Returns the modulated (AC) values. 600Hz and 1200Hz modes can be selected between by using the set_dsp_mode() function.

get_dsp_mode()
set_dsp_mode(mode)
get_sample()

Returns the modulated (AC) values. 600Hz and 1200Hz modes can be selected between by using the set_dsp_mode() function.

compatible_sensors = [<nxt.sensor.digital._SCompatibility object>, <nxt.sensor.digital._SCompatibility object>]
class nxt.sensor.hitechnic.EOPD(brick, port)

Object for HiTechnic Electro-Optical Proximity Detection sensors.

set_range_long()

Choose this mode to increase the sensitivity of the EOPD sensor by approximately 4x. May cause sensor overload.

set_range_short()

Choose this mode to prevent the EOPD sensor from being overloaded by white objects.

get_raw_value()

Unscaled value read from sensor.

get_processed_value()

Derived from the square root of the raw value.

get_scaled_value()

Returns a value that will scale linearly as distance from target changes. This is the method that should generally be called to get EOPD sensor data.

get_sample()

Returns a value that will scale linearly as distance from target changes. This is the method that should generally be called to get EOPD sensor data.

class nxt.sensor.hitechnic.Colorv2(brick, port, check_compatible=True)

Object for HiTechnic Color v2 Sensors. Coded to HiTechnic’s specs for the sensor but not tested. Please report whether this worked for you or not!

I2C_ADDRESS = {'all_data': (66, '9B'), 'all_raw_data': (66, '<4H'), 'blue': (69, 'B'), 'factory_scale_divisor': (19, 'B'), 'factory_scale_factor': (18, 'B'), 'green': (68, 'B'), 'index': (71, 'B'), 'mode': (65, 'B'), 'normblue': (74, 'B'), 'normgreen': (73, 'B'), 'normred': (72, 'B'), 'number': (66, 'B'), 'product_id': (8, '8s'), 'rawblue': (70, '<H'), 'rawgreen': (68, '<H'), 'rawred': (66, '<H'), 'rawwhite': (72, '<H'), 'red': (67, 'B'), 'sensor_type': (16, '8s'), 'version': (0, '8s'), 'white': (70, 'B')}
class Modes
ACTIVE = 0
PASSIVE = 1
RAW = 3
BLACK_CALIBRATION = 66
WHITE_CALIBRATION = 67
LED_POWER_LOW = 76
LED_POWER_HIGH = 72
RANGE_NEAR = 78
RANGE_FAR = 70
FREQ_50 = 53
FREQ_60 = 54
class ActiveData(number, red, green, blue, white, index, normred, normgreen, normblue)
class PassiveData(red, green, blue, white)
get_active_color()

Returns color values when in active mode.

get_sample()

Returns color values when in active mode.

get_passive_color()

Returns color values when in passive or raw mode.

get_mode()
set_mode(mode)
compatible_sensors = [<nxt.sensor.digital._SCompatibility object>, <nxt.sensor.digital._SCompatibility object>, <nxt.sensor.digital._SCompatibility object>, <nxt.sensor.digital._SCompatibility object>]
class nxt.sensor.hitechnic.Gyro(brick, port)

Object for gyro sensors

This class is for the hitechnic gyro sensor. When the gyro is not moving there will be a constant offset that will change with temperature and other ambient factors. The calibrate() function takes the current value and uses it to offset subsequent ones.

get_rotation_speed()
set_zero(value)
calibrate()
get_sample()
class nxt.sensor.hitechnic.Prototype(brick, port, check_compatible=True)

Object for HiTechnic sensor prototype boards.

I2C_ADDRESS = {'A0': (66, '<H'), 'A1': (68, '<H'), 'A2': (70, '<H'), 'A3': (72, '<H'), 'A4': (74, '<H'), 'all_analog': (66, '<5H'), 'digital_cont': (78, 'B'), 'digital_in': (76, 'B'), 'digital_out': (77, 'B'), 'factory_scale_divisor': (19, 'B'), 'factory_scale_factor': (18, 'B'), 'product_id': (8, '8s'), 'sample_time': (79, 'B'), 'sensor_type': (16, '8s'), 'version': (0, '8s')}
class Digital_Data(pins)

Container for 6 bits of digital data. Takes an integer or a list of six bools and can be converted into a list of bools or an integer.

tolist(val)
toint(lst)
class Analog_Data(a0, a1, a2, a3, a4)
get_analog()
get_digital()
set_digital(pins)

Can take a Digital_Data() object

set_digital_modes(modes)

Sets input/output mode of digital pins. Can take a Digital_Data() object.

compatible_sensors = [<nxt.sensor.digital._SCompatibility object>]
class nxt.sensor.hitechnic.SuperPro(brick, port, check_compatible=True)

Object for HiTechnic sensor SuperPro boards.

I2C_ADDRESS = {'analog_a0': (66, '<H'), 'analog_a1': (68, '<H'), 'analog_a2': (70, '<H'), 'analog_a3': (72, '<H'), 'analog_out0_freq': (83, '<H'), 'analog_out0_mode': (82, 'B'), 'analog_out0_volts': (85, 'H'), 'analog_out1_freq': (88, '<H'), 'analog_out1_mode': (87, 'B'), 'analog_out1_volts': (90, 'H'), 'digital_dir': (78, '<B'), 'digital_in': (76, '<B'), 'digital_out': (77, '<B'), 'factory_scale_divisor': (19, 'B'), 'factory_scale_factor': (18, 'B'), 'led_out': (81, 'B'), 'manufacturer': (8, '8s'), 'product_id': (8, '8s'), 'sensor_type': (16, '8s'), 'strobe_out': (80, 'B'), 'version': (0, '8s')}
I2C_DEV = 16
ANALOG_LSB = 0.0009775171065493646
class AnalogOutputMode(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)
DC = 0
SINE = 1
SQUARE = 2
UPWARDS_SAWTOOTH = 3
DOWNWARDS_SAWTOOTH = 4
TRIANGLE = 5
PWM = 6
get_analog()

Get analog input pins (A0-A3) result in raw bits

Return type:

Dict[str, int]

Returns:

Map of pin name, pin value (bits)

get_analog_volts(voltage_reference=3.3)

Get analog input pins (A0-A3) results in volts. Resolution to ~3mV (voltage reference * (1/1023) volts)

Parameters:

voltage_reference (float) – optionally provide measured voltage from 3.3V regulator for more accurate calculations

Return type:

Dict[str, float]

Returns:

Map of pin name, pin voltage (in volts)

get_digital()

Get digital input pins (D0-D7)

Return type:

Dict[str, bool]

Returns:

Boolean list, LSB first.

set_digital(pins)

Set digital output pins (D0-D7)

Parameters:

pins (List[bool]) – boolean list (LSB first)

set_digital_byte(integer, lsb=True)

Set digital output pins (D0-D7) from byte

Parameters:
  • integer (int) – Byte (0-255 inclusive)

  • lsb – Whether to output in LSB order (0x01 = pin B0)

set_digital_modes(modes)

Set digital pin mode (D0-D7)

Parameters:

modes (List[bool]) – boolean list (LSB first, True = Output, False = Input)

set_digital_modes_byte(mode_int, lsb=True)

Set digital output pins mode (D0-D7) from byte

Parameters:
  • mode_int (int) – Byte (0-255 inclusive)

  • lsb – Whether to output in LSB order (0x01 = pin B0)

set_strobe_output(mode_int)

Strobe output - behaves like any other output, but has strobe signal sent whenever a digital read/write sent. When a digital read is done, it will send a spike on RD (inverse of the current RD pin state), when a digital write is done, it will send a spike on WR (inverse of the current WR pin state) This ‘digital read’ and ‘digital write’ actions causing a spike applies to the B0-7 pins Bits to write: S0 = 1, S1 = 2 S2 = 4 S3 = 8 RD = 16 WR = 32

Parameters:

mode_int (int) – mode_int: Byte (0-63 inclusive)

set_led_output(red=False, blue=False)

Set LED output. True = On, False = Off

Parameters:
  • red – Boolean for Red LED

  • blue – Boolean for Blue LED

analog_out(pin, mode, freq, voltage_bits)

Analog Output Pins

Parameters:
  • pin (int) – 0 for O0, 1 for O1

  • mode (AnalogOutputMode) – 0-5 for various modes, see AnalogOutputMode class

  • freq (int) – 0 to 2^13Hz (~8kHz) Note: if 0 provided for wave, will get 1Hz.

  • voltage_bits (int) – 0-1023 for 0V to 3.3V (ish)

analog_out_voltage(pin, mode, freq, voltage, voltage_reference=3.3)

Analog Output Pins

Parameters:
  • pin (int) – 0 for O0, 1 for O1

  • mode (AnalogOutputMode) – 0-5 for various modes, see AnalogOutputMode class

  • freq (int) – 0 to 2^13Hz (~8kHz) Note: if 0 provided for wave, will get 1Hz

  • voltage (float) – The desired voltage (between 0 and the voltage reference)

  • voltage_reference – Output 1023 in the analog_out mode to find the maximum voltage, enter it here.

compatible_sensors = [<nxt.sensor.digital._SCompatibility object>]
class nxt.sensor.hitechnic.ServoCon(brick, port, check_compatible=True)

Object for HiTechnic FIRST Servo Controllers. Coded to HiTechnic’s specs for the sensor but not tested. Please report whether this worked for you or not!

I2C_ADDRESS = {'factory_scale_divisor': (19, 'B'), 'factory_scale_factor': (18, 'B'), 'product_id': (8, '8s'), 'pwm': (72, 'B'), 's1pos': (66, 'B'), 's2pos': (67, 'B'), 's3pos': (68, 'B'), 's4pos': (69, 'B'), 's5pos': (70, 'B'), 's6pos': (71, 'B'), 'sensor_type': (16, '8s'), 'status': (64, 'B'), 'steptime': (65, 'B'), 'version': (0, '8s')}
class Status
STOPPED = 0
RUNNING = 1
get_status()

Returns the status of the motors. 0 for all stopped, 1 for some running.

set_step_time(time)

Sets the step time (0-15).

set_pos(num, pos)

Sets the position of a server. num is the servo number (1-6), pos is the position (0-255).

get_pwm()

Gets the “PWM enable” value. The function of this value is nontrivial and can be found in the documentation for the sensor.

set_pwm(pwm)

Sets the “PWM enable” value. The function of this value is nontrivial and can be found in the documentation for the sensor.

compatible_sensors = [<nxt.sensor.digital._SCompatibility object>]
class nxt.sensor.hitechnic.MotorCon(brick, port, check_compatible=True)

Object for HiTechnic FIRST Motor Controllers.

I2C_ADDRESS = {'batteryvoltage': (84, '2B'), 'factory_scale_divisor': (19, 'B'), 'factory_scale_factor': (18, 'B'), 'm1enccurrent': (76, '>l'), 'm1enctarget': (64, '>l'), 'm1gearratio': (86, 'b'), 'm1mode': (68, 'B'), 'm1pid': (87, '3B'), 'm1power': (69, 'b'), 'm2enccurrent': (80, '>l'), 'm2enctarget': (72, '>l'), 'm2gearratio': (90, 'b'), 'm2mode': (71, 'B'), 'm2pid': (91, '3B'), 'm2power': (70, 'b'), 'product_id': (8, '8s'), 'sensor_type': (16, '8s'), 'version': (0, '8s')}
class PID_Data(p, i, d)
set_enc_target(mot, val)

Set the encoder target (-2147483648-2147483647) for a motor

get_enc_target(mot)

Get the encoder target for a motor

get_enc_current(mot)

Get the current encoder value for a motor

set_mode(mot, mode)

Set the mode for a motor. This value is a bit mask, and you can find details about it in the sensor’s documentation.

get_mode(mot)

Get the mode for a motor. This value is a bit mask, and you can find details about it in the sensor’s documentation.

set_power(mot, power)

Set the power (-100-100) for a motor

get_power(mot)

Get the power for a motor

set_gear_ratio(mot, ratio)

Set the gear ratio for a motor

get_gear_ratio(mot)

Get the gear ratio for a motor

set_pid(mot, piddata)

Set the PID coefficients for a motor. Takes data in MotorCon.PID_Data(p, i, d) format.

get_pid(mot)

Get the PID coefficients for a motor. Returns a PID_Data() object.

get_battery_voltage()

Gets the battery voltage (in millivolts/20)

compatible_sensors = [<nxt.sensor.digital._SCompatibility object>]
class nxt.sensor.hitechnic.Angle(brick, port, check_compatible=True)

HiTechnic Angle Sensor.

I2C_ADDRESS = {'angle': (66, '2B'), 'angle_acc': (68, '>l'), 'factory_scale_divisor': (19, 'B'), 'factory_scale_factor': (18, 'B'), 'mode': (65, 'c'), 'product_id': (8, '8s'), 'rpm': (72, '>h'), 'sensor_type': (16, '8s'), 'version': (0, '8s')}
get_angle()
get_sample()
get_accumulated_angle()
get_rpm()
calibrate()
reset()