ev3dev / ev3dev-lang-python

Pure python bindings for ev3dev
MIT License
431 stars 145 forks source link

HiTechnic Color Sensor does not work #333

Closed logwet closed 7 years ago

logwet commented 7 years ago

I was attempting to read values from a HiTechnic v2 NXT color sensor (which is listed as supported on the ev3dev wiki) in my project. Here is the code from the script:

#!/usr/bin/env python3

from ev3dev.ev3 import *
btn = Button()

mB = LargeMotor('outB')
mA = LargeMotor('outA')

cl = ColorSensor('in2')
cl.mode='RED'

while not btn.any():
        if cl.value() < 30:
                mB.run_forever(speed_sp=300)
                mA.run_forever(speed_sp=-10)
        else:
                mB.run_forever(speed_sp=300)
                mA.run_forever(speed_sp=300)

mB.stop(stop_action='brake')
mA.stop(stop_action='brake')

When run with sudo python3 first.py I recieve the following errors:

robot@ev3dev:~$ sudo python3 first.py
Traceback (most recent call last):
  File "first.py", line 10, in <module>
    cl.mode='RED'
  File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 1665, in mode
    self._mode = self.set_attr_string(self._mode, 'mode', value)
  File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 223, in set_attr_st                                                                                                             ring
    return self._set_attribute(attribute, name, value)
  File "/usr/lib/python3/dist-packages/ev3dev/core.py", line 210, in _set_attrib                                                                                                             ute
    raise Exception('Device is not connected')
Exception: Device is not connected

This is very weird because the sensor is definetly connected (it is powered), I can access and read values from it through Brickman, and the driver is also listed as ht-nxt-color-v2

I urgently need this particular sensor working for my project because I'm participating in RoboCup Junior Aus.

dlech commented 7 years ago

The ColorSensor class is for the LEGO EV3 Color sensor only. Use the generic Sensor class instead.

http://python-ev3dev.readthedocs.io/en/stable/sensors.html#sensor

Try something like this...

cl = Sensor(address='in2', driver='ht-nxt-color-v2')
Kvothe1997 commented 7 years ago

" Try something like this...

cl = Sensor(address='in2', driver='ht-nxt-color-v2') "

Did it work? I have the same problem. I did that but I got the same error message: Exception: Device is not connected.

dlech commented 7 years ago

It might need to be:

cl = Sensor(address='in2', driver_name='ht-nxt-color-v2')

Or if you have an older version of the sensor, leave off the -v2.

ddemidov commented 7 years ago

That is correct, it should be driver_name. Also, the address could be wrong. Can you go to your command line on EV3, and see if there is sensor in /sys/class/lego-sensor? If there is not, then you do not have it connected (or there is another problem); if there is, try to see its address with cat /sys/class/lego-sensor/sensor*/address.

Kvothe1997 commented 7 years ago

Thanks for answearing. I noticed the driver_name mistake but it is still not working. The Brickman recognizes the sensor because when I go to sensors in the ev3 panel I see the sensor ht-nxt-color-v2. I also see the address which is 'In1:i2c1'.

I guess that the lines you tell me to use should give me the same result, or not? Anyway, /sys/class/lego-sensor/sensor*/address that line is suppossed to be written on mobaxterm (I created the SSH connection with that program), right?

I wanna thank you guys in advance.

dlech commented 7 years ago

How about this?

cl = Sensor(address='in2:i2c1', driver_name='ht-nxt-color-v2')
Kvothe1997 commented 7 years ago

I have already done it and I keep getting the same error.

ddemidov commented 7 years ago

Just a thought: why do you use sudo? Can you try without it?

Kvothe1997 commented 7 years ago

Do you mean executing the program from de EV3? I did that and the screen goes empty for a second. Then it returns to the screen of program selection.

ddemidov commented 7 years ago

I mean you had this in your example:

robot@ev3dev:~$ sudo python3 first.py
...

Why do you have sudo here? Can you just try

robot@ev3dev:~$ python3 first.py
...

?

dlech commented 7 years ago

Also, I just noticed I got the input port wrong from what you reported seeing in Brickman. The values in python should match exactly what you see in Brickman.

cl = Sensor(address='in1:i2c1', driver_name='ht-nxt-color-v2')
Kvothe1997 commented 7 years ago

Thanks guys, it seems that I had put some blank spaces before and after the equals that are inside the parenthesis. I could read the sensor in mode 'RAW'. However, I have to say that the measured values are not the ones that the docs (http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-jessie/sensor_data.html#hitechnic-nxt-color-sensor-v2) indicate.

dlech commented 7 years ago

I don't have one of these sensors for testing, so the values could very well be wrong. If you have any insights, please open an issue at https://github.com/ev3dev/ev3dev/issues, or even better, edit https://github.com/ev3dev/lego-linux-drivers/blob/ev3dev-stretch/sensors/nxt_i2c_sensor_defs.c and send a pull request.

Related ev3dev/ev3dev#920

logwet commented 7 years ago

Hi guys, I'm a bit late to the party but @Kvothe1997 I have found just

cl = Sensor(address='in2')

To work with the hiTechnic v2s. No idea why I can't define the driver but this works perfectly. I can also use all 6 modes :)

dwalton76 commented 7 years ago

Looks like this is resolved so closing....if not please re-open