Frame. Msg Not Working with Db_Decode from Cantools

I want to use the can_msgs/Frame.msg for decoding can messages using db.decode_message(Frame.id, Frame.data) but it is giving error

I want to try and write a new Frame.msg format but will it help?

def callback(Frame):
   rospy.loginfo(rospy.get_caller_id() + "I heard %s", Frame.data)
   Temp = db.decode_message(Frame.id, Frame.data)
   temp.data = Temp
   pub.publish(temp)

I want to print the message in the dbc format that the cantools package helps decoding.

Error:

File "safa.py", line 42, in callback temp = db.decode_message(Frame.id, Frame.data) File "build/bdist.linux-x86_64/egg/cantools/database/can/database.py", line 379, in decode_message message = self._name_to_message[frame_id_or_name] KeyError: 10

10

2 Answers

Looking at the cantools documentation for db.decode_message, the KeyError is a bad description/error msg for the following.

db is a database (class cantools.database.can.Database), which stores all the weird encodings we may choose to use with cantools. To decode a message, it must already have an encoding stored within it, using one of the db.add_* methods with the class.

Edit: The definition of the can_msgs/Frame is

std_msgs/Header header
uint32 id
bool is_rtr
bool is_extended
bool is_error
uint8 dlc
uint8[8] data

The data term requires an array/list of 8 uint8 values, not a string. Any CANbus data encoding must finish in this form.

Additionally, ROS provides an interface to the CANbus already: socketcan_bridge can be called in a launch file with your node at the same time.

Before you can decode messages you should setup the database e.g by loading a dbc file:

   try:
        dbc_file_object = open(dbc_file, 'r')
    except IOError as e:
        rospy.loginfo('Unable to open file {}'.format(e))
        sys.exit(-1)

    self.db = cantools.db.load(dbc_file_object)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest