Go to Post "I would love to change the world but no one will give me the source code" - programmr [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Prev Previous Post   Next Post Next
  #1   Spotlight this post!  
Unread 04-19-2015, 01:53 PM
WillNess's Avatar
WillNess WillNess is offline
Programmer
AKA: Will Ness
FRC #4944 (The Hi Fives)
Team Role: Programmer
 
Join Date: Apr 2014
Rookie Year: 2014
Location: United States
Posts: 90
WillNess is just really niceWillNess is just really niceWillNess is just really niceWillNess is just really nice
Getting Gyro Working w/ Mecanum Drive

Although our season is over, the programming team is still tasked with completing several things before the start of the next season. One of the more difficult things (and something I struggled with implementing) during the build season was using a gyro to help the drivers. This is the code I've come up with so far, but I only included the drive code, nothing else:
Code:
teleopPeriodic(){
   driveCode();
}
    public static void driveCode(){
    	RobotMap.calculateTurnSpeedsAndAngles();
        if(OI.stick.getZ() < .1){
        	double subtractValue = 0; 
        	if(Robot.turnSpeedForGyroCorrection > .3){
        		subtractValue = Robot.turnSpeedForGyroCorrection - .3;
        	}else{
                         //I do not want to robot traveling past 30%, so this subtractValue will never let it go past 50%
        		subtractValue = 0;
        	}
        	RobotMap.setVelocityGyro(OI.stick.getX(), OI.stick.getY(), OI.stick.getZ());
        }else{
        	RobotMap.setVelocity(OI.stick.getX(), OI.stick.getY(), OI.stick.getZ());
        	Robot.setAngle = RobotMap.driveGyro.getAngle();
        }
    }

public static void setVelocity(double x,double y,double z){
		double fMax = Math.sin(-0*Math.PI/4+1*Math.PI/4);
		double fR = Math.sin(x*Math.PI/4+y*Math.PI/4)/fMax;
		double rL = -Math.sin(x*Math.PI/4+y*Math.PI/4)/fMax;
		double fL = -Math.sin(-x*Math.PI/4+y*Math.PI/4)/fMax;
		double rR = Math.sin(-x*Math.PI/4+y*Math.PI/4)/fMax;
    	if(Math.abs(z)<.1){
	    	drivefrontLeft.set(fL);
	    	drivefrontRight.set(fR);
	    	driverearLeft.set(rL);
	    	driverearRight.set(rR);
    	}else{
    		drivefrontLeft.set(fL+z/2);
    		drivefrontRight.set(fR+z/2);
    		driverearLeft.set(rL+z/2);
    		driverearRight.set(rR+z/2);
    	}
    }
    public static void setVelocityGyro(double x,double y,double z){
    	        RobotMap.calculateTurnSpeedsAndAngles();
		double fMax = Math.sin(-0*Math.PI/4+1*Math.PI/4);
		double fR = Math.sin(x*Math.PI/4+y*Math.PI/4)/fMax;
		double rL = -Math.sin(x*Math.PI/4+y*Math.PI/4)/fMax;
		double fL = -Math.sin(-x*Math.PI/4+y*Math.PI/4)/fMax;
		double rR = Math.sin(-x*Math.PI/4+y*Math.PI/4)/fMax;
		double errorOfAngles = Robot.actualAngle - Robot.setAngle;
		double turnSpeedRight = 0;
		double turnSpeedLeft = 0;
		if(errorOfAngles > 0){
			if(fL > 0){
				turnSpeedLeft = Math.abs(Robot.turnSpeedForGyroCorrection);
			}else{
				turnSpeedLeft = -Math.abs(Robot.turnSpeedForGyroCorrection);
			}
			turnSpeedRight = 0;
		}else if(errorOfAngles < 0){
			turnSpeedLeft = 0;
			if(fR > 0){
				turnSpeedRight = Math.abs(Robot.turnSpeedForGyroCorrection);
			}else{
				turnSpeedRight = -Math.abs(Robot.turnSpeedForGyroCorrection);
			}
		}else{
			turnSpeedLeft = 0;
			turnSpeedRight = 0;
		}
            drivefrontLeft.set(fL - turnSpeedLeft);
	    drivefrontRight.set(fR - turnSpeedRight);
	    driverearLeft.set(rL - turnSpeedLeft);
	    driverearRight.set(rR - turnSpeedRight);
    } 
    public static void calculateTurnSpeedsAndAngles(){
    	double turnValue = 0.02;
    	Robot.actualAngle = RobotMap.driveGyro.getAngle();
    	double angleError = Robot.actualAngle - Robot.setAngle;
    	Robot.turnSpeedForGyroCorrection = angleError * turnValue;
    }
Sorry for the weird spacing, that's just how the copy+paste did it. Any help would be appreciated! Also: This is untested
__________________

Outreach Lead // Lead Programmer // Junior

2014 FRC:
Rookie Allstar, Highest Rookie Seed & Semifinalist @ Utah
Rookie Allstar, Highest Rookie Seed & Semifinalist @ Colorado
2015 FRC:
Creativity In Engineering & Semifinalist @ Arizona West
Reply With Quote
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 08:49 AM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi