Skip to content

Commit

Permalink
docs: Add electrical_protocol page, add Fletcher's checksum implement…
Browse files Browse the repository at this point in the history
…ation in docs, add documentation for ROSSerialDevice in electrical_protocol
  • Loading branch information
cbrxyz committed Sep 21, 2024
1 parent fd2d495 commit 18d8364
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 151 deletions.
132 changes: 0 additions & 132 deletions docs/reference/can.rst
Original file line number Diff line number Diff line change
Expand Up @@ -195,117 +195,6 @@ CommandPacket
.. automodule:: mil_usb_to_can.sub9
.. currentmodule:: mil_usb_to_can.sub9

Packet Format
~~~~~~~~~~~~~
In order to reliably communicate with the USB to CAN board, a consistent packet format
must be defined.

Below is the USBtoCAN Packet Format. This packet format wraps every message being
sent over the serial connection to the USB to CAN board from ROS.

.. list-table:: USBtoCAN Packet Format
:header-rows: 1

* - Name
- Length
- Description
* - Sync character 1 (``0x37``)
- 1 byte
- First sync character indicating the start of packets.
* - Sync character 2 (``0x01``)
- 1 byte
- Second sync character indicating the start of packets.
* - Class ID
- 1 byte
- Message class. Determines the family of messages the packet belongs to.
* - Subclass ID
- 1 byte
- Message subclass. In combination with class, determines specific qualities
of message.
* - Payload Length
- 2 bytes
- Length of payload.
* - Payload
- 0-65535 bytes
- Payload. Meaning of payload is determined by specific packet class/subclass.
* - Checksum A
- 1 byte
- First byte of Fletcher's checksum.
* - Checksum B
- 1 byte
- Second byte of Fletcher's checksum.

Checksums
~~~~~~~~~
All messages contain a checksum to help verify data integrity. However, receiving
packets also have a special byte containing a slightly modified checksum formula.

The checksum in all packets is found by adding up all bytes in the byte string,
including the start/end flags, and then using modulo 16 on this result.

Packet Listing
~~~~~~~~~~~~~~
Below is a listing of all available packets. The payload format is the format
used by the :mod:`struct` module. For more information, see the Python documentation
on the :ref:`list of format characters<format-characters>`, and their corresponding
byte length.

+------------+--------------+----------------+-------------------------------------------------------------------------+
| Message ID | Subclass ID | Payload Format | Class |
+============+==============+================+=========================================================================+
| 0x00 | 0x00 | Empty | :class:`mil_usb_to_can.sub9.NackPacket` |
+ (Meta) +--------------+----------------+-------------------------------------------------------------------------+
| | 0x01 | Empty | :class:`mil_usb_to_can.sub9.AckPacket` |
+------------+--------------+----------------+-------------------------------------------------------------------------+
| 0x01 | 0x00 | Empty | :class:`sub8_thrust_and_kill_board.HeartbeatPacket` |
+ (Sub8 +--------------+----------------+-------------------------------------------------------------------------+
| Thrust/ | 0x01 | ``Bf`` | :class:`sub8_thrust_and_kill_board.ThrustSetPacket` |
+ Kill) +--------------+----------------+-------------------------------------------------------------------------+
| | 0x02 | ``B`` | :class:`sub8_thrust_and_kill_board.KillSetPacket` |
+ +--------------+----------------+-------------------------------------------------------------------------+
| | 0x03 | ``B`` | :class:`sub8_thrust_and_kill_board.KillReceivePacket` |
+------------+--------------+----------------+-------------------------------------------------------------------------+
| 0x02 | 0x00 | Empty | :class:`sub9_thrust_and_kill_board.HeartbeatSetPacket` |
+ (Sub9 +--------------+----------------+-------------------------------------------------------------------------+
| Thrust/ | 0x01 | Empty | :class:`sub9_thrust_and_kill_board.HeartbeatReceivePacket` |
+ Kill) +--------------+----------------+-------------------------------------------------------------------------+
| | 0x02 | ``Bf`` | :class:`sub9_thrust_and_kill_board.ThrustSetPacket` |
+ +--------------+----------------+-------------------------------------------------------------------------+
| | 0x03 | ``B`` | :class:`sub9_thrust_and_kill_board.KillSetPacket` |
+ +--------------+----------------+-------------------------------------------------------------------------+
| | 0x04 | ``B`` | :class:`sub9_thrust_and_kill_board.KillReceivePacket` |
+------------+--------------+----------------+-------------------------------------------------------------------------+
| 0x03 | 0x00 | Empty | :class:`sub8_battery_monitor_board.BatteryPollRequestPacket` |
+ (Battery +--------------+----------------+-------------------------------------------------------------------------+
| Monitor) | 0x01 | ``ffff`` | :class:`sub8_battery_monitor_board.BatteryPollResponsePacket` |
+------------+--------------+----------------+-------------------------------------------------------------------------+
| 0x04 | 0x00 | ``BB`` | :class:`sub_actuator_board.ActuatorSetPacket` |
+ (Actuator +--------------+----------------+-------------------------------------------------------------------------+
| Board) | 0x01 | Empty | :class:`sub_actuator_board.ActuatorPollRequestPacket` |
+ +--------------+----------------+-------------------------------------------------------------------------+
| | 0x02 | ``B`` | :class:`sub_actuator_board.ActuatorPollResponsePacket` |
+------------+--------------+----------------+-------------------------------------------------------------------------+
| 0x05 | 0x00 | Empty | :class:`sub9_system_status_board.SetLedRequestPacket` |
| (System | | | |
| Status) | | | |
+------------+--------------+----------------+-------------------------------------------------------------------------+

Exceptions
~~~~~~~~~~

Exception Hierarchy
"""""""""""""""""""
.. currentmodule:: mil_usb_to_can.sub9

.. exception_hierarchy::

- :exc:`ChecksumException`

Exception List
"""""""""""""""""""
.. autoclass:: mil_usb_to_can.sub9.ChecksumException
:members:

CANDeviceHandle
~~~~~~~~~~~~~~~
.. attributetable:: mil_usb_to_can.sub9.CANDeviceHandle
Expand All @@ -319,24 +208,3 @@ SimulatedCANDeviceHandle

.. autoclass:: mil_usb_to_can.sub9.SimulatedCANDeviceHandle
:members:

Packet
~~~~~~
.. attributetable:: mil_usb_to_can.sub9.Packet

.. autoclass:: mil_usb_to_can.sub9.Packet
:members:

NackPacket
~~~~~~~~~~
.. attributetable:: mil_usb_to_can.sub9.NackPacket

.. autoclass:: mil_usb_to_can.sub9.NackPacket
:members:

AckPacket
~~~~~~~~~
.. attributetable:: mil_usb_to_can.sub9.AckPacket

.. autoclass:: mil_usb_to_can.sub9.AckPacket
:members:
154 changes: 154 additions & 0 deletions docs/reference/electrical_protocol.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
:mod:`electrical_protocol` - Electrical-software communication standard
-----------------------------------------------------------------------

.. automodule:: electrical_protocol
.. currentmodule:: electrical_protocol

Packet Format
~~~~~~~~~~~~~
In order to reliably communicate with an electrical board, a consistent packet format
must be defined.

Below is the electrical protocol packet format. This packet format wraps every message being
sent over the serial connection to the USB to CAN board from ROS.

.. list-table:: Packet Format
:header-rows: 1

* - Name
- Length
- Description
* - Sync character 1 (``0x37``)
- 1 byte
- First sync character indicating the start of packets.
* - Sync character 2 (``0x01``)
- 1 byte
- Second sync character indicating the start of packets.
* - Class ID
- 1 byte
- Message class. Determines the family of messages the packet belongs to.
* - Subclass ID
- 1 byte
- Message subclass. In combination with class, determines specific qualities
of message.
* - Payload Length
- 2 bytes
- Length of payload.
* - Payload
- 0-65535 bytes
- Payload. Meaning of payload is determined by specific packet class/subclass.
* - Checksum A
- 1 byte
- First byte of Fletcher's checksum.
* - Checksum B
- 1 byte
- Second byte of Fletcher's checksum.

Checksums
~~~~~~~~~
All messages contain a checksum to help verify data integrity. However, receiving
packets also have a special byte containing a slightly modified checksum formula.

The checksum in all packets is found by adding up all bytes in the byte string,
including the start/end flags, and then using modulo 16 on this result. The
library checksum is implemented like so:

.. literalinclude:: ../../mil_common/drivers/electrical_protocol/electrical_protocol/packet.py
:pyobject: Packet._calculate_checksum

Packet Listing
~~~~~~~~~~~~~~
Below is a listing of all available packets. The payload format is the format
used by the :mod:`struct` module. For more information, see the Python documentation
on the :ref:`list of format characters<format-characters>`, and their corresponding
byte length.

