Log in

View Full Version : Sample Code


ssirovica
08-11-2011, 10:02
Hi everyone,

I just recently became the head programmer on my team :D , but would like some more example code, other than what can be found on the tutorials.

Any useful links or Google code pages?

Thanks,
Sasha Sirovica

austin1743
08-11-2011, 10:50
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);

}
}

EricH
08-11-2011, 10:52
You might want to specify a language for the sample code to be in. If you're programming in C++ and the sample is in Labview, you won't be able to make head or tail of it.

ebarker
08-11-2011, 11:00
Here are code samples that do a lot of random stuff in C++ and Java.

We use these files for training.

http://www.kellrobotics.org/files/software/MyProject.java

http://www.kellrobotics.org/files/software/MyRobot.cpp

ssirovica
08-11-2011, 11:18
Sorry, C++ would be great.

Roger
08-11-2011, 12:07
And to be complete, though not what was (finally) asked for, LabVIEW has many robot and non-robot examples available within the provided LabVIEW program. They show small bits of complete code for various parts of the robot that you can run on your robot (after changing to your address). I would assume the C++ and Java have similar.
If you're programming in C++ and the sample is in Labview, you won't be able to make head or tail of it.Should be "some people may not" (my highlighted words). :D

~Cory~
08-11-2011, 13:57
I know I sound like a broken record but the main tools you will be using are ctrl-c ctrl-v Dont worry about learning programming. worry about learning solving the problem (i.e. make a usable robot). Many teams have posted their code from previous years, and you can copy and paste to get the problem done. Overtime, you will learn to solve problems on your own.

I can help you find some stuff, but you have to define the problem first. What are you most likely to be programming? A tank drive? holomonic? or do you want to focus on manipulators? or do you want to dive into Autonomous?