Skip to content

Commit

Permalink
Merge pull request #155 from met4000/v_drive
Browse files Browse the repository at this point in the history
Add velocity stuffs, and some other fixes
  • Loading branch information
JaciBrunning authored Feb 19, 2019
2 parents de57135 + 4efb8ee commit 943bbf2
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion 5333/src/main/cpp/Robot5333.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void Robot::RobotInit() {
robotmap.drivetrain.leftGearbox.encoder->ZeroEncoder();
robotmap.drivetrain.rightGearbox.encoder->ZeroEncoder();

drivetrain = new Drivetrain(robotmap.drivetrain.config);
drivetrain = new Drivetrain(robotmap.drivetrain.config, robotmap.drivetrain.gainsVelocity);
drivetrain->SetDefault(std::make_shared<DrivetrainManualStrategy>(*drivetrain, robotmap.contGroup));
drivetrain->StartLoop(100);
stratFOC = std::make_shared<DrivetrainFOCStrategy>(*drivetrain, robotmap.contGroup, robotmap.drivetrain.gainsFOC);
Expand Down
3 changes: 2 additions & 1 deletion 5333/src/main/include/BoxIntake.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "strategy/Strategy.h"
#include "strategy/StrategySystem.h"
#include "devices/DeployableDevice.h"
#include "CurtinControllers.h"
#include "Toggle.h"
Expand All @@ -14,7 +15,7 @@ struct BoxIntakeConfig : public curtinfrc::devices::DeployableDeviceConfig {
BoxIntakeConfig(curtinfrc::Gearbox &motorsIn, curtinfrc::actuators::BinaryActuator &actuatorIn, bool canEjectIn = true) : curtinfrc::devices::DeployableDeviceConfig(actuatorIn, canEjectIn), motors(motorsIn) {};
};

class BoxIntake : public curtinfrc::devices::DeployableDevice {
class BoxIntake : public curtinfrc::devices::DeployableDevice, public curtinfrc::StrategySystem {
public:
BoxIntake(BoxIntakeConfig config) : DeployableDevice(config), _config(config) {};

Expand Down
3 changes: 2 additions & 1 deletion 5333/src/main/include/HatchIntake.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "strategy/Strategy.h"
#include "strategy/StrategySystem.h"
#include "devices/DeployableDevice.h"
#include "actuators/BinaryActuator.h"
#include "CurtinControllers.h"
Expand All @@ -15,7 +16,7 @@ struct HatchIntakeConfig : public curtinfrc::devices::DeployableDeviceConfig {
HatchIntakeConfig(curtinfrc::actuators::BinaryActuator &manipulatorIn, curtinfrc::actuators::BinaryActuator &actuatorIn, curtinfrc::actuators::BinaryActuatorState stowedStateIn = curtinfrc::actuators::kForward, bool canEjectIn = false) : curtinfrc::devices::DeployableDeviceConfig(actuatorIn, canEjectIn), stowedState(stowedStateIn), manipulator(manipulatorIn) {};
};

class HatchIntake : public curtinfrc::devices::DeployableDevice {
class HatchIntake : public curtinfrc::devices::DeployableDevice, public curtinfrc::StrategySystem {
public:
HatchIntake(HatchIntakeConfig config) : DeployableDevice(config), _config(config) {};

Expand Down
1 change: 1 addition & 0 deletions 5333/src/main/include/RobotMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct RobotMap {
curtinfrc::PathfinderGains gainsPathfinder{ "Drivetrain Pathfinder", 24.0, 0, 1.5, 0.36, 0.08, 12.0 / 90.0 }; // PIDVAG

curtinfrc::DrivetrainConfig config{ leftGearbox, rightGearbox, &gyro, 0.71, 0.71, 0.0762, 50 };
curtinfrc::control::PIDGains gainsVelocity{ "Drivetrain Velocity", 1 };
};

DriveTrain drivetrain;
Expand Down
33 changes: 31 additions & 2 deletions common/src/main/cpp/Drivetrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ void curtinfrc::Drivetrain::Set(double leftPower, double rightPower) {
}

void curtinfrc::Drivetrain::SetVoltage(double left, double right) {
_setpoint = std::pair<double, double>{left, right};
_setpoint = std::pair<double, double>{ left, right };
SetState(curtinfrc::DrivetrainState::kManual);
}

void curtinfrc::Drivetrain::SetVelocity(double left, double right) {
_setpoint = std::pair<double, double>{ left, right };
SetState(curtinfrc::DrivetrainState::kVelocity);
}

void curtinfrc::Drivetrain::SetExternalLoop(std::function<std::pair<double, double>(curtinfrc::Drivetrain &, double)> func) {
_externalLoop = func;
SetState(curtinfrc::DrivetrainState::kExternalLoop);
Expand Down Expand Up @@ -43,16 +48,40 @@ double curtinfrc::Drivetrain::GetRightDistance() {

// Protected

void curtinfrc::Drivetrain::OnStateChange(DrivetrainState newState, DrivetrainState oldState) {
switch (newState) {
case curtinfrc::DrivetrainState::kVelocity:
_pidLeft.SetSetpoint(_setpoint.first);
_pidRight.SetSetpoint(_setpoint.second);
break;

default:
break;
}
}

void curtinfrc::Drivetrain::OnStatePeriodic(curtinfrc::DrivetrainState state, double dt) {
std::pair<double, double> outputs{0, 0};
std::pair<double, double> outputs{ 0, 0 };

switch (state) {
case curtinfrc::DrivetrainState::kManual:
outputs = _setpoint;
break;

case curtinfrc::DrivetrainState::kVelocity:
_pidLeft.SetSetpoint(_setpoint.first, false);
_pidRight.SetSetpoint(_setpoint.second, false);

outputs = {
_pidLeft.Calculate(GetLeft().encoder->GetEncoderAngularVelocity() * _config.wheelRadius, dt),
_pidRight.Calculate(GetRight().encoder->GetEncoderAngularVelocity() * _config.wheelRadius, dt)
};
break;

case curtinfrc::DrivetrainState::kExternalLoop:
outputs = _externalLoop(*this, dt);
break;

default:
break;
}
Expand Down
4 changes: 2 additions & 2 deletions common/src/main/cpp/control/PIDController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ double PIDGains::GetkF() const {

PIDController::PIDController(PIDGains gains, double setpoint) : _gains(gains), _setpoint(setpoint), _lastError(0), _filterPos(LinearFilter::MovingAverage(20)), _filterVel(LinearFilter::MovingAverage(20)) {}

void PIDController::SetSetpoint(double setpoint) {
Reset();
void PIDController::SetSetpoint(double setpoint, bool reset) {
if (reset) Reset();
_setpoint = setpoint;
if (_threshAvgSet == false) {
_threshAvgPos = setpoint * 0.05;
Expand Down
4 changes: 2 additions & 2 deletions common/src/main/cpp/strategy/MPStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ void DrivetrainCharacterizationStrategy::OnUpdate(double dt) {
double leftPos = _drivetrain.GetLeftDistance();
double rightPos = _drivetrain.GetRightDistance();

double leftRate = _drivetrain.GetConfig().leftDrive.encoder->GetEncoderAngularVelocity() * 3.14159265 * 2 * _drivetrain.GetConfig().wheelRadius;
double rightRate = _drivetrain.GetConfig().rightDrive.encoder->GetEncoderAngularVelocity() * 3.14159265 * 2 * _drivetrain.GetConfig().wheelRadius;
double leftRate = _drivetrain.GetConfig().leftDrive.encoder->GetEncoderAngularVelocity() * _drivetrain.GetConfig().wheelRadius;
double rightRate = _drivetrain.GetConfig().rightDrive.encoder->GetEncoderAngularVelocity() * _drivetrain.GetConfig().wheelRadius;

double battery = frc::RobotController::GetInputVoltage();
double motorVolts = battery * std::abs(_lastAutospeed);
Expand Down
9 changes: 6 additions & 3 deletions common/src/main/include/Drivetrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ namespace curtinfrc {
bool reversed = false;
};

enum class DrivetrainState { kManual = 0, kIdle, kExternalLoop };
enum class DrivetrainState { kManual = 0, kVelocity, kIdle, kExternalLoop };

class Drivetrain : public devices::StateDevice<DrivetrainState> {
class Drivetrain : public devices::StateDevice<DrivetrainState>, public StrategySystem {
public:
Drivetrain(DrivetrainConfig config) : _config(config) {};
Drivetrain(DrivetrainConfig config, control::PIDGains gains = { "Drivetrain Velocity" }) : _config(config), _pidLeft(gains), _pidRight(gains) {};

void Set(double leftPower, double rightPower);
void SetVoltage(double left, double right);
void SetVelocity(double left, double right);
void SetExternalLoop(std::function<std::pair<double, double>(Drivetrain &, double)> func);
void SetIdle();

Expand All @@ -72,12 +73,14 @@ namespace curtinfrc {
double GetRightDistance();

protected:
void OnStateChange(DrivetrainState newState, DrivetrainState oldState) override;
void OnStatePeriodic(DrivetrainState state, double dt) override;

Gearbox &GetLeft();
Gearbox &GetRight();

private:
control::PIDController _pidLeft, _pidRight;
std::pair<double, double> _setpoint;
std::function<std::pair<double,double>(Drivetrain &,double)> _externalLoop;

Expand Down
3 changes: 2 additions & 1 deletion common/src/main/include/Elevator.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Gearbox.h"
#include "sensors/BinarySensor.h"
#include "control/PIDController.h"
#include "strategy/StrategySystem.h"

#include "Usage.h"

Expand Down Expand Up @@ -31,7 +32,7 @@ namespace curtinfrc {
};

enum class ElevatorState { kStationary = 0, kMoving, kZeroing, kManual };
class Elevator : public devices::StateDevice<ElevatorState> {
class Elevator : public devices::StateDevice<ElevatorState>, public StrategySystem {
public:
Elevator(ElevatorConfig config, control::PIDGains gain) : _config(config), _gain(gain), _controller(gain) {};

Expand Down
2 changes: 1 addition & 1 deletion common/src/main/include/control/PIDController.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace control {
public:
PIDController(PIDGains gains, double setpoint = 0);

void SetSetpoint(double setpoint);
void SetSetpoint(double setpoint, bool reset = true);
double GetSetpoint();

void SetIZone(double threshIZone);
Expand Down
3 changes: 1 addition & 2 deletions common/src/main/include/devices/StateDevice.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#pragma once

#include "loops/LoopSystem.h"
#include "strategy/StrategySystem.h"

namespace curtinfrc {
namespace devices {

template <typename StateType>
class StateDevice : public StrategySystem, public loops::LoopSystem {
class StateDevice : public loops::LoopSystem {
public:
StateDevice() { _state = (StateType)0; };

Expand Down

0 comments on commit 943bbf2

Please sign in to comment.