|
Re: Sample Code
Welcome to Programming, i am in the same position that you are,
I have been learning java programming for sometime (self-teaching). I wrote some java code that i pasted in this message bellow. Your welcome to check it out: If you got any questions on it let me know. I program commenting everything so that if someone else looks at it they can see what was done and a short explanation on most things.
The code is for a robot that uses mecanum (sorry about spelling on this one) drive. It is a little complicated to understand but working through that part still.
From what i understand i wrote the following code:
// things below this need to be imported first
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.Dashboard;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import com.sun.squawk.util.MathUtils;
public class RobotTemplate extends IterativeRobot {
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
RobotDrive m_robotDrive;// robot will use PWM 1-4 for drive motors
//declears speed controllers
Victor leftFrontspeed = new Victor(4, 1);
Victor rightFrontspeed = new Victor(4, 2);
Victor leftBackspeed = new Victor(4,3);
Victor rightBackspeed = new Victor(4,4);
DriverStation driverStation; // driver station object
Dashboard dashboard;
// Declare variables for the two joysticks being used
Joystick js1; // joystick 1 (arcade stick or right tank stick)
Joystick js2; // joystick 2 (tank left stick)
public void robotInit() {
}
/**
* This function is called periodically during autonomous
*/
//public void autonomousPeriodic() {
// }
/**This function is called periodically during operator control
*/
public void teleopPeriodic() {
//leftspeed.set(js1.getY());
//rightspeed.set(js1.getY());
//is called when teloporated mode is enabled
double lf; //is what will be set to the left wheel speed
double rf; //is what will be set to the right wheel speed
double lb; // is what will be set to the left back wheel speed
double rb; // is what will be set to the left back wheel speed
double y = js1.getY(); //gets the value of the 1'st joystick's y axis
double x = js1.getX(); //gets the value of the 1'st joystick's x axis
y = y * Math.abs(y);//makes the y value quadratic
x = x * Math.abs(x);//makes the x value quadratic
x=-x;
//begins meckanum code which I do not understand but it works from what i understand
double magnitude =Math.sqrt(js1.getY()*js1.getY()+js1.getX()*js1.ge tX());
magnitude = magnitude*Math.sqrt(2);
double rotation = js1.getTwist();
double direction=MathUtils.atan2(x, y);
double dirInRad = (direction+45);
double sinD=Math.sin(dirInRad);
double cosD=Math.cos(dirInRad)*Math.sqrt(2);
lf=-sinD*magnitude+rotation;
rf=-cosD*magnitude-rotation;
lb=-cosD*magnitude+rotation;
rb=-sinD*magnitude-rotation;
//ends mecanum code
lf=y;
lb=y;
rb=y;
rf=y;
//runs function limit to ensure the speed of the motors is in exceptable range
//lf =limit(lf,1,-1);
//lb =limit(lb,1,-1);
// rf =limit(rf,1,-1);
// rb =limit(rb,1,-1);
/*magnitude = js1.getMagnitude();
direction = js1.getDirectionDegrees();
rotation = js1.getX();
driveRobot.mecanumDrive_Polar(magnitude, direction, rotation);
*/
//sets the motors speed
leftFrontspeed.set(lf);
leftBackspeed.set(lb);
rightFrontspeed.set(-rf);
rightBackspeed.set(-rb);
System.out.println("js1.y:"+js1.getY()+"\t js1.x:"+js1.getX()+"\t lf:"+lf+"\t rf:"+rf+"\t lb:"+lb+"\t rb:"+rb);
}
}
|