Skip to content
This repository has been archived by the owner on Nov 23, 2019. It is now read-only.

How to create a new sensor class

GongYi edited this page Jul 26, 2014 · 5 revisions

Create a new sensor class

Introduction

Python-ev3 is built on ev3dev system. So please first read the ev3dev Using Sensors
There are two types of sensor in the ev3dev

  • MSENSOR
  • I2CS

MSENSOR

Msensor can be recognized by ev3dev automatically. Ev3dev creates a new node at /sys/class/msensor when msensor attached to ev3.
To create a new msensor class, inherit from ev3dev.Msensor and give a type_id.
For example the ColorSensor

class ColorSensor(Msensor):

    def __init__(self):
        Msensor.__init__(self, type_id=29)

You may also give some shortcut of the meaningful sensor property. For example

    @property
    def rgb(self):
        self.mode = 'RGB-RAW'
        return self.value0, self.value1, self.value2

I2CS

A lot of 3rd party i2c sensors can't be recognized by ev3dev automatically. They are I2CS sensor. Ev3dev creates node at /dev/i2c-in*. Please reference Using I2C Sensors.
Each I2CS sensor has an i2c address. The number is (lego i2c address)/2. See I2C Sensor Addressing to find out how and why.
To create a new I2CS class, inherit from ev3dev.I2CS, use the i2c address to initialize. Use ``I2CS.create_i2c_property``` class decorator to automatically populate properties. For example

@I2CS.create_i2c_property(
    command=(0x41, {'read_only': False}),
    button_set_1 = 0x42,
    button_set_2= 0x43,
    x_left= 0x44,
    y_left= 0x45,
    x_right= 0x46,
    y_right= 0x47,
    up= 0x4A,
    right= 0x4B,
    down= 0x4C,
    left= 0x4D,
    l2= 0x4E,
    r2= 0x4F,
    l1= 0x50,
    r1= 0x51,
    triangle= 0x52,
    circle= 0x53,
    cross= 0x54,
    square= 0x55)
class PSPNxV4(MindSensorI2CS): #MindSensorI2CS is a subclass of ev3dev.I2CS

In this case, the PSPNxV4 class has command, button_set_1, button_set_2 ... properties.
A user can use below code to access the device property

d = PSPNxV4(1) #port 1
print (d.button_set_1) #access button_set_1 which read one byte at 0x42
Clone this wiki locally