View unanswered posts | View active topics It is currently Thu Oct 23, 2014 5:35 am






Reply to topic  [ 7 posts ] 
Joystick buttons getting corrupted 
Author Message
Rookie

Joined: Fri Apr 15, 2011 10:29 am
Posts: 37
Post Joystick buttons getting corrupted
If you run the provided code on a raw NXT brick (no other controllers or sensors), it'll behave badly if you hold down the button 6 (middle upper-left). What *should* happen is that you should get a beep when you release the button (we're doing edge triggered on release). What does happen is that the NXT brick will beep randomly, indicating that it's getting button releases.

Another (related) issue is that in the bgTask(), it checks for button 9 (upper-left button on the back of the controller). If you hold this button down, it should do a longer beep when the button is released. However, the program will just crash after a few seconds inside the Joystick driver, which makes no sense.

This occurs using USB, Bluetooth, or Samantha/Wi-Fi, so it doesn't seem to be related to the transfer medium.

If you disable starting the bgTask(), things work fine. However, the crash won't occur because the button 9 code is never attempted.

The code supplied pretty basic, and is a shortened version of what we're trying to do. If there is a better way of doing this I'm open to suggestions.

Code:
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

//Include file to "handle" the Bluetooth messages.
#include "JoystickDriver.c"

// Forward declarations
task bgTask();

bool buttonWasPressed = false;

// Task to keep track of the current heading using the HT Gyro
task bgTask()
{
    while (true) {
        // Check the button
        bool btnPress = joy1Btn(6);
        if (!btnPress && buttonWasPressed) {
            PlaySound(soundBeepBeep);
        }
        buttonWasPressed = btnPress;

        // Wait a bit..
        wait1Msec(200);
    }
}

// Keep track of the other button
bool otherButtonWasPressed = false;
bool buttonToggle = false;

task main()
{
//    StartTask(bgTask);

    while (true) {
        // Get current joystick buttons and analog movements
        getJoystickSettings(joystick);

        // Check the toggle!
        bool btnPress = joy1Btn(9);
        if (!btnPress && otherButtonWasPressed) {
            // Beep to let them know we're switching!
            PlaySound(soundBlip);
            buttonToggle = !buttonToggle;
            nxtDisplayString(0, "toggle=%d", buttonToggle);
        }
        otherButtonWasPressed = btnPress;
    }
}



Nate


Fri Oct 07, 2011 8:02 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Joystick buttons getting corrupted
I did not spend too much time looking at your program but one thing pops out at me is that you are accessing the joystick structure in two different tasks. I know you have the bgTask commented out in the above code but if you do run bgTask, you may have contention. In most circumstances, you can avoid using tasks. For example, we have a library module that provides joystick button edge event callbacks so when a button is either pressed or released, you will get an event callback. The way it was written is very similar to using task but it is just a function that got called periodically inside your robot loop. If you want to look at it, you can find it here.
http://proj.titanrobotics.net/hg/Ftc/20 ... b/joybtn.h


Sat Oct 08, 2011 12:40 am
Profile
Rookie

Joined: Fri Apr 15, 2011 10:29 am
Posts: 37
Post Re: Joystick buttons getting corrupted
Yep, that was it. I ended up going into the low-level Joystick driver and verifying that it wasn't corrupting data, but it was due to the joy1Btn() method not being re-entrant and having multiple callers in the same method at the same time. (I wish we had a stack here...)

I was sure we had done this last year (checking the buttons in multiple tasks), but I just went and reviewed the code and in fact we always checked out joysticks in the 'main' task. I guess we got accidentally luck, or else I forgot that I couldn't do that this year. :)

I understand I don't need the background for what I've shown. However, that background task is (currently) doing the Gyro updates, and I wanted the ability to re-synchronize it's definition of '0' degrees by hitting a button. Unfortunately, that won't work due to the re-entrancy issues, so I'll need to checks the buttons and set a flag that the task will have to watch for.

Re: callback code, I'll look at this shortly, thanks!


Nate


Sat Oct 08, 2011 12:30 pm
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 613
Post Re: Joystick buttons getting corrupted
We're just about to release some documentation around the Semaphore system in ROBOTC. You could use Semaphores to ensure that only one task uses a resource at a time.

The other option is to pluck the code out of the joystick driver and do your bit-mask to individual button conversion in your own task to avoid the re-entrant issue.

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Mon Oct 10, 2011 9:48 am
Profile
Rookie

Joined: Fri Apr 15, 2011 10:29 am
Posts: 37
Post Re: Joystick buttons getting corrupted
tfriez wrote:
We're just about to release some documentation around the Semaphore system in ROBOTC. You could use Semaphores to ensure that only one task uses a resource at a time.

The other option is to pluck the code out of the joystick driver and do your bit-mask to individual button conversion in your own task to avoid the re-entrant issue.


Thanks Tim, having example Semaphore code would be fantastic. I already re-wrote the joystick driver code, but the re-entrancy issues are still biting me due to the lack of a stack. :(


Mon Oct 10, 2011 8:47 pm
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 613
Post Re: Joystick buttons getting corrupted
We're still working on finalizing the documentation, but the support is in 3.00 and above. Here's a sample using two tasks. I'll post the documentation on advanced multitasking and semaphores to the blog once it's ready (maybe sometime this week)

Code:
TSemaphore lock;
string str = "";

task test()
{
  while(true)
  {
    SemaphoreLock(lock);
    if(bDoesTaskOwnSemaphore(lock))
    {
      str = "test";
      wait1Msec(500);
      SemaphoreUnlock(lock);
    }
  }
}
task main()
{
  SemaphoreInitialize(lock);
  StartTask(test);

  while(true)
  {
    SemaphoreLock(lock);
    if(bDoesTaskOwnSemaphore(lock))
    {
      str = "main";
      wait1Msec(500);
      SemaphoreUnlock(lock);
    }
  }
}

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Tue Oct 11, 2011 9:00 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Joystick buttons getting corrupted
A question on the behavior of the SemaphorLock function: When somebody is holding the lock, does this function wait or does it just return? Since you are checking whether you own the lock or not in the next statement, I assume SemaphoreLock is non-blocking and you just return if the lock is not available. If so, why can't the two functions be combined:
Code:
task test()
{
  while(true)
  {
    if (AcquireLock(lock))
    {
      //
      // If AcquireLock returns true, we own the lock.
      //
      str = "test";
      wait1Msec(500);
      ReleaseLock(lock);
    }
    else
    {
      //
      // Somebody else owns the lock, try again next time.
      //
      EndTimeSlice();
    }
  }
}

If may be more useful if there is an option to wait until the lock is available. For example:
Code:
bool AcquireLock(TSemaphore lock, bool fWait);
task test()
{
  while(true)
  {
    AcquireLock(lock, true);
    //
    // Since fWait == true, it means when AcquireLock returns,
    // we own the lock. Otherwise, AcquireLock will not return.
    //
    str = "test";
    wait1Msec(500);
    ReleaseLock(lock);
  }
}

In addition, do the semaphore functions support counting semaphore? It seems the functions are really implementing locks and not semaphores.


Tue Oct 11, 2011 1:37 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.