Skip to content

Commit

Permalink
add second CAN example
Browse files Browse the repository at this point in the history
  • Loading branch information
PonomarevDA committed Aug 10, 2024
1 parent 6a3cf9a commit 1e7db63
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Src/modules/canopen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (C) 2023 Dmitry Ponomarev <[email protected]>
# Distributed under the terms of the GPL v3 license, available in the file LICENSE.

list(APPEND APPLICATION_SOURCES
${CMAKE_CURRENT_LIST_DIR}/canopen.cpp
)
30 changes: 30 additions & 0 deletions Src/modules/canopen/canopen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
* Author: Dmitry Ponomarev <[email protected]>
*/

#include "canopen.hpp"
#include "can_driver.h"

REGISTER_MODULE(CanopenModule)

void CanopenModule::init() {
int8_t res = canDriverInit(1000000, CAN_DRIVER_SECOND);
health = (res < 0) ? Status::FATAL_MALFANCTION : Status::OK;

mode = Module::Mode::OPERATIONAL;
}

void CanopenModule::spin_once() {
if (health == Status::FATAL_MALFANCTION) {
return;
}

CanardCANFrame frame;
frame.id = 100;
frame.data_len = 4;

int8_t res = canDriverTransmit(&frame, CAN_DRIVER_SECOND);
health = (res > 0) ? Status::OK : Status::MINOR_FAILURE;
}
29 changes: 29 additions & 0 deletions Src/modules/canopen/canopen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
* Author: Dmitry Ponomarev <[email protected]>
*/

#ifndef SRC_MODULES_CANOPEN_HPP_
#define SRC_MODULES_CANOPEN_HPP_

#include "module.hpp"

#ifdef __cplusplus
extern "C" {
#endif

class CanopenModule : public Module {
public:
CanopenModule() : Module(10.0) {}
void init() override;

protected:
void spin_once() override;
};

#ifdef __cplusplus
}
#endif

#endif // SRC_MODULES_CANOPEN_HPP_

0 comments on commit 1e7db63

Please sign in to comment.