Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NXT Color Sensor support? #1553

Open
TheDoctor2017 opened this issue Jan 15, 2023 · 10 comments
Open

NXT Color Sensor support? #1553

TheDoctor2017 opened this issue Jan 15, 2023 · 10 comments

Comments

@TheDoctor2017
Copy link

ev3dev version: 4.14.117-ev3dev-2.3.5-ev3
ev3dev-lang-python version:
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
ii micropython-ev3dev2 2.1.0 all Python language bindings for ev3dev for MicroPython
ii python3-ev3dev 1.2.0 all Python language bindings for ev3dev
ii python3-ev3dev2 2.1.0 all Python language bindings for ev3dev

I am trying to use an NXT color sensor, but I can't get the EV3 brick to recognize it. I know the sensor is good, because if i boot the EV3 into the default firmware from Lego the sensor is detected and work properly, but when I boot to EV3DEV and check Device Browser/Sensors the sensor does not show. Note that this is the NXT color sensor, 4546542, that comes with the NXT 2.0 kit, not the ambient light sensor. Is this sensor supported? Thanks!

@dlech
Copy link
Member

dlech commented Jan 15, 2023

The NXT color sensor is an unusual sensor in that it has a protocol unlike any other sensor. They only way to use it with ev3dev is with Pybricks (or port this code to the language of your choice).

@TheDoctor2017
Copy link
Author

The NXT color sensor is an unusual sensor in that it has a protocol unlike any other sensor. They only way to use it with ev3dev is with Pybricks

I tried Pybricks and got the same result. Any other suggestions?

@dlech dlech transferred this issue from ev3dev/ev3dev-lang-python Jan 15, 2023
@dlech
Copy link
Member

dlech commented Jan 15, 2023

Can you share the code to reproduce the problem?

@TheDoctor2017
Copy link
Author

TheDoctor2017 commented Jan 16, 2023

Can you share the code to reproduce the problem?

I haven't gotten to the point of writing any code yet. When I connect the NXT color sensor and boot using EV3DEV, then go into Device Browser/Sensors, the sensor does not show as connected. And I am a little confused. When I go to pybricks.com, it says the ev3 brick isn't supported, but when I go to https://pybricks.com/ev3-micropython/ it says it is, but using MicroPython, not Pybricks. I followed the instructions to create the flash and set up MicroPython. Is that the same as Pybricks? If not, where do I find Pybricks for the EV3?

@TheDoctor2017
Copy link
Author

Can you share the code to reproduce the problem?

When I try to initialize the NXT color sensor using MicroPython using:

color_sensor = ColorSensor(Port.S1)

I get the error:

A sensor or motor is not connected to the specified port

@dlech
Copy link
Member

dlech commented Jan 16, 2023

When I connect the NXT color sensor and boot using EV3DEV, then go into Device Browser/Sensors, the sensor does not show as connected

It cannot be automatically detected and there is no kernel driver for it, so this sensor will never display in the device browser. The Pybricks library takes care of properly configuring the port when you run your program.

using MicroPython, not Pybricks

Pybricks is built on top of MicroPython, so they are basically the same thing in this context.

If not, where do I find Pybricks for the EV3?

It is pre-installed on ev3dev. Just use #!/usr/bin/env pybricks-micropython as the first line of your program to use the Pybricks MicroPython runtime.

A sensor or motor is not connected to the specified port

Can you share the full program to reproduce the problem?

@TheDoctor2017
Copy link
Author

TheDoctor2017 commented Jan 16, 2023

Can you share the full program to reproduce the problem?

This is the entire code:

#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
                                 InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import Port, Stop, Direction, Button, Color
from pybricks.tools import wait, StopWatch, DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile, ImageFile

ev3 = EV3Brick()
ev3.speaker.beep()

color_sensor = ColorSensor(Port.S1)

@dlech
Copy link
Member

dlech commented Jan 16, 2023

This is the NXT color sensor, so you need from pybricks.nxtdevices import ColorSensor, not ev3devices.

@TheDoctor2017
Copy link
Author

This is the NXT color sensor, so you need from pybricks.nxtdevices import ColorSensor, not ev3devices.

Wow. Sorry I missed that. Thank you so much!

@flemmii
Copy link

flemmii commented Mar 10, 2023

So I tried porting it to java with chatGPT, but I don't know if it's correct

import java.io.FileWriter;
import java.io.IOException;

public class Main {

    private static final int IN = 0;
    private static final int OUT = 1;

    private static final int PBIO_PORT_ID_1 = 1;

    private static class nxtcolor_pininfo_t {
        final int digi0; // GPIO on wire 5
        final int digi1; // GPIO on wire 6
        final int adc_val; // ADC on wire 6 for getting reflection data
        final int adc_con; // ADC on wire 1 for detecting sensor

        nxtcolor_pininfo_t(int digi0, int digi1, int adc_val, int adc_con) {
            this.digi0 = digi0;
            this.digi1 = digi1;
            this.adc_val = adc_val;
            this.adc_con = adc_con;
        }
    }

    private static final nxtcolor_pininfo_t[] pininfo = new nxtcolor_pininfo_t[] {
        new nxtcolor_pininfo_t(2, 15, 5, 6),
        new nxtcolor_pininfo_t(14, 13, 7, 8),
        new nxtcolor_pininfo_t(12, 30, 9, 10),
        new nxtcolor_pininfo_t(1, 31, 11, 12),
    };

    private enum nxtcolor_color_state {
        NXT_LAMP_RED,
        NXT_LAMP_GREEN,
        NXT_LAMP_BLUE,
        NXT_LAMP_OFF
    }

    private static class nxtcolor_t {
        boolean ready;
        boolean fs_initialized;
        boolean waiting;
        nxtcolor_color_state state;
        nxtcolor_color_state lamp;
        int[][] calibration = new int[3][4];
        int[] threshold = new int[2];
        int raw_min;
        int raw_max;
        int crc;
        int wait_start;
        nxtcolor_pininfo_t pins;
        File f_digi0_val;
        FileWriter f_digi0_dir;
        File f_digi1_val;
        FileWriter f_digi1_dir;
        boolean digi1_dir;
        File f_adc_val;
        FileWriter f_adc_con;

        nxtcolor_t(nxtcolor_pininfo_t pins) {
            this.pins = pins;
        }
    }

    private static final nxtcolor_t[] nxtcolorsensors = new nxtcolor_t[4];

    private static int min(int a, int b) {
        return a < b ? a : b;
    }

    private static int max(int a, int b) {
        return a > b ? a : b;
    }

    private static int nxtcolor_wait(nxtcolor_t nxtcolor, int ms) {

        int now = (int) System.currentTimeMillis();

        // Wait for existing wait to complete
        if (nxtcolor.waiting) {
            if (now - nxtcolor.wait_start > ms) {
                nxtcolor.waiting = false;
                return 0;
            } else {
                return 1;
            }
        }
        // We are not waiting, so start a new wait
        else {
            nxtcolor.waiting = true;
            nxtcolor.wait_start = now;
            return 1;
        }
    }

    private static int nxtcolor_set_digi0(nxtcolor_t nxtcolor, boolean val) {
        try {
            FileWriter f = new FileWriter(nxtcolor.f_digi0_val);
            f.write(val ? "1" : "0");
            f.close();
            return 0;
        } catch (IOException e) {
            return 1;
        }
   

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants