Node and MasterNode

olaf.NetworkError

alias of CanNetworkError

class olaf.NodeStop(value)[source]

Bases: IntEnum

Node stop commands.

NO_STOP = 0

Default value for no stop.

SOFT_RESET = 1

Just stop the app and exit. Systemd will restart the app.

HARD_RESET = 2

Reboot system after app has stopped

FACTORY_RESET = 3

Clear all file cachces and reboot system after app has stopped

POWER_OFF = 4

Just power off the system.

class olaf.Node(network: CanNetwork, od: ObjectDictionary)[source]

OreSat CANopen Node class

Jobs:

  • It abstracts away the canopen.LocalNode and canopen.Network from Resources and Services.

  • Provides access to the OD for Resources and Services.

  • Lets Resources and Services send TPDOs.

  • Lets Resources and Services send EMCY messages.

  • Set up the file transfer caches.

  • Starts/stops all Resources and Services.

  • Sets up all timer-base TPDOs.

  • Sets up all RPDO callbacks.

Basically it tries to abstract all the CANopen things as much a possible, while providing a basic API for CANopen things.

Parameters:
  • network (CanNetwork) – The CAN network

  • od (canopen.ObjectDictionary) – The CANopen ObjectDictionary

run() NodeStop[source]

Go into operational mode, start all the resources, start all the threads, and monitor everything in a loop.

Returns:

Reset / power off condition.

Return type:

NodeStop

stop(reset: NodeStop | None = None)[source]

End the run loop

add_daemon(name: str)[source]

Add a daemon for the node to monitor and/or control

add_sdo_callbacks(index: str, subindex: str, read_cb: Callable[[None], Any], write_cb: Callable[[Any], None])[source]

Add an SDO read callback for a variable at index and optional subindex.

Parameters:
  • index (int or str) – The index to call the callback on.

  • subindex (int or str) – The subindex to call the callback on.

  • read_cb (Callable[[None], Any]) – The SDO read callback. Allows overriding the data being sent on a SDO read. If overriding read data return the value or return None to use the the value from the od. Set to None for no read_cb.

  • write_cb (Callable[[Any], None]) – The SDO writecallback. Gives access to the data being received on a SDO write. Set to None for no write_cb. Note: data is still written to object dictionary before call.

send_tpdo(tpdo: int, raise_error: bool = True)[source]

Send a TPDO. Will not be sent if not node is not in operational state.

Parameters:
  • tpdo (int) – TPDO number to send, should be between 1 and 16.

  • raise_error (bool) – Set to False to not raise NetworkError.

Raises:

NetworkError – Cannot send a TPDO message when the network is down.

send_emcy(code: EmcyCode | int, data: bytes = b'', raise_error: bool = True)[source]

Send a EMCY message.

Parameters:
  • code (Emcy, int) – The EMCY code.

  • data (bytes) – Optional data to add to the message (up to 5 bytes).

  • raise_error (bool) – Set to False to not raise NetworkError.

Raises:

NetworkError – Cannot send a EMCY message when the network is down.

property bus: str

The CAN bus.

Type:

str

property bus_state: str

The CAN bus status.

Type:

str

property name: str

The nodes name.

Type:

str

property od: ObjectDictionary

Access to the object dictionary.

Type:

canopen.ObjectDictionary

property fread_cache: OreSatFileCache

Cache the CANopen master node can read to.

Type:

OreSatFile

property fwrite_cache: OreSatFileCache

Cache the CANopen master node can write to.

Type:

OreSatFile

property is_running: bool

Is the node loop running

Type:

bool

property daemons: Dict[str, Daemon]

The dictionary of external daemons that are monitored and/or controllable

Type:

dict

od_get_obj(index: int | str, subindex: int | str | None = None) ODVariable | ODArray | ODRecord[source]

Quick helper function to get an object from the od.

Returns:

The object from the OD.

Return type:

ODVariable | ODArray | ODRecord

od_read(index: int | str, subindex: int | str | None) int | str | float | bytes | bool[source]

Read a value from the OD.

Parameters:
  • index (int or str) – The index to read from.

  • subindex (int, str, or None) – The subindex to read from or None.

Returns:

The value read.

Return type:

int | str | float | bytes | bool

od_read_bitfield(index: int | str, subindex: int | str | None, field: str) int[source]

Read a field from a object from the OD.

Parameters:
  • index (int or str) – The index to read from.

  • subindex (int, str, or None) – The subindex to read from or None.

Returns:

The field value.

Return type:

int

od_read_enum(index: int | str, subindex: int | str | None) str[source]

Read a enum str from the OD.

Parameters:
  • index (int or str) – The index to read from.

  • subindex (int, str, or None) – The subindex to read from or None.

Returns:

The enum str value.

Return type:

str

od_write(index: int | str, subindex: int | str | None, value: int | str | float | bytes | bool)[source]

Write an value to the OD.

Parameters:
  • index (int | str) – The index to read from.

  • subindex (int | str | None) – The subindex to read from or None.

  • value (int | str | float | bytes | bool) – The value to write.

Raises:

