From b3e74548ce90706acfb30a1af4dbbd5948c8e670 Mon Sep 17 00:00:00 2001 From: Elham Islam Date: Mon, 21 Oct 2024 13:50:04 -0700 Subject: [PATCH] [driver/labjack] verified working writes on hardware --- driver/labjack/BUILD.bazel | 15 ++++++++++++++ driver/labjack/factory.cpp | 4 ++++ driver/labjack/writer.cpp | 8 ++++---- driver/labjack/writer.h | 7 ++++--- driver/labjack/writer_test.cpp | 36 +++++++++++++++++++++++++++------- 5 files changed, 56 insertions(+), 14 deletions(-) diff --git a/driver/labjack/BUILD.bazel b/driver/labjack/BUILD.bazel index ec48f7593e..a89579a9da 100644 --- a/driver/labjack/BUILD.bazel +++ b/driver/labjack/BUILD.bazel @@ -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", diff --git a/driver/labjack/factory.cpp b/driver/labjack/factory.cpp index a6737a1f5d..4a5956fc13 100644 --- a/driver/labjack/factory.cpp +++ b/driver/labjack/factory.cpp @@ -17,10 +17,14 @@ std::pair, bool> labjack::Factory::configure_task( const std::shared_ptr &ctx, const synnax::Task &task ) { + if (task.type == "labjack_scan") return {std::make_unique(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}; } diff --git a/driver/labjack/writer.cpp b/driver/labjack/writer.cpp index ed2596d5aa..749923fbf1 100644 --- a/driver/labjack/writer.cpp +++ b/driver/labjack/writer.cpp @@ -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) ); } @@ -134,9 +134,9 @@ labjack::WriteSink::WriteSink( this->get_index_keys(); // retrieve state index from first state channel this->state_source = std::make_shared( - 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 ); } diff --git a/driver/labjack/writer.h b/driver/labjack/writer.h index 17cd9410dd..76704888f9 100644 --- a/driver/labjack/writer.h +++ b/driver/labjack/writer.h @@ -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 // @@ -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 }; }); } diff --git a/driver/labjack/writer_test.cpp b/driver/labjack/writer_test.cpp index b6040c2f99..791b08cc4f 100644 --- a/driver/labjack/writer_test.cpp +++ b/driver/labjack/writer_test.cpp @@ -28,12 +28,12 @@ TEST(write_tests, labjack_t4){ LOG(INFO) << "Test labjack writes t4"; - auto client std::make_shared(new_test_client()); + auto client = std::make_shared(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 @@ -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{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(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); } \ No newline at end of file