TBH Question

Hi guys!

So, I decided to try using the TBH velocity control to control the flywheel. I researched a little about it and decided it was the best option for our flywheel.

I’m using jpearman’s simplified version of the code that he posted a while ago. However, I’m having trouble stopping the flywheel. We are using buttons 8u, 8d, 8l and 8r to have different speeds, and when I was not using the TBH algorithm, it looked a little bit like this:

			
              if (vexRT[Btn8D] == 1) 
			{
				motor[port1] = -127;
			       motor[port3] = 85;
				motor[port5] = 85;
				motor[port6] = 85;
				motor[port8] = 85;
			}

			else if (vexRT[Btn8L] == 1) 
			{
				motor[port1] = -127;
				motor[port3] = 68;
				motor[port5] = 68;
				motor[port6] = 68;
				motor[port8] = 68;
			}

			else if (vexRT[Btn8U] == 1) 
			{
				motor[port1] = -127;
				motor[port3] = 53;
				motor[port5] = 53;
				motor[port6] = 53;
				motor[port8] = 53;
			}

			else if (vexRT[Btn8R] == 1) 
			{
				motor[port1] = -127;
				motor[port3] = 70;
				motor[port5] = 70;
				motor[port6] = 70;
				motor[port8] = 70;
			}
			else   // If buttons in 8 are  NOT pressed: stop everything
			{
				motor[port1] = 0;
				motor[port3] = 0;
				motor[port5] = 0;
				motor[port6] = 0;
				motor[port8] = 0;
			}

I tried doing the same thing with TBH, but once I press the button, it doesn’t stop and it starts stopping and continuing and it looks really weird. I don’t know what to do! This is what that looks like.

	if( vexRT Btn8L ] == 1 )
		{
			FwVelocitySet( 144, 0.55 );
		}

		else if( vexRT Btn8U ] == 1 )
	 	  {
	  		 FwVelocitySet( 100, 0.38 );
		 }

		else if( vexRT Btn8R ] == 1 )
		  {
			FwVelocitySet( 50, 0.2 );
 	         }

  	      else if( vexRT Btn8D ] == 1 )
		  {
				FwVelocitySet( 20,0.1);
		  }

		else
			{
				FwVelocitySet(0,0);
			}

If any of you have any idea of what’s happening, please help me out! Our tournament is in a few weeks and I’m honestly freaking out. THANKS!! :slight_smile:

You have two problems here. In my original code I pressed the button to select the flywheel speed but assumed that the button would not be held down. I had a button to set the stop speed. You code assumes that the buttons needs to be held down to run the flywheel at speed, if no button is pressed then you send stop. Repeatedly calling the FwVelocitySet function causes the TBH algorithm to reset, it never has a chance to actually run the flywheel. You could use some code like this that remembers what you have already asked the flywheel to do (I’m using a dummy version of FwVelocitySet just to test the code, remove that from your version).

void
FwVelocitySet( int speed, float predicted )
{
    writeDebugStreamLine("Speed %d\n", speed );
}

task main()
{
    int selection = -1;
    
    // start TBH here
    
    while(1)
        {
        // Detect button press
        if( vexRT Btn8L ] )
            {
            // If we are at a different speed then reset TBH
            if(selection != 1 ) {
                FwVelocitySet( 144, 0.55 );
                // and save the fact we were here already
                selection = 1;
                }
            }
        else if( vexRT Btn8U ] )
            {
            if(selection != 2 ) {
                FwVelocitySet( 100, 0.38 );
                selection = 2;
                }
            }
        else if( vexRT Btn8R ] )
            {
            if(selection != 3 ) {
                FwVelocitySet( 50, 0.2 );
                selection = 3;
                }
            }
        else if( vexRT Btn8D ] )
            {
            if(selection != 4 ) {
                FwVelocitySet( 20,0.1);
                selection = 4;
                }
            }
        else
            {
            if(selection != 0 ) {
                FwVelocitySet(0,0);
                selection = 0;
                }
            }
        }
}