Skip to content

Instantly share code, notes, and snippets.

@ThomasJClark
Last active August 29, 2015 14:12
Show Gist options
  • Save ThomasJClark/a5069c83c3fa926b096b to your computer and use it in GitHub Desktop.
Save ThomasJClark/a5069c83c3fa926b096b to your computer and use it in GitHub Desktop.
A speed controller for WPILib that controlls multiple motors
#include "SpeedController.h"
#include <vector>
/**
* A speed controller that controls an array of speed controllers with
* the same value
*
* @author Thomas Clark
*/
class MultiSpeedController: public SpeedController {
std::vector<SpeedController*> m_speedControllers;
float m_speed;
public:
/**
* Construct a MultiSpeedController
*
* @param speedControllers A vector of speed controllers to control with
* the same value. These are owned by this class.
*/
MultiSpeedController(std::vector<SpeedController*> const& speedControllers) :
m_speedControllers(speedControllers), m_speed(0.0f) {
}
virtual ~MultiSpeedController() {
for (int i = 0; i < m_speedControllers.size(); i++)
delete m_speedControllers[i];
}
/**
* @return The speed of all of the motors
*/
virtual float Get() {
return m_speed;
}
/**
* @param speed The speed of all of the motors
*/
virtual void Set(float speed, uint8_t syncGroup = 0) {
for (int i = 0; i < m_speedControllers.size(); i++)
m_speedControllers[i]->Set(speed, syncGroup);
m_speed = speed;
}
virtual void PIDWrite(float output) {
this->Set(output);
}
/**
* Disable all of the motors
*/
virtual void Disable() {
for (int i = 0; i < m_speedControllers.size(); i++)
m_speedControllers[i]->Disable();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment