I am trying to code the manual control for our robot. Right now, the robot uses a certain reload and fire function for the catapult. It looks like this:
This method works, but the problem is that the robot cannot move while the catapult is reloading or firing, so I tried to create a separate task so that the normal manual controls work while the robot is reloading or firing. It looks like this:
int catapult(){
while(true){
Controller.ButtonR1.pressed(reload);
Controller.ButtonR2.pressed(fire);
vex::task::sleep(20);
}
return(0);
}
void usercontrol( void ) {
vex::task pos = vex::task(catapult);
while(true){ //rest of manual control code
The problem is that this doesn’t resolve the issue, and the robot still can’t move while it is reloading or shooting. Is this an issue with my code or Vex Coding Studios itself? If we can get this working, then that will save us a few seconds, but every second counts.
I personally i’m trying to create a driver control program in VCS that uses a limit sensor that when released will set the linear puncher motor to turn for 1 rotation while still being able to drive. But I cannot drive my robot around or use any of the other motors unit the Puncher motor has finished its 1 rotation. I Believable this is a similar issue to what
moponthebucket is having.
Here is some example code. It’s from VEXcode but should work in VCS as well.
It uses additional tasks for drive and controlling a motor with one button.
#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;
// define your global instances of motors and other devices here
vex::motor leftMotor( vex::PORT1 );
vex::motor rightMotor( vex::PORT2 );
vex::motor puncher( vex::PORT3 );
vex::controller Controller1;
vex::task dtask; // variable to hold drive task instance
vex::task ptask; // variable to hold puncher task instance
void pre_auton( void ) {
// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, ...
}
int puncherTask() {
while(1) {
if( Controller1.ButtonB.pressing() ) {
// send motor around once
puncher.rotateFor(fwd, 1, rotationUnits::rev, 70, rpm );
// wait for button release
while( Controller1.ButtonB.pressing() ) {
vex::task::sleep(10);
}
}
vex::task::sleep(20);
}
return(0);
}
int driveTask() {
while(1) {
leftMotor.spin( fwd, Controller1.Axis3.position(),velocityUnits::pct );
rightMotor.spin( fwd, Controller1.Axis2.position(),velocityUnits::pct );
vex::task::sleep(20);
}
return(0);
}
void autonomous( void ) {
int count = 0;
// make sure tasks are not running in auton
dtask.stop();
ptask.stop();
// make sure motors are stopped
leftMotor.stop();
rightMotor.stop();
puncher.stop();
while(1) {
// just show we are running
Brain.Screen.printAt( 10, 140, "Auton running %d", count++ );
vex::task::sleep(100);
}
}
void usercontrol( void ) {
int count = 0;
dtask = vex::task( driveTask );
ptask = vex::task( puncherTask );
while (1) {
// just show we are running
Brain.Screen.printAt( 10, 40, "Driver running %d", count++ );
Brain.Screen.printAt( 10, 60, "Left %7.2f", leftMotor.velocity(pct) );
Brain.Screen.printAt( 10, 80, "Right %7.2f", rightMotor.velocity(pct) );
Brain.Screen.printAt( 10, 100, "Punch %7.2f", puncher.rotation(rotationUnits::rev) );
vex::task::sleep(20); //Sleep the task for a short amount of time to prevent wasted resources.
}
}
//
// Main will set up the competition functions and callbacks.
//
int main() {
int count = 0;
//Set up callbacks for autonomous and driver control periods.
Competition.autonomous( autonomous );
Competition.drivercontrol( usercontrol );
//Run the pre-autonomous function.
pre_auton();
//Prevent main from exiting with an infinite loop.
while(1) {
Brain.Screen.printAt( 10, 20, "Main running %d", count++ );
vex::task::sleep(100);
}
}
The most likely explanation is that you’re using blocking commands or the equivalent with while loops. Methods like rotateTo and rotateFor are blocking by default, while startRotateTo and startRotateFor are not. You can change rotateTo and rotateFor to non-blocking by adding “,false” to the end of the arguments.
Oh, also @jpearman gave you an approach that makes them separate tasks. You can alternatively use what I wrote above to keep them inline without blocking.
Like everyone else has said, you could use tasks or non-blocking commands. I just use a series of if statements without a while. The summary of these is that if the catapult is not at the desired level and the fire button is not being pressed, then set the catapult to move down. If the catapult is at the desired level and the fire button is not being pressed, then hold the catapult there. If the fire button is being pressed, then run the catapult so that it fires.