+------------+--------------+----------------+-------------------------------------------------------------------------+
| Message ID | Subclass ID | Payload Format | Class |
+============+==============+================+=========================================================================+
| 0x00 | 0x00 | Empty | :class:`electrical_protocol.NackPacket` |
+ (Meta) +--------------+----------------+-------------------------------------------------------------------------+
| | 0x01 | Empty | :class:`electrical_protocol.AckPacket` |
+------------+--------------+----------------+-------------------------------------------------------------------------+
| 0x01 | 0x00 | Empty | :class:`sub8_thrust_and_kill_board.HeartbeatPacket` |
+ (Sub8 +--------------+----------------+-------------------------------------------------------------------------+
| Thrust/ | 0x01 | ``Bf`` | :class:`sub8_thrust_and_kill_board.ThrustSetPacket` |
+ Kill) +--------------+----------------+-------------------------------------------------------------------------+
| | 0x02 | ``B`` | :class:`sub8_thrust_and_kill_board.KillSetPacket` |
+ +--------------+----------------+-------------------------------------------------------------------------+
| | 0x03 | ``B`` | :class:`sub8_thrust_and_kill_board.KillReceivePacket` |
+------------+--------------+----------------+-------------------------------------------------------------------------+
| 0x02 | 0x00 | Empty | :class:`sub9_thrust_and_kill_board.HeartbeatSetPacket` |
+ (Sub9 +--------------+----------------+-------------------------------------------------------------------------+
| Thrust/ | 0x01 | Empty | :class:`sub9_thrust_and_kill_board.HeartbeatReceivePacket` |
+ Kill) +--------------+----------------+-------------------------------------------------------------------------+
| | 0x02 | ``Bf`` | :class:`sub9_thrust_and_kill_board.ThrustSetPacket` |
+ +--------------+----------------+-------------------------------------------------------------------------+
| | 0x03 | ``B`` | :class:`sub9_thrust_and_kill_board.KillSetPacket` |
+ +--------------+----------------+-------------------------------------------------------------------------+
| | 0x04 | ``B`` | :class:`sub9_thrust_and_kill_board.KillReceivePacket` |
+------------+--------------+----------------+-------------------------------------------------------------------------+
| 0x03 | 0x00 | Empty | :class:`sub8_battery_monitor_board.BatteryPollRequestPacket` |
+ (Battery +--------------+----------------+-------------------------------------------------------------------------+
| Monitor) | 0x01 | ``ffff`` | :class:`sub8_battery_monitor_board.BatteryPollResponsePacket` |
+------------+--------------+----------------+-------------------------------------------------------------------------+
| 0x04 | 0x00 | ``BB`` | :class:`sub_actuator_board.ActuatorSetPacket` |
+ (Actuator +--------------+----------------+-------------------------------------------------------------------------+
| Board) | 0x01 | Empty | :class:`sub_actuator_board.ActuatorPollRequestPacket` |
+ +--------------+----------------+-------------------------------------------------------------------------+
| | 0x02 | ``B`` | :class:`sub_actuator_board.ActuatorPollResponsePacket` |
+------------+--------------+----------------+-------------------------------------------------------------------------+
| 0x05 | 0x00 | Empty | :class:`sub9_system_status_board.SetLedRequestPacket` |
| (System | | | |
| Status) | | | |
+------------+--------------+----------------+-------------------------------------------------------------------------+
| 0x10 | 0x00 | ``?B`` | :class:`navigator_pico_kill_board.KillSetPacket` |
| (NaviGator +--------------+----------------+-------------------------------------------------------------------------+
| Temporary | 0x01 | ``?B`` | :class:`navigator_pico_kill_board.KillReceivePacket` |
| Pico Kill | | | |
| Board) | | | |
+------------+--------------+----------------+-------------------------------------------------------------------------+

Exceptions
~~~~~~~~~~

Exception Hierarchy
"""""""""""""""""""
.. currentmodule:: electrical_protocol

.. exception_hierarchy::

- :exc:`ChecksumException`

Exception List
"""""""""""""""""""
.. autoclass:: electrical_protocol.ChecksumException
:members:

ROSSerialDevice
~~~~~~~~~~~~~~~
.. attributetable:: electrical_protocol.ROSSerialDevice

.. autoclass:: electrical_protocol.ROSSerialDevice
:members:

Packet
~~~~~~
.. attributetable:: electrical_protocol.Packet

.. autoclass:: electrical_protocol.Packet
:members:

NackPacket
~~~~~~~~~~
.. attributetable:: electrical_protocol.NackPacket

.. autoclass:: electrical_protocol.NackPacket
:members:

AckPacket
~~~~~~~~~
.. attributetable:: electrical_protocol.AckPacket

.. autoclass:: electrical_protocol.AckPacket
:members:
3 changes: 2 additions & 1 deletion docs/reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ by MIL. These subsystems relate to a variety of processes.
mission/index
rviz
pcodar
electrical_protocol
can
resources
rc
alarms
pathplanning
battery
passivesonar
can
vision
poi
pneumatic
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
"""
The :mod:`electrical_protocol` module is meant to serve as a bridge between
a software library and a physical electrical device. The library is not dependent
on a particular communication standard (UART, CAN, etc.) being used. Rather, the
library just provides a packet structure that can be used by any project, and a
list of packets using this structure.
The library also provides a simple driver for UART/serial communication with a
physical electrical device -- :class:`~.electrical_protocol.ROSSerialDevice`.
If a CAN standard is desired, the :class:`mil_usb_to_can.sub9.CANDeviceHandle`
class can be used, which supports packets built through this library.
"""

from .driver import ROSSerialDevice
from .packet import AckPacket, NackPacket, Packet
from .packet import AckPacket, ChecksumException, NackPacket, Packet
Loading

0 comments on commit 18d8364

Please sign in to comment.