Callbacks

set a variable like
int speed=0 ;/*always have a initial value for variables or there will be trouble */
and in your operator control loop
while(1)
{
speed= Controller1.ButtonX.pressed()? speed+1:speed;
speed= speed>=4? 0:speed;
switch (speed)
{
case 0: motor.spin(directionType::fwd,50,velocityUnits::pct);
case 1: motor.spin(directionType::fwd,70,velocityUnits::pct);
case 2: motor.spin(directionType::fwd,90,velocityUnits::pct);
case 3: motor. stop(brakeType:hold) ;
}
}
edit : @enothecool

1 Like

What do you mean by trouble when you say always have a number for variables or something will go wrong

the variable’s value is saved in the brain’s RAM and when you declare a variable you clear up a slot to store it. however the previous stuff saved in there isn’t always cleared and may lead to errors while running the programme.
edit @enothecool
dang it why do I always forget to @

1 Like

I have now realized that our driver wants us to change the wheel speed not the arm speed, but it is found through an equation. Anyone know how I would slow this down with the press of a button?

  LW2.spin(vex::directionType::fwd, Controller1.Axis2.value() + Controller1.Axis4.value() - Controller1.Axis1.value(), vex::velocityUnits::pct);
  LW1.spin(vex::directionType::fwd, Controller1.Axis2.value() + Controller1.Axis4.value() + Controller1.Axis1.value(), vex::velocityUnits::pct);
  RW2.spin(vex::directionType::fwd, Controller1.Axis2.value() - Controller1.Axis4.value() + Controller1.Axis1.value(), vex::velocityUnits::pct);
  RW1.spin(vex::directionType::fwd, Controller1.Axis2.value() - Controller1.Axis4.value() - Controller1.Axis1.value(), vex::velocityUnits::pct);
1 Like

use a variable as a coefficient of the stick values and have it change with the press of a button

Alright now repeat that in english

int coefficient;
make the motor spin for speed coefficient*joystick value
coefficient = buttonpressed? coefficient + 0.5:coefficient
coefficient = coefficient >1? 0. 5:coefficient;

I think I get what your saying. I use the current equation separately and then multiply it by something to find out it’s value?

yeah you got it BTW r u using mecanums?

No holonomic drive
20 characters

x drive then? then I assume you have a lift bot?

Yeah
20 characters
Needs more

Well, after putting some heads together in robotics our solution was this
(Controller1.Axis2.value() + Controller1.Axis4.value() - Controller1.Axis1.value())*x
with x as a variable we change for the different speeds with the change activated by this
Controller1.ButtonY.pressed(function1);
anyone know why this wont work?

1 Like

Anyone have a solution?

have the function run upon release of the Button

I will try that
20 characters

go like
if(pressed)
{
while (pressed) {}
function;
}

our code was like this
controller.button.pressed(function)
we had two different buttons and they changed the value of x

@jpearman, could you please confirm that when we register .pressed() or .released() event on a button for the second time, it does not consume additional thread resource out of the limited pool of 100, but uses already allocated thread for the new callback function?

4 Likes

It does consume additional resource, event handlers should only be registered once, if you register either the same or an additional callback the event will call both handlers.

On V5 each event handler is a thread blocked on a semaphore, when the event fires, vexos released the semaphore allowing the thread to run, the thread calls the user registered callback.

There is currently no way to unregister an individual event handler.

On IQ, which uses the same core functionality in VEXcode, we don’t have enough memory to dedicate a thread to each event so we have to use a thread pool instead, this limits the number of events that can simultaneously fire and be serviced.

5 Likes