Our team is using multiple blocks “when controller button” blocks (for example “when controller button R Up Pressed”, “when controller button F down Pressed”, “when controller Axis A is changed”)
What is the expected behavior: if controller button R Up Pressed, then controller button F down Pressed while the previous is not finished, are both subroutines going to execute in parallel gracefully? what if one command contradicts one that was issued by the other branch?
what is supposed to happen when a drive train command is issued while an existing one is still executing?
is there a limit of how many “when controller” blocks can be used?
some times we get weird behavior and the robot does not respond (even to simple button pressed)
thanks
It’s difficult to give a comprehensive answer to very general questions about event handling because it does depend a lot on your specific project, but here’s some background info that may help.
The code underneath a “when button” hat is called an event handler, the button is pressed, that’s an event, and code runs to do something in response to that event. The best situation is to run your code and get out of the event handler as fast as possible. There are limits to how many events can be created (meaning how many when blocks) but that’s quite a large number and it’s unlikely you would run into that limit. However, there is also a limit to how many event handlers can run at any one time, that limit is fairly low and it sounds like you are running into that situation if occasionally a button event is being missed, perhaps you could combine some event handling or use polling for the controller axis rather than having them trigger a changed event due to the number of events that will cause.
Motors will usually perform the last action they receive, so starting to move a motor in one event handler followed by stopping in another should not be an issue.
Right now, they are using the “when controller Axis A is changed” , then read the controller A position to adjust the speed of motor. I wonder if it would be better to use a “when started” and a forever loop instead. Would that be more robust?
Also they are using a lot of broadcast messages. I assume that the “when I receive message” blocks are not really event handlers. these should be pretty safe. correct?
Everything under a When hat is an event handler. Broadcasting a message is a user generated event, pushing a button is a hardware generated event. A myblock function can replace the use of broadcast in many cases. IQ is very resource constrained, there’s not much memory, it limits the number of things that can run simultaneously.
You are going to want to pull out an events block that says when controller 1 button R1 ( or what ever button. Then attach the turn motor 1 ( or whatever) and Motor whatever hope this helps