We are trying to program our catapult to automatically reload. It uses a slip gear with a ratchet and we want it go immediately back to the firing position before shooting. The motors for the catapult rotate the same degree before firing.
We are using Robot Mesh Studio and are trying to used the “pressed” label, the same one found in VCS, but aren’t able to find it. When we type “press”, the only thing that pops up is “pressing.” There is no “pressed” label. Is there something that we are missing, any other labels, or way to do a pressed function within Robot Mesh Studio?
On a limit switch, VCS has an event called pressed, where the VCS runtime schedule calls the function provided whenever the limit switch is pressed. It’s defined as
vex::limit.pressed(void(*callback)(void))
.
The
vex::limit.pressing()
function returns the current state of the limit switch as either 1 or 0. This is available in both VCS and Robot Mesh Studio. Use pressing() in a loop to see if the switch is pressed.
Robot Mesh Studio uses a different runtime, a real-time preemptive threading system, which doesn’t yet support event callbacks like limit.pressed. However, it’s quite easy to create a separate thread which polls the states that you’re interested in and calls separate functions, for example when a limit switch is pressed. Let me know if you need an example, and whether you’re using Blockly, Python or C++.
We are using V5 with C++ for Robot Mesh Studio. An example of that limit switch function would be extremely helpful.
It’s untested because I wrote it 5 minutes ago, but try something like this.
Here’s the code @John TYler wrote (corrected version):
// VEX V5 C++ Project
#include "vex.h"
using namespace vex;
//#region config_globals
vex::brain Brain;
vex::limit limit_switch(Brain.ThreeWirePort.A);
//#endregion config_globals
void handleSwitch () {
static bool lastPressed = limit_switch.pressing();
while (true) {
if (limit_switch.pressing() && !lastPressed) {
lastPressed = true;
//do something on rising edge
} else if (!limit_switch.pressing() && lastPressed) {
lastPressed = false;
//do something else on falling edge
}
this_thread::yield();
}
}
int main(void) {
// Start here
//create the event handler thread and start it:
thread T = thread(handleSwitch);
while (true) {
//body of program
this_thread.yield();
}
}
Just noticed a pretty big error in it. Fixed it.
Is this only for the red limit switches? Is there any way we can switch it to the readers within the new V5 Motors?
You could replace
limit_switch.pressing()
with any function that returned a boolean value, like a button off the handheld controller, or a conditional construct based on the motor’s encoder, such as
motor.rotation(vex::rotationUnits::degrees) < 120.0
. The rising edge block would fire when it became true and the falling edge block would fire when it became false. If you want to watch the motor’s encoder with a slip gear catapult, make sure that you reset the encoder between firings with either the
resetRotation
or
setRotation
methods on the motor or write your code to be ambivalent about multiples of 360 degrees.