The functions that I created don’t work properly help would be much appreciated. The if statements that rely on strings is what doesn’t work.
/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: Robert Voss */
/* Created: 2019 */
/* Description: Used for team 9799A */
/* */
/*---------------------------------------------------------------------------------------------------------*/
/*--------------------| Header Files & Namespaces |--------------------------------------------------------*/
#include "vex.h" // | Allows for the use of the |
// // | Vex API. |
// // | |
using namespace vex; // | Allows for use of the vex |
// // | class so we don't need to |
// // | add "vex::" to every line |
// // | that uses the class. |
// // | |
#include <string> // | Allows for the use of string |
// // | variables. |
// // | |
using namespace std; // | Allows for use of the std |
// // | class so we don't need to |
// // | add "std::" everytime we |
// // | want to use a string |
// // | variable. |
/*---------------------------------------------------------------------------------------------------------*/
/*-------------------------| Brain & Controller |----------------------------------------------------------*/
vex::brain Brain; // | Sets up Brain to use screen |
vex::controller Controller = vex::controller(); // | Sets up Controller |
/*---------------------------------------------------------------------------------------------------------*/
/*-------------------------| Competition Instance |--------------------------------------------------------*/
vex::competition Competition; // | Allows use of competition |
// // | towers for switching between |
// // | autonomous and driver control.|
/*---------------------------------------------------------------------------------------------------------*/
/*-------------------------| Forward Drive Motors |--------------------------------------------------------*/
const float wheelCircumference = 12.56; // | Used to calculate wheel |
// // | rollout, made variable for |
// // | ease of changing later. |
// // | |
const float turnRate = 6; // | Used so that we can calculate |
// // | the number of revolutions the |
// // | wheels will need to make a |
// // | certain number of degrees in |
// // | autonomous. |
// // | |
vex::motor RightMotor = vex::motor( vex::PORT9 , true ); // | Right Motor Port: 09 |
vex::motor LeftMotor = vex::motor( vex::PORT10 ); // | Left Motor Port: 02 |
vex::motor_group driveMotors = vex::motor_group(RightMotor, LeftMotor ); // | Drive Motors' Group |
/*---------------------------------------------------------------------------------------------------------*/
/*-------------------------| Angler Motor |----------------------------------------------------------------*/
const float anglerRate = 1;
vex::motor AnglerL = vex::motor( vex::PORT7 ); // | Left Angler Motor Port: 01 |
vex::motor AnglerR = vex::motor( vex::PORT6 , true ); // | Right Angler Motor Port: 10 |
vex::motor_group Anglers = vex::motor_group(AnglerL, AnglerR ); // | Angler Motors' Group |
/*---------------------------------------------------------------------------------------------------------*/
/*-------------------------| Intake Motors |---------------------------------------------------------------*/
vex::motor IntakeMotorR = vex::motor( vex::PORT11 ); // | Right Intake Motor Port: ## |
vex::motor IntakeMotorL = vex::motor( vex::PORT20, true ); // | Left Intake Motor Port: ## |
vex::motor_group Intakes = motor_group( IntakeMotorR, IntakeMotorL); // | Intake Motors' Group |
/*---------------------------------------------------------------------------------------------------------*/
/*-------------------------| Tray Motor |------------------------------------------------------------------*/
const float liftRate = 1; // | Allows us to lift the tray up |
// // | For stacking. |
// // | |
vex::motor TrayMotor = vex::motor( vex::PORT2 ); // | Tray Motor Port: 11 |
/*---------------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------*/
/* | Pre-Autonomous Functions | */
/*---------------------------------------------------------------------------------------------------------*/
void pre_auton( void ) {
}
void timeWait( int t ) {
task::sleep(t);
}
double timeConversion( int timeIn ) {
float timeOut;
if(timeIn >= 15){ // This is so if when we call the function
timeOut = timeIn *= .001;// we use milliseconds instead of seconds
return timeOut; // it will auto convert to seconds.
} else { // We can know when this has happened
timeOut = timeIn; // if timeout is greater than 15 since
return timeIn; // we know that there's only 15 seconds
} // autonomous, and we won't be setting
// // a timeout for longer than we have.
}
void driveStraight_FB( directionType dir, int speed, int dist, int timeout, bool blocking ) {
double rev = dist/wheelCircumference;
timeout = timeConversion(timeout);
driveMotors.setVelocity(speed, velocityUnits::pct);
driveMotors.rotateFor(dir, rev , rotationUnits::rev, blocking);
driveMotors.setTimeout(timeout, timeUnits::sec);
}
void sharpTurn_LR( string dir, int speed, int deg, int timeout, bool blocking ) {//change later.-------------
double rev = deg/turnRate;
timeout = timeConversion(timeout);
driveMotors.setVelocity(speed, pct);
if(dir == "left"||"Left"){
LeftMotor.rotateFor(directionType::rev, 1, rotationUnits::rev, false);
RightMotor.rotateFor(directionType::fwd, 1, rotationUnits::rev, blocking);
driveMotors.setTimeout(timeout, timeUnits::sec);
driveMotors.setTimeout(timeout, timeUnits::sec);
} else if (dir == "right"||"Right") {
LeftMotor.rotateFor(directionType::fwd, 1, rotationUnits::rev, false);
RightMotor.rotateFor(directionType::rev, 1, rotationUnits::rev, blocking);
driveMotors.setTimeout(timeout, timeUnits::sec);
driveMotors.setTimeout(timeout, timeUnits::sec);
}
}
void tray_UD( string dir, int speed, int dist, int timeout, bool blocking ) {//change later.----------
double rev = dist/liftRate;
timeout = timeConversion(timeout);
TrayMotor.setVelocity(speed, pct);
if(dir == "up"||"Up"){
TrayMotor.rotateFor(directionType::fwd, 1, rotationUnits::rev, blocking);
TrayMotor.setTimeout(timeout, timeUnits::sec);
} else if (dir == "down"||"Down") {
TrayMotor.rotateFor(directionType::rev, 1, rotationUnits::rev, blocking);
TrayMotor.setTimeout(timeout, timeUnits::sec);
}
}
void angler_UD( string dir, int speed, int deg, int timeout, bool blocking ) {
double rev = deg/anglerRate;
timeout = timeConversion(timeout);
if(dir == "Up"||"up"||"U"||"u"){
Brain.Screen.print("up");
Anglers.setVelocity(speed, velocityUnits::pct);
Anglers.rotateFor(directionType::rev, deg, rotationUnits::rev, blocking);
Anglers.setTimeout(timeout, timeUnits::sec);
} else if (dir == "Down"||"down"||"D"||"d") {
Brain.Screen.print("down");
Anglers.setVelocity((speed*-1), velocityUnits::pct);
Anglers.rotateFor(directionType::fwd, deg, rotationUnits::rev, blocking);
Anglers.setTimeout(timeout, timeUnits::sec);
}
}
void intake_OC( string dir, int speed, int timeToSpin, bool blocking ) {
timeToSpin = timeConversion(timeToSpin);
Intakes.setVelocity(speed, pct);
if(dir =="Open"||"open"||"O"||"o"){
Brain.Screen.print("open");
Intakes.spinFor(directionType::fwd, timeToSpin, timeUnits::sec);
Intakes.setVelocity(20, velocityUnits::pct);
Intakes.spin(directionType::fwd);
} else if (dir =="Close"||"close"||"C"||"c") {
Brain.Screen.print("close");
Intakes.spinFor(directionType::rev, timeToSpin, timeUnits::sec);
} else if (Intakes.isSpinning()) {
Intakes.stop();
}
}
void encoderAuton( void ) {
angler_UD("up", 100, 15, 1000, true);
angler_UD("down", 100, 10, 1000, true);
driveStraight_FB(fwd, 50, 50, 10000, false);
intake_OC("open", 100, 5, true);
driveStraight_FB(reverse, 100, 25, 1000, true);
sharpTurn_LR("left", 75, 1, 1000, true);
driveStraight_FB(fwd, 50, 5, 1000, true);
tray_UD("up", 40, 2, 1000, true);
driveStraight_FB(reverse, 20, 30, 1000, true);
timeWait(500);
driveMotors.stop();
timeWait(100);
}
void test( void ) {
angler_UD("d", 100, 1, 1000, true);
Anglers.rotateFor(directionType::fwd, 1, rotationUnits::rev, true);
intake_OC("c", 100, 1, true);
sharpTurn_LR("right", 100, 1, 1000, true);
}
/*---------------------------------------------------------------------------------------------------------*/
/* | Autonomous Task | */
/*---------------------------------------------------------------------------------------------------------*/
void autonomous( void ) {
//encoderAuton();
test();
}
/*---------------------------------------------------------------------------*/
/* */
/* 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 stickDrive( void ) {
RightMotor.spin(vex::directionType::fwd,Controller.Axis2.position(), vex::velocityUnits::pct);//Spins the right motor at the value of the
// //right stick.
LeftMotor.spin(vex::directionType::fwd,Controller.Axis3.position(), vex::velocityUnits::pct);//Spins the left motor at the value of the
// //left stick.
if(abs(Controller.Axis2.value()) < 5 ) {
RightMotor.stop();
}
// // Controller Deadzone. Checks if the absolute value of either joystick is less than 5, and if it
// // is, it stops the motors.
if(abs(Controller.Axis3.value()) < 5 ) {
LeftMotor.stop();
}
}
void buttonControl( int anglerSpeed, int intakeSpeed, int traySpeed ) {
Anglers.spin(vex::directionType::fwd, (anglerSpeed*(Controller.ButtonL2.pressing() - Controller.ButtonL1.pressing())), vex::velocityUnits::pct);
//^^^ Spins the angler motors at angler speed, defined when you call the function, multiplied by the value of the left triggers subtracted, which
//is either 1, 0, or -1. If it's 1 the motors will go forward, if 0, they will stop, and if -1 the motors will spin in reverse.
Intakes.spin(vex::directionType::fwd, (intakeSpeed*(Controller.ButtonR2.pressing() - Controller.ButtonR1.pressing())), vex::velocityUnits::pct);
//^^^ Spins the intake motors at intake speed, defined when you call the function, multiplied by the value of the right triggers subtracted, which
//is either 1, 0, or -1. If it's 1 the motors will go forward, if 0, they will stop, and if -1 the motors will spin in reverse.
TrayMotor.spin(vex::directionType::fwd, (traySpeed*(Controller.ButtonUp.pressing() - Controller.ButtonDown.pressing())), vex::velocityUnits::pct);
//^^^ Spins the tray motor at tray speed, defined when you call the function, multiplied by the value of the up & down buttons subtracted, which
//is either 1, 0, or -1. If it's 1 the motor will go forward, if 0, they will stop, and if -1 the motor will spin in reverse.
}
void usercontrol( void ) {
while(1) {
stickDrive();
buttonControl(100, 80, 50);
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() {
//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) {
vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
}
}
this is the actual code, the picture didn’t work right.
Nice to have code - but what functions are you specifically concerned with and what did you expect them to do - once that is specified, help can be provided.
please edit your post by putting (without spaces) [ code ] before and [ /code ] after.
most of the if statements comparing strings will always evaluate to true. restructure them into this form
if( dir == "Up" || dir == "U" ) {
cout << "this works" << endl;
}
6 Likes
Thank you, haven’t gotten to test it yet but that is exactly what was happening. Why do they currently always evaluate to true?