# canopen-python/canopen architecture

> Architecture diagram of the GitHub repository canopen-python/canopen, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/canopen-python/canopen
- Repository: https://github.com/canopen-python/canopen

## Overview

##### Project type and purpose
- This is a Python library implementing the CANopen protocol (CiA-301) for use as a master (and limited slave) in testing and automation.
- It provides high-level abstractions for Network, Node, SDO, PDO, NMT, SYNC, EMCY, TIME, LSS, and object dictionary functionality.

##### File structure
- Top-level package “canopen/”: contains modules for each CANopen service:
- network.py – Network abstraction
- node/ – Node classes (LocalNode, RemoteNode, base Node)
- sdo/, pdo/, nmt.py, sync.py, emcy.py, timestamp.py, lss.py
- objectdictionary/ – EDS parsing, datatypes, EPF support
- profiles/ – CiA-402 profile support and test tools
- utils.py, variable.py
- Configuration and tooling:
- setup.py, pyproject.toml, MANIFEST.in – packaging
- .github/workflows, codecov.yml – CI/CD
- doc/ – Sphinx docs
- examples/ – usage scripts
- test/ – pytest and unittest test suite
- External dependency integration:
- python-can (“network.connect” passes parameters to python-can Bus)

##### README for additional insights
- Clarifies mission: simple Pythonic interface, master-oriented.
- Lists supported services: NMT, SDO client, PDO producer/consumer, SYNC, EMCY, TIME, LSS.
- Shows quick-start usage: instantiate Network, add RemoteNode, connect, perform SDO/PDO operations, start/stop SYNC, change NMT state.

