GithubHelp home page GithubHelp logo

reinzor / libcan-encode-decode Goto Github PK

View Code? Open in Web Editor NEW
34.0 8.0 23.0 17 KB

C++ functions for encoding and decoding CAN frames ( uint8_t[8] )

License: MIT License

Shell 0.47% C++ 70.01% C 29.52%
can encode decode frame encoding decoding uint8 q iq

libcan-encode-decode's Introduction

libcan-encode-decode

Build Status

C++ functions for encoding and decoding CAN frames ( uint8_t[8] )

Usage

Copy the include/can_encode_decode.inl into your c++ project and use the inline functions.

uint8_t src_array[8];

encode(src_array, 3.0, 56, 3, false, false, 1.000000, 0);
decode(src_array, 56, 3, false, false, 1.000000, 0); // 3.0

encode(src_array, 35, 2, 6, true, false, 1.0, 0);
decode(src_array, 2, 6, true, false, 1.000000, 0) // 35.000000

encode(src_array, 1.0, 0, 2, true, false, 1.0, 0);
decode(src_array, 0, 2, true, false, 1.000000, 0); // 1.000000

Run tests

./run_tests.sh

libcan-encode-decode's People

Contributors

ebroecker avatar laercio-barbosa avatar ntosme avatar reinzor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

libcan-encode-decode's Issues

storeSignal does not clear out stale bits

Hello,

Thank you for this wonderful library! One thing I've noticed is that when you call storeSignal, it does not clear out the old bits that were in the CAN frame. Instead, it leaves them there so if the new value is smaller those bits get overwritten, but the remaining bits (left-over from the last value that was there) are not changed. Any suggestions on how to clear the entire range (i.e. from startbit 5 to a length of 16) out before storing a value? Thanks!

Start bit in motorola signals from Vector dbc files

Hi

I noted in Vector dbc files (opened as a txt file) the start bit field is the real start bit when signal is intel byte order, but is the 'end bit' when signal is motorola byte order. Have you seen this? How do you handle that (because storeSignal and extractSignal must receive both the real start bit position)?

Thanks

Laercio

encoding into more than 8 bits

I would like to encode a value into 11 bits in a CAN frame. It seems like there is an overflow condition or something similar that is happening and I am not able to stuff these into required bits. Is there a limitation of this repo that would prevent me from encoding to more than 8 bits?

datatype error

Hi.

I am trying to use your inline decode function from your repo and I am getting error message as below.

Foe the frame, I am sending data field from a struct called msg as below which is an uint8_t array of 8 members. The error is also attached below. Please let me know

Code

msg.data[0] = 0x53;
msg.data[1] = 0x40;
msg.data[2] = 0x40;
msg.data[3] = 0x0;
msg.data[4] = 0x0;
msg.data[5] = 0x0;
msg.data[6] = 0x10;
msg.data[7] = 0xBD;

msg.header.frame_id = ""; // "0" for no frame.
msg.header.stamp = ros::Time::now();

uint8_t msg.data[8];

//encode(src_array, 3.0, 56, 3, false, false, 1.000000, 0);
float PhysValue = decode(msg.data, 2,11, false, false, 1.000000, 0); // 3.0

Error

home/jpm/catkin_ws/src/ros_canopen/socketcan_bridge/src/steering_pub_socketcan.cpp: In function ‘int main(int, char**)’:
/home/jpm/catkin_ws/src/ros_canopen/socketcan_bridge/src/steering_pub_socketcan.cpp:118:12: error: expected initializer before ‘.’ token
uint8_t msg.data[8] = 0;
^
/home/jpm/catkin_ws/src/ros_canopen/socketcan_bridge/src/steering_pub_socketcan.cpp:121:67: error: cannot convert ‘can_msgs::Frame_<std::allocator >::_data_type {aka boost::array<unsigned char, 8ul>}’ to ‘const uint8_t* {aka const unsigned char*}’ for argument ‘1’ to ‘float decode(const uint8_t*, uint16_t, uint16_t, bool, bool, float, float)’
float PhysValue = decode(msg.data, 2,11, false, false, 1.000000, 0); // 3.0
^
ros_canopen/socketcan_bridge/CMakeFiles/talker.dir/build.make:62: recipe for target 'ros_canopen/socketcan_bridge/CMakeFiles/talker.dir/src/steering_pub_socketcan.cpp.o' failed
make[2]: *** [ros_canopen/socketcan_bridge/CMakeFiles/talker.dir/src/steering_pub_socketcan.cpp.o] Error 1
CMakeFiles/Makefile2:3705: recipe for target 'ros_canopen/socketcan_bridge/CMakeFiles/talker.dir/all' failed
make[1]: *** [ros_canopen/socketcan_bridge/CMakeFiles/talker.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2

Fix missing semicolon

The last chenge introduced a error. There is a missing semicolon in can_encode_decode_inl.h - line 38.

frame[start_byte] |= value << startbit_in_byte

Signal extension implementation

Hi,

Here is the implementation for signal extension in storeSignal as I promissed. Regarding issue ebroecker/canmatrix#35.

        if (is_signed)
        {
            uint64_t signal_value;
            // Save signal value
            // perform sign extension
            // update value
            signal_value = value & 0x8000000000000000;
            value |= signal_value >> (64 - length);
        }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.