Programming states


#1

Does anyone know how to programme ‘states’? This is when you press either the small E or F buttons on the controller and the brain will turn a motor a specific amount. Very useful if you want to raise or lower an arm to an exact position. Thanks


#2

In what programming language?


#3

We do this using RobotC. We set a task up at the top with a global variable, similar to this:
#pragma config(Motor, motor1, armMotor, tmotorVexIQ, PIDControl, encoder)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

int armState = 0;

task armMovement()
{
if(armState == 0)
{
setMotorSpeed(armMotor, 0);
wait1Msec(10);
}
if(armState == 1)
{
setMotorSpeed(armMotor, 25);
wait1Msec(10);
}
}

task main()
{
startTask(armMovement);

while(true)
if(getJoystickValue(BtnLUp) == 1)
{
armState = 0;
while(getJoystickValue(BtnLUp) == 1)
{
wait1Msec(10);
}
}
if(getJoystickValue(BtnLDown) == 1)
{
armState = 1;
while(getJoystickValue(BtnLDown) == 1)
{
wait1Msec(10);
}
}

}


#4

We use robotC graphical. Thanks


#5

If you are using the graphical version it would be difficult to do what you’re asking. However, this can be done fairly easily with regular RobotC, as shown by DaddyCrusader. He takes advantage of multitasking. See his code. The task armmovement() does what you want. It is called from task main() as shown.

If you like, you can write the non-task code in Graphical then convert the file to text. Under View|Convert Graphical File to Text. Once done a new file appears with a different extension (".c*"). It can also be compiled and downloaded to the robot after you have added in the desire task code. You can’t go backwards to the Graphical version from here, but the original Graphical file is unchanged.


#6

I can tell you that you need to check the rotation degrees and stop when it is equal to OR greater than your target. It’s very hard to catch it when it is exactly equal to the target degrees.

We did this using modkit. Use rotation degrees and a variable that toggles active/stop states (to reset for when you want to begin movement again).


#7

If you like the ideas of motor limits be sure to see my post in the tech discussion area. we have a code example there.


#8

Thank you. I have converted the file below. Where does daddycrusader’s code go?



Screenshot 2019-02-01 21.46.16.jpg