Vexcode v5 text example

Hi everyone! I am having issues with programming my robot with a competition template. I have tried a lot, but don’t see a switch for drivercontrol. If anyone can spare a working code that has an autonomous section, and a driver control section, that would help a lot! Thanks!

In the standard competition there should be a spot for the user code and a autonomous code, along with a section for things before that.

This is what is has - no spot. Just driver control

Summary

/----------------------------------------------------------------------------/
/* /
/
Module: main.cpp /
/
Author: C:\Users\ /
/
Created: Sat Oct 19 2019 /
/
Description: V5 project /
/
/
/
----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// ---- END VEXCODE CONFIGURED DEVICES ----

#include “vex.h”

using namespace vex;

int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();

}

Make sure you’re using the ‘Competition Template’ from File / Open Examples.

image

3 Likes

Alright, thanks! Quick question, under void usercontrol(void) { do you put the motor defining here or within the while loop?

Tried this, why am I gettong errors here: " " and here: “/----------------------------------------------------------------------------/”

nope, you can put that right under the global instance of ‘competition’. That way it is declared before autonomous as well.

You put it in the while loop

image image

The motors have to be defined before everything else or the rest of the code won’t work. You aren’t declaring the motors every time through the loop - only at the start of your code.

1 Like

@Deicer , do you know why those are marked as errors?

I can’t tell what the error is on the first line, but in the second one your ‘include’ and ‘namespace’ lines are supposed to be at the top of your file. Unless you moved them they should have already been in the right place.

EDIT: just to add some reasoning in here, those statements are supposed to be called globally. Putting them in your while loop means it doesn’t function for the rest of the file

2 Likes

Alright, moved them to the top, and the int main bracket is marked as an error? image

Whoops I thought he meant the controls for the robot. Definitely do NOT define the motors in the while loop.

Alright I think I see the big problem here. ‘int main’ defines the ‘main’ function, which is already defined by the template at the bottom of the file. I’m assuming you copied your previous code directly into the user control function, but you can’t define main inside the defining of usercontrol.

Your fix is to take out the ‘int main’ line and its brackets. Also make sure usercontrol has its corresponding end bracket.

2 Likes

Alright, Thanks! Bad news though, when building, it doesn’t work, but it says there is an error: image

Edit: no i dont think i defined brain twice, but there is vex::brain Brain;

You didn’t define brain twice, but it’s already defined by the template in the pesky ‘robot-config.cpp’ file :stuck_out_tongue:

Just delete it in main.cpp and you should be all set!

Just a note though, if you have any more problems it would be helpful if we could see the whole code. Copying the text into the forums works, just make sure you format it so it doesn’t turn into a huge mesh of words

1 Like

Alright, do you mean in the files of the application? Also, here is the code:

Code; Click with caution

// VEX V5 C++ Project
#include
#include
#include “vex.h”
// ---- START VEXCODE CONFIGURED DEVICES ----
// ---- END VEXCODE CONFIGURED DEVICES ----

using namespace vex;

// A global instance of competition
competition Competition;
//#region config_globals
vex::brain Brain;
vex::motor back_right_motor(vex::PORT1, vex::gearSetting::ratio18_1, true);
vex::motor back_left_motor(vex::PORT10, vex::gearSetting::ratio18_1, false);
vex::motor front_right_motor(vex::PORT11, vex::gearSetting::ratio18_1, true);
vex::motor front_left_motor(vex::PORT20, vex::gearSetting::ratio18_1, false);
vex::motor intake_arm_mover(vex::PORT5, vex::gearSetting::ratio36_1, false);
vex::motor intake_left(vex::PORT12, vex::gearSetting::ratio18_1, false);
vex::motor intake_right(vex::PORT9, vex::gearSetting::ratio18_1, false);
vex::motor tray_mover(vex::PORT16, vex::gearSetting::ratio36_1, false);
vex::controller con(vex::controllerType::primary);
//#endregion config_globals

// define your global instances of motors and other devices here

/---------------------------------------------------------------------------/
/* 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 V5 has been powered on and /
/
not every time that the robot is disabled. /
/
---------------------------------------------------------------------------*/

void pre_auton(void) {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();

// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, …
}

/---------------------------------------------------------------------------/
/* /
/
Autonomous Task /
/
/
/
This task is used to control your robot during the autonomous phase of /
/
a VEX Competition. /
/
/
/
You must modify the code to add your own robot specific commands here. /
/
---------------------------------------------------------------------------*/

void autonomous(void) {
// …
// Insert autonomous user code here.
// …
}

/---------------------------------------------------------------------------/
/* /
/
User Control Task /
/
/
/
This task is used to control your robot during the user control phase of /
/
a VEX Competition. /
/
/
/
You must modify the code to add your own robot specific commands here. /
/
---------------------------------------------------------------------------*/