ValueError – An invalid value.

od_write_bitfield(index: int | str, subindex: int | str | None, field: str, value: int)[source]

Write a bit field value to a object to the OD.

Parameters:
  • index (int | str) – The index to read from.

  • subindex (int | str | None) – The subindex to read from or None.

  • field (str) – Name of field to write to.

  • value (int) – The value to write.

Raises:

ValueError – An invalid value.

od_write_enum(index: int | str, subindex: int | str | None, value: str)[source]

Write a enum str to the OD.

Parameters:
  • index (int | str) – The index to read from.

  • subindex (int | str | None) – The subindex to read from or None.

  • value (str) – The enum string to write.

class olaf.MasterNode(network: CanNetwork, od: ObjectDictionary, od_db: Dict[Any, ObjectDictionary])[source]

Bases: Node

OreSat CANopen Master Node (only used by the C3)

Parameters:
  • network (CanNetwork) – The CAN network

  • od (canopen.ObjectDictionary) – The CANopen ObjectDictionary

  • od_db (Dict[Any, canopen.ObjectDictionary]) – Database of other nodes’s ODs. The dict key will be used by class fields and methods.

send_sync()[source]

Send a CANopen SYNC message.

property remote_nodes: dict[Any, RemoteNode]

All other node as remote node.

Type:

dict[Any, canopen.RemoteNode]

property od_db: dict[Any, ObjectDictionary]

All other node ODs.

Type:

dict[Any, canopen.ObjectDictionary]

sdo_read(key: Any, index: int | str, subindex: int | str | None) int | str | float | bytes | bool[source]

Read a value from a remote node’s object dictionary using an SDO.

Parameters:
  • key (Any) – The dict key for the node to read from.

  • index (int | str) – The index to read from.

  • subindex (int | str | None) – The subindex to read from or None.

Raises:
  • NetworkError – Cannot send a SDO read message when the network is down.

  • canopen.sdo.exceptions.SdoError – Error with the SDO.

Returns:

The value read.

Return type:

int | str | float | bytes | bool

sdo_read_bitfield(key: Any, index: int | str, subindex: int | str | None, field: str) int[source]

Read an field from a object from another node’s OD using a SDO.

Parameters:
  • key (Any) – The dict key for the node to read from.

  • index (int | str) – The index to read from.

  • subindex (int | str | None) – The subindex to read from or None.

Raises:
  • NetworkError – Cannot send a SDO read message when the network is down.

  • canopen.sdo.exceptions.SdoError – Error with the SDO.

Returns:

The field value.

Return type:

int

sdo_read_enum(key: Any, index: int | str, subindex: int | str | None) str[source]

Read an enum str from another node’s OD using a SDO.

Parameters:
  • key (Any) – The dict key for the node to read from.

  • index (int | str) – The index to read from.

  • subindex (int | str | None) – The subindex to read from or None.

Raises:
  • NetworkError – Cannot send a SDO read message when the network is down.

  • canopen.sdo.exceptions.SdoError – Error with the SDO.

Returns:

The enum str value.

Return type:

str

sdo_write(key: Any, index: int | str, subindex: int | str | None, value: int | str | float | bytes | bool)[source]

Write a value to a remote node’s object dictionary using an SDO.

Parameters:
  • key (Any) – The dict key for the node to write to.

  • index (int | int) – The index to write to.

  • subindex (int | str | None) – The subindex to write to or None.

  • value (int | str | float | bytes | bool) – The value to write.

Raises:
  • NetworkError – Cannot send a SDO write message when the network is down.

  • canopen.sdo.exceptions.SdoError – Error with the SDO.

sdo_write_bitfield(key: Any, index: int | str, subindex: int | str | None, field: str, value: int)[source]

Write a field from a object to another node’s OD using a SDO.

Parameters:
  • key (Any) – The dict key for the node to write to.

  • index (int | int) – The index to write to.

  • subindex (int | str | None) – The subindex to write to or None.

  • field (str) – Name of field to write to.

  • value (int) – The value to write.

Raises:
  • NetworkError – Cannot send a SDO read message when the network is down.

  • canopen.sdo.exceptions.SdoError – Error with the SDO.

sdo_write_enum(key: Any, index: int | str, subindex: int | str | None, value: str)[source]

Write a enum str to another node’s OD using a SDO.

Parameters:
  • key (Any) – The dict key for the node to write to.

  • index (int | int) – The index to write to.

  • subindex (int | str | None) – The subindex to write to or None.

  • value (str) – The enum str value to write.

Raises:
  • NetworkError – Cannot send a SDO read message when the network is down.

  • canopen.sdo.exceptions.SdoError – Error with the SDO.

send_rpdo(rpdo: int, raise_error: bool = True)[source]

Send a RPDO. Will not be sent if not node is not in operational state.

Parameters:
  • rpdo (int) – RPDO number to send, should be between 1 and 16.

  • raise_error (bool) – Set to False to not raise NetworkError.

Raises:
  • ValueError – Invalud rpdo value.

  • NetworkError – Cannot send a RPDO read message when the network is down.