canopen

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2023 License: MIT Imports: 5 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
)
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 MaxNodeID uint8 = 0x7F

MaxNodeID defines the highest node id

Variables

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

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.

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