Have 2 callback functions managing speed changes in specific situations based. Use a button released event to set a boolean variable that is then checked during user control to manage motor speeds.
Works as expected except when our robot is being controlled by field control software during competition. Any ideas about what might be going on?
Here’s the relevant code you requested. Thanks for your prompt reply.
#include "vex.h"
using namespace vex;
// A global instance of vex::brain used for printing to the V5 brain screen
//vex::brain Brain;
// A global instance of vex::competition
vex::competition Competition;
bool isSlow = false;
//Function Prototypes
void toggleSpeed();
void toggleIntake();
// define your global instances of motors and other devices here
vex::motor LeftMotor = vex::motor( vex::PORT1, false );
vex::motor RightMotor = vex::motor( vex::PORT10, true );
vex::motor LiftMotor = vex::motor( vex::PORT2, true );
//vex::motor LiftMotor2 = vex::motor( vex::PORT9, false );
vex::motor IntakeMotor1 = vex::motor( vex::PORT3, false );
vex::motor IntakeMotor2 = vex::motor( vex::PORT8, true );
vex::controller Controller1 = vex::controller();
/*---------------------------------------------------------------------------*/
/* Pre-Autonomous Functions */
/* */
/* You may want to perform some actions before the competition starts. */
/* Do them in the following function. You must return from this function */
/* or the autonomous and usercontrol tasks will not be started. This */
/* function is only called once after the cortex has been powered on and */
/* not every time that the robot is disabled. */
/*---------------------------------------------------------------------------*/
void pre_auton( void ) {
}
void autonomous( void ) {
}
void usercontrol( void ) {
// User control code here, inside the loop
while(1) {
LeftMotor.spin(vex::directionType::fwd, Controller1.Axis3.position(), vex::velocityUnits::pct);
RightMotor.spin(vex::directionType::fwd, Controller1.Axis2.position(), vex::velocityUnits::pct);
if(isSlow){
LeftMotor.spin(vex::directionType::fwd, Controller1.Axis3.position() * 0.25, vex::velocityUnits::pct);
RightMotor.spin(vex::directionType::fwd, Controller1.Axis2.position() * 0.25, vex::velocityUnits::pct);
}
// Sleep the task for a short amount of time to prevent wasted resources.
vex::task::sleep(20);
}
}
//
// Main will set up the competition functions and callbacks.
//
int main() {
Competition.autonomous( autonomous );
Competition.drivercontrol( usercontrol );
//Run the pre-autonomous function.
pre_auton();
Controller1.ButtonX.released(toggleSpeed);
Controller1.ButtonR1.released(toggleIntake);
//Prevent main from exiting with an infinite loop.
while(1) {
vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
}
}
void toggleSpeed(){
isSlow = !isSlow;
}
I tried you code, it does work for me, however, one change I would make is not sending values to the motors twice in the while loop when the isSlow flag is set.
void usercontrol( void ) {
// User control code here, inside the loop
while(1) {
if(isSlow){
LeftMotor.spin(vex::directionType::fwd, Controller1.Axis3.position() * 0.25, vex::velocityUnits::pct);
RightMotor.spin(vex::directionType::fwd, Controller1.Axis2.position() * 0.25, vex::velocityUnits::pct);
}
else {
LeftMotor.spin(vex::directionType::fwd, Controller1.Axis3.position(), vex::velocityUnits::pct);
RightMotor.spin(vex::directionType::fwd, Controller1.Axis2.position(), vex::velocityUnits::pct);
}
// Sleep the task for a short amount of time to prevent wasted resources.
vex::task::sleep(20);
}
}
perhaps also add some display on the brain or controller screen so you can see what the value of the isSlow flag is.
//Prevent main from exiting with an infinite loop.
while(1) {
Controller1.Screen.setCursor(3,1);
Controller1.Screen.print( "%d", isSlow );
vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
}
Thanks for those helpful suggestions. My students competed for the first time last weekend. Their code works well in all cases except when they are connected to the field control cables during a match. I do not know how to help them because we can not simulate the match conditions. Do you have any suggestions? I’m not sure I understand polling vs interrupt event processing in the multi-thread or multi-process VEX system.
Wondering what restrictions are placed on processing while under field control that are not present otherwise.