summaryrefslogtreecommitdiff
path: root/src/org/usfirst/frc/team4272/robot2015/Robot.java
blob: f9569a671e9025d89fe3e9e8ff2a7dd14c2fd032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

package org.usfirst.frc.team4272.robot2015;

import org.usfirst.frc.team4272.robotlib.Toggler;
import org.usfirst.frc.team4272.robotlib.Xbox360Controller.Axis;
import org.usfirst.frc.team4272.robotlib.Xbox360Controller.Button;

import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Relay;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.Timer;

import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * The VM is configured to automatically run this class, and to call the
 * functions corresponding to each mode, as described in the IterativeRobot
 * documentation. If you change the name of this class or the package after
 * creating this project, you must also update the manifest file in the resource
 * directory.
 */
public class Robot extends IterativeRobot {
	private HwRobot robot = new HwRobot();
	private HwOI oi;
	
	private  PrintWriter log;
	
	private class Control {
		double lDrive, rDrive, winch;
		Relay.Value lIntake, rIntake;
		boolean grab, push;
		public Control() {
			lDrive = rDrive = winch = 0;
			grab = push = false;
			lIntake = rIntake = Relay.Value.kOff;
		}
	}
	Control control = new Control();
	
    /**
     * This function is run when the robot is first started up and should be
     * used for any initialization code.
     */
    public void robotInit() {
    	try {
    		inLog();
    	} catch (Exception e) {}
    }
    
    private void inLog() throws IOException {
    	log = new PrintWriter(new FileWriter("/usr/local/frc/share/lukeshu.log", true));
    	log.println("\ntime"
    			+",c:lDrive,c:rDrive,c:winch,c:lIntake,c:rIntake,c:grab,c:push"
    			+",i:lDrive.dist,i:lDrive.rate"
    			+",i:rDrive.dist,i:rDrive.rate"
    			+",i:winch.dist");
    }
    
    private void doLog() throws Exception {
    	log.println(0
    			+","+control.lDrive
    			+","+control.rDrive
    			+","+control.winch
    			+","+fmtRelay(control.lIntake)
    			+","+fmtRelay(control.rIntake)
    			+","+control.grab
    			+","+control.push
    			+","+robot.lDriveE.getDistance()
    			+","+robot.lDriveE.getRate()
    			+","+robot.rDriveE.getDistance()
    			+","+robot.rDriveE.getRate()
    			+","+robot.winchE.getRate()
    			);
    }
    
    private String fmtRelay(Relay.Value v) {
    	if (v == Relay.Value.kForward) {
    		return "kForward";
    	} else if (v == Relay.Value.kReverse) {
    		return "kReverse";
    	} else if (v == Relay.Value.kOn) {
    		return "kOn";
    	} else if (v == Relay.Value.kOff) {
    		return "kOff";
    	} else {
    		return "unknown";
    	}
    }
    
    private void doRobot() {
    	try {
    		robot.lDriveM.pidWrite( control.lDrive);
    		robot.rDriveM.pidWrite(-control.rDrive);    	
    		robot.winchM.pidWrite(-control.winch);
    		robot.lIntake.set(control.lIntake);
    		robot.rIntake.set(control.rIntake);
    		robot.grab.setForward(control.grab);
    		robot.push.setForward(!control.push);
    		doLog();
    	} catch (Exception e) {}
    }

    private Timer autotime = new Timer();
    public void autonomousInit() {
    	autotime.reset();
    	autotime.start();
    	robot.rDriveE.reset();
    	robot.lDriveE.reset();
    }
    public void autonomousPeriodic() {
    	if (/*autotime.get() < 4*/robot.rDriveE.getDistance() < (15.5*140*1.1)) {
    		control.rDrive = control.lDrive = -0.7;
    	} else {
    		control.rDrive = control.lDrive = 0;
    	}
    	doRobot();
    }

    private Toggler grabButton = new Toggler();
    private Toggler pushButton = new Toggler();
    private Toggler ledsButton = new Toggler();
    public void teleopInit() {
    	//robot.lDrive.reset();
    	//robot.rDrive.reset();
    	//robot.winch.reset();
    	//robot.lDrive.enable();
    	//robot.rDrive.enable();
    	//robot.winch.enable();
    	robot.grab.setEnabled(true);
    	robot.push.setEnabled(true);
    	oi = new HwOI();
    	grabButton.update(false);
    	pushButton.update(false);
    	ledsButton.update(false);
    }
    
    private static double jsScale(Joystick j) {
    	double y = -j.getY();/* +:forward; -:backward */
    	double z = -j.getZ();/* +:more-sensitive; -:less-sensitive */
    	return Math.copySign(Math.pow(Math.abs(y), 2.0-z), y);
    }
    
    public void teleopPeriodic() {
    	/* This runs at 50Hz */

    	/* Drive */
    	control.lDrive = jsScale(oi.lStick);
    	control.rDrive = jsScale(oi.rStick);
    	/* Winch */
    	control.winch = oi.xbox.getAxis(Axis.LY);/* up is neg, down is pos */
    	if (Math.abs(control.winch) < 0.1) { control.winch = 0; }
    	
    	SmartDashboard.putNumber("winch M", -control.winch);
    	SmartDashboard.putNumber("winch E", robot.winchE.get());
    	SmartDashboard.putBoolean("winch T", robot.winchT.get());
    	SmartDashboard.putBoolean("winch B", robot.winchB.get());
    	
    	/* left intake */
    	if (oi.xbox.getButton(Button.LB)) {
    		control.lIntake = Relay.Value.kReverse;
    	} else if (oi.xbox.getAxis(Axis.LT) > 0.75) {
    		control.lIntake = Relay.Value.kForward;
    	} else {
    		control.lIntake = Relay.Value.kOff;
    	}
    	  	
    	/* right intake */
    	if (oi.xbox.getButton(Button.RB)) {
    		control.rIntake=(Relay.Value.kReverse);
    	} else if (oi.xbox.getAxis(Axis.RT) > 0.75) {
    		control.rIntake=(Relay.Value.kForward);
    	} else {
    		control.rIntake=(Relay.Value.kOff);
    	}
    	
    	control.grab = grabButton.update(oi.xbox.getButton(Button.A));
    	control.push = pushButton.update(oi.xbox.getButton(Button.B));

    	doRobot();
    }
    
    public void disabledInit() {
    	//robot.lDrive.reset();
    	//robot.rDrive.reset();
    	//robot.winch.reset();
    	robot.grab.setEnabled(false);
    	robot.push.setEnabled(false);
    }
    
    public void disabledPeriodic() {
    	
    }
    
    /**
     * This function is called periodically during test mode
     */
    public void testPeriodic() {
    
    }
    
}