Home→Forums→MonoBrick Communication Library→How to handle when a brick is turned off & back on?
- This topic has 6 replies, 2 voices, and was last updated 10 years, 10 months ago by Anders Søborg.
-
AuthorPosts
-
January 4, 2014 at 19:47 #3746
Mike O’CParticipantGreets! 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:
- Attach a brick (with an “A” motor connected) via Bluetooth
- Run Program.cs
- Hit ‘C’ to connect to a brick (make sure the target brick is powered on)
- Choose the # corresponding to the brick, and then Y or N as to whether it is an EV3
- The brick should play an ascending set of notes when it gets “activated”
- Hit ‘T’ to test the brick
- Choose the # corresponding to the brick
- Use the up & down arrow keys to change the “A” motor speed (just as in the original Test Project)
- Turn off the brick
- The next time you hit up or down arrow, the state will change to “LOST” – this is OK and expected.
- Now, turn the brick back on, and then hit ‘R’ to reset the connection
- 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
January 4, 2014 at 19:51 #3747
Mike O’CParticipantWhoops, 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, 10 months ago by Mike O'C.
Attachments:
You must be logged in to view attached files.January 4, 2014 at 20:01 #3752
Anders SøborgKeymasterHi
I just had a quick look and I don’t see you calling disconnect anywhere. Where do you close the connection?
Anders
January 4, 2014 at 20:21 #3753
Mike O’CParticipantHuh! 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.January 4, 2014 at 20:31 #3755
Anders SøborgKeymasterGreat 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
January 4, 2014 at 20:48 #3758
Mike O’CParticipantNot 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.January 4, 2014 at 20:51 #3760
Anders SøborgKeymasterHi
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
-
AuthorPosts
You must be logged in to reply to this topic.
Follow