void usercontrol(void) {
// User control code here, inside the loop
while (1) {

 //Setting the speed of the intake rollers and the arms holding the intake rollers
int intakeleftSpeedPCT = 100;
int intakerightSpeedPCT = 100;
int intakearmSpeedPCT = 50;
int traymoverSpeedPCT = 25;
while(true) {
    if(con.ButtonL1.pressing()) {
            intake_left.spin(directionType::fwd, intakeleftSpeedPCT, velocityUnits::pct);
        }
        else if (con.ButtonL2.pressing()) {
            intake_left.spin(directionType::rev,intakeleftSpeedPCT, velocityUnits::pct);
        } 
        else {
            intake_left.stop(brakeType::brake);
        }
        if(con.ButtonL1.pressing()) {
            intake_right.spin(directionType::rev, intakerightSpeedPCT, velocityUnits::pct);
        }
        else if (con.ButtonL2.pressing()) {
            intake_right.spin(directionType::fwd,intakerightSpeedPCT, velocityUnits::pct);
        } 
        else {
            intake_right.stop(brakeType::brake);
        }
            if(con.ButtonUp.pressing()) {
            intake_arm_mover.spin(directionType::fwd, intakearmSpeedPCT, velocityUnits::pct);
        }
        else if (con.ButtonDown.pressing()) {
            intake_arm_mover.spin(directionType::rev,intakearmSpeedPCT, velocityUnits::pct);
        } 
        else {
            intake_arm_mover.stop(brakeType::brake);
        }
                 if(con.ButtonLeft.pressing()) {
            tray_mover.spin(directionType::fwd, traymoverSpeedPCT, velocityUnits::pct);
        }
        else if (con.ButtonRight.pressing()) {
            tray_mover.spin(directionType::rev,traymoverSpeedPCT, velocityUnits::pct);
        } 
        else {
            tray_mover.stop(brakeType::brake);
        }
    //Get the raw sums of the X and Y joystick axes
    double front_left  = (double)(con.Axis3.position(pct) + con.Axis4.position(pct));
    double back_left   = (double)(con.Axis3.position(pct) - con.Axis4.position(pct));
    double front_right = (double)(con.Axis3.position(pct) - con.Axis4.position(pct));
    double back_right  = (double)(con.Axis3.position(pct) + con.Axis4.position(pct));
    
    //Find the largest possible sum of X and Y
    double max_raw_sum = (double)(abs(con.Axis3.position(pct)) + abs(con.Axis4.position(pct)));
    
    //Find the largest joystick value
    double max_XYstick_value = (double)(std::max(abs(con.Axis3.position(pct)),abs(con.Axis4.position(pct))));
    
    //The largest sum will be scaled down to the largest joystick value, and the others will be
    //scaled by the same amount to preserve directionality
    if (max_raw_sum != 0) {
        front_left  = front_left / max_raw_sum * max_XYstick_value;
        back_left   = back_left / max_raw_sum * max_XYstick_value;
        front_right = front_right / max_raw_sum * max_XYstick_value;
        back_right  = back_right / max_raw_sum * max_XYstick_value;
    }
    
    //Now to consider rotation
    //Naively add the rotational axis
    front_left  = front_left  + con.Axis1.position(pct);
    back_left   = back_left   + con.Axis1.position(pct);
    front_right = front_right - con.Axis1.position(pct);
    back_right  = back_right  - con.Axis1.position(pct);
    
    //What is the largest sum, or is 100 larger?
    max_raw_sum = std::max(std::abs(front_left),std::max(std::abs(back_left),std::max(std::abs(front_right),std::max(std::abs(back_right),100.0))));
    
    //Scale everything down by the factor that makes the largest only 100, if it was over
    front_left  = front_left  / max_raw_sum * 100.0;
    back_left   = back_left   / max_raw_sum * 100.0;
    front_right = front_right / max_raw_sum * 100.0;
    back_right  = back_right  / max_raw_sum * 100.0;
    
    //Write the manipulated values out to the motors
     front_left_motor.spin(fwd,front_left, velocityUnits::pct);
      back_left_motor.spin(fwd,back_left,  velocityUnits::pct);
    front_right_motor.spin(fwd,front_right,velocityUnits::pct);
     back_right_motor.spin(fwd,back_right, velocityUnits::pct);
}

}

// This is the main execution loop for the user control program.
// Each time through the loop your program should update motor + servo
// values based on feedback from the joysticks.

// ........................................................................
// Insert user code here. This is where you use the joystick values to
// update your motors, etc.
// ........................................................................

wait(20, msec); // Sleep the task for a short amount of time to
                // prevent wasted resources.

}

//
// Main will set up the competition functions and callbacks.
//
int main() {
// 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 (true) {
wait(100, msec);
}
}

On the left sidebar is a list of files in the project that are uploaded to your robot. The bottom one is ‘robot-config.cpp’, which is where the graphical robot config puts its declarations.

Also, at the top of the code you sent why are there 2 empty '#include’s? (And that third one is missing its semicolon. [;]

Your header is all messed up, idk what happened to it. It should look like this:

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       Deicer on the forums lol                                                       */
/*    Created:      Thu Sep 26 2019                                           */
/*    Description:  Competition Template                                      */
/*                                                                            */
/*----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "vex.h"

using namespace vex;

// A global instance of competition
competition Competition;
//insert rest of code here

copypaste it if you want… or just use the template again.

4 Likes

It put the empty includes and they were causing issues so i put them at the top. The header, I dont know what happened, and it works! Thank you so much, I couldnt figure this out!

1 Like