void AutomaticLineup() {
		double leftVolts = leftIR.GetAverageVoltage() - leftIRZero;
		double rightVolts = rightIR.GetAverageVoltage() - leftIRZero;
		if (leftVolts <= VOLTAGE_TO_PICK && rightVolts <= VOLTAGE_TO_PICK) {
			robotDrive.MecanumDrive_Cartesian(0, 0, 0);
		} else if (leftVolts > VOLTAGE_TO_PICK
				&& rightVolts > VOLTAGE_TO_PICK) {
			robotDrive.MecanumDrive_Cartesian(0, -0.3, 0);
		} else if (leftVolts > VOLTAGE_TO_PICK) {
			robotDrive.MecanumDrive_Cartesian(0, 0, 0.2);
		} else if (rightVolts > VOLTAGE_TO_PICK) {
			robotDrive.MecanumDrive_Cartesian(0, 0, -0.2);
		}
	}
Exemple #2
0
	/**
	 *  Tells the robot to drive to a set distance (in inches) from an object using
	 *  proportional control.
	 */
	void OperatorControl() {

		double currentDistance; //distance measured from the ultrasonic sensor values
		double currentSpeed; //speed to set the drive train motors

		while (IsOperatorControl() && IsEnabled()) {
			currentDistance = ultrasonic->GetValue() * valueToInches; //sensor returns a value from 0-4095 that is scaled to inches
			currentSpeed = (holdDistance - currentDistance) * pGain; //convert distance error to a motor speed
			myRobot->Drive(currentSpeed, 0); //drive robot
		}
	}
	void ReadDistance() {
//		SmartDashboard::PutNumber("the raw distance from the distance sensor is", distanceSensor->GetValue());
//		SmartDashboard::PutNumber("the voltage is", distanceSensor->GetAverageVoltage());
//		SmartDashboard::PutNumber("the vcc is", getVcc->GetAverageVoltage());
//		scalingFactor = getVcc->GetAverageVoltage()/float(512);
//		SmartDashboard::PutNumber("using scaling factor", scalingFactor * distanceSensor->GetValue());
//		convertedDist = float(512) * distanceSensor->GetValue() / getVcc->GetAverageVoltage();
//		SmartDashboard::PutNumber("the converted distance is", convertedDist/1000);

		// y = 0.1319x-1.3456
		// y is actual distance, x is raw value
		Smart Dashboard::PutNumber("Actual distance:", 0.1319(distanceSensor->GetValue())-1.3456);
	}
	void ReadDistance() {
		// y = 0.1309x + 0.0383
		// y is actual distance, x is raw value
		convertedDist = 0.1309*(distanceSensor->GetValue())+0.0383;
		SmartDashboard::PutNumber("Actual distance:", convertedDist);
	}
Exemple #5
0
	/**
	 * Runs the motors with Mecanum drive.
	 */
	void OperatorControl()//teleop code
	{
		robotDrive.SetSafetyEnabled(false);
		gyro.Reset();
		grabEncoder.Reset();
		timer.Start();
		timer.Reset();
		double liftHeight = 0; //variable for lifting thread
		int liftHeightBoxes = 0; //another variable for lifting thread
		int liftStep = 0; //height of step in inches
		int liftRamp = 0; //height of ramp in inches
		double grabPower;
		bool backOut;
		uint8_t toSend[10];//array of bytes to send over I2C
		uint8_t toReceive[10];//array of bytes to receive over I2C
		uint8_t numToSend = 1;//number of bytes to send
		uint8_t numToReceive = 0;//number of bytes to receive
		toSend[0] = 1;//set the byte to send to 1
		i2c.Transaction(toSend, 1, toReceive, 0);//send over I2C
		bool isGrabbing = false;//whether or not grabbing thread is running
		bool isLifting = false;//whether or not lifting thread is running
		bool isBraking = false;//whether or not braking thread is running
		float driveX = 0;
		float driveY = 0;
		float driveZ = 0;
		float driveGyro = 0;
		bool liftLastState = false;
		bool liftState = false; //button pressed
		double liftLastTime = 0;
		double liftTime = 0;
		bool liftRan = true;
		Timer switchTimer;
		Timer grabTimer;
		switchTimer.Start();
		grabTimer.Start();


		while (IsOperatorControl() && IsEnabled())
		{
			// Use the joystick X axis for lateral movement, Y axis for forward movement, and Z axis for rotation.
			// This sample does not use field-oriented drive, so the gyro input is set to zero.

			toSend[0] = 1;
			numToSend = 1;


			driveX = driveStick.GetRawAxis(Constants::driveXAxis);//starts driving code
			driveY = driveStick.GetRawAxis(Constants::driveYAxis);
			driveZ = driveStick.GetRawAxis(Constants::driveZAxis);
			driveGyro = gyro.GetAngle() + Constants::driveGyroTeleopOffset;


			if (driveStick.GetRawButton(Constants::driveOneAxisButton)) {//if X is greater than Y and Z, then it will only go in the direction of X
				toSend[0] = 6;
				numToSend = 1;

				if (fabs(driveX) > fabs(driveY) && fabs(driveX) > fabs(driveZ)) {
					driveY = 0;
					driveZ = 0;
				}
				else if (fabs(driveY) > fabs(driveX) && fabs(driveY) > fabs(driveZ)) {//if Y is greater than X and Z, then it will only go in the direction of Y
					driveX = 0;
					driveZ = 0;
				}
				else {//if Z is greater than X and Y, then it will only go in the direction of Z
					driveX = 0;
					driveY = 0;
				}
			}

			if (driveStick.GetRawButton(Constants::driveXYButton)) {//Z lock; only lets X an Y function
				toSend[0] = 7;
				driveZ = 0;//Stops Z while Z lock is pressed
			}

			if (!driveStick.GetRawButton(Constants::driveFieldLockButton)) {//robot moves based on the orientation of the field
				driveGyro = 0;//gyro stops while field lock is enabled
			}

			driveX = Constants::scaleJoysticks(driveX, Constants::driveXDeadZone, Constants::driveXMax * (.5 - (driveStick.GetRawAxis(Constants::driveThrottleAxis) / 2)), Constants::driveXDegree);
			driveY = Constants::scaleJoysticks(driveY, Constants::driveYDeadZone, Constants::driveYMax * (.5 - (driveStick.GetRawAxis(Constants::driveThrottleAxis) / 2)), Constants::driveYDegree);
			driveZ = Constants::scaleJoysticks(driveZ, Constants::driveZDeadZone, Constants::driveZMax * (.5 - (driveStick.GetRawAxis(Constants::driveThrottleAxis) / 2)), Constants::driveZDegree);
			robotDrive.MecanumDrive_Cartesian(driveX, driveY, driveZ, driveGyro);//makes the robot drive




			if (pdp.GetCurrent(Constants::grabPdpChannel) < Constants::grabManualCurrent) {
				pickup.setGrabber(Constants::scaleJoysticks(grabStick.GetX(), Constants::grabDeadZone, Constants::grabMax, Constants::grabDegree)); //defines the grabber
				if(grabTimer.Get() < 1) {
					toSend[0] = 6;
				}
			}
			else {
				pickup.setGrabber(0);
				grabTimer.Reset();
				toSend[0] = 6;
			}

			if (Constants::grabLiftInverted) {
				pickup.setLifter(-Constants::scaleJoysticks(grabStick.GetY(), Constants::liftDeadZone, Constants::liftMax, Constants::liftDegree)); //defines the lifter
			}
			else {
				pickup.setLifter(Constants::scaleJoysticks(grabStick.GetY(), Constants::liftDeadZone, Constants::liftMax, Constants::liftDegree)); //defines the lifter
			}


			SmartDashboard::PutNumber("Lift Power", Constants::scaleJoysticks(grabStick.GetY(), Constants::liftDeadZone, Constants::liftMax, Constants::liftDegree));
			SmartDashboard::PutBoolean("Is Lifting", isLifting);

			if (Constants::scaleJoysticks(grabStick.GetY(), Constants::liftDeadZone, Constants::liftMax, Constants::liftDegree) != 0 || isLifting) { //if the robot is lifting
				isBraking = false; //stop braking thread
				SmartDashboard::PutBoolean("Braking", false);
			}
			else if(!isBraking) {
				isBraking = true; //run braking thread
				pickup.lifterBrake(isBraking);//brake the pickup
			}



			if (grabStick.GetRawButton(Constants::liftFloorButton)) {
				liftHeight = 0;
				pickup.lifterPosition(liftHeight, isLifting, grabStick);//start lifting thread
				liftRan = true;
			}

			liftTime = timer.Get();
			liftState = grabStick.GetRawButton(Constants::liftButton);

			if (liftState) { //if button is pressed
				if (!liftLastState) {
					if (liftTime - liftLastTime < Constants::liftMaxTime) {
						if (liftHeightBoxes < Constants::liftMaxHeightBoxes) {
							liftHeightBoxes++; //adds 1 to liftHeightBoxes
						}
					}
					else {
						liftHeightBoxes = 1;
						liftRamp = 0;
						liftStep = 0;
					}
				}
				liftLastTime = liftTime;
				liftLastState = true;
				liftRan = false;
			}
			else if (grabStick.GetRawButton(Constants::liftRampButton)) {
				if (liftTime - liftLastTime > Constants::liftMaxTime) {
					liftHeight = 0;
					liftStep = 0;
				}
				liftRamp = 1; //prepares to go up ramp
				liftLastTime = liftTime;
				liftRan = false;
			}
			else if (grabStick.GetRawButton(Constants::liftStepButton)) {
				if (liftTime - liftLastTime > Constants::liftMaxTime) {
					liftHeight = 0;
					liftRamp = 0;
				}
				liftStep = 1; //prepares robot for step
				liftLastTime = liftTime;
				liftRan = false;
			}
			else {
				if (liftTime - liftLastTime > Constants::liftMaxTime && !liftRan) {

					liftHeight = liftHeightBoxes * Constants::liftBoxHeight + liftRamp * Constants::liftRampHeight + liftStep * Constants::liftStepHeight; //sets liftHeight
					if (liftHeightBoxes > 0) {
						liftHeight -= Constants::liftBoxLip;
					}
					pickup.lifterPosition(liftHeight, isLifting, grabStick);//start lifting thread
					liftRan = true;
				}
				liftLastState = false;
			}

			if (grabStick.GetRawButton(Constants::grabToteButton)) {//if grab button is pressed
				grabPower = Constants::grabToteCurrent;
				backOut = true;
				if (!isGrabbing) {
					pickup.grabberGrab(isGrabbing, grabPower, backOut, grabStick);//start grabber thread
				}
			}
			else if (grabStick.GetRawButton(Constants::grabBinButton)) {//if grab button is pressed
				grabPower = Constants::grabBinCurrent;

				backOut = false;
				if (!isGrabbing) {
					pickup.grabberGrab(isGrabbing, grabPower, backOut, grabStick);//start grabber thread
				}
			}
			else if (grabStick.GetRawButton(Constants::grabChuteButton)) {//if grab button is presset
				SmartDashboard::PutBoolean("Breakpoint -2", false);
				SmartDashboard::PutBoolean("Breakpoint -1", false);
				SmartDashboard::PutBoolean("Breakpoint 0", false);
				SmartDashboard::PutBoolean("Breakpoint 1", false);
				SmartDashboard::PutBoolean("Breakpoint 2", false);
				SmartDashboard::PutBoolean("Breakpoint 3", false);
				SmartDashboard::PutBoolean("Breakpoint 4", false);
				//Wait(.5);
				if (!isGrabbing) {
					//pickup.grabberChute(isGrabbing, grabStick);//start grabber thread
				}
			}

			//determines what the LED's look like based on what the Robot is doing
			if (isGrabbing) {
				toSend[0] = 5;
				numToSend = 1;
			}
			if (isLifting) {//if the grabbing thread is running
				if (Constants::encoderToDistance(liftEncoder.Get(),Constants::liftEncoderTicks, Constants::liftEncoderBase, Constants::liftEncoderRadius) < liftHeight) {
					toSend[0] = 3;
				}
				else {
					toSend[0] = 4;
				}
				numToSend = 1;//sends 1 byte to I2C
			}

			if(!grabOuterLimit.Get()) { //tells if outer limit is hit with lights
				if(switchTimer.Get() < 1) {
					toSend[0] = 6;
				}
			}
			else {
				switchTimer.Reset();
			}

			if (driveStick.GetRawButton(Constants::sneakyMoveButton)) {
				toSend[0] = 0;
				numToSend = 1;
			}

			float distance = prox.GetVoltage() * Constants::ultrasonicVoltageToInches / 12;	// distance from ultrasonic sensor
			float rotations = (float) liftEncoder.Get();	// rotations on encoder
			SmartDashboard::PutNumber("Distance", distance);	// write stuff to smart dash
			SmartDashboard::PutNumber("Current", pdp.GetCurrent(Constants::grabPdpChannel));
			SmartDashboard::PutNumber("LED Current", pdp.GetCurrent(Constants::ledPdpChannel));
			SmartDashboard::PutNumber("Lift Encoder", rotations);
			SmartDashboard::PutNumber("Lift Height", liftHeight);
			SmartDashboard::PutNumber("Grab Encoder", grabEncoder.Get());
			SmartDashboard::PutBoolean("Grab Inner", grabInnerLimit.Get());
			SmartDashboard::PutBoolean("Grab Outer", grabOuterLimit.Get());
			SmartDashboard::PutNumber("Drive Front Left Current", pdp.GetCurrent(Constants::driveFrontLeftPin));
			SmartDashboard::PutNumber("Drive Front Right Current", pdp.GetCurrent(Constants::driveFrontRightPin));
			SmartDashboard::PutNumber("Drive Rear Left Current", pdp.GetCurrent(Constants::driveRearLeftPin));
			SmartDashboard::PutNumber("Drive Rear Right Current", pdp.GetCurrent(Constants::driveRearRightPin));
			SmartDashboard::PutNumber("Throttle", grabStick.GetZ());


			i2c.Transaction(toSend, 1, toReceive, 0);//send and receive information from arduino over I2C
			Wait(0.005); // wait 5ms to avoid hogging CPU cycles
		} //end of teleop
		isBraking = false;
		toSend[0] = 0;
		i2c.Transaction(toSend, numToSend, toReceive, numToReceive);
	}
