How to handle when a brick is turned off & back on?

HomeForumsMonoBrick Communication LibraryHow to handle when a brick is turned off & back on?

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #3746
    Author Image
    Mike O’C
    Participant

    Greets! After many e-mails with Anders, I felt it was best to share my results to date here & continue the discussion in the forums. So far, I’ve downloaded the Monobrick Test Project ( http://www.monobrick.dk/software/monobrick/ ) and tweaked it to work for our purposes.

    Background

    Our ultimate goal is to write a PC application where kids in LEGO robotics clubs can use USB-connected game controllers to ‘remote control’ LEGO robots which are Bluetooth-paired with the computer. The robots might be built around NXT, or around EV3. A single match can have up to four (4) robots being controlled at the same time, probably a mix of NXTs and EV3s.

    I created a ‘wrapper class’ called “GenericBrick.cs” which abstracts away whether a specific Bluetooth-connected LEGO brick is an NXT or an EV3. And also a “BrickManager.cs” class which figures out the list of Bluetooth-connected bricks, and handles when a new one is connected to the laptop, whether a given brick has been “activated” via MonoBrick, etc. These classes, and an updated “Program.cs”, are attached. They should be able to get dropped into the ‘Monobrick Test Project’ and run.

    The Current Problem

    My main problem so far is how to deal with the situation where a brick, after being “activated” via MonoBrick, gets turned off and then turned back on. At the events we run, we have so many kids involved that someone is always needing to replace batteries, or accidentally cycles the power on their brick, etc. However, it seems that once they turn the brick off and then back on, I cannot get MonoBrick to re-establish working operations.

    How to reproduce using the attached files:

    1. Attach a brick (with an “A” motor connected) via Bluetooth
    2. Run Program.cs
    3. Hit ‘C’ to connect to a brick (make sure the target brick is powered on)
    4. Choose the # corresponding to the brick, and then Y or N as to whether it is an EV3
    5. The brick should play an ascending set of notes when it gets “activated”
    6. Hit ‘T’ to test the brick
    7. Choose the # corresponding to the brick
    8. Use the up & down arrow keys to change the “A” motor speed (just as in the original Test Project)
    9. Turn off the brick
    10. The next time you hit up or down arrow, the state will change to “LOST” – this is OK and expected.
    11. Now, turn the brick back on, and then hit ‘R’ to reset the connection
    12. Hitting up & down arrow keys is supposed to work again, but so far it does not

    In “GenericBrick.cs”, in the ‘resetBrokenState()’ method, I just change the ‘state’ value back to NXT or EV3.

    Do I need to do something with the underlying EV3.Brick or NXT.Brick objects? Previously I tried to make a new connection on the same Bluetooth COM port, but it would always fail. How do I get things to work again?

    – Mike

    #3747
    Author Image
    Mike O’C
    Participant

    Whoops, it wouldn’t let me attach *.cs files. Trying again with *.txt files – if these work, just rename them to *.cs and drop them into the “\TestApplication” folder in the MonoBrick Test Project.

    – Mike

    Edit: And also, I apologize in advance for any bad coding practices you see. I’m brand-new to C# development. ^_^;;

    • This reply was modified 10 years, 3 months ago by Author ImageMike O'C.
    Attachments:
    You must be logged in to view attached files.
    #3752
    Author Image
    Anders Søborg
    Keymaster

    Hi

    I just had a quick look and I don’t see you calling disconnect anywhere. Where do you close the connection?

    Anders

    #3753
    Author Image
    Mike O’C
    Participant

    Huh! I’d swear in the past I had written code which tried to call Brick.Connection.Close(), and that did not work – yet when I just updated my class here, now the process works. Good news, regardless!

    The updated “GenericBrick.txt” class is attached. I refactored the brick connection code into its own method (rather than being in the constructor), so I could reuse it in the ‘reset’ method, e.g.:

    public int resetBrokenState()
    {
        if (state == STATE_BROKEN)
        {
            if (ev3Brick != null)
            {
                ev3Brick.Connection.Close();
                connectToBrick(true, false);
                //state = STATE_EV3;
            }
            else
            {
                nxtBrick.Connection.Close();
                connectToBrick(false, false);
                //state = STATE_NXT;
            }
        }
    
        return state;
    }
    
    Attachments:
    You must be logged in to view attached files.
    #3755
    Author Image
    Anders Søborg
    Keymaster

    Great so everything is now in place? Keep me posted on your project. You might even create a new thread/topic when you are done to show off the result.

    Anders

    #3758
    Author Image
    Mike O’C
    Participant

    Not quite in place; the “GenericBrick.cs” class has a constructor which doesn’t take a bool ‘isEv3’, but tries to automatically detect whether a brick is NXT or EV3. This also has not worked yet. I tried updating it to do a similar thing to the ‘resetConnection()’ method, but so far no luck:

    public GenericBrick(string newBrickName, string newConnName)
    {
        brickName = newBrickName + "";
        connName = newConnName + "";
        state = STATE_NEW;
    
        // first, try to connect as if it is an NXT
        connectToBrick(false, true);
        if (state == STATE_NXT)
            return;
    
        // not a found NXT?
        if (nxtBrick != null)
        {
            if (nxtBrick.Connection != null)
                nxtBrick.Connection.Close();
        }
        nxtBrick = null;
    
        // try to connect as if it is an EV3
        connectToBrick(true, true);
    }
    

    However, in the meantime I wrote the Winforms UI to pop a dialog box asking if a brick is EV3 or NXT. That will work for the near term.

    Attachments:
    You must be logged in to view attached files.
    #3760
    Author Image
    Anders Søborg
    Keymaster

    Hi

    You could also try to send a EV3 command to see if the brick sends a reply. If yes it is a EV3 otherwise a NXT.

    Anders

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

Posted in

Make a donation