ApolloAuto / apollo

An open autonomous driving platform
Apache License 2.0
25.18k stars 9.71k forks source link

In Apollo 8.0, I'm unable to use CreateReader to obtain ControlCommand data within a component, but it works when done in an executable file. #15459

Open zwz214 opened 4 months ago

zwz214 commented 4 months ago

Describe the bug I am using Apollo 8.0 and have referenced the implementation in teleop.cc and canbus_component.cc. I want to read ControlCommand in my timer_components, using the following code: `bool VTD::Init() { apollo::cyber::ReaderConfig guardian_cmd_reader_config; guardian_cmd_reader_config.channel_name = FLAGS_guardian_topic; guardian_cmd_reader_config.pending_queue_size = FLAGS_guardian_cmd_pending_queue_size;

apollo::cyber::ReaderConfig control_cmd_reader_config;
control_cmd_reader_config.channel_name = FLAGS_control_command_topic;
control_cmd_reader_config.pending_queue_size =  
      FLAGS_control_cmd_pending_queue_size;

if (FLAGS_receive_guardian) {
    guardian_cmd_reader_ = node_->CreateReader<GuardianCommand>(
        guardian_cmd_reader_config,
        [this](const std::shared_ptr<GuardianCommand> &cmd) {
        AINFO << "Received guardian data: run canbus callback.";
        OnGuardianCommand(*cmd);
        });
} else {
    control_command_reader_ = node_->CreateReader<ControlCommand>(
        control_cmd_reader_config,
        [this](const std::shared_ptr<ControlCommand> &cmd) {
        AINFO << "Received control data: run canbus callback.";
        OnControlCommand(*cmd);
        });
}

return true;

} void VTD::OnControlCommand(const ControlCommand &control_command) { int64_t current_timestamp = Time::Now().ToMicrosecond(); // if command coming too soon, just ignore it. if (current_timestamp - lasttimestamp < FLAGS_min_cmd_interval 1000) { AINFO << "Control command comes too soon. Ignore.\n Required " "FLAGS_min_cmd_interval[" << FLAGS_min_cmd_interval << "], actual time interval[" << current_timestamp - lasttimestamp << "]."; return; } lasttimestamp = current_timestamp; AINFO << "Control_sequence_number:" << control_command.header().sequence_num() << ", Time_of_delay:" << current_timestamp - static_cast(control_command.header().timestamp_sec() 1e6) << " micro seconds"; ControlCommand_=control_command; }

void VTD::OnGuardianCommand( const GuardianCommand &guardian_command) { OnControlCommand(guardian_command.control_command()); }`

But I still can't see the printout of the callback function execution in the log file, the contents of the log boove.info are as follows:

Log file created at: 2024/07/04 16:35:27 Running on machine: in-dev-docker Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg W0704 16:35:27.256419 328203 scheduler_factory.cc:63] Scheduler conf named /apollo/cyber/conf/vtd_process.conf not found, use default. I0704 16:35:27.257534 328211 processor.cc:42] processor_tid: 328211 I0704 16:35:27.257954 328212 processor.cc:42] processor_tid: 328212 I0704 16:35:27.258366 328213 processor.cc:42] processor_tid: 328213 I0704 16:35:27.264626 328225 processor.cc:42] processor_tid: 328225 I0704 16:35:27.264729 328226 processor.cc:42] processor_tid: 328226 I0704 16:35:27.264744 328203 init.cc:113] Register exit handle succ. I0704 16:35:27.265743 328203 module_controller.cc:65] Start initialize dag: /apollo/ADASAdapter/component_control_reader/vtd.dag I0704 16:35:27.265807 328203 class_loader.cc:37] Begin LoadLibrary: /apollo/bazel-bin/ADASAdapter/component_control_reader/component_control_reader.so I0704 16:35:27.381282 328203 class_loader_utility.h:79] registerclass:VTD,apollo::cyber::ComponentBase,/apollo/bazel-bin/ADASAdapter/component_control_reader/component_control_reader.so I0704 16:35:27.387667 328203 component_vtd.cc:57] -------------Init----------------- I0704 16:35:27.507040 328203 scheduler.cc:55] create croutine: vtdprocess/apollo/control I0704 16:35:27.508675 328203 timer.cc:137] start timer [0] I0704 16:35:27.518870 328234 scheduler.cc:55] create croutine: /internal/task0 I0704 16:35:27.518911 328234 scheduler.cc:55] create croutine: /internal/task1 I0704 16:35:27.518932 328234 scheduler.cc:55] create croutine: /internal/task2 I0704 16:35:27.518944 328234 scheduler.cc:55] create croutine: /internal/task3 I0704 16:35:27.518956 328234 scheduler.cc:55] create croutine: /internal/task4 I0704 16:35:27.518966 328234 scheduler.cc:55] create croutine: /internal/task5 I0704 16:35:27.518978 328234 scheduler.cc:55] create croutine: /internal/task6 I0704 16:35:27.518988 328234 scheduler.cc:55] create croutine: /internal/task7 I0704 16:35:27.519002 328234 scheduler.cc:55] create croutine: /internal/task8 I0704 16:35:27.519012 328234 scheduler.cc:55] create croutine: /internal/task9 I0704 16:35:27.519022 328234 scheduler.cc:55] create croutine: /internal/task10 I0704 16:35:27.519032 328234 scheduler.cc:55] create croutine: /internal/task11 I0704 16:35:27.519042 328234 scheduler.cc:55] create croutine: /internal/task12 I0704 16:35:27.519054 328234 scheduler.cc:55] create croutine: /internal/task13 I0704 16:35:27.519064 328234 scheduler.cc:55] create croutine: /internal/task14 I0704 16:35:27.519074 328234 scheduler.cc:55] create croutine: /internal/task15 W0704 16:36:04.955174 328234 rate.cc:96] Detect forward jumps in time W0704 16:36:16.727293 328234 rate.cc:96] Detect forward jumps in time W0704 16:36:27.733143 328234 rate.cc:96] Detect forward jumps in time W0704 16:36:38.736196 328234 rate.cc:96] Detect forward jumps in time W0704 16:37:01.711699 328234 rate.cc:96] Detect forward jumps in time I0704 16:38:14.790624 328203 timer.cc:144] stop timer, the timer_id: 0 I0704 16:38:14.794736 328203 mainboard.cc:45] exit mainboard.

But if I compile the code directly into an executable instead of using components, the ControlCommand callback function can be called as normal. I0704 17:10:19.621912 355717 component_vtd.cc:79] [component_control_reader]Received control data: run canbus callback. I0704 17:10:19.622009 355717 component_vtd.cc:111] [component_control_reader]Control_sequence_number:98965, Time_of_delay:327 micro seconds I0704 17:10:19.632030 355718 component_vtd.cc:79] [component_control_reader]Received control data: run canbus callback. I0704 17:10:19.632084 355718 component_vtd.cc:111] [component_control_reader]Control_sequence_number:98966, Time_of_delay:327 micro seconds I0704 17:10:19.642028 355719 component_vtd.cc:79] [component_control_reader]Received control data: run canbus callback. I0704 17:10:19.642098 355719 component_vtd.cc:111] [component_control_reader]Control_sequence_number:98967, Time_of_delay:300 micro seconds ^C^Z [2]+ Stopped ./component_control_reader

The dag file is as follows: `module_config { module_library : "/apollo/bazel-bin/ADASAdapter/component_control_reader/component_control_reader.so"

timer_components {
    class_name : "VTD"
    config {
        name : "vtd_process"
        interval: 10
    }
}

} `

MH798968733 commented 1 week ago

@daohu527 Hello,I meet the same issue , how to solve it?

MH798968733 commented 1 week ago

I have solved it, maybe you can take a look at #14755 @zwz214