Browse Source

改造了 optoforce 接口,但是没有测试

jcsyshc 2 năm trước cách đây
mục cha
commit
595deab9f6
1 tập tin đã thay đổi với 28 bổ sung47 xóa
  1. 28 47
      src/sensor/optoforce/optoforce_daq.cpp

+ 28 - 47
src/sensor/optoforce/optoforce_daq.cpp

@@ -1,6 +1,6 @@
 #include "optoforce_daq.h"
-
 #include "core/basic_obj_types.hpp"
+#include "utility/config_utility.hpp"
 #include "utility/coro_worker.hpp"
 #include "utility/coro_worker_helper_func.hpp"
 #include "utility/versatile_buffer2.hpp"
@@ -43,8 +43,8 @@ namespace sophiar {
 
         double force_resolution, torque_resolution;
 
-        global_obj_index_type force_obj_index;
-        global_obj_index_type torque_obj_index;
+        variable_index_type force_var_index;
+        variable_index_type torque_var_index;
 
         template<size_t MessageLength>
         uint16_t calc_checksum(const char *data) {
@@ -66,13 +66,13 @@ namespace sophiar {
         }
 
         awaitable<bool> handle_type_3_message() {
-            using memory_type = static_memory<message_length_3>;
-            auto reader = versatile_readable_buffer<daq_endian, memory_type>();
-            co_await reader.async_read_from(*conn, message_length_3);
+            auto buf = static_memory<message_length_3>{};
+            co_await async_fill_memory_from(*conn, buf);
+            auto reader = versatile_reader<daq_endian>(buf);
 
             // check header and checksum
-            assert(memcmp(header_magic_3, reader.cur_data(), 4) == 0);
-            assert(check_checksum<message_length_3>(reader.cur_data()));
+            assert(memcmp(header_magic_3, buf.data(), 4) == 0);
+            assert(check_checksum<message_length_3>(buf.data()));
 
             reader.manual_offset(4); // ignore header
             uint16_t sample_count, status;
@@ -89,7 +89,7 @@ namespace sophiar {
                     fx * force_resolution,
                     fy * force_resolution,
                     fz * force_resolution);
-            UPDATE_GLOBAL_OBJ_VALUE_TS(scalarxyz_obj, force_obj_index, std::move(force_value), ts);
+            UPDATE_VARIABLE_VAL_WITH_TS(scalarxyz_obj, force_var_index, std::move(force_value), ts);
 
             // torque component
             uint16_t tx, ty, tz;
@@ -98,44 +98,34 @@ namespace sophiar {
                     tx * torque_resolution,
                     ty * torque_resolution,
                     tz * torque_resolution);
-            UPDATE_GLOBAL_OBJ_VALUE_TS(scalarxyz_obj, torque_obj_index, std::move(torque_value), ts);
+            UPDATE_VARIABLE_VAL_WITH_TS(scalarxyz_obj, torque_var_index, std::move(torque_value), ts);
 
             co_return true;
         }
 
         awaitable<void> send_config_message(uint8_t speed, uint8_t filter, uint8_t zero = 0) {
             assert(zero == 0); // zero will be done manually
-            using memory_type = static_memory<message_length_config>;
-            auto writer = versatile_writable_buffer<daq_endian, memory_type>();
-            memcpy(writer.cur_data(), header_magic_config, 4);
+            auto buf = static_memory<message_length_config>{};
+            auto writer = versatile_writer<daq_endian>(buf);
+            memcpy(buf.data(), header_magic_config, 4);
             writer.manual_offset(4);
             writer << speed << filter << zero;
-            auto checksum = calc_checksum<message_length_config>(writer.get_buffer().data());
+            auto checksum = calc_checksum<message_length_config>(buf.data());
             writer << checksum;
-            co_await writer.async_write_to(*conn);
+            co_await async_write_memory_to(*conn, buf);
             co_return;
         }
 
         awaitable<void> load_device_config(const nlohmann::json &config) {
             // device type
-            assert(config.contains("device_type"));
-            assert(config["device_type"].is_number_unsigned());
-            device_type = config["device_type"].get<uint8_t>();
+            device_type = LOAD_UINT_ITEM("device_type");
             assert(device_type == 3); // TODO only type 3 is supported
 
             // global objects
             switch (device_type) {
                 case 3: {
-                    assert(config.contains("force_output_name"));
-                    assert(config.contains("torque_output_name"));
-                    assert(config["force_output_name"].is_string());
-                    assert(config["torque_output_name"].is_string());
-                    auto force_output_name = config["force_output_name"].get<std::string>();
-                    auto torque_output_name = config["torque_output_name"].get<std::string>();
-                    force_obj_index = get_manager().
-                            register_global_obj<scalarxyz_obj>(force_output_name);
-                    torque_obj_index = get_manager().
-                            register_global_obj<scalarxyz_obj>(torque_output_name);
+                    force_var_index = LOAD_VARIABLE_INDEX(scalarxyz_obj, "force_output_name");
+                    torque_var_index = LOAD_VARIABLE_INDEX(scalarxyz_obj, "torque_output_name");
                     break;
                 }
                 default: {
@@ -145,27 +135,19 @@ namespace sophiar {
             }
 
             // resolution
-            assert(config.contains("force_resolution"));
-            assert(config.contains("torque_resolution"));
-            assert(config["force_resolution"].is_number_float());
-            assert(config["torque_resolution"].is_number_float());
-            force_resolution = config["force_resolution"].get<double>();
-            torque_resolution = config["torque_resolution"].get<double>();
+            force_resolution = LOAD_FLOAT_ITEM("force_resolution");
+            torque_resolution = LOAD_FLOAT_ITEM("torque_resolution");
 
             // report config
-            assert(config.contains("report_frequency"));
-            assert(config.contains("filter_type"));
-            assert(config["report_frequency"].is_number_unsigned());
-            assert(config["filter_type"].is_number_unsigned());
-            auto freq = config["report_frequency"].get<uint16_t>();
+            auto freq = LOAD_UINT_ITEM("report_frequency");
             assert(freq >= 10);
-            auto filter = config["filter_type"].get<uint8_t>();
+            auto filter = LOAD_UINT_ITEM("filter_type");
             co_await send_config_message(1000 / freq, filter);
         }
 
         void setup_connection(const nlohmann::json &config) {
             assert(conn == nullptr);
-            conn.reset(new serial_port(get_context()));
+            conn.reset(new serial_port(*global_context));
 
             // open serial port
             assert(config.contains("com_port"));
@@ -188,8 +170,7 @@ namespace sophiar {
                 case 3: {
                     auto func_wrapper = make_noexcept_func(
                             std::bind(&impl::handle_type_3_message, this));
-                    worker = make_infinite_coro_worker(
-                            get_context(), std::move(func_wrapper), std::move(exit_func));
+                    worker = make_infinite_coro_worker(std::move(func_wrapper), std::move(exit_func));
                     break;
                 }
                 default: {
@@ -209,11 +190,11 @@ namespace sophiar {
 
     optoforce_daq::~optoforce_daq() = default;
 
-    awaitable<bool> optoforce_daq::on_init(const nlohmann::json &config) {
+    awaitable<bool> optoforce_daq::on_init(const nlohmann::json &config) noexcept {
         co_return true;
     }
 
-    awaitable<bool> optoforce_daq::on_start(const nlohmann::json &config) {
+    awaitable<bool> optoforce_daq::on_start(const nlohmann::json &config) noexcept {
         try {
             pimpl->setup_connection(config);
             co_await pimpl->load_device_config(config);
@@ -225,14 +206,14 @@ namespace sophiar {
         co_return true;
     }
 
-    awaitable<void> optoforce_daq::on_stop() {
+    awaitable<void> optoforce_daq::on_stop() noexcept {
         pimpl->worker->cancel();
         co_await pimpl->worker->coro_wait_stop();
         pimpl->worker = nullptr;
         co_return;
     }
 
-    awaitable<void> optoforce_daq::on_reset() {
+    awaitable<void> optoforce_daq::on_reset() noexcept {
         co_return;
     }
 }