Exemple #6
0
	void Autonomous()
	{
		Timer timer;
		float power = 0;
		bool isLifting = false;
		bool isGrabbing = false;
		double liftHeight = Constants::liftBoxHeight-Constants::liftBoxLip;
		double grabPower = Constants::grabAutoCurrent;
		bool backOut;

		uint8_t toSend[1];//array of bytes to send over I2C
		uint8_t toReceive[0];//array of bytes to receive over I2C
		uint8_t numToSend = 1;//number of bytes to send
		uint8_t numToReceive = 0;//number of bytes to receive
		toSend[0] = 2;//set the byte to send to 1
		i2c.Transaction(toSend, numToSend, toReceive, numToReceive);//send over I2C

		bool isSettingUp = true;

		//pickup.setGrabber(-1); //open grabber all the way
		pickup.setLifter(0.8);

		while (isSettingUp && IsEnabled() && IsAutonomous()) {
			isSettingUp = false;
			/*if (grabOuterLimit.Get() == false) {
				pickup.setGrabber(0); //open until limit
			}
			else {
				isSettingUp = true;
			}*/

			if (liftLowerLimit.Get()) {
				pickup.setLifter(0); //down till bottom
			}
			else {
				isSettingUp = true;
			}
		}

		gyro.Reset();
		liftEncoder.Reset();
		grabEncoder.Reset();

		if (grabStick.GetZ() > .8) {
			timer.Reset();
			timer.Start();
			while (timer.Get() < 1) {
				robotDrive.MecanumDrive_Cartesian(0, power, 0, gyro.GetAngle());	// drive back
				if(power>-.4){
					power-=0.005;
					Wait(.005);
				}
			}
			robotDrive.MecanumDrive_Cartesian(0, 0, 0, gyro.GetAngle());	// STOP!!!
			timer.Stop();
			timer.Reset();
			Wait(1);
		}
		power = 0;

		while (isLifting && IsEnabled() && IsAutonomous()) {
			Wait(.005);
		}

		backOut = Constants::autoBackOut;
		pickup.grabberGrab(isGrabbing, grabPower, backOut, grabStick);
		Wait(.005);

		while (isGrabbing && IsEnabled() && IsAutonomous()) {
			Wait(.005);
		}

		liftHeight = 3*Constants::liftBoxHeight;
		Wait(.005);
		pickup.lifterPosition(liftHeight, isLifting, grabStick);
		Wait(.005);
		while (isLifting && IsEnabled() && IsAutonomous()) {
			Wait(.005);
		}

		while(prox.GetVoltage() * Constants::ultrasonicVoltageToInches / 12 < 2 && IsEnabled() && IsAutonomous());	// while the nearest object is closer than 2 feet

		timer.Start();

		while(prox.GetVoltage() * Constants::ultrasonicVoltageToInches  < Constants::autoBackupDistance && timer.Get() < Constants::autoMaxDriveTime && IsEnabled() && IsAutonomous()) {	// while the nearest object is further than 12 feet
			if (power < .45) { //ramp up the power slowly
				power += .00375;
			}
			robotDrive.MecanumDrive_Cartesian(0, power, 0, gyro.GetAngle());	// drive back
			float distance = prox.GetVoltage() * Constants::ultrasonicVoltageToInches / 12;	// distance from ultrasonic sensor
			SmartDashboard::PutNumber("Distance", distance);	// write stuff to smart dash
			SmartDashboard::PutNumber("Drive Front Left Current", pdp.GetCurrent(Constants::driveFrontLeftPin));
			SmartDashboard::PutNumber("Drive Front Right Current", pdp.GetCurrent(Constants::driveFrontRightPin));
			SmartDashboard::PutNumber("Drive Rear Left Current", pdp.GetCurrent(Constants::driveRearLeftPin));
			SmartDashboard::PutNumber("Drive Rear Right Current", pdp.GetCurrent(Constants::driveRearRightPin));
			SmartDashboard::PutNumber("Gyro Angle", gyro.GetAngle());
			SmartDashboard::PutNumber("Distance (in)", prox.GetVoltage() * Constants:: ultrasonicVoltageToInches);

			Wait(.005);
		}

		timer.Reset();

		while(timer.Get() < Constants::autoBrakeTime && IsEnabled() && IsAutonomous()) {	// while the nearest object is further than 12 feet
			robotDrive.MecanumDrive_Cartesian(0,Constants::autoBrakePower,0); ///Brake
		}

		float turn = 0;

		while (fabs(turn) < 85 && IsEnabled() && IsAutonomous())  { //turn 90(ish) degrees
			robotDrive.MecanumDrive_Cartesian(0,0,.1);
			turn = gyro.GetAngle();
			if (turn > 180) {
				turn -= 360;
			}
		}


		robotDrive.MecanumDrive_Cartesian(0,0,0); ///STOP!!!

		timer.Stop();
		toSend[0] = 8;
		i2c.Transaction(toSend, numToSend, toReceive, numToReceive);

		while(IsAutonomous() && IsEnabled());

		toSend[0] = 0;
		i2c.Transaction(toSend, numToSend, toReceive, numToReceive);
	}
	void TeleopPeriodic() {

		if(tick==10) if (ds->IsSysBrownedOut()) {
			ds->ReportError("[ERROR] BROWNOUT DETECTED!!");
		}
		if(tick == 15) if (!ds->IsNewControlData()) {
			ds->ReportError(
					"[ERROR] NO DATA FROM DRIVER STATION IN THIS TICK!");
		}
		if(tick==20) if (!ds->IsDSAttached()) {
			ds->ReportError("[ERROR] DRIVER STATION NOT DETECTED!");
		}

		if (stick.GetRawButton(10))
			zeroSanics();

		if (stick.GetRawButton(8)) {
			leftIRZero = 0;
			rightIRZero = 0;
		}
		tick++;

		if (liftStick.GetRawButton(2)) {
			double canScale = liftStick.GetRawAxis(2);
			canScale += 1;
			canScale = 2 - canScale;
			canScale /= 2;
			canGrabber.SetSpeed(canScale);
		} else if (liftStick.GetRawButton(3)) {
			double canScale = liftStick.GetRawAxis(2);
			canScale += 1;
			canScale = 2 - canScale;
			canScale /= 2;
			canGrabber.SetSpeed(-canScale);
		} else
			canGrabber.SetSpeed(0);

		double speed;

		//Calculate scalar to use for POV/Adjusted drive
		double scale = stick.GetRawAxis(3);
		scale += 1;
		scale = 2 - scale;
		scale /= 2;
		//Use pov/hat switch for movement if enabled
		if (stick.GetRawButton(1) && stick.GetRawButton(2)) {
			AutomaticLineup();
		} else if (stick.GetRawButton(1)) {
			double leftVolts = leftIR.GetAverageVoltage() - leftIRZero;
			double rightVolts = rightIR.GetAverageVoltage() - leftIRZero;

			if (rightVolts + VOLTAGE_TOLERANCE > leftVolts
					&& rightVolts - VOLTAGE_TOLERANCE < leftVolts) {
				robotDrive.MecanumDrive_Cartesian(0, 0, 0);
			} else if (rightVolts > leftVolts)
				robotDrive.MecanumDrive_Cartesian(0, 0, 0.2);
			else if (leftVolts > rightVolts)
				robotDrive.MecanumDrive_Cartesian(0, 0, -0.2);
		} else if (stick.GetRawButton(6)) {
			//Rotate
			robotDrive.MecanumDrive_Polar(0, 0, scale);
		} else if (stick.GetRawButton(5)) {
			//Rotate
			robotDrive.MecanumDrive_Polar(0, 0, -scale);
		} else if (stick.GetPOV(0) != -1) {
			//If POV moved, move polar (getPOV returns an angle in degrees)
			robotDrive.MecanumDrive_Polar(scale, -stick.GetPOV(0), 0);
		} else if (stick.GetRawButton(2)) {
			//Drive with scalar
			robotDrive.MecanumDrive_Cartesian(-stick.GetRawAxis(0) * scale,
					stick.GetRawAxis(1) * scale, stick.GetRawAxis(2) * scale);
		} else {
			//Drive normally
			robotDrive.MecanumDrive_Cartesian(-stick.GetX(), stick.GetY(),
					stick.GetZ());
		}
		speed = -liftStick.GetY();

		//bool canGoUp = maxUp.Get();
		bool canGoUp = true;
		//bool canGoDown = maxDown.Get();
		bool canGoDown = true;

		//If at a limit switch and moving in that direction, stop
		if (speed > 0 && !canGoUp)
			speed = 0;
		if (speed < 0 && !canGoDown)
			speed = 0;

		chainLift.SetSpeed(speed);

		if (tick >50) {
			if (SmartDashboard::GetBoolean("Smart Dashboard Enabled")) {
				//Smart Dash outputs
				//SmartDashboard::PutNumber("X Acceleration: ", accel.GetX());
				//SmartDashboard::PutNumber("Y Acceleration: ", accel.GetY());
				//SmartDashboard::PutNumber("Z Acceleration: ", accel.GetZ());
				SmartDashboard::PutBoolean("Switch 1: (up)", maxUp.Get());
				SmartDashboard::PutBoolean("Switch 2: (down)", maxDown.Get());
				SmartDashboard::PutBoolean("Switch 3: (mid)", midPoint.Get());
				SmartDashboard::PutBoolean("Auto switch A: ",
						autoSwitch1.Get());
				SmartDashboard::PutBoolean("Auto switch B: ",
						autoSwitch2.Get());

				//SmartDashboard::PutBoolean("RobotDrive Alive?",
					//	robotDrive.IsAlive());
				//SmartDashboard::PutBoolean("ChainLift Alive?",
					//	robotDrive.IsAlive());

				SmartDashboard::PutNumber("Left Sensor",
						leftIR.GetAverageVoltage());
				SmartDashboard::PutNumber("Right Sensor",
						rightIR.GetAverageVoltage());

				SmartDashboard::PutNumber("Left w zero",
						leftIR.GetAverageVoltage() - leftIRZero);
				SmartDashboard::PutNumber("Rigt w zero",
						rightIR.GetAverageVoltage() - rightIRZero);

				SmartDashboard::PutNumber("PDP 14 Current", pdp.GetCurrent(14));
				SmartDashboard::PutNumber("PDP 15 Current", pdp.GetCurrent(15));
			}

			tick = 0;
		}
	}
	void zeroSanics() {
		leftIRZero = leftIR.GetAverageVoltage();
		rightIRZero = rightIR.GetAverageVoltage();
	}