a. Main components to include:
- CANopen Library Core
- Network Manager
- Node Manager (LocalNode, RemoteNode)
- Service Modules: SDO, PDO, NMT, SYNC, EMCY, TIME, LSS
- Object Dictionary Parser (EDS)
- Profile Support Layer (CiA-402)
- External Interfaces
- python-can Bus backend (hardware abstraction layer)
- CAN bus network (physical layer)
- Consumer Code / Examples / Tests
- User Application / Automation Scripts
- Test Harness
b. Relationships and interactions:
- User Application → invokes Library Core API
- Library Core → uses Network Manager to open python-can Bus
- Network Manager ↔ python-can Bus → sends/receives raw CAN frames
- Node Manager holds Node instances (Local or Remote)
- Service Modules attach to Node and Network to implement protocol flows:
- SDO client/server messages over CAN frames
- PDO mapping and transmission
- NMT state controls
- SYNC generation
- EMCY and TIME producer/consumer
- Object Dictionary → used by Node Manager and SDO/PDO modules
- Profiles layer builds on Object Dictionary for CiA-402 state machine
c. Architectural patterns/design principles:
- Modular layering: transport (python-can) → network abstraction → protocol services → application
- Plug-in extensibility: object dictionary loading (EDS), hardware backend customization
- Separation of concerns: each CANopen service in its own module
d. Relevant technologies/frameworks:
- Python 3.8+
- python-can for hardware interface
- Sphinx for documentation
- pytest/unittest for testing

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Consumers
    subgraph "User & Tests" 
        direction TB
        UA["User Application / Scripts"]:::dashed
        TH["Test Harness"]:::dashed
    end

    UA -->|uses API| LibraryCore
    TH -->|tests API| LibraryCore

    %% Core Library
    subgraph "CANopen Library Core"
        direction TB
        LibraryCore["Library Core API"]:::core

        subgraph "Network & Nodes"
            direction TB
            NM["Network Manager"]:::core
            subgraph "Node Manager"
                direction TB
                BN["BaseNode"]:::core
                LN["LocalNode"]:::core
                RN["RemoteNode"]:::core
            end
        end

        subgraph "Service Modules"
            direction TB
            SDO["SDO Service"]:::core
            PDO["PDO Service"]:::core
            NMT["NMT Service"]:::core
            SYNC["SYNC Service"]:::core
            EMCY["EMCY Service"]:::core
            TIME["TIME Service"]:::core
            LSS["LSS Service"]:::core
        end

        subgraph "Object Dictionary" 
            direction TB
            EDS["EDS Parser"]:::od
            DT["Data Types"]:::od
            EPF["EPF Support"]:::od
        end

        PS["Profile Support (CiA-402)"]:::core

        %% Internal flows
        LibraryCore --> NM
        LibraryCore --> BN
        LibraryCore --> SDO
        LibraryCore --> PDO
        LibraryCore --> NMT
        LibraryCore --> SYNC
        LibraryCore --> EMCY
        LibraryCore --> TIME
        LibraryCore --> LSS
        LibraryCore --> EDS
        LibraryCore --> PS

        NM -->|spawns| BN
        BN -->|extends| LN
        BN -->|extends| RN

        SDO --> BN
        PDO --> BN
        NMT --> BN
        SYNC --> BN
        EMCY --> BN
        TIME --> BN
        LSS --> BN

        SDO --> NM
        PDO --> NM
        NMT --> NM
        SYNC --> NM
        EMCY --> NM
        TIME --> NM
        LSS --> NM

        EDS --> BN
        EDS --> SDO
        EDS --> PDO
        PS --> EDS
        PS --> BN
    end

    %% External interfaces
    subgraph "External Interfaces"
        direction TB
        CANBus["Physical CAN Bus"]:::external
        PCAN["python-can Backend"]:::external
    end

    NM -->|uses python-can| PCAN
    PCAN -->|sends/receives frames| CANBus

    %% Click events
    click NM "https://github.com/canopen-python/canopen/blob/master/canopen/network.py"
    click BN "https://github.com/canopen-python/canopen/blob/master/canopen/node/base.py"
    click LN "https://github.com/canopen-python/canopen/blob/master/canopen/node/local.py"
    click RN "https://github.com/canopen-python/canopen/blob/master/canopen/node/remote.py"
    click SDO "https://github.com/canopen-python/canopen/blob/master/canopen/sdo/base.py"
    click SDO "https://github.com/canopen-python/canopen/blob/master/canopen/sdo/client.py"
    click SDO "https://github.com/canopen-python/canopen/blob/master/canopen/sdo/server.py"
    click SDO "https://github.com/canopen-python/canopen/blob/master/canopen/sdo/constants.py"
    click SDO "https://github.com/canopen-python/canopen/blob/master/canopen/sdo/exceptions.py"
    click PDO "https://github.com/canopen-python/canopen/blob/master/canopen/pdo/base.py"
    click PDO "https://github.com/canopen-python/canopen/blob/master/canopen/pdo/__init__.py"
    click NMT "https://github.com/canopen-python/canopen/blob/master/canopen/nmt.py"
    click SYNC "https://github.com/canopen-python/canopen/blob/master/canopen/sync.py"
    click EMCY "https://github.com/canopen-python/canopen/blob/master/canopen/emcy.py"
    click TIME "https://github.com/canopen-python/canopen/blob/master/canopen/timestamp.py"
    click LSS "https://github.com/canopen-python/canopen/blob/master/canopen/lss.py"
    click EDS "https://github.com/canopen-python/canopen/blob/master/canopen/objectdictionary/eds.py"
    click DT "https://github.com/canopen-python/canopen/blob/master/canopen/objectdictionary/datatypes.py"
    click EPF "https://github.com/canopen-python/canopen/blob/master/canopen/objectdictionary/epf.py"
    click PS "https://github.com/canopen-python/canopen/blob/master/canopen/profiles/p402.py"
    click TH "https://github.com/canopen-python/canopen/tree/master/test/"
    click UA "https://github.com/canopen-python/canopen/blob/master/examples/simple_ds402_node.py"

    %% Styling
    classDef core fill:#DAE8FC,stroke:#6C8EBF,stroke-width:1px
    classDef external fill:#D5E8D4,stroke:#82B366,stroke-width:1px
    classDef od fill:#FFF2CC,stroke:#D6B656,stroke-width:1px
    classDef dashed stroke-dasharray: 5 5,fill:none,stroke:#999
```
