Potentiometer Catapult Reload within Autonomous


#1

I am currently working on my programming skills autonomous and am curious about how to implement my potentiometer code to automatically reload to the same location after the shot. Here’s what I have currently.

Function Before Autonomous:

bool TargetReached = 0;
void CatapultResetting()
{
while(true)
{
if(Potentiometer1.value(analogUnits::range12bit) < 900 && TargetReached == 0)
{
TargetReached = 1;
}
else if(TargetReached == 1)
{
if(Potentiometer1.value(analogUnits::range12bit) < 790)
{
Catapult.spin(fwd,100,pct);
Catapult2.spin(fwd,100,pct);
}
else
{
Catapult.spin(fwd,0,pct);
Catapult2.spin(fwd,0,pct);
}
}
sleepMs(50);
}

The problem I am running into is when I use this function after I put in the command to shoot, the POT Value is already at the desired location and it doesn’t shoot because of the sleepMs command. What would be the best way to override the sleepMs command when I want the catapult to shoot?


Quadrature encoder help
#2

I’m confused by your code, so I rewrote it with comments that might help you find whats wrong. As it is written, it does nothing.

	void CatapultResetting()
	{
		bool TargetReached = 0; // reset when void is called (moved into the void so it gets reset before the while)
		while(true) // repeat forever (normally you would have a while(TargetReached == 0) so you can break out at the desired time)
		{
			if(Potentiometer1.value(analogUnits::range12bit) < 900 && TargetReached == 0) // if small and not at target
			{
				TargetReached = 1; // target reached? (but how, no motors are spinning, so the pot value cannot change)
			}
			else if(TargetReached == 1) // if at target (but you cannot get here if the motors are not spinning)
			{
				if(Potentiometer1.value(analogUnits::range12bit) < 790) // if also small
				{
					Catapult.spin(fwd,100,pct); // spin in the direction to make pot value larger?
					Catapult2.spin(fwd,100,pct);
				}
				else
				{
					Catapult.spin(fwd,0,pct); // stop (but not with the stop method)
					Catapult2.spin(fwd,0,pct); // presumably this is where you want to exit this void
				}
			}
			sleepMs(50); // wait 
		}
	}

#3

I am also confused. What would be the best way to implement a command to reach the desired target value within autonomous (Where you put “Target reached?”)? In addition, is there an easier way to have the catapult shoot and return to a specific POT value within autonomous?


#4
while not target reached:
    stuff to reach target
clean up steps (stopping motors, etc.)

Is the basic method.


#5

elaborating a bit on @John_TYler

void fireAndReset() {
   // run the motors enough to release the catapult
   while ( pot value is not what you want){
   // run motors in the direction that gets to the pot value to what you want
   // wait timer here if you want 
   }
   // stop the pot motors
}

#6

Just for clarification, this is a separate function correct? This doesn’t have to do with the original code. It’s a different way to do it?


#7

Yes, this is a replacement of what you were writing.


#8

I tried rewriting it with your “template.” Does this look a little better?

void FireAndReset()
{
Catapult.startRotateFor(540,rotationUnits::deg,100,velocityUnits::pct);
Catapult2.rotateFor(540,rotationUnits::deg,100,velocityUnits::pct);
while(Potentiometer1.value(analogUnits::range12bit) < 790)
{
Catapult.spin(vex::directionType::fwd, 60,vex::velocityUnits::pct);
Catapult2.spin(vex::directionType::fwd, 60,vex::velocityUnits::pct);
}
{
sleepMs(50);
}
{
Catapult.spin(vex::directionType::fwd, 0,vex::velocityUnits::pct);
Catapult2.spin(vex::directionType::fwd, 0,vex::velocityUnits::pct);
}
}


#9

Better, yes. FWIW your sleepMs(50); is doing not much. If you would like to check the sensor only every 50ms, put that sleep inside the while loop. If you want the motors to hold the catapult arm under tension, you should use motor.stop() rather than motor.spin() with zero for speed. rotateFor automatically stops with breaktype::hold, which will supply power to the motors as needed to hold them in place.

Be aware you might overheat your motors.

when posting code. if you type ``` on the line before and after the code, it will be more readable.


#11

Like this?

void FireAndReset() 
    {
        Catapult.startRotateFor(540,rotationUnits::deg,100,velocityUnits::pct);
        Catapult2.rotateFor(540,rotationUnits::deg,100,velocityUnits::pct);
    while(Potentiometer1.value(analogUnits::range12bit) < 790)
    {
        Catapult.spin(vex::directionType::fwd, 60,vex::velocityUnits::pct);
        Catapult2.spin(vex::directionType::fwd, 60,vex::velocityUnits::pct);
        sleepMs(50);
    }
    {
        Catapult.stop(vex::brakeType::coast);
        Catapult2.stop(vex::brakeType::coast);
    }
    }

#12

Compile and see what happens. (You have some extra {} that the compiler will point out, but should work after.)