ok, now that I fixed the spelling of competition in all the screen shots ![]()
first topic is incorrect use of event handlers, something we have covered before but it seems everyone forgets.
The symptom is that, for example, an event handler on a controller button sometimes works and sometimes doesn’t. At worlds the most common use was for firing pneumatics, a button push to set one state and a second button push to revert back to original state.
simple code for a callback could look as follows.
void button_callback() {
static int calls = 0;
Brain.Screen.setFont( mono40 );
Brain.Screen.printAt( 100, 160, "Button %4d", ++calls );
}
This is just for demonstration purposes and prints the number of callbacks on the brain screen.
We often saw event registration at the beginning of driver control like this.
void usercontrol( void ) {
Controller.ButtonA.pressed( button_callback );
// more code
}
So what’s the problem with that ?
usercontrol can be called more than once.
The reason is that if the controller is connected (radio linked) to the robot and the field control is not connected (either new or legacy) then usercontrol will run. This is expected and what we usually want to happen when practicing. Once the field control is connected then the robot will usually be disabled unless the match has already started. When the driver control period starts then usercontrol will run again and the button_callback registered a second time, when controller button A is pressed button_callback will be called twice (and potentially more if field control was connected and disconnected more than once) and obviously if it is toggling a pneumatic solenoid nothing will actually happen (the solenoid will be enable and disabled very quickly or not at all).
Event registration should only happen once at the beginning of main. This would be the correct place for the code.
int main() {
Controller.ButtonA.pressed( button_callback );
Competition.autonomous( autonomous );
Competition.drivercontrol( usercontrol );
pre_auton();
while(1) {
this_thread::sleep_for(10);
}
}
The order that teams connected to the field and started their code varied and they had inconsistent results because of registering events in usercontrol.