Skip to content

Commit

Permalink
[driver/labjack] verified working writes on hardware
Browse files Browse the repository at this point in the history
  • Loading branch information
Lham42 committed Oct 21, 2024
1 parent d97e3ca commit b3e7454
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 14 deletions.
15 changes: 15 additions & 0 deletions driver/labjack/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ cc_test(
})
)

cc_test(
name = "writer_test",
srcs = ["writer_test.cpp"],
deps = [
":labjack",
"@com_google_googletest//:gtest_main",
"//driver/testutil",
],
linkopts = select({
# TODO: This is a temporary fix to supress multiple definitions error when building, should find better workaroud
"@bazel_tools//src/conditions:windows": ["/FORCE:MULTIPLE"],
"//conditions:default": [],
})
)


cc_binary(
name = "eReadNames",
Expand Down
4 changes: 4 additions & 0 deletions driver/labjack/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ std::pair<std::unique_ptr<task::Task>, bool> labjack::Factory::configure_task(
const std::shared_ptr<task::Context> &ctx,
const synnax::Task &task
) {

if (task.type == "labjack_scan")
return {std::make_unique<labjack::ScannerTask>(ctx, task), true}; // TODO: replace with configure for scanner
if (task.type == "labjack_read")
return {labjack::ReaderTask::configure(ctx, task), true};
if(task.type == "labjack_write")
return {labjack::WriterTask::configure(ctx, task), true};

LOG(ERROR) << "[labjack] Unknown task type: " << task.type;
return {nullptr, false};
}
Expand Down
8 changes: 4 additions & 4 deletions driver/labjack/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ synnax::Frame labjack::StateSource::get_state(){
for(auto &[key, value]: this->state_map) {
auto s = val_to_series(value.state, value.data_type);
state_frame.add(
key,
value.state_key,
std::move(s)
);
}
Expand Down Expand Up @@ -134,9 +134,9 @@ labjack::WriteSink::WriteSink(
this->get_index_keys(); // retrieve state index from first state channel

this->state_source = std::make_shared<labjack::StateSource>(
writer_config.state_rate,
writer_config.state_index_key,
writer_config.initial_state_map
this->writer_config.state_rate,
this->writer_config.state_index_key,
this->writer_config.initial_state_map
);
}

Expand Down
7 changes: 4 additions & 3 deletions driver/labjack/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct out_state{
std::string location = "";
double state = 0.0;
synnax::DataType data_type = synnax::FLOAT64;
synnax::ChannelKey state_key = 0;
};
///////////////////////////////////////////////////////////////////////////////////
// StateSource //
Expand Down Expand Up @@ -122,11 +123,11 @@ struct WriterConfig{
if(channels.back().data_type == synnax::SY_UINT8){
initial_val = 1.0;
}

initial_state_map[channels.back().state_key] = labjack::out_state{
initial_state_map[channels.back().cmd_key] = labjack::out_state{
.location = channels.back().location,
.state = initial_val,
.data_type = channels.back().data_type
.data_type = channels.back().data_type,
.state_key = channels.back().state_key
};
});
}
Expand Down
36 changes: 29 additions & 7 deletions driver/labjack/writer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ TEST(write_tests, labjack_t4){

LOG(INFO) << "Test labjack writes t4";

auto client std::make_shared<synnax::Synnax>(new_test_client());
auto client = std::make_shared<synnax::Synnax>(new_test_client());

auto [state_idx, tErr1] = client->channels.create("do_state_idx", synnax::TIMESTAMP, 0, true);
ASSERT_FALSE(tErr1) << tErr1.message();

auto [cmd_idx, tErr2] = channel->channels.create("do_cmd_idx", synnax::INT32, true);
auto [cmd_idx, tErr2] = client->channels.create("do_cmd_idx", synnax::TIMESTAMP,0, true);
ASSERT_FALSE(tErr2) << tErr2.message();

// TODO: test schematic using a float channel
Expand All @@ -43,12 +43,34 @@ TEST(write_tests, labjack_t4){
auto [cmd, cErr] = client->channels.create("do_cmd", synnax::SY_UINT8, cmd_idx.key, false);
ASSERT_FALSE(cErr) << cErr.message();

// create a writer to write commands out the cmd pipe
auto cmd_writer_config = synnax::WriterConfig{
.channels = std::vector<synnax::ChannelKey>{cmd_idx.key, cmd.key},
.start = TimeStamp::now(),
.mode = synnax::StreamOnly

auto config = json{
{"device_type", "T4"},
{"device_key", "440022190"},
{"serial_number", "440022190"},
{"connection_type", "USB"},
{"channels", json::array({
{
{"location", "FIO4"},
{"enabled", true},
{"data_type", "uint8"},
{"cmd_key", cmd.key},
{"state_key", state.key},
{"channel_types", "DIO"}
}
})},
{"data_saving", true},
{"state_rate", 10}
};

auto task = synnax::Task("my_task", "labjack_write", to_string(config));
auto mockCtx = std::make_shared<task::MockContext>(client);

auto writer_task = labjack::WriterTask::configure(mockCtx, task);

auto start_cmd = task::Command(task.key, "start", {});
auto stop_cmd = task::Command{task.key, "stop", {}};
writer_task->exec(start_cmd);
std::this_thread::sleep_for(std::chrono::seconds(300000));
writer_task->exec(stop_cmd);
}

0 comments on commit b3e7454

Please sign in to comment.