Skip to content

Commit d66a3b0

Browse files
authored
Support follower mode (#12)
* Fix built in PID calculation * Add follower mode
1 parent cd5c3bd commit d66a3b0

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

expansionhub/MotorNtState.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define VOLTAGE_MODE 1
77
#define POSITION_PID_MODE 2
88
#define VELOCITY_PID_MODE 3
9+
#define FOLLOWER_MODE 4
910

1011
using namespace eh;
1112

@@ -23,28 +24,31 @@ void MotorNtState::SetEncoder(double positionRaw, double velocityRaw) {
2324
velocityPublisher.Set(lastEncoderVelocity);
2425
}
2526

26-
double MotorNtState::ComputeMotorPower(double batteryVoltage) {
27+
std::pair<double, int> MotorNtState::ComputeMotorPower(double batteryVoltage) {
2728
double reversed = reversedSubscriber.Get(false) ? -1.0 : 1.0;
2829
if (batteryVoltage == 0) {
29-
return 0;
30+
return {0.0, -1};
3031
}
3132
double setpoint = setpointSubscriber.Get(0);
3233
switch (modeSubscriber.Get(PERCENTAGE_MODE)) {
3334
case VOLTAGE_MODE:
34-
return (setpoint / batteryVoltage) * reversed;
35+
return {(setpoint / batteryVoltage) * reversed, -1};
3536

3637
case POSITION_PID_MODE:
37-
return (positionPid.Compute(setpoint, lastEncoderPosition) /
38+
return {(positionPid.Compute(setpoint, lastEncoderPosition) /
3839
batteryVoltage) *
39-
reversed;
40+
reversed, -1};
4041

4142
case VELOCITY_PID_MODE:
42-
return (velocityPid.Compute(setpoint, lastEncoderVelocity) /
43+
return {(velocityPid.Compute(setpoint, lastEncoderVelocity) /
4344
batteryVoltage) *
44-
reversed;
45+
reversed, -1};
46+
47+
case FOLLOWER_MODE:
48+
return {0.0, static_cast<int>(setpoint)};
4549

4650
default:
47-
return setpoint * reversed;
51+
return {setpoint * reversed, -1};
4852
}
4953
}
5054

expansionhub/MotorNtState.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "networktables/DoubleTopic.h"
66
#include "PidConstants.h"
77
#include "CachedCommand.h"
8+
#include <utility>
89

910
namespace eh {
1011

@@ -37,7 +38,7 @@ struct MotorNtState {
3738
double lastEncoderPosition{0};
3839
double lastEncoderVelocity{0};
3940

40-
double ComputeMotorPower(double batteryVoltage);
41+
std::pair<double, int> ComputeMotorPower(double batteryVoltage);
4142

4243
void SetEncoder(double positionRaw, double velocityRaw);
4344
};

expansionhub/main.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,23 @@ void ExpansionHubState::SendCommands(bool canEnable, bool deviceReset) {
129129
}
130130
}
131131

132-
// First the 4 motors.
132+
// Compute motor powers
133+
std::pair<double, int> motorPowers[NUM_MOTORS_PER_HUB];
133134
for (int i = 0; i < NUM_MOTORS_PER_HUB; i++) {
134-
currentHub->SendMotorConstantPower(
135-
i, ntStore.motors[i].ComputeMotorPower(ntStore.lastBattery));
135+
motorPowers[i] =
136+
ntStore.motors[i].ComputeMotorPower(ntStore.lastBattery);
137+
}
138+
139+
// Then send the 4 motors.
140+
for (int i = 0; i < NUM_MOTORS_PER_HUB; i++) {
141+
if (motorPowers[i].second < 0) {
142+
currentHub->SendMotorConstantPower(i, motorPowers[i].first);
143+
} else if (motorPowers[i].second < 4) {
144+
// TODO, determine if a follower is following a follower, and what to do
145+
currentHub->SendMotorConstantPower(i, motorPowers[motorPowers[i].second].first);
146+
} else {
147+
currentHub->SendMotorConstantPower(i, 0.0);
148+
}
136149
}
137150

138151
// Then get the motor currents
@@ -175,7 +188,8 @@ void ExpansionHubState::SendCommands(bool canEnable, bool deviceReset) {
175188
currentHub->Flush();
176189
}
177190

178-
void ExpansionHubState::OnDeviceAdded(std::unique_ptr<eh::ExpansionHubSerial> hub) {
191+
void ExpansionHubState::OnDeviceAdded(
192+
std::unique_ptr<eh::ExpansionHubSerial> hub) {
179193
currentHub = std::move(hub);
180194

181195
ntStore.Initialize(*ntInstance, busId);
@@ -275,7 +289,8 @@ static void OnDeviceAdded(wpi::uv::Loop& loop,
275289
return;
276290
}
277291

278-
std::unique_ptr<eh::ExpansionHubSerial> sl = std::make_unique<eh::ExpansionHubSerial>();
292+
std::unique_ptr<eh::ExpansionHubSerial> sl =
293+
std::make_unique<eh::ExpansionHubSerial>();
279294
bool isInit = sl->Initialize(loop, ret, devPath);
280295
printf("initialized %d\n", isInit ? 1 : 0);
281296

@@ -341,8 +356,8 @@ int main() {
341356
};
342357

343358
bool success = false;
344-
loopRunner.ExecSync([&success, &states, &loopStorage, &ntInst,
345-
&usbMonitor, &enabledState](wpi::uv::Loop& loop) {
359+
loopRunner.ExecSync([&success, &states, &loopStorage, &ntInst, &usbMonitor,
360+
&enabledState](wpi::uv::Loop& loop) {
346361
loopStorage.loop = &loop;
347362
for (size_t i = 0; i < states.size(); i++) {
348363
success = states[i].StartUvLoop(i, ntInst, loop);

0 commit comments

Comments
 (0)