Questions on LEDS and Integrated Shaft encoders

I am new to programming VEX this year and i have some questions. 1) How do you program LEDs? 2) How do you program integrated shaft encoders with a velocity and not a power lever?
Thanks guys

There’s a few conversations regarding velocity control that are very helpful.
Flywheel Velocity Control
Flyhweel Velocity Control Revisited
Calculate RPM for Flywheel

I use this, which takes how many ticks the encoder has read in 20 milliseconds. Works nicely, however it has issues with multi court speeds. (It goes backwards for 1 seconds before going back up to speed.) (Not trying to hijack a conversation, but does anyone know how to fix this?)

//Global Variables
int target = -0; //Target value (Ticks)
int integrator = 0; //Integrator value
int Sensor_last = 0; // Previous sensor reading
int Sensor_Currant = 0;   // Currant sensor reading
int Velocity_Currant = 0; // Currant speed
int Velocity_Error = 0; // Error in the speed
int Motor_Out = 0; //Motor Output
float KI = 0.027; //Gain constant
//________________________________________________________________________________________________________________________
//Flywheel Task

  task Flywheel_System()
  {
  while(true)
	{
//====================================================

	//sensor cache
	Sensor_Currant = -SensorValue[Rotationsensor];
	Velocity_Currant = Sensor_Currant - Sensor_last;
	Sensor_last = Sensor_Currant;
//---------------------------------------------------------
	//Error Calculation
	Velocity_Error = target - Velocity_Currant;
//---------------------------------------------------------
	//Add to the integrator
	integrator += (Velocity_Error * KI);
//---------------------------------------------------------
	//Cap the Integrator at 127
	if (integrator > 127){ integrator = 127;}
	if (integrator < -127){integrator = -127;}
//---------------------------------------------------------
	//Set integrator to motors
	Motor_Out = integrator;
	motor[FlyM1] = Motor_Out;
	motor[FlyM2] = Motor_Out;
//----------------------------------------------------------
	wait1Msec(20);
  }//ends loop
  }
//___________________

Thank you this helps a lot.

To program LEDs, configure them in Motor and Sensor Setup, and then change the value by doing this:


SensorValue[led] = 1 or 0; //Turn on (1) or off (0)

To prevent the flywheel motors from running backwards, collar the motor power to 0 and 127, like this:


if(mtrPwr < 0) {
  mtrPwr = 0;
}

else if(mtrPwr > 127) {
  mtrPwr = 127;
}

No problem, one last thing I forgot to mention that makes things easier, just add a clear sensor value ( SensorValue[Sonar] = 0 ; ) statement in the pre-auton part of your competition code. That way the motors won’t do anything funky in autonomous.
Its probably just a problem with my code, but it helps.

Thank you! I never really worked hard to fix the problem because we have a ratchet, so it just laged. :slight_smile:

If you run autonomous mutiple times or drive around and then run autonomous without restarting the Cortex before running the autonomous program, your encoders won’t start with a value of 0, which would certainly cause problems in a program that depends on encoders (say, for moving a certain distance).

For some reason it wasn’t clearing even when turned off. I think anyway, it was a while ago.
Also our encoder is near to our main flywheel gear, so it is effected by the tiniest of movements by the flywheel. Thus the encoder clear is still in our program.

Yeah, that’s possible. But either way, it’s a good “just in case” type of thing; you never know what will hapoen when you’re at a tournament and short on time before a match (restarting a robot takes time).