// Make sure all required headers are included.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include "vex.h"
using namespace vex;
// Brain should be defined by default
brain Brain;
// START V5 MACROS
#define waitUntil(condition) \
do { \
wait(5, msec); \
} while (!(condition))
#define repeat(iterations) \
for (int iterator = 0; iterator < iterations; iterator++)
// END V5 MACROS
// Robot configuration code.
controller Controller1 = controller(primary);
motor leftMotorA = motor(PORT12, ratio18_1, false);
motor leftMotorB = motor(PORT10, ratio18_1, false);
motor_group LeftDriveSmart = motor_group(leftMotorA, leftMotorB);
motor rightMotorA = motor(PORT14, ratio18_1, true);
motor rightMotorB = motor(PORT13, ratio18_1, true);
motor_group RightDriveSmart = motor_group(rightMotorA, rightMotorB);
drivetrain Drivetrain = drivetrain(LeftDriveSmart, RightDriveSmart, 319.19, 295, 40, mm, 1);
motor roller = motor(PORT7, ratio18_1, false);
digital_out endgame1 = digital_out(Brain.ThreeWirePort.B);
digital_out shooterthing = digital_out(Brain.ThreeWirePort.H);
digital_out endgame2 = digital_out(Brain.ThreeWirePort.A);
motor flywheel = motor(PORT2, ratio18_1, false);
motor intakeMotorA = motor(PORT3, ratio18_1, false);
motor intakeMotorB = motor(PORT4, ratio18_1, false);
motor_group intake = motor_group(intakeMotorA, intakeMotorB);
// define variable for remote controller enable/disable
bool RemoteControlCodeEnabled = true;
// define variables used for controlling motors based on controller inputs
bool Controller1LeftShoulderControlMotorsStopped = true;
bool Controller1RightShoulderControlMotorsStopped = true;
bool Controller1UpDownButtonsControlMotorsStopped = true;
bool DrivetrainLNeedsToBeStopped_Controller1 = true;
bool DrivetrainRNeedsToBeStopped_Controller1 = true;
// define a task that will handle monitoring inputs from Controller1
int rc_auto_loop_function_Controller1() {
// process the controller input every 20 milliseconds
// update the motors based on the input values
while(true) {
if(RemoteControlCodeEnabled) {
// calculate the drivetrain motor velocities from the controller joystick axies
// left = Axis3 + Axis1
// right = Axis3 - Axis1
int drivetrainLeftSideSpeed = Controller1.Axis3.position() + Controller1.Axis1.position();
int drivetrainRightSideSpeed = Controller1.Axis3.position() - Controller1.Axis1.position();
// check if the value is inside of the deadband range
if (drivetrainLeftSideSpeed < 5 && drivetrainLeftSideSpeed > -5) {
// check if the left motor has already been stopped
if (DrivetrainLNeedsToBeStopped_Controller1) {
// stop the left drive motor
LeftDriveSmart.stop();
// tell the code that the left motor has been stopped
DrivetrainLNeedsToBeStopped_Controller1 = false;
}
} else {
// reset the toggle so that the deadband code knows to stop the left motor nexttime the input is in the deadband range
DrivetrainLNeedsToBeStopped_Controller1 = true;
}
// check if the value is inside of the deadband range
if (drivetrainRightSideSpeed < 5 && drivetrainRightSideSpeed > -5) {
// check if the right motor has already been stopped
if (DrivetrainRNeedsToBeStopped_Controller1) {
// stop the right drive motor
RightDriveSmart.stop();
// tell the code that the right motor has been stopped
DrivetrainRNeedsToBeStopped_Controller1 = false;
}
} else {
// reset the toggle so that the deadband code knows to stop the right motor next time the input is in the deadband range
DrivetrainRNeedsToBeStopped_Controller1 = true;
}
// only tell the left drive motor to spin if the values are not in the deadband range
if (DrivetrainLNeedsToBeStopped_Controller1) {
LeftDriveSmart.setVelocity(drivetrainLeftSideSpeed, percent);
LeftDriveSmart.spin(forward);
}
// only tell the right drive motor to spin if the values are not in the deadband range
if (DrivetrainRNeedsToBeStopped_Controller1) {
RightDriveSmart.setVelocity(drivetrainRightSideSpeed, percent);
RightDriveSmart.spin(forward);
}
// check the ButtonL1/ButtonL2 status to control intake
if (Controller1.ButtonL1.pressing()) {
intake.spin(forward);
Controller1LeftShoulderControlMotorsStopped = false;
} else if (Controller1.ButtonL2.pressing()) {
intake.spin(reverse);
Controller1LeftShoulderControlMotorsStopped = false;
} else if (!Controller1LeftShoulderControlMotorsStopped) {
intake.stop();
// set the toggle so that we don't constantly tell the motor to stop when the buttons are released
Controller1LeftShoulderControlMotorsStopped = true;
}
// check the ButtonR1/ButtonR2 status to control roller
if (Controller1.ButtonR1.pressing()) {
roller.spin(forward);
Controller1RightShoulderControlMotorsStopped = false;
} else if (Controller1.ButtonR2.pressing()) {
roller.spin(reverse);
Controller1RightShoulderControlMotorsStopped = false;
} else if (!Controller1RightShoulderControlMotorsStopped) {
roller.stop();
// set the toggle so that we don't constantly tell the motor to stop when the buttons are released
Controller1RightShoulderControlMotorsStopped = true;
}
// check the ButtonUp/ButtonDown status to control flywheel
if (Controller1.ButtonUp.pressing()) {
flywheel.spin(forward);
Controller1UpDownButtonsControlMotorsStopped = false;
} else if (Controller1.ButtonDown.pressing()) {
flywheel.spin(reverse);
Controller1UpDownButtonsControlMotorsStopped = false;
} else if (!Controller1UpDownButtonsControlMotorsStopped) {
flywheel.stop();
// set the toggle so that we don't constantly tell the motor to stop when the buttons are released
Controller1UpDownButtonsControlMotorsStopped = true;
}
}
// wait before repeating the process
wait(20, msec);
}
return 0;
}
task rc_auto_loop_task_Controller1(rc_auto_loop_function_Controller1);
#pragma endregion VEXcode Generated Robot Configuration
// Include the V5 Library
#include "vex.h"
// Allows for easier use of the VEX Library
using namespace vex;
competition Competition;
float myVariable;
// "when autonomous" hat block
int onauton_autonomous_0() {
endgame1.set(true);
endgame2.set(true);
Brain.Screen.clearScreen();
Brain.Screen.setPenColor(white);
Brain.Screen.setFillColor(blue);
Brain.Screen.drawRectangle(100, 50, 279, 139);
Brain.Screen.setPenColor(white);
Brain.Screen.setFillColor(cyan);
Brain.Screen.drawRectangle(150, 75, 179, 89);
Brain.Screen.setFont(prop20);
Brain.Screen.setCursor(6, 22);
Brain.Screen.setFillColor(cyan);
Brain.Screen.setPenColor(purple);
Brain.Screen.print("Roger64");
Brain.Screen.setFont(prop20);
Brain.Screen.setCursor(7, 22);
Brain.Screen.setFillColor(cyan);
Brain.Screen.print("6390-R");
roller.setVelocity(100.0, percent);
shooterthing.set(true);
intake.setVelocity(100.0, percent);
flywheel.setVelocity(100.0, percent);
Drivetrain.setDriveVelocity(30.0, percent);
Drivetrain.setTurnVelocity(65.0, percent);
Drivetrain.setStopping(brake);
Drivetrain.driveFor(forward, 2.5, inches);
roller.spinFor(reverse, 0.6, turns);
Drivetrain.driveFor(reverse, 22, inches);
Drivetrain.turnFor(right, 155, degrees);
Drivetrain.driveFor(forward, 22.5, inches);
roller.spinFor(reverse, 0.6, turns);
Drivetrain.driveFor(reverse, 20, inches);
Drivetrain.turnFor(right, 240, degrees);
Drivetrain.driveFor(forward, 123, inches);
Drivetrain.turnFor(right, 75, degrees);
Drivetrain.driveFor(forward, 14, inches);
roller.spinFor(forward, 0.6, turns);
Drivetrain.driveFor(reverse, 15, inches);
Drivetrain.turnFor(left, 155, degrees);
Drivetrain.driveFor(forward, 15, inches);
roller.spinFor(reverse, 0.6, turns);
Drivetrain.driveFor(reverse, 13, inches);
Drivetrain.turnFor(right, 155, degrees);
Drivetrain.driveFor(reverse, 13, inches);
flywheel.spinFor(forward, 5, turns);
shooterthing.set(false);
flywheel.spinFor(forward, 5, turns);
shooterthing.set(true);
flywheel.spinFor(forward, 5, turns);
shooterthing.set(false);
flywheel.spinFor(forward, 5, turns);
Drivetrain.driveFor(forward, 26, inches);
Drivetrain.turnFor(left, 80, degrees);
endgame2.set(false);
endgame1.set(false);
return 0;
}
// "when driver control" hat block
int ondriver_drivercontrol_0() {
endgame1.set(true);
endgame2.set(true);
Brain.Screen.clearScreen();
Brain.Screen.setPenColor(white);
Brain.Screen.setFillColor(blue);
Brain.Screen.drawRectangle(100, 50, 279, 139);
Brain.Screen.setPenColor(white);
Brain.Screen.setFillColor(cyan);
Brain.Screen.drawRectangle(150, 75, 179, 89);
Brain.Screen.setFont(prop20);
Brain.Screen.setCursor(6, 22);
Brain.Screen.setFillColor(cyan);
Brain.Screen.setPenColor(purple);
Brain.Screen.print("Roger64");
Brain.Screen.setFont(prop20);
Brain.Screen.setCursor(7, 22);
Brain.Screen.setFillColor(cyan);
Brain.Screen.print("6390-R");
intake.setVelocity(100, percent);
roller.setVelocity(100, percent);
flywheel.setVelocity(100, percent);
Drivetrain.setDriveVelocity(100, percent);
//pneumatics
while(true){
if(Controller1.ButtonA.pressing()){
endgame2.set(false);
}
else{
endgame2.set(true);
wait(5, msec);
//pneumatics
if (Controller1.ButtonY.pressing()){
endgame1.set(false);
}
else{
endgame1.set(true);
wait(5, msec);
//pneumatics
if (Controller1.ButtonX.pressing()){
shooterthing.set(false);
}
else{
shooterthing.set(true);
}
wait(5, msec);
}
}return 0;
}
}
void VEXcode_driver_task() {
// Start the driver control tasks....
vex::task drive0(ondriver_drivercontrol_0);
while(Competition.isDriverControl() && Competition.isEnabled()) {this_thread::sleep_for(10);}
drive0.stop();
return;
}
void VEXcode_auton_task() {
// Start the auton control tasks....
vex::task auto0(onauton_autonomous_0);
while(Competition.isAutonomous() && Competition.isEnabled()) {this_thread::sleep_for(10);}
auto0.stop();
return;
}
int main() {
vex::competition::bStopTasksBetweenModes = false;
Competition.autonomous(VEXcode_auton_task);
Competition.drivercontrol(VEXcode_driver_task);
}```
This is my code. It used to have 3 loops in the pneumatics section, and only one of them would work (working solenoid, cord, etc.)
A senior teacher at my school checked it and had me remove the loops, making it only one.
It stopped working.
I made it not loop at all, and it still did not work.
The teacher doesn't know what's wrong, and says there seems to be a problem with the bits I didn't write (automatically filled in) (started by making blocks code, just putting in an autonomous hat block and a driver hat block, then switching to c++, thus making a template). I have not edited the pragma, functions, or the stuff that comes after the driver code.
There are no errors in the feed. It does nothing. Previously only one worked. Now none do. The ports, cords, and solenoids work, and the base code for the pneumatics has been confirmed to work.
Please help.
Hello. Next time u send code, make sure u only send the part u need help on, it makes it easier to find your code. For me to help I need you to answer a couple of questions:
Is this in your driver control or autonomous
Autonomous does not loop
Driver does loop, and you may have to use multitasking/threading to run multiple functions
You may be missing closing brackets here. Clearly indenting your code helps with these sorts of issues.
Both autonomous and driver. Can you explain further?
I have closing brackets. There are no errors. It is VEX V5 C++, it downloads without error, it just does nothing.
There is no loop in autonomous. The driver does have loop, and does nothing.
If I put all of the driver in one loop, only one of the three pneumatics (the last one) fires off, and the other two do nothing upon being pressed. (The solenoids, cords, and ports work. We have tested and confirmed this.)
There are no errors in the code when downloading or using “build”. It is VEXCODE V5 C++.
The part I need help on:
while(true){
if(Controller1.ButtonA.pressing()){
endgame2.set(false);
}
else{
endgame2.set(true);
wait(5, msec);
//pneumatics
if (Controller1.ButtonY.pressing()){
endgame1.set(false);
}
else{
endgame1.set(true);
wait(5, msec);
//pneumatics
if (Controller1.ButtonX.pressing()){
shooterthing.set(false);
}
else{
shooterthing.set(true);
}```
Okay, so what I did was put both endgames into one button, and now they both work? The only thing that I can think of is that maybe the other button was broken.
Problem seems to be solved.
while(true){
if(Controller1.ButtonA.pressing()){
endgame2.set(false);
}
else{
endgame2.set(true);
wait(5, msec);
**}**
//pneumatics
if (Controller1.ButtonY.pressing()){
endgame1.set(false);
}
else{
endgame1.set(true);
wait(5, msec);
**}**
//pneumatics
if (Controller1.ButtonX.pressing()){
shooterthing.set(false);
}
else{
shooterthing.set(true);
}
wait(5, msec);
}
return 0;
}
This is where you were missing closing brackets. The reason you didn’t get any errors is you placed them at the end. I am under the assumption you want each string shooter to be controlled separately.
To add on to this, if you properly format your code you get this:
while (true) {
if (Controller1.ButtonA.pressing()) {
endgame2.set(false);
} else {
endgame2.set(true);
wait(5, msec);
//pneumatics
if (Controller1.ButtonY.pressing()) {
endgame1.set(false);
} else {
endgame1.set(true);
wait(5, msec);
//pneumatics
if (Controller1.ButtonX.pressing()) {
shooterthing.set(false);
} else {
shooterthing.set(true);
}
// your ending brackets are somewhere later on in the code,
// so you don't get an error when compiling
As you can now see, you are missing your closing brackets on two of the else blocks, so the compiler thinks they should be inside each other. This means endgame1
only gets set to false
when A and B are pressed, and shooterthing
only gets set to false
when A, Y, and X are all pressed. If you don’t want to have to go through all your code to format it manually, I’m pretty sure VexCode pro has a “Format Document” option in the right-click menu which will do it for you, and make it a lot easier to find bugs.
I did that. Now it gives me an error.
while(true){
if(Controller1.ButtonY.pressing()){
endgame2.set(true);
}
else{
endgame2.set(false);
wait(5, msec);
}
//pneumatics
while(true){
if(Controller1.ButtonA.pressing()){
endgame1.set(true);
}
else{
endgame1.set(false);
wait(5, msec);
}
//pneumatics
if (Controller1.ButtonX.pressing()){
shooterthing.set(false);
}
else{
shooterthing.set(true);
}
wait(5, msec);
}return 0;
}
void VEXcode_driver_task() {
// Start the driver control tasks....
vex::task drive0(ondriver_drivercontrol_0);
while(Competition.isDriverControl() && Competition.isEnabled()) {this_thread::sleep_for(10);}
drive0.stop();
return;
}
void VEXcode_auton_task() {
// Start the auton control tasks....
vex::task auto0(onauton_autonomous_0);
while(Competition.isAutonomous() && Competition.isEnabled()) {this_thread::sleep_for(10);}
auto0.stop();
return;
}
int main() {
vex::competition::bStopTasksBetweenModes = false;
Competition.autonomous(VEXcode_auton_task);
Competition.drivercontrol(VEXcode_driver_task);
}```
Now you have a second while (true)
inside of your outer while (true)
, which is stealing the closing bracket of the function this is defined in. This means cpp thinks you are defining functions inside of functions, which isn’t allowed, hence the errors. When the error checker reaches the end of the file, it realized it hadn’t found the closing bracket yet, and gives the error expected '}'
. All you have to do to fix this is to remove the second line that says while(true){
.
Try replacing your ondriver_drivercontrol_0()
function with this:
// "when driver control" hat block
int ondriver_drivercontrol_0() {
endgame1.set(true);
endgame2.set(true);
Brain.Screen.clearScreen();
Brain.Screen.setPenColor(white);
Brain.Screen.setFillColor(blue);
Brain.Screen.drawRectangle(100, 50, 279, 139);
Brain.Screen.setPenColor(white);
Brain.Screen.setFillColor(cyan);
Brain.Screen.drawRectangle(150, 75, 179, 89);
Brain.Screen.setFont(prop20);
Brain.Screen.setCursor(6, 22);
Brain.Screen.setFillColor(cyan);
Brain.Screen.setPenColor(purple);
Brain.Screen.print("Roger64");
Brain.Screen.setFont(prop20);
Brain.Screen.setCursor(7, 22);
Brain.Screen.setFillColor(cyan);
Brain.Screen.print("6390-R");
intake.setVelocity(100, percent);
roller.setVelocity(100, percent);
flywheel.setVelocity(100, percent);
Drivetrain.setDriveVelocity(100, percent);
//pneumatics:
//Create three callbacks which watch the controller buttons A, Y, and X
Controller1.ButtonA.pressed([](){ endgame2.set(!endgame2.value()); });
Controller1.ButtonY.pressed([](){ endgame2.set(!endgame2.value()); });
/*
By using the "shooterthingActivationTime" variable,
you should get more consistant results every time button X is pressed.
(because the amount of thime that the solenoid is activated does not depend on how long you hold the button.)
if the solenoid is not activated for the correct period of time, you can just adjust "shooterthingActivationTime"
*/
float shooterthingActivationTime = 0.125;
Controller1.ButtonX.pressed([](){
shooterthing.set(false);
wait(shooterthingActivationTime, sec);
shooterthing.set(true);
});
//prevent this thread from exiting
while(true){
wait(20, msec);
}
return 0;
}
As everyone has said above, these types of bugs are MUCH more easily avoided if you stick to consistent styling/indentation conventions… (I should know… I have made many programs that have similar bugs for similar reasons )
The biggest change I made was to the X button on the controller. I am assuming you are using a flywheel, one thing that can cause inconsistencies in launching discs is how long the solenoid is activated. Because of this, I moved the functionality of button X into a callback (using a lambda) which will activate your launcher for a predetermined amount of time.
I moved the functionality of the other buttons into callbacks for the sake of consistency.
I guess the biggest piece of advice I would give would be for your coder(s) to decide on a formatting convention and stick to it, you would be surprised how much more readable your code can become if you do this.
BTW, the code above is not the best either… it should be up to you to make it better.