Permalink
Cannot retrieve contributors at this time
Fetching contributors…
| package org.usfirst.frc.team236.robot; | |
| import java.util.ArrayList; | |
| import java.util.Timer; | |
| import java.util.TimerTask; | |
| /** | |
| * Takes care of updating all updatables that should update with the main robot thread, | |
| * as well as updatables that should run in their own separate thread. | |
| * | |
| * This class cannot be instantiated externally, so call Updater.getInstance() to get | |
| * the instance of this class | |
| * @author team236 | |
| * | |
| */ | |
| public class Updater { | |
| //the single instance of updater is held within the updater class | |
| static Updater updater; | |
| boolean controlLoopsStarted = false; | |
| //the list of all the things that can be updated | |
| ArrayList<Updatable> updatabales = new ArrayList<Updatable>(); | |
| //the instance of the threadedUpdater class which updates all updatables that should | |
| //be updated in a separate thread. | |
| ThreadedUpdater threadedUpdater; | |
| ElevatorThreadedUpdater elev; | |
| //a private constructor means that a new instance of updater cannot be created | |
| //outside of this class | |
| private Updater(){ | |
| //construct and start the threaded updater for control loops like PID/profile follower | |
| threadedUpdater = new ThreadedUpdater(); | |
| elev = new ElevatorThreadedUpdater(); | |
| } | |
| public void initControllers(){ | |
| if(!controlLoopsStarted){ | |
| Timer e_timer = new Timer(); | |
| Timer timer = new Timer(); | |
| timer.scheduleAtFixedRate(threadedUpdater, 0, 20); | |
| e_timer.scheduleAtFixedRate(elev, 0, 20); | |
| } | |
| controlLoopsStarted = true; | |
| } | |
| //this returns the single instance of updater | |
| //if the instance has not been created yet, a new one is made | |
| public static Updater getInstance(){ | |
| if(updater == null){ | |
| updater = new Updater(); | |
| } | |
| return updater; | |
| } | |
| //iterate through the list of updatables, updating each | |
| public void updateAll(){ | |
| for(int i = 0; i < updatabales.size(); i++){ | |
| updatabales.get(i).update(); | |
| } | |
| } | |
| //add a new updatable object to the list that will be updated. | |
| public void addUpdatable(Updatable updatable){ | |
| updatabales.add(updatable); | |
| } | |
| //add a new updatable object to be run in a separate thread | |
| public void addThreadedUpdatable(Updatable updatable){ | |
| threadedUpdater.addThreadedUpdatable(updatable); | |
| } | |
| public void addElevatorUpdater(Updatable updatable){ | |
| elev.addThreadedUpdatable(updatable); | |
| } | |
| //class for threaded updates | |
| private class ThreadedUpdater extends TimerTask { | |
| //list of updatables that should run in their own thread | |
| ArrayList<Updatable> _50hz_updatables = new ArrayList<Updatable>(); | |
| //add an updatable that will run in a separate thread | |
| public void addThreadedUpdatable(Updatable updatable){ | |
| _50hz_updatables.add(updatable); | |
| } | |
| //method called automatically by the timer in Updater | |
| @Override | |
| public void run() { | |
| for(int i = 0; i < _50hz_updatables.size(); i++){ | |
| _50hz_updatables.get(i).update(); | |
| } | |
| } | |
| } | |
| //class for threaded updates | |
| private class ElevatorThreadedUpdater extends TimerTask { | |
| //list of updatables that should run in their own thread | |
| ArrayList<Updatable> _50hz_updatables = new ArrayList<Updatable>(); | |
| //add an updatable that will run in a separate thread | |
| public void addThreadedUpdatable(Updatable updatable){ | |
| _50hz_updatables.add(updatable); | |
| } | |
| //method called automatically by the timer in Updater | |
| @Override | |
| public void run() { | |
| for(int i = 0; i < _50hz_updatables.size(); i++){ | |
| _50hz_updatables.get(i).update(); | |
| } | |
| } | |
| } | |
| } |