rm_control
Loading...
Searching...
No Matches
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
47namespace rm_hw
48{
50{
51 can_frame frame;
52 ros::Time stamp;
53};
54
55class CanBus
56{
57public:
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
80private:
85 void frameCallback(const can_frame& frame);
86
87 can::SocketCAN socket_can_;
88 CanDataPtr data_ptr_;
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
Definition socketcan.h:49
Definition can_bus.h:56
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:44
void write()
Write commands to can bus.
Definition can_bus.cpp:59
const std::string bus_name_
Definition can_bus.h:78
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:119
Definition control_loop.h:49
Definition types.h:94
Definition can_bus.h:50
can_frame frame
Definition can_bus.h:51
ros::Time stamp
Definition can_bus.h:52