canopen

package module
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2023 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 (
	SDO_ERR_TOGGLE_BIT         = 0x05030000
	SDO_ERR_TIMEOUT            = 0x05040000
	SDO_ERR_COMMAND            = 0x05040001
	SDO_ERR_BLOCK_SIZE         = 0x05040002
	SDO_ERR_BLOCK_SEQUENCE     = 0x05040003
	SDO_ERR_BLOCK_CRC          = 0x05040004
	SDO_ERR_MEMORY             = 0x05040005
	SDO_ERR_ACCESS_UNSUPPORTED = 0x06010000
	SDO_ERR_ACCESS_WO          = 0x06010001
	SDO_ERR_ACCESS_RO          = 0x06010002
	SDO_ERR_NO_OBJECT          = 0x06020000
	SDO_ERR_MAPPING_OBJECT     = 0x06040041
	SDO_ERR_MAPPING_LENGTH     = 0x06040042
	SDO_ERR_GENERAL_PARAMETER  = 0x06040043
	SDO_ERR_GENERAL_DEVICE     = 0x06040047
	SDO_ERR_HARDWARE           = 0x06060000
	SDO_ERR_DATATYPE           = 0x06070010
	SDO_ERR_DATATYPE_HIGH      = 0x06070012
	SDO_ERR_DATATYPE_LOW       = 0x06070013
	SDO_ERR_NO_SUB_INDEX       = 0x06090011
	SDO_ERR_VALUE_RANGE        = 0x06090030
	SDO_ERR_VALUE_HIGH         = 0x06090031
	SDO_ERR_VALUE_LOW          = 0x06090032
	SDO_ERR_VALUE_MIN_MAX      = 0x06090036
	SDO_ERR_SDO_CONNECTION     = 0x060A0023
	SDO_ERR_GENERAL            = 0x08000000
	SDO_ERR_DATA_STORE         = 0x08000020
	SDO_ERR_DATA_STORE_LOCAL   = 0x08000021
	SDO_ERR_DATA_STORE_STATE   = 0x08000022
	SDO_ERR_OBJECT_DICTIONARY  = 0x08000023
	SDO_ERR_NO_DATA            = 0x08000024
)
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 uint32) string

func Marshal

func Marshal(frm Frame) (b []byte, err error)

Marshal returns the byte encoding of frm.

func Produce

func Produce(frame Frame, bus *can.Bus, timeout time.Duration) chan<- struct{}

Produce repeatedly sends a frame on a bus after a timeout. The sending can be stopped by using the returned channel.

func ProduceHeartbeat

func ProduceHeartbeat(nodeID uint8, state uint8, bus *can.Bus, timeout time.Duration) chan<- struct{}

ProduceHeartbeat repeatedly sends a CANopen heartbeat frame.

func Unmarshal

func Unmarshal(b []byte, frm *Frame) error

Unmarshal parses the bytes b and stores the result in the value pointed to by frm.

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 NewHeartbeatFrame

func NewHeartbeatFrame(id uint8, state uint8) Frame

NewHeartbeatFrame returns a new CANopen heartbeat frame containing the node id and state.

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) 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.

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.

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 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.

Jump to

Keyboard shortcuts

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