Skip to content

Instantly share code, notes, and snippets.

@yabberyabber
Created January 29, 2018 01:24
Show Gist options
  • Save yabberyabber/0bcb50842fb4152b97c3c7d8401a6b2e to your computer and use it in GitHub Desktop.
Save yabberyabber/0bcb50842fb4152b97c3c7d8401a6b2e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <cmath>
struct TalonPIDConfig {
float p = 0.0;
float i = 0.0;
float d = 0.0;
float f = 0.0;
};
/**
* A TalonConfig object might have a field for every configerable
* option that is pre-populated with a sane value
**/
struct TalonConfig {
bool inverted = false;
TalonPIDConfig pid_0 = { 0.0, 0.0, 0.0, 0.0 };
float continuousCurrentLimit = 40.0; /* ContinuousCurrentLimit */
float voltageCap = NAN; /* VoltageCompSaturation */
};
class TalonSRX {
private:
int m_canId;
public:
TalonSRX(int canId): m_canId(canId) {}
};
class GreyTalonSRX: public TalonSRX {
public:
/**
* The constructor in GreyTalonSRX could take a TalonConfig object
* that has a bunch of default values but where any of those values can be
* over-written
**/
GreyTalonSRX(int canId, TalonConfig config)
: TalonSRX(canId) {
printf(" Inverted: %d\n", config.inverted);
printf(" PID_0: { %f %f %f %f }\n", config.pid_0.p, config.pid_0.i,
config.pid_0.d, config.pid_0.f);
/* Having one field for configuring current-limit would prevent mistakes
like setting (but not enabling) current limit */
printf(" CurrentLimitEnabled: %d\n",
config.continuousCurrentLimit == config.continuousCurrentLimit);
printf(" CurrentLimit: %f\n", config.continuousCurrentLimit);
printf(" VoltageCapEnabled: %d\n",
config.voltageCap == config.voltageCap);
printf(" VoltageCap: %f\n", config.voltageCap);
}
};
int main(void) {
TalonConfig conf = {};
/* You could make a TalonConfig object that pre-populates with sane defaults
then you could change whatever fields you wanted */
conf.inverted = true;
conf.pid_0 = {
/* If you forget one of these fields you will get a compile error */
p : 8,
i : 2,
d : 6,
f : 8,
};
TalonSRX *talon = new GreyTalonSRX(9, conf);
}
Inverted: 1
PID_0: { 8.000000 2.000000 6.000000 8.000000 }
CurrentLimitEnabled: 1
CurrentLimit: 40.000000
VoltageCapEnabled: 0
VoltageCap: nan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment