| 1 | package org.usfirst.frc.team3501.robot; |
| 2 | |
| 3 | import java.io.BufferedReader; |
| 4 | import java.io.FileReader; |
| 5 | import java.io.IOException; |
| 6 | import java.util.Arrays; |
| 7 | import java.util.HashMap; |
| 8 | |
| 9 | public class AutonData { |
| 10 | |
| 11 | HashMap<String, Double> speeds; |
| 12 | HashMap<String, Double> times; |
| 13 | |
| 14 | public AutonData() { |
| 15 | speeds = new HashMap<String, Double>(); |
| 16 | times = new HashMap<String, Double>(); |
| 17 | |
| 18 | populate(); |
| 19 | } |
| 20 | |
| 21 | public double getSpeed(String key) { |
| 22 | Double ret = speeds.get(key); |
| 23 | |
| 24 | return (ret != null) ? ret : 0; |
| 25 | } |
| 26 | |
| 27 | public double getTime(String key) { |
| 28 | Double ret = times.get(key); |
| 29 | |
| 30 | return (ret != null) ? ret : 0; |
| 31 | } |
| 32 | |
| 33 | public void update() { |
| 34 | speeds.clear(); |
| 35 | times.clear(); |
| 36 | |
| 37 | populate(); |
| 38 | } |
| 39 | |
| 40 | private void populate() { |
| 41 | String file; |
| 42 | |
| 43 | try { |
| 44 | file = readConfigFile(); |
| 45 | } catch (IOException e) { |
| 46 | e.printStackTrace(); |
| 47 | populateDefaults(); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | try { |
| 52 | Arrays.stream(file.split("\n")) |
| 53 | .map(line -> line.split(" ")) |
| 54 | .forEach((action) -> { |
| 55 | double speed = Double.parseDouble(action[0]); |
| 56 | double time = Double.parseDouble(action[1]); |
| 57 | String name = action[2]; |
| 58 | |
| 59 | speeds.put(name, speed); |
| 60 | times.put(name, time); |
| 61 | }); |
| 62 | } catch (Exception e) { |
| 63 | e.printStackTrace(); |
| 64 | populateDefaults(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private void populateDefaults() { |
| 69 | speeds.clear(); |
| 70 | times.clear(); |
| 71 | |
| 72 | speeds.put("drive_over_step", 0.7); |
| 73 | speeds.put("drive_past_step", 0.5); |
| 74 | speeds.put("pickup_container", 0.5); |
| 75 | |
| 76 | times.put("drive_over_step", 1.2); |
| 77 | times.put("drive_past_step", 1.5); |
| 78 | times.put("pickup_container", 1.4); |
| 79 | } |
| 80 | |
| 81 | private String readConfigFile() throws IOException { |
| 82 | BufferedReader in = new BufferedReader(new FileReader( |
| 83 | "auton_times_and_speeds.conf")); |
| 84 | |
| 85 | StringBuilder sb = new StringBuilder(); |
| 86 | |
| 87 | in.readLine(); // get rid of first line |
| 88 | |
| 89 | String curLine; |
| 90 | while ((curLine = in.readLine()) != null) |
| 91 | sb.append(curLine + "\n"); |
| 92 | String finalString = sb.toString(); |
| 93 | |
| 94 | in.close(); |
| 95 | |
| 96 | return finalString; |
| 97 | } |
| 98 | } |