So, I am completely new to coding (only know a few basics) so I would really like some feedback on my VEX V5 c++ autonomous code. So basically, this is for the pushback stuff where the bot has to put in blocks in long goals. I used some GPS grid to see where exactly to program the bot to go or stop. But what are some subtle errors that I’d need to fix and what I can do for improvement?
Code:
#pragma region VEXcode Generated Robot Configuration
// 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(PORT1, ratio18_1, false);
motor leftMotorB = motor(PORT3, ratio18_1, false);
motor_group LeftDriveSmart = motor_group(leftMotorA, leftMotorB);
motor rightMotorA = motor(PORT2, ratio18_1, true);
motor rightMotorB = motor(PORT4, ratio18_1, true);
motor_group RightDriveSmart = motor_group(rightMotorA, rightMotorB);
gyro DrivetrainGyro = gyro(Brain.ThreeWirePort.A);
smartdrive Drivetrain = smartdrive(LeftDriveSmart, RightDriveSmart, DrivetrainGyro, 319.19, 320, 40, mm, 1);
inertial Inertial5 = inertial(PORT5);
// generating and setting random seed
void initializeRandomSeed(){
int systemTime = Brain.Timer.systemHighResolution();
double batteryCurrent = Brain.Battery.current();
double batteryVoltage = Brain.Battery.voltage(voltageUnits::mV);
// Combine these values into a single integer
int seed = int(batteryVoltage + batteryCurrent * 100) + systemTime;
// Set the seed
srand(seed);
}
bool vexcode_initial_drivetrain_calibration_completed = false;
void calibrateDrivetrain() {
wait(200, msec);
Brain.Screen.print("Calibrating");
Brain.Screen.newLine();
Brain.Screen.print("Gyro");
DrivetrainGyro.calibrate();
while (DrivetrainGyro.isCalibrating()) {
wait(25, msec);
}
vexcode_initial_drivetrain_calibration_completed = true;
// Clears the screen and returns the cursor to row 1, column 1.
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
}
void vexcodeInit() {
// Calibrate the Drivetrain
calibrateDrivetrain();
//Initializing random seed.
initializeRandomSeed();
}
// Helper to make playing sounds from the V5 in VEXcode easier and
// keeps the code cleaner by making it clear what is happening.
void playVexcodeSound(const char *soundName) {
printf("VEXPlaySound:%s\n", soundName);
wait(5, msec);
}
// define variable for remote controller enable/disable
bool RemoteControlCodeEnabled = true;
#pragma endregion VEXcode Generated Robot Configuration
// Include the V5 Library
#include "vex.h"
// Allows for easier use of the VEX Library
using namespace vex;
float myVariable;
// "when started" hat block
int whenStarted1() {
return 0;
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
autonomous(); // run autonomous
while (true) {
task::sleep(10, msec)
}Drivetrain.setDriveVelocity(70, percent)
Drivetrain.driveFor(forward, 600, mm)
Drivetrain.stop()
//Grabs hold of blocks
Motor.setVelocity(80, percent)
Motor.spin(forward)
Motor.spin(forward, 10.0, volt)
Motor.isSpinning(forward, 720, degrees, true)
wait(2, seconds)
task::sleep(100, msec)
Motor.stop;
//Scoring the blocks into the long goal
Drivetrain.turnFor(right, 90, degrees)
Drivetrain.driveFor(forward, 300, mm)
Drivetrain.turnFor(right, 90, degrees)
Drivetrain.driveFor(forward, 600, mm)
Drivetrain.turnFor(left, 90, degrees)
task::sleep(100, msec)
Motor.spin(reverse)
Motor.isSpinning(reverse, 720, degrees, true)
wait(2, seconds)
task::sleep(100, msec)
Motor.stop;
}





