Programming help for flywheel speeds

Hi i am from team 4221c from Georgia and i am needing some help with programming my flywheels. I am wanting to have 3 buttons to have different speeds of my flywheel’s rpm. I want too press one of the three buttons and let the motors keep running in the specified motor value until i press it again for it too stop. I am confused on how to do it. Can any of you guys help me?:confused::eek: thanks

what you want is a toggle, as to how you will actually get your flywheel to stay at a certain velocity, that is a different issue, but the joystick buttons are relatively simple

for one button it will be done like this, and you will repeat the same thing for the other buttons

int power; // power represents the desired velocity of our fly wheel
Task ShooterControl(){
bool but1 = false;

if(vexRT[Btn7D] == 1) { // you could use any button you want for this

if(but1 = false){
but1 = true;
power = 30;
wait1Msec(50);
}
else if(but1 = true){
but1 = false;
power = 0;
wait1Msec(50);
}

}

}

we use a PID control loop to keep our shooters at one velocity, so we feed the shooter control task a global variable power, and the PID loop keeps the wheel spinning at the speed set by power

Thanks for the help but i am new to programming so i dont know what is happening in the code you stated. i dont know where to even start. I use easy c for programming btw

ok, so the code I posted is for robotC and I am not terribly familiar with EasyC, so I can’t help you with the technical details, but I can at least help with the ideas, so to start off with and “if” statement is exactly what it sounds like, so
if(x == 2){
do this
}

will only “do this” IF x is equal to 2

so you can use these if statements to run or not run certain groups of code

next , the buttons on a vex JoyStick return a value of either one of zero, and you can find out the value of a joystick with the line vexRT[Btn…]

vexRT is a prefix that allows you to access anything on the vex controller, Btn… will tell the code what buttons you want to access, they are all labeled on the controller

so if we use this code

if(vexRT[Btn7D] == 1){
do this
}

the program will only “do this” if button seven down on the controller is pressed

in the code you can see I declare variables, and Booleans, so a variable can have different types and are all declared the same way

so this line

int x = 2;
declares an integer named x and says it is equal to 2, so you could use the value stored to x to do things

for example in my code

int power;
is declared and then later I say

power = 127;
so this sets the value of power to 127

later we could for example, use this integer to send power to a motor

a Boolean is a variable that has only two possible values, true or false, alternatively one or zero

so what the code I posted does is this

if you press a certain button, and the shooter is currently off, it sets power to 30, and then tells the robot that the shooter is now on. However, if the shooter is off, it sets power to 0 and tells the program the shooter is now off.

we can use power in other parts of the code to do things, for example we could set a motor to the value “power” and if power does not = zero, the motor would turn on

you also expressed interest in controlling the flywheels via rpm, this is a bit more complicated, but most of the required knowledge is math based and not programing based, so if you have a good grip on math it shouldn’t be too difficult

My team used to use easy c and we never figured out how to assign 1 motor to multiple buttons. My best advice would be to have someone on your team learn how to program so that you can use robot c.

totally agree with Michael, using robotC will help you in the long run, there are lots of great tutorials out there for RobotC and robotC is closer to other programing languages that easyC

I use easy C and I it’s possible to program different speeds for the same motors, I don’t know how to post the code so I’ll post screenshots. What you have to do is set the motor value of a joystick to digital motor to an int variable, the one I used is shooter_variable. Then you go ahead and create an if statement that if the button you’re using for the joystick to digital motor is pressed (==0) shooter_variable is assign to x speed. Then you can go ahead and create multiple if statement inside that if you press another button while pressing the same button, shooter_variable is assign to another speed.
PD: You don’t have to create the multiple speeds if statements inside the first if statement, I did that to use up to 7 different speeds with the right digital buttons on the joystick
Screenshot 2015-10-20 21.45.28.jpg

Vex_2382A, is it possible to create user functions, say “Full Power” where the motors are = 127 and “Partial Power” where the motors = 63 and then use the buttons to execute those functions?




Yes, you would just create a function where the speed is set to a certain value when it is called.

e.g. (Note this is robotC although the concepts are the same as easyC)
Setting up your functions.


void fw_fullPower()
{
	motor[flywheel] = 127;
}

void fw_halfPower()
{
	motor[flywheel] = 64;
}

void fw_stop()
{
	motor[flywheel] = 64;
}

void fw_setSpeed(int speed)
{
	motor[flywheel] = speed;
}

Setting your flywheel speed.


// If the button is pressed do something
if( vexRT[Btn5U] == 1 )
{
	fw_fullPower();
}
// if the first button was not pressed check if this button is pressed
// and do something
else if( vexRT[Btn5D] == 1 )
{
	fw_halfPower();
}
// If the other buttons are not pressed check if this button is pressed
// and do something
else if( vexRT[Btn6U] == 1 )
{
	fw_stop();
}

Note that this would be essentially identical to just changing the motor values if a button is pressed and thus the example above would be identical to doing this:

