Skip to content

Instantly share code, notes, and snippets.

@sarahhodne
Created February 16, 2011 00:33
Show Gist options
  • Save sarahhodne/828607 to your computer and use it in GitHub Desktop.
Save sarahhodne/828607 to your computer and use it in GitHub Desktop.
To add the connectivity detection code to your program, copy and paste the code
below into your program.
void enterFailureMode()
{
}
void exitFailureMode()
{
}
bool kInFailureMode = false;
task checkConnectivity()
{
int missedMessageCount = 0;
long lastMessageCount = 0;
while(true) {
if (ntotalMessageCount == lastMessageCount) {
if (++missedMessageCount > 500) {
// The total message count hasn't incremented for more than five
// hundred iterations of the while loop.
kInFailureMode = true;
enterFailureMode();
}
}
else { // The total message count changed, we have a connection!
if (kInFailureMode)
exitFailureMode();
kInFailureMode = false;
missedMessageCount = 0;
}
lastMessageCount = ntotalMessageCount;
}
}
You need to add StartTask(checkConnectivity) in your main task, as well as
adding code that's specific to your program. Add everything you want to do when
the connection drops in the enterFailureMode function. You will probably want
to set all your motor speeds to 0 in this function. If the connection drops,
the NXT won't get any new joystick messages. This means that if you're driving
motors based on the joystick settings, they will continue to run at the same
speed as they were just before the connection dropped. So anything that depends
on the joysticks should be stopped in this function. Another good thing to do
is to do something unique here to tell you and the FTAs that the connection
dropped. Ideas for this: Turn on a light, raise some kind of arm, etc. This
makes it much easier to debug when the match is running. This should be undone
again in exitFailureMode to show that the connection is restored again. Another
thing you need to do is wrap all the code that you're stopping/undoing in
startFailureMode with "if (!kInFailureMode)".
For an example on how to implement this in your code I made a really simple
program and put it online here: https://gist.github.com/828598. For a more
advanced example, you can check out my team's (4560) code here:
https://github.com/dvyjones/FTC_4560.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment