-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update basic can protocol structure:
- Loading branch information
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
control_ws/src/stanley/include/stanley/canprotocol/can_protocol.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
|
||
#include "utils/car_struct.h" | ||
#include "utils/msg_structs.h" | ||
|
||
#include "dbcppp/include/dbcppp/Network.h" | ||
|
||
#include <linux/can.h> | ||
#include <linux/can/raw.h> | ||
#include <sys/socket.h> | ||
#include <sys/ioctl.h> | ||
#include <net/if.h> | ||
#include <unistd.h> | ||
#include <iostream> | ||
#include <fstream> | ||
#include <unordered_map> | ||
#include <memory> | ||
#include <chrono> | ||
|
||
class CANReceiver { | ||
public: | ||
CANReceiver(const std::string& dbc_file, const std::string& can_interface); | ||
~CANReceiver(); | ||
void receiveMessages(); | ||
|
||
private: | ||
void printMessage(const dbcppp::IMessage& msg, const can_frame& frame); | ||
|
||
std::unique_ptr<dbcppp::INetwork> net; | ||
std::unordered_map<uint64_t, const dbcppp::IMessage*> messages; | ||
int sock; | ||
uint64_t last_received; | ||
std::chrono::time_point<std::chrono::steady_clock> last_received_time; | ||
std::chrono::seconds message_timeout; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// CANReceiver.cpp | ||
#include "CANReceiver.hpp" | ||
|
||
CANReceiver::CANReceiver(const std::string& dbc_file, const std::string& can_interface) | ||
: last_received(0), message_timeout(std::chrono::seconds(2)) { | ||
std::ifstream idbc(dbc_file); | ||
net = dbcppp::INetwork::LoadDBCFromIs(idbc); | ||
if (!net) { | ||
throw std::runtime_error("Failed to parse DBC file."); | ||
} | ||
|
||
for (const auto& msg : net->Messages()) { | ||
messages[msg.Id()] = &msg; | ||
} | ||
|
||
sock = socket(PF_CAN, SOCK_RAW, CAN_RAW); | ||
if (sock < 0) { | ||
throw std::runtime_error("Error while opening socket."); | ||
} | ||
|
||
struct sockaddr_can addr; | ||
struct ifreq ifr; | ||
strcpy(ifr.ifr_name, can_interface.c_str()); | ||
ioctl(sock, SIOCGIFINDEX, &ifr); | ||
|
||
addr.can_family = AF_CAN; | ||
addr.can_ifindex = ifr.ifr_ifindex; | ||
bind(sock, (struct sockaddr *)&addr, sizeof(addr)); | ||
} | ||
|
||
CANReceiver::~CANReceiver() { | ||
close(sock); | ||
} | ||
|
||
void CANReceiver::receiveMessages() { | ||
struct can_frame frame; | ||
int nbytes = read(sock, &frame, sizeof(struct can_frame)); | ||
if (nbytes > 0) { | ||
auto current_time = std::chrono::steady_clock::now(); | ||
auto iter = messages.find(frame.can_id); | ||
if (iter != messages.end()) { | ||
const auto& msg = *iter->second; | ||
if (msg.Id() == 254 || (msg.Id() == 255 && (last_received != 254 || std::chrono::duration_cast<std::chrono::seconds>(current_time - last_received_time) >= message_timeout))) { | ||
printMessage(msg, frame); | ||
last_received = msg.Id(); | ||
last_received_time = current_time; | ||
} | ||
} | ||
} | ||
|
||
// Check timeout for switching back to SpeedMessage1 | ||
if (last_received == 254) { | ||
auto current_time = std::chrono::steady_clock::now(); | ||
if (std::chrono::duration_cast<std::chrono::seconds>(current_time - last_received_time) >= message_timeout) { | ||
last_received = 0; // Allow SpeedMessage1 to be printed again | ||
} | ||
} | ||
} | ||
|
||
void CANReceiver::printMessage(const dbcppp::IMessage& msg, const can_frame& frame) { | ||
std::cout << "Received Message: " << msg.Name() << "\n"; | ||
for (const auto& sig : msg.Signals()) { | ||
std::cout << "\t" << sig.Name() << " = " << sig.RawToPhys(sig.Decode(frame.data)) << " " << sig.Unit() << "\n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters