Skip to content

Commit

Permalink
[hal] Update SmartIO PWM implementation (#7571)
Browse files Browse the repository at this point in the history
* Integer microsecond setters and getters
* Per-port subtables
  • Loading branch information
bhjelstrom authored Dec 22, 2024
1 parent 529bab6 commit 78b14c5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
24 changes: 9 additions & 15 deletions hal/src/main/native/systemcore/SmartIo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,19 @@ int32_t SmartIo::InitializeMode(SmartIoMode mode) {
options.periodic = 0.005;

auto channelString = std::to_string(channel);
auto subTableString = "/io/" + channelString + "/";

modePublisher = inst.GetDoubleTopic("/io/type" + channelString).Publish();
getSubscriber = inst.GetDoubleTopic("/io/valread" + channelString)
.Subscribe(0.0, options);
modePublisher = inst.GetIntegerTopic(subTableString + "type").Publish();
getSubscriber =
inst.GetIntegerTopic(subTableString + "valget").Subscribe(0.0, options);

currentMode = mode;
switch (mode) {
case SmartIoMode::PWMOutput:
modePublisher.Set(3);
modePublisher.Set(4);
setPublisher =
inst.GetDoubleTopic("/io/valset" + channelString).Publish(options);
inst.GetIntegerTopic(subTableString + "valset").Publish(options);
setPublisher.Set(0);
pwmMinPublisher =
inst.GetDoubleTopic("/io/pwmmin" + channelString).Publish();
pwmMinPublisher.Set(0);
pwmMaxPublisher =
inst.GetDoubleTopic("/io/pwmmax" + channelString).Publish();
pwmMaxPublisher.Set(4096);
return 0;

default:
Expand All @@ -67,8 +62,7 @@ int32_t SmartIo::SetPwmMicroseconds(uint16_t microseconds) {
microseconds = 4095;
}

// Scale from 0-4096 to 0.0-2.0, then to -1.0-1.0
setPublisher.Set((microseconds / 2048.0) - 1);
setPublisher.Set(microseconds);

return 0;
}
Expand All @@ -78,10 +72,10 @@ int32_t SmartIo::GetPwmMicroseconds(uint16_t* microseconds) {
return INCOMPATIBLE_STATE;
}

double val = getSubscriber.Get();
int val = getSubscriber.Get();

// Get to 0-2, then scale to 0-4096;
*microseconds = (val + 1) * 2048;
*microseconds = val;

return 0;
}
Expand Down
11 changes: 4 additions & 7 deletions hal/src/main/native/systemcore/SmartIo.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "PortsInternal.h"
#include "hal/handles/DigitalHandleResource.h"
#include "hal/handles/HandlesInternal.h"
#include "networktables/DoubleTopic.h"
#include "networktables/IntegerTopic.h"

namespace hal {

Expand All @@ -32,13 +32,10 @@ struct SmartIo {
int32_t minPwm = 0;
std::string previousAllocation;
SmartIoMode currentMode{SmartIoMode::DigitalInput};
nt::DoublePublisher modePublisher;
nt::IntegerPublisher modePublisher;

nt::DoublePublisher setPublisher;
nt::DoubleSubscriber getSubscriber;

nt::DoublePublisher pwmMinPublisher;
nt::DoublePublisher pwmMaxPublisher;
nt::IntegerPublisher setPublisher;
nt::IntegerSubscriber getSubscriber;

int32_t InitializeMode(SmartIoMode mode);
int32_t SetPwmMicroseconds(uint16_t microseconds);
Expand Down

0 comments on commit 78b14c5

Please sign in to comment.