# monash-railway-express/can_mrex architecture

> Architecture diagram of the GitHub repository monash-railway-express/can_mrex, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/monash-railway-express/can_mrex
- Repository: https://github.com/monash-railway-express/can_mrex

## Overview

CAN MREX is an embedded CAN/CANopen-style wrapper for MREx locomotive nodes. A node application supplies its node ID and calls the library’s handler; the handler services operational PDO transmission and heartbeat work, receives or accepts a CAN frame, then dispatches it by COB-ID to NMT, EMCY, PDO, SDO, or heartbeat handling. The object dictionary provides shared node state and indexed values. README documents all six protocol capabilities; the available source excerpts establish handler dispatch and several dictionary dependencies, but do not show the public wrapper, configuration, or NMT implementation.

Main entry points: [`src/CM_Handler.cpp`](https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_Handler.cpp) (Frame handler), [`src/CM_Heartbeat.cpp`](https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_Heartbeat.cpp) (Heartbeat monitor), [`src/CM_ObjectDictionary.cpp`](https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_ObjectDictionary.cpp) (Object dictionary), [`src/CM_EMCY.cpp`](https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_EMCY.cpp) (Emergency messages)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Node Interface"]
  node_api["Library API<br/>[CAN_MREx.h]"]
  node_config["CAN configuration<br/>[CM_Config.cpp]"]
end

subgraph group_dispatch["CAN Dispatch"]
  node_handler["Frame handler<br/>[CM_Handler.cpp]"]
end

subgraph group_protocol["CANopen Services"]
  node_nmt["NMT control<br/>[CM_NMT.cpp]"]
  node_emcy["Emergency messages<br/>[CM_EMCY.cpp]"]
  node_pdo["Process data<br/>[CM_PDO.cpp]"]
  node_sdo["Structured data<br/>[CM_SDO.cpp]"]
  node_heartbeat["Heartbeat monitor<br/>[CM_Heartbeat.cpp]"]
end

subgraph group_state["Node State"]
  node_dictionary[("Object dictionary")]
end

node_application(("Node application"))
node_canbus["TWAI CAN driver"]

node_application -.->|"uses"| node_api
node_api -.->|"dispatches"| node_handler
node_config -.->|"configures"| node_canbus
node_handler -->|"receives frames"| node_canbus
node_handler -->|"services and processes"| node_pdo
node_handler -->|"sends and checks"| node_heartbeat
node_handler -->|"dispatches NMT"| node_nmt
node_handler -->|"dispatches EMCY"| node_emcy
node_handler -->|"dispatches SDO"| node_sdo
node_handler -->|"dispatches frames"| node_heartbeat
node_emcy -->|"uses entries"| node_dictionary
node_pdo -->|"uses entries"| node_dictionary
node_sdo -->|"uses entries"| node_dictionary
node_heartbeat -->|"uses state"| node_dictionary
node_heartbeat -->|"reports faults"| node_emcy

click node_api "https://github.com/monash-railway-express/can_mrex/blob/main/src/CAN_MREx.h"
click node_config "https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_Config.cpp"
click node_handler "https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_Handler.cpp"
click node_nmt "https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_NMT.cpp"
click node_emcy "https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_EMCY.cpp"
click node_pdo "https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_PDO.cpp"
click node_sdo "https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_SDO.cpp"
click node_heartbeat "https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_Heartbeat.cpp"
click node_dictionary "https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_ObjectDictionary.cpp"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_config toneBlue
class node_handler toneAmber
class node_nmt,node_emcy,node_pdo,node_sdo,node_heartbeat toneMint
class node_dictionary toneRose
class node_application,node_canbus toneIndigo
```

## Components

### Node Interface

- [Library API](https://github.com/monash-railway-express/can_mrex/blob/main/src/CAN_MREx.h): `src/CAN_MREx.h`
- [CAN configuration](https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_Config.cpp): `src/CM_Config.cpp`

### CAN Dispatch

- [Frame handler](https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_Handler.cpp): `src/CM_Handler.cpp`

### CANopen Services

- [NMT control](https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_NMT.cpp): `src/CM_NMT.cpp`
- [Emergency messages](https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_EMCY.cpp): `src/CM_EMCY.cpp`
- [Process data](https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_PDO.cpp): `src/CM_PDO.cpp`
- [Structured data](https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_SDO.cpp): `src/CM_SDO.cpp`
- [Heartbeat monitor](https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_Heartbeat.cpp): `src/CM_Heartbeat.cpp`

### Node State

- [Object dictionary](https://github.com/monash-railway-express/can_mrex/blob/main/src/CM_ObjectDictionary.cpp): `src/CM_ObjectDictionary.cpp`

### Other components

- **Node application**
- **TWAI CAN driver**

## Connections

- Node application → Library API: uses
- Library API → Frame handler: dispatches
- CAN configuration → TWAI CAN driver: configures
- Frame handler → TWAI CAN driver: receives frames (evidence: `src/CM_Handler.cpp`)
- Frame handler → Process data: services and processes (evidence: `src/CM_Handler.cpp`)
- Frame handler → Heartbeat monitor: sends and checks (evidence: `src/CM_Handler.cpp`)
- Frame handler → NMT control: dispatches NMT (evidence: `src/CM_Handler.cpp`)
- Frame handler → Emergency messages: dispatches EMCY (evidence: `src/CM_Handler.cpp`)
- Frame handler → Structured data: dispatches SDO (evidence: `src/CM_Handler.cpp`)
- Frame handler → Heartbeat monitor: dispatches frames (evidence: `src/CM_Handler.cpp`)
- Emergency messages → Object dictionary: uses entries (evidence: `src/CM_EMCY.cpp`)
- Process data → Object dictionary: uses entries (evidence: `src/CM_PDO.cpp`)
- Structured data → Object dictionary: uses entries (evidence: `src/CM_SDO.cpp`)
- Heartbeat monitor → Object dictionary: uses state (evidence: `src/CM_Heartbeat.cpp`)
- Heartbeat monitor → Emergency messages: reports faults (evidence: `src/CM_Heartbeat.cpp`)