if( vexRT[Btn5U] == 1 )
{
	motor[flywheel] = 127;
}
else if( vexRT[Btn5D] == 1 )
{
	motor[flywheel] = 64;
}
else if( vexRT[Btn6U] == 1 )
{
	motor[flywheel] = 0;
}

I personally prefer to use a function as a wrapper with a parameter which sets a speed when controlling motors.

The wrapper function:

void fw_setSpeed(int speed)
{
	motor[flywheel] = speed;
}

Used in code (e.g.)


if( vexRT[Btn5U] == 1 )
{
	fw_setSpeed(127);
}

So the simplest way to do what you are describing is just to use a if-else statement to set a speed on a button press.

To create a toggle we just need to know if the flywheel is moving and thus whether a button has been pressed. So on the press of a button on your controller we set a boolean that holds whether the flywheel is meant to be moving to true. We also need to only make these controls work if the flywheel is not working, so we encompass everything in an if statement.


bool flywheelMoving = false;

// If the flywheel is not moving
if( flywheelMoving = false)
{
	if( vexRT[Btn5U] == 1 )
	{
		motor[flywheel] = 127;
		flywheelMoving = true;
	}
	else if( vexRT[Btn5D] == 1 )
	{
		motor[flywheel] = 64;
		flywheelMoving = true;
	}
	else if( vexRT[Btn6U] == 1 )
	{
		motor[flywheel] = 0;
		flywheelMoving = true;
	}
}

Then we can create a if statement dedicated just to if the flywheel is moving and in that statement we put a command so if the flywheel is running and any of these buttons are pressed, the program turns off the flywheel

// If the flywheel is moving
if( flywheelMoving == true )
{
	// If any of the buttons are pressed
	// We set the flywheel motors to speed 0
	if( vexRT[Btn5U] == 1 )
	{
		motor[flywheel] = 0;
		flywheelMoving =  false;
	}
	else if( vexRT[Btn5D] == 1 )
	{
		motor[flywheel] = 0;
		flywheelMoving = false;
	}
	else if( vexRT[Btn6U] == 1 )
	{
		motor[flywheel] = 0;
		flywheelMoving = false;
	}
}

This is a simpleish example of what you would have to make. Good Luck :smiley:

I like to use words instead of all of these numbers.

#define (no, not hashtag define) is a piece of code put in the header (below all of the motor things) that tells RobotC that something can be typed as something else. #define FULL 127 tells it that when FULL is typed, you want it to interpret as 127. The advantage of using #define is that you can easily change the values (since it is up near the top) instead of taking hours to dig through your code to find that part of the function. Also, others can look at your program and understand it a bit more.

I think this part will just cycle through super fast, giving you no control over the speed of the flywheel, so you will need a check to see if the button has been released yet (something waiting for vexRT[Btn] to be 0 before going on).

And to make the program a bit shorter,


if(flywheelMoving)   //An if statement looks at if the condition inside its parenthesis is true, so the "==true" is not needed.
{
[the "true" stuff]
}
else  //If it's not true, it's false.
{
[the "false" stuff]
}

Sorry for the image quality
A simple way to have different flywheel speeds on your joystick…
vexforum.JPG

Yes, I know. You could also use


const int Full 127

though I personally prefer using #define as you describe.

I think in this case understanding is irrelevant, this was designed as a SIMPLE example, that as a person grows in knowledge and skill they can make improvements( or rewrite it :stuck_out_tongue: ). If I wanted to write a program I would write it very differently to how I have written it here. Also remember we are only setting this value… once …meaning it shouldn’t take hours to dig through your code. Your suggestions is a good solution if you are writing autonomous ( with many references to certain speeds ), but we are not.

Well, again it was designed as a simple program, although objectively I should have added a wait. I also do agree I should have made a variable that holds the last state of the button ( something i will correct in another post )

Thanks for reducing my code by!.. 21 Characters. But having the if statement means it’s easier to read IMO ( something that you are advocating for above? ) :stuck_out_tongue:

And besides, this was provided as pseudo-type code example. Just as a basis for what you could do.

Okay I edited the code and an example is available here.

Basically in this example I created a function that checks if any buttons are pressed and added this to the flywheel moving if statement:


if( flywheelMoving == true && fw_anyButtonsPressed() == false)

This means you will only be able to turn off the flywheel after you have turned it on and you have let go of the controller. I also added a wait, as it too is a good idea which I didn’t provide in my original example as I was focusing on the question at hand:

    
// Dont Hog CPU
wait1Msec(25)

Thank you all for the suggestions and help. I’ll admit that some of the nuanced back and forth is lost on me. I appreciate the attempts at simplicity. I’m trying to learn this and in turn teach it to middle schoolers, 1 of whom has 1 year in robotics and 1 who has none.

Couldn’t he use a PID Control for his flywheel

Hey it is me again and i just wanted to thank all of u guys for helping. I finally got it to work and credits goes to you guys, so once again THANK YOU!:smiley:

Psst, just copy paste from sample programs :smiley:

I don’t believe there is a toggle switch example (especially when mapped across different buttons) in the sample programs :confused: