canopen

package module
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2024 License: MIT Imports: 6 Imported by: 0

README

canopen

canopen lets you send and received data in a CANopen network.

The library contains basic functionality and doesn't aim to be a complete implementation of the CANopen protocol. You can find a complete CANopen implementation in the CANopenNode project.

CANopen

CANopen is a protocol to communicate on a CAN bus. Data is exchanged between nodes using CANopen frames, which uses a subset of the bytes in a CAN frames. This project extends the can library to interact with a CANopen nodes. Setup your hard- and software as described there.

You can find a very good documentation about CANopen here.

Usage

Setup the CAN bus interface
bus, _ := can.NewBusForInterfaceWithName("can0")
bus.ConnectAndPublish()
CANopen request/response communication

Parts of CANopen protocol are based on request/response communication. The library makes it easy to send a request and wait for the reponse.

// Frame to be sent
payload := []byte{...}
frame := canopen.NewFrame(canopen.MessageTypeRSDO, payload)

// Expected id of response frame
respID := canopen.MessageTypeTSDO

req := canopen.NewRequest(frame, respID)

// Create client which sends request and waits for response
client := &canopen.Client{bus, time.Second * 1}
resp, _ := client.Do(req)

Contact

Matthias Hochgatterer

Github: https://github.com/brutella

Twitter: https://twitter.com/brutella

License

canopen is available under the MIT license. See the LICENSE file for more info.

Documentation

Index

Constants

View Source
const (
	MessageTypeNMT       uint16 = 0x000
	MessageTypeSync      uint16 = 0x080
	MessageTypeTimestamp uint16 = 0x100
	MessageTypeTPDO1     uint16 = 0x180
	MessageTypeRPDO1     uint16 = 0x200
	MessageTypeTPDO2     uint16 = 0x280
	MessageTypeRPDO2     uint16 = 0x300
	MessageTypeTPDO3     uint16 = 0x380
	MessageTypeRPDO3     uint16 = 0x400
	MessageTypeTPDO4     uint16 = 0x480
	MessageTypeRPDO4     uint16 = 0x500
	// MessageTypeTSDO represents the type of SDO server response messages
	MessageTypeTSDO uint16 = 0x580
	// MessageTypeRSDO represents the type of SDO client request messages
	MessageTypeRSDO      uint16 = 0x600
	MessageTypeHeartbeat uint16 = 0x700
)
View Source
const (
	// MaskCobID is used to get 11 bits from an uint16 for the COB-ID
	MaskCobID = 0x7FF
	// MaskNodeID is used to extract the 7-bit node id from the COB-ID
	MaskNodeID = 0x7F
	// MaskMessageType is used to extract the 4-bit message type from the COB-ID
	MaskMessageType = 0x780

	// MaskIDSff is used to extract the valid 11-bit CAN identifier bits from the frame ID of a standard frame format.
	MaskIDSff = 0x000007FF
	// MaskIDEff is used to extract the valid 29-bit CAN identifier bits from the frame ID of an extended frame format.
	MaskIDEff = 0x1FFFFFFF
	// MaskErr is used to extract the the error flag (0 = data frame, 1 = error message) from the frame ID.
	MaskErr = 0x20000000
	// MaskRtr is used to extract the rtr flag (1 = rtr frame) from the frame ID
	MaskRtr = 0x40000000
	// MaskEff is used to extract the eff flag (0 = standard frame, 1 = extended frame) from the frame ID
	MaskEff = 0x80000000
)
View Source
const (
	GoToOperational        uint8 = 0x1
	GoToStopped            uint8 = 0x2
	GoToPreOperation       uint8 = 0x80
	GoToResetNode          uint8 = 0x81
	GoToResetCommunication uint8 = 0x82
)
View Source
const (
	BootUp         uint8 = 0x0
	Stopped        uint8 = 0x04
	Operational    uint8 = 0x05
	PreOperational uint8 = 0x7f
)
View Source
const MPDO uint8 = 0x80
View Source
const MaxNodeID uint8 = 0x7F

MaxNodeID defines the highest node id

Variables

View Source
var Lock = maplock.New()
View Source
var RefDate = time.Date(
	1984,
	1,
	1,
	0,
	0,
	0,
	0,
	time.UTC,
)

RefDate is the reference date of CAN timestamp messages.

Functions

func GetAbortCodeText added in v0.5.2

func GetAbortCodeText(code SDOAbortCode) string

Types

type Client

type Client struct {
	Bus     *can.Bus
	Timeout time.Duration
}

A Client handles message communication by sending a request and waiting for the response.

func (*Client) Do

func (c *Client) Do(req *Request) (*Response, error)

Do sends a request and waits for a response. If the response frame doesn't arrive on time, an error is returned.

func (*Client) DoMinDuration added in v0.1.6

func (c *Client) DoMinDuration(req *Request, min time.Duration) (*Response, error)

DoMinDuration sends a request and waits for a response. If the response frame doesn't arrive on time, an error is returned.

type Frame

type Frame struct {
	// CobID is the 11-bit communication object identifier – CANopen only uses 11-bit identifiers.
	// Bits 0-6 represent the 7-bit node ID. Bits 7-11 represent the 4-bit message type.
	CobID uint16
	// Rtr represents the Remote Transmit Request flag.
	Rtr bool
	// Data contains 8 bytes
	Data []uint8
}

A Frame represents a CANopen frame.

func CANopenFrame

func CANopenFrame(frm can.Frame) Frame

CANopenFrame returns a CANopen frame from a CAN frame.

func NewFrame

func NewFrame(id uint16, data []uint8) Frame

NewFrame returns a frame with an id and data bytes.

func (Frame) CANFrame

func (frm Frame) CANFrame() can.Frame

CANFrame returns a CAN frame representing the CANopen frame.

CANopen frames are encoded as follows:

         -------------------------------------------------------
CAN     | ID           | Length    | Flags | Res0 | Res1 | Data |
         -------------------------------------------------------
CANopen | COB-ID + Rtr | len(Data) |       |      |      | Data |
         -------------------------------------------------------

func (Frame) MessageType

func (frm Frame) MessageType() uint16

MessageType returns the message type.

func (Frame) NodeID

func (frm Frame) NodeID() uint8

NodeID returns the node id.

func (*Frame) ObjectIndex added in v0.6.0

func (frm *Frame) ObjectIndex() ObjectIndex

func (Frame) Timestamp

func (frm Frame) Timestamp() (*time.Time, error)

Timestamp returns the time encoded in the frame.

type Index

type Index struct {
	B0 byte
	B1 byte
}

Index represents the 2-byte index in an object index.

func (*Index) Index added in v0.6.0

func (index *Index) Index() uint16

type ObjectIndex

type ObjectIndex struct {
	Index    Index
	SubIndex byte
}

ObjectIndex represents the index of an object.

func NewObjectIndex

func NewObjectIndex(index uint16, subIndex uint8) ObjectIndex

NewObjectIndex returns an object index from a 2-byte index and 1-byte sub index.

func (*ObjectIndex) Bytes added in v0.6.0

func (objectIndex *ObjectIndex) Bytes() []byte

func (*ObjectIndex) Compare added in v0.6.0

func (objectIndex *ObjectIndex) Compare(other ObjectIndex) bool

type Request

type Request struct {
	// The Frame of the request
	Frame Frame

	// The ResponseID of the response frame
	ResponseID uint32
}

A Request represents a CANopen request published on a CAN bus and received by another CANopen node.

func NewRequest

func NewRequest(frm Frame, respID uint32) *Request

NewRequest returns a request containing the frame to be sent and the expected response frame id.

type Response

type Response struct {
	// The response frame
	Frame Frame

	// The Request which triggers the response
	Request *Request
}

A Response represents the response which resulted from a request.

type SDOAbortCode added in v0.6.0

type SDOAbortCode int
const (
	SDO_ERR_TOGGLE_BIT         SDOAbortCode = 0x05030000
	SDO_ERR_TIMEOUT            SDOAbortCode = 0x05040000
	SDO_ERR_COMMAND            SDOAbortCode = 0x05040001
	SDO_ERR_BLOCK_SIZE         SDOAbortCode = 0x05040002
	SDO_ERR_BLOCK_SEQUENCE     SDOAbortCode = 0x05040003
	SDO_ERR_BLOCK_CRC          SDOAbortCode = 0x05040004
	SDO_ERR_MEMORY             SDOAbortCode = 0x05040005
	SDO_ERR_ACCESS_UNSUPPORTED SDOAbortCode = 0x06010000
	SDO_ERR_ACCESS_WO          SDOAbortCode = 0x06010001
	SDO_ERR_ACCESS_RO          SDOAbortCode = 0x06010002
	SDO_ERR_NO_OBJECT          SDOAbortCode = 0x06020000
	SDO_ERR_MAPPING_OBJECT     SDOAbortCode = 0x06040041
	SDO_ERR_MAPPING_LENGTH     SDOAbortCode = 0x06040042
	SDO_ERR_GENERAL_PARAMETER  SDOAbortCode = 0x06040043
	SDO_ERR_GENERAL_DEVICE     SDOAbortCode = 0x06040047
	SDO_ERR_HARDWARE           SDOAbortCode = 0x06060000
	SDO_ERR_DATATYPE           SDOAbortCode = 0x06070010
	SDO_ERR_DATATYPE_HIGH      SDOAbortCode = 0x06070012
	SDO_ERR_DATATYPE_LOW       SDOAbortCode = 0x06070013
	SDO_ERR_NO_SUB_INDEX       SDOAbortCode = 0x06090011
	SDO_ERR_VALUE_RANGE        SDOAbortCode = 0x06090030
	SDO_ERR_VALUE_HIGH         SDOAbortCode = 0x06090031
	SDO_ERR_VALUE_LOW          SDOAbortCode = 0x06090032
	SDO_ERR_VALUE_MIN_MAX      SDOAbortCode = 0x06090036
	SDO_ERR_SDO_CONNECTION     SDOAbortCode = 0x060A0023
	SDO_ERR_GENERAL            SDOAbortCode = 0x08000000
	SDO_ERR_DATA_STORE         SDOAbortCode = 0x08000020
	SDO_ERR_DATA_STORE_LOCAL   SDOAbortCode = 0x08000021
	SDO_ERR_DATA_STORE_STATE   SDOAbortCode = 0x08000022
	SDO_ERR_OBJECT_DICTIONARY  SDOAbortCode = 0x08000023
	SDO_ERR_NO_DATA            SDOAbortCode = 0x08000024
	NO_ERROR                   SDOAbortCode = 0x0
)

type TransferAbort added in v0.0.17

type TransferAbort struct {
	AbortCode []uint8
}

func (TransferAbort) Error added in v0.0.17

func (e TransferAbort) Error() string

type UnexpectedResponseLength added in v0.0.19

type UnexpectedResponseLength struct {
	Expected  int
	Actual    int
	AbortCode []uint8
}

func (UnexpectedResponseLength) Error added in v0.0.19

func (e UnexpectedResponseLength) Error() string

type UnexpectedSCSResponse added in v0.0.17

type UnexpectedSCSResponse struct {
	Expected  uint8
	Actual    uint8
	AbortCode []uint8
}

func (UnexpectedSCSResponse) Error added in v0.0.17

func (e UnexpectedSCSResponse) Error() string

type UnexpectedToggleBit added in v0.0.17

type UnexpectedToggleBit struct {
	Expected  bool
	Actual    bool
	AbortCode []uint8
}

func (UnexpectedToggleBit) Error added in v0.0.17

func (e UnexpectedToggleBit) Error() string

Directories

Path Synopsis
This program logs CANopen frames to the console.
This program logs CANopen frames to the console.
sdo

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL