can_bus.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2021, Qiayuan Liao
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *******************************************************************************/
33 
34 //
35 // Created by qiayuan on 12/28/20.
36 //
37 
38 #pragma once
39 
42 
43 #include <chrono>
44 #include <mutex>
45 #include <thread>
46 
47 namespace rm_hw
48 {
49 struct CanFrameStamp
50 {
51  can_frame frame;
53 };
54 
55 class CanBus
56 {
57 public:
64  CanBus(const std::string& bus_name, CanDataPtr data_ptr, int thread_priority);
70  void read(ros::Time time);
74  void write();
75 
76  void write(can_frame* frame);
77 
78  const std::string bus_name_;
79 
80 private:
85  void frameCallback(const can_frame& frame);
86 
89  std::vector<CanFrameStamp> read_buffer_;
90 
91  can_frame rm_frame0_{}; // for id 0x201~0x204
92  can_frame rm_frame1_{}; // for id 0x205~0x208
93 
94  mutable std::mutex mutex_;
95 };
96 
97 } // namespace rm_hw
rm_hw::CanBus::data_ptr_
CanDataPtr data_ptr_
Definition: can_bus.h:119
types.h
rm_hw::CanBus::CanBus
CanBus(const std::string &bus_name, CanDataPtr data_ptr, int thread_priority)
Initialize device at can_device, retry if fail. Set up header of CAN frame.
Definition: can_bus.cpp:75
rm_hw::CanBus::rm_frame0_
can_frame rm_frame0_
Definition: can_bus.h:122
rm_hw::CanDataPtr
Definition: types.h:124
rm_hw::CanBus::mutex_
std::mutex mutex_
Definition: can_bus.h:125
rm_hw::CanFrameStamp::frame
can_frame frame
Definition: can_bus.h:113
rm_hw::CanFrameStamp::stamp
ros::Time stamp
Definition: can_bus.h:114
rm_hw::CanBus::rm_frame1_
can_frame rm_frame1_
Definition: can_bus.h:123
rm_hw::CanBus::read
void read(ros::Time time)
Read active data from read_buffer_ to data_ptr_, such as position, velocity, torque and so on....
Definition: can_bus.cpp:150
ros::Time
rm_hw::CanBus::bus_name_
const std::string bus_name_
Definition: can_bus.h:109
rm_hw::CanBus::write
void write()
Write commands to can bus.
Definition: can_bus.cpp:90
rm_hw::CanBus::read_buffer_
std::vector< CanFrameStamp > read_buffer_
Definition: can_bus.h:120
rm_hw
Definition: control_loop.h:48
rm_hw::CanBus::socket_can_
can::SocketCAN socket_can_
Definition: can_bus.h:118
can::SocketCAN
Definition: socketcan.h:79
rm_hw::CanBus::frameCallback
void frameCallback(const can_frame &frame)
This function will be called when CAN bus receive message. It push frame which received into a vector...
Definition: can_bus.cpp:301
socketcan.h


rm_hw
Author(s): Qiayuan Liao
autogenerated on Tue May 6 2025 02:23:44