My students are using the code below for their V5 clawbot. Right now they are just trying to get the drivetrain working correctly. The problem is, when we disconnect the brain from the computer, the robot stops performing the code. For example, although the code was designed to make the wheels go in the same direction, but when it’s disconnected, they go in opposite directions again. Also, when they click button a, the whole robot freaks out, suddenly going as fast as possible. Any suggestions?
/*----------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: james */
/* Created: Sat Nov 02 2019 */
/* Description: V5 project */
/* */
/*----------------------------------------------------------------------------*/
// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name] [Type] [Port(s)]
// LeftMotor1 motor 1
// RightMotor1 motor 2
// LeftMotor2 motor 3
// RightMotor2 motor 4
// Controller1 controller
// ---- END VEXCODE CONFIGURED DEVICES ----
#include "vex.h"
using namespace vex;
using signature = vision::signature;
using code = vision::code;
// A global instance of brain used for printing to the V5 Brain screen
brain brain;
// VEXcode device constructors
motor LeftMotor1 = motor(PORT1, ratio18_1, false);
motor RightMotor1 = motor(PORT2, ratio18_1, false);
motor LeftMotor2 = motor(PORT3, ratio18_1, true);
motor RightMotor2 = motor(PORT4, ratio18_1, true);
controller Controller1 = controller(primary);
// VEXcode generated functions
/**
* Used to initialize code/tasks/devices added using tools in VEXcode Text.
*
* This should be called at the start of your int main function.
*/
void vexcodeInit( void ) {
// nothing to initialize
}
/*----------------------------------------------------------------------------*/
// define the motor groups
// the motors should have been setup with correct the gear cartridge ratios
// and the reverse flag set as necessary so they rotate in the correct direction
// when commanded to more forwards
// left hand side of the robot has two motors
motor_group leftDrive( LeftMotor1, LeftMotor2 );
// right hand side of the robot has two motors
motor_group rightDrive( RightMotor1, RightMotor2 );
/*----------------------------------------------------------------------------*/
// define the drivetrain
// this one is a smart drive which uses the gyro
// gyro and all motors were defined using graphical config
// we have 4 inch wheels
// drive is 16 inches wide between the wheels
// drive has a 16 inch wheelbase (between fron and back wheel axles)
//
drivetrain robotDrive(leftDrive, rightDrive, 12.56, 16, 16, distanceUnits::in );
// this is how the above definition would be if no gyro is used
//drivetrain robotDrive( leftDrive, rightDrive, 12.56, 16, 16, distanceUnits::in );
/*----------------------------------------------------------------------------*/
//
// A task that just displays motor velocity and position
// The motors that are part of a motor group and/or drivetrain can still be accessed
// directly.
//
int displayTask() {
while(1) {
// display some useful info
Brain.Screen.setCursor(2,1);
Brain.Screen.print( " MotorLb speed: %4.0f position: %6.2f", LeftMotor1.velocity( percent ), LeftMotor1.position( rev ) );
Brain.Screen.newLine();
Brain.Screen.print( " MotorLf speed: %4.0f position: %6.2f", RightMotor1.velocity( percent ), RightMotor1.position( rev ));
Brain.Screen.newLine();
Brain.Screen.print( " MotorRb speed: %4.0f position: %6.2f", LeftMotor2.velocity( percent ), LeftMotor2.position( rev ));
Brain.Screen.newLine();
Brain.Screen.print( " MotorRf speed: %4.0f position: %6.2f", RightMotor2.velocity( percent ), RightMotor2.position( rev ));
Brain.Screen.newLine();
Brain.Screen.newLine();
// motor group velocity and position is returned for the first motor in the group
Brain.Screen.print( " leftDrive speed: %4.0f position: %6.2f", leftDrive.velocity( percent ), leftDrive.position( rev ));
Brain.Screen.newLine();
Brain.Screen.print( " rightDrive speed: %4.0f position: %6.2f", rightDrive.velocity( percent ), rightDrive.position( rev ));
Brain.Screen.newLine();
Brain.Screen.newLine();
// drivetrain velocity is the average of the motor velocities for left and right
Brain.Screen.print( " robotDrive speed: %4.0f", robotDrive.velocity( percent ) );
Brain.Screen.newLine();
// no need to run this loop too quickly
wait( 20, timeUnits::msec );
}
return 0;
}
/*----------------------------------------------------------------------------*/
//
// main program entry and drive control
//
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// set 10 second timeout for robot drive movements
robotDrive.setTimeout(10, seconds);
// set the speed used for drive turns
robotDrive.setTurnVelocity(50, percent);
// start the display task
task displayTaskInstance( displayTask );
// loop forever
while( true ) {
// Button A is used to rotate drive 90 degrees
if( Controller1.ButtonA.pressing() ) {
// tell drivetrain to rotate 90 deg
robotDrive.turnFor( 90, degrees );
// wait for it to be done
while( robotDrive.isMoving() )
wait( 10, timeUnits::msec );
}
else {
// not rotating, then we can drive using controller
// read percent from controller axis
int leftSpeed = Controller1.Axis3.position();
int rightSpeed = Controller1.Axis2.position();
// deadband
if( abs(leftSpeed) < 10 ) leftSpeed = 0;
if( abs(rightSpeed) < 10 ) rightSpeed = 0;
// send to motors
leftDrive.spin( forward, leftSpeed, percent );
rightDrive.spin( forward, rightSpeed, percent );
}
// no need to run this loop too quickly
wait( 20, timeUnits::msec );
}
return 0;
}
edit by mods: code tags