User Tools

Site Tools


projects:crazyflie:firmware:comm_protocol

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
projects:crazyflie:firmware:comm_protocol [2012-08-07 19:53]
macke
projects:crazyflie:firmware:comm_protocol [2021-06-24 16:49] (current)
kimberly
Line 1: Line 1:
 +<WRAP center round important 60%>
 +This page has deprecated and will be archived. Please go to [[https://www.bitcraze.io/]].
 +</WRAP>
 ====== Overview ====== ====== Overview ======
-===== CRTP ===== +CRTP is the protocol used to communicate with Crazyflie. Is originally stands for //Crazy RealTime Protocol//. The protocol permits to communicate independently with the copter subsystem and would have the capability to handle packet priorities. 
-The CRTP (Crazy RealTime Protocol) is the name of the protocol used to communication with the Crazyflie. The protocol is used for multiple physical layers: + 
-  * [[comm_protocol#serial_port | UART]] +CRTP is implemented in 3 layers: 
-  [[comm_protocol#usb_port | USB]] + 
-  [[comm_protocol#2.4_GHz_NordicSemi_Radio | 2.4 GHz NordicSemi Radio]] +<ditaa> 
-  * ...+ +-----------------------+ 
 +   Application/ports   | 
 + +-----------------------+ 
 + | CRTP packets handling | 
 + +-----------------------+   
 +       CRTP Link       | 
 + +-----------------------+ 
 +</ditaa> 
 + 
 +  * **CRTP Link** is responsible for transferring the packets between the copter and the PC. It will typically handles packet size and error detection. 
 +  * **CRTP Packet handling** delivers the packet to the right subsystem in the copter and in the PC control software. 
 +  * **Application/ports** represents the subsystems that sends and receives messages. 
 + 
 +Implemented link drivers: 
 +^ **Link**                                                                                       ^ 
 +[[comm_protocol#serial_port | UART]]               | Uart link, mainly used in early development | 
 +[[comm_protocol#usb_port | USB]]                   | To be implemented USB link. (Crazyflie USB port) | 
 +[[comm_protocol#2.4_GHz_NordicSemi_Radio | radio]] | 2.4 GHz NordicSemi Radio link (any nRF24L01 compatible radio, eg. Crazyradio dongle) | 
 + 
 +Current port allocation: 
 + **Port**  ^   **Target**    **Used for**   ^ 
 +|          |   [[projects:crazyflie:firmware:console | Console ]]      | Read console text that is printed to the console on the Crazyflie using consoleprintf | 
 +|          |   [[projects:crazyflie:firmware:param | Parameters]]   | Get/set parameters from the CrazyflieParameters are defined using a macro in the Crazyflie source-code | 
 +|          |   [[projects:crazyflie:crtp:commander |Commander]]    | Sending control set-points for the roll/pitch/yaw/thrust regulators | 
 +|          |   [[projects:crazyflie:firmware:log#communication_protocol|Log]]    | Set up log blocks with variables that will be sent back to the Crazyflie at a specified periodLog variables are defined using a macro in the Crazyflie source-code | 
 +|   14         [[projects:crazyflie:pc_utils:debugdriver|Client-side debugging]]    | Debugging the UI and exists only in the Crazyflie Python API and not in the Crazyflie itself | 
 +|   15         [[projects:crazyflie:firmware:linklayer |Link layer ]]   | Used to control and query the communication link |
  
 ====== Communication ====== ====== Communication ======
Line 13: Line 42:
   7               0   7               0
 +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+
-| R |     Port      |  Channel  |+|     Port      |  Res. | Chan. 
 +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+
 |            DATA 0             | |            DATA 0             |
Line 21: Line 50:
 |            DATA 30            | |            DATA 30            |
 +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+
 +
 </code> </code>
 +^ **Field** ^ **Byte** ^ **Bit** ^   **Description**      ^
 +|   Header  |        |   0-1   | The destination channel  |
 +|    :::    |        |   2-3   | Reserved for transport layer  |
 +|    :::    |        |   4-7   | The destination port  |
 +|    Data      1-31  |         | The data in the packet |
 +
 +A port maps to a major function in the Crazyflie that is normally implemented as a separate task. When a CRTP packet is received in the Crazyflie the packet is routed according to the port. The channel can be seen as a sub-set for the port where and can be used to further define commands for the receiving function but no routing will be done using the channel.
  
 ===== Physical layers ===== ===== Physical layers =====
Line 27: Line 64:
  
 ==== 2.4 GHz NordicSemi Radio ==== ==== 2.4 GHz NordicSemi Radio ====
-The wireless interface will add additional header data like CRC when sending the packet and therefore the packet sent to the wireless interface is only a CRTP packet.+The wireless interface will add additional header data like CRC when sending the packet and therefore the packet sent to the wireless interface is only a CRTP packet (header and data).
  
 ==== USB port ==== ==== USB port ====
Line 33: Line 70:
  
 ==== Serial port ==== ==== Serial port ====
 +The serial port is configured in 115200 8N1. CRTP packets are sent and received asynchronously using the following packet format:
 +
 <code> <code>
   7               0   7               0
Line 40: Line 79:
 |             0xAA              | |             0xAA              |
 +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+
-R |     Port      |  Channel  |+|      Port     |  Res  | Chan. |
 +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+
 |         Packet length         | |         Packet length         |
Line 50: Line 89:
 |            DATA 30            | |            DATA 30            |
 +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+
-             CRC              |+           Cksum              |
 +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+
 +
 ++--------+--------+--------+--------+--------+--....--+--------+
 +|  0xAA  |  0xAA  | Header | Length | Data0  | Packet | Cksum  |
 ++--------+--------+--------+--------+--------+--....--+--------+
 +
 </code> </code>
 +^ **Field** ^ **Byte** ^ **Bit** ^     **Description**     ^
 +|   Start      0-1     0-2   | Start token for synchronization (0xAAAA)  |
 +|   Header  |        |   0-1   | The destination channel  |
 +|    :::    |        |   2-3   | Reserved for the link layer |
 +|    :::    |        |   4-7   | The destination port  |
 +|    Size          |         | Number of data byte |
 +|    Data      4-..  |         | The data in the packet |
 +|   Cksum    4+size  |         | Checksum. Sum of all the bytes, excluding //Start//, modulo 256. |
  
 +=== Examples ===
 +To send setpoints to the [[projects:crazyflie:crtp:commander|commander]] send: 
 +<code>
 +0xaa 0xaa 0x30 0x0e 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x3e
 +</code>
 +
 +To ping Crazyflie:
 +<code>
 + ->   0xaa 0xaa 0xf0 0x01 0x01 0xf2     # Sent to the copter 
 + <  0xaa 0xaa 0xf0 0x01 0x01 0xf2     # Received from the copter
 +</code>
 ===== Ports ===== ===== Ports =====
-| *Port*                                    | *Target*     | 
-| [[comm_protocol#console|000]]     | Console      | 
-| [[comm_protocol#logging|*5*]]      | Logging | 
-| [[comm_protocol#parameters|*9*]]      | Parameters | 
-| [[comm_protocol#flight_control|*13*]]     | Flight control | 
-| 0:13:                                   | Send/Receive yaw, pith, roll and throttle | 
-| [[comm_protocol#link|*15*]]       | Link layer reserved | 
-| 3:15:                                   | Echo (profiling)  | 
-| 3:15:                                   | Source (profiling)| 
-| 3:15:                                   | Sink (profiling)  | 
-| 3:15:                                   | NULL packet       | 
  
-==== Console ==== +==== Logging ==== 
-This port is used as a one-way text consolePrintouts using printf will be sent on this port and can be read by receiver if connected.+The purpose of the logging is to be able to log variables that are available in the copter during runtimeThe available variables are listed using a TOC that can be read from the copter. A logging block can then be registered consisting of there variables and the data will be sent periodically via CRTP to a receiver. 
 + 
 +^ **Port**  ^ **Channel** ^ **Function**      ^ 
 +|            0        | [[comm_protocol#table_of_content_access|Table of content access]] | 
 +|            1        | [[comm_protocol#log_settings_access| Log settings access]] | 
 +|            2        | [[comm_protocol#log_data_access|Log data access]] | 
 + 
 +=== Table of content access (Port 5 channel 0) === 
 +This channel is used to access the loggable variables table of content. 
 + 
 +== Messages == 
 +Each message uses an additional command byte that is followed by 0 to 31 bytes of data depending of the command. For every command byte there's a difference in usage depending on if the message is sent from the Crazyflie to a Computer or the other way around. 
 + 
 +The logging variables are requested by the receiver until all variables are returned. When the variable index is out of range, empty reply is returned (1 byte reply with 0x00 identifying the command). The Get CRC command returns the number of loggable variables N, individual variables are indexed from 0 to N-1. 
 + 
 +The CRC32 is a hash of the copter's loggable variable TOC. The aim of having this is to be able to cache data on the receiver end and to quickly check the validity of it when connecting. CRC is now computed over raw log section contents, so variable address and address of variable name string in ROM is CRC'd. CRC will change if memory layout changes (any change in firmware) even when TOC contents remains the same. 
 + 
 +The following messages are sent from the Client to the Crazyflie: 
 +^  **Request for**  ^  **Byte**  ^  **Description** 
 +| TOC item  |          | 0x00 (CMD_GET_ITEM) to identify the command 
 +|   :::    |          | The index of the TOC item to request | 
 +|  TOC CRC and count |  0   | 0x01 (CMD_GET_INFO) Request the TOC CRC and item count | 
 + 
 +The following messages are sent from the Crazyflie to the Client: 
 +^  **Reply for**  ^  **Byte**  ^  **Description** 
 +|     TOC item                | 0x00 (CMD_GET_ITEM) to identify the command | 
 +|        :::                  | Variable ID 
 +|        :::                  | Type (ref to variable type )  | 
 +|        :::            3-n     | Null terminated string containing group name  | 
 +|        :::            n-m     | Null terminated string containing variable name  | 
 +| TOC CRC and count  |          | 0x01 (CMD_GET_INFO) to identify the command 
 +|        :::                  | Number of total variables in TOC  | 
 +|        :::            2-5     | CRC32 value of the TOC (calculated from raw contents of log TOC section) 
 +|        :::                  | Maximum number of log blocks that can be registered 
 +|        :::                  | Maximum number of log variables that can be registered 
 + 
 +== Usage == 
 +Example of checking the CRC of TOC to determine if the TOC needs to be refreshed. 
 +<code> 
 +   Computer                 Crazyflie 
 +   --------                 --------- 
 +Get TOC CRC32      ---> 
 +                   <---     TOC CRC32 
 +[Compare CRC]                    
 +</code> 
 + 
 +Example of downloading TOC contents from Crazyflie. 
 +<code> 
 +   Computer                 Crazyflie 
 +   --------                 --------- 
 +Request for TOC item 0   --->   
 +                         <---  First TOC element  
 +Request for TOC item 1   ---> 
 +                         <--- Requested TOC element 
 +                         .... 
 +                         .... 
 +Request for TOC item N-1 ---> 
 +  where N is Number of  
 + total variables in TOC 
 +                         <--- Last TOC element  
 +[optional] 
 +Request for TOC item N   ---> 
 +   (or any higher) 
 +                         <--- Reply with length 1, byte 0x00 identifying TOC request returned 
 +[Fetch complete]                    
 +</code> 
 + 
 +=== Log settings access (Port 5 channel 1) === 
 +This channel is used to access the log settings to add/remove/append/start/stop logging. Once a logging block is added to the settings the data it is set to log will periodically be pushed from the Crazyflie to the Client. 
 + 
 +== Messages == 
 +The following messages are sent from the Client to the Crazyflie: 
 +^  **Request for**  ^  **Byte**  ^  **Description** 
 +|  Create a new log block                    | 0x00 (CONTROL_CREATE_BLOCK) for identifying the command to add a logging block  | 
 +|        :::                        |          | Desired block id  | 
 +|        :::                        |    2-31    | [[comm_protocol#variable_format|Variables to log]]  | 
 +|  Append to existing log block              | 0x01 (CONTROL_APPEND_BLOCK) for identifying the command to append logging variables to an existing block  | 
 +|        :::                        |          | Block ID | 
 +|        :::                        |    2-31    | [[comm_protocol#variable_format|Variables to log]]  | 
 +|  Delete a log block                        | 0x02 (CONTROL_DELETE_BLOCK) to identify the command for delete an existing block  | 
 +|        :::                        |          | Block ID | 
 +| Start a log block                          | 0x03 (CONTROL_START_BLOCK) to identify the command for enabling logging data from an existing block  | 
 +|        :::                        |          | Block ID | 
 +|        :::                        |          | Period in 10ms increments | 
 +|  Stop a log block                          | 0x04 (CONTROL_STOP_BLOCK) to identify the command for disabling logging data from an existing block  | 
 +|        :::                        |          | Block ID | 
 +|     Reset log                              | 0x05 (CONTROL_RESET) to identify the command to reset log. All blocks are stopped and deleted   | 
 + 
 + 
 + 
 +The response for all of the above messages is sent from the Crazyflie to the Client: 
 +^  **Byte**  ^  **Description** 
 +|          | The command byte that was sent  | 
 +|          | The ID of the log block that the command operated on (undefined for Reset log)  | 
 +|          | Return status for the command. If != 0 see [[comm_protocol#list_of_return_codes|list of return codes ]]  | 
 + 
 + 
 +== Variable format == 
 +The following format is used to identify a variable that is to be logged from the Crazyflie 
 + 
 +Log Type represents type of value sent in packet. Logged value is converted to this type before storing into log packet. Upper bytes will be discarded if Log Type is integer and value does not fit.  
 + 
 +^  **Log Type**  ^  **Description** 
 +|              | uint8_t       | 
 +|              | uint16_t      | 
 +|              | uint32_t      | 
 +|              | int8_t        | 
 +|              | int16_t       | 
 +|              | int32_t       | 
 +|              | float32       | 
 +|              | float16       | 
 + 
 +^  **Storage Type**  ^  **Description**                         ^ 
 +|                  | Variable in TOC                          | 
 +|                  | Memory address pointing to uint8_t       | 
 +|                  | Memory address pointing to uint16_t      | 
 +|                  | Memory address pointing to uint32_t      | 
 +|                  | Memory address pointing to int8_t        | 
 +|                  | Memory address pointing to int16_t       | 
 +|                  | Memory address pointing to int32_t       | 
 +|                  | Memory address pointing to float32       |
  
 +TOC variable request (Storage type is ignored, it is taken from Crazyflie internal TOC):
 <code> <code>
   7               0   7               0
 +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+
-            CHAR 0            |+Storage type  |    Log type   |
 +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+
-:   :   :   :   :   :   :   :   :+|        Variable ID            |
 +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+
-|             CHAR 30           |+</code> 
 + 
 +Arbitrary memory address request: 
 +<code> 
 +  7               0 
 ++---+---+---+---+---+---+---+---+ 
 + Storage type |    Log type   | 
 ++---+---+---+---+---+---+---+---+ 
 +|              0xff             
 ++---+---+---+---+---+---+---+---+ 
 +|        Mem Address 0          | 
 ++---+---+---+---+---+---+---+---+ 
 +|        Mem Address 1          | 
 ++---+---+---+---+---+---+---+---+ 
 +|        Mem Address 2          | 
 ++---+---+---+---+---+---+---+---+ 
 +|        Mem Address 3          |
 +---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+
 </code> </code>
  
-The contents of the buffer on the copter side is sent if any of the following is fulfulled: 
-  * The output buffer (of 31 bytes) is full 
-  * A "newline" character has to be send (\n and/or \r) 
-  * A flush command as been issued 
  
-==== Logging ==== +== List of return codes ( errno.h codes are used, returned values are positive ) == 
-The purpose of the logging is to be able to log variables that are available in the copter during runtime. The available variables are listed using a TOC that can be read from the copter. A logging block can then be registered consisting of there variables and the data will be sent periodically via CRTP to a receiver.+^  **Error**    ^  **Description** 
 +|  0            | No error  | 
 +|  ENOENT (2)   | Block or variable not found  | 
 +|  E2BIG (7)    | Log block is too long  | 
 +|  ENOEXEC (8)  | Unknown command received 
 +|  ENOMEM (12)  | No memory to allocate Log Block or Log Item  |
  
-| **Port**  | **Function**      | +== Usage == 
-|  (0,5,0)  | TOC access        | +Adding a new log block with id 0x0a with variable 0x55 type uint32_t and variable 0x66 type float32 to the settings: 
-|  (0,5,1)  | Block access      | +<code> 
-|  (0,5,2)  | Logging data      | +   Computer                 Crazyflie 
- +   --------                 --------- 
-=== TOC access === +Add log block      ---> 
-This port is used to access the loggable variables table of content. The first byte of the message is a message ID, three messages ID are defined:+                   <---     Log block added ok 
 +[DONE]
  
-| **Message ID** | **Meaning in upstream packets** | **Meaning in downstream packets** | +Packet structure: 
-|            0 | Reset TOC pointer             | Last TOC element                | +Computer -> CF : 0x[5,1] 0x00 0x0a 0x03 0x55 0x07 0x66 
-|            | Get next TOC element          | TOC element                     | +CF -> Computer 0x[5,1] 0x00 0x0a 0x00                    
-|            3 | Get TOC CRC32                 | TOC CRC32                       | +</code>
- +
-The upstream ID are commands and are sent alone. The downstream have the following formats:+
  
 +Deleting an existing log block 0x55 from the settings:
 <code> <code>
-Bytes                    1    Null terminated strings +   Computer                 Crazyflie 
-        +---+------------+------+----------+--------------+ +   --------                 --------- 
-        | 0 |            |      |          |              | +Delete log packet  ---> 
-        +---+   Var ID   | Type |  Group       Name     | +                   <---     Log packet deleted ok 
-        | 1 |            |      |          |              | +[DONE]                    
-        +---+------------+------+---+------+--------------+ + 
-        | 3 | Num. Vars  |  CRC32   | +Packet structure: 
-        +---+------------+----------+ +Computer -> CF : 0x[5,1] 0x02 0x55 
-Bytes                     4+CF -> Computer : 0x[5,1] 0x02 0x55 0x00
 </code> </code>
  
-The logging variables are sequentially requested by the receiver until the endWhen the last loggable variable is reached it has the ID 0 'Last TOC element'. The reset command permits to reset the TOC pointers so that the next sent TOC element will be the first oneThe Get CRC commands also returns the number of loggable variables.+=== Log data access (Port 5 channel 2) === 
 +Logging data is sent back to the copter as raw data with only the block idThis means that the lookup of what data is for what variable has to be done on the computer sideLog  
 +block header contains 3 byte timestamp, its value is number of microseconds from Crazyflie system start (it will wrap around in about 16 seconds. Is is also currently based on xTaskGetTickCount, so resolution is only 1ms). All values are sent in little endian format.
  
-The CRC32 is a hash of the copter's loggable variable TOC. The aim of having this is to be able to cache data on the receiver end and to quickly check the validity of it when connecting.+^  **Byte**  ^  **Description** 
 +|          | Block ID | 
 +|    1-3     | Block send time timestamp in us, LSB first  | 
 +|    4-31    | Logged variable values, LSB first  |
  
-=== Block access === +<code> 
-**TODO** +Adding a log block 0xbb at 100ms interval logging variable ID 0x55 of type uint16: 
-=== Logging data === +Computer -> CF : 0x[5,1] 0x00 0xbb 0x02 0x55       // create logging block 
-**TODO**+CF -> Computer : 0x[5,1] 0x00 0x55 0x00            // OK 
 +Computer -> CF : 0x[5,1] 0x03 0xbb 0x0A            // start sending block with 100ms interval 
 +CF -> Computer : 0x[5,1] 0x03 0x55 0x00            // OK
  
 +Receiving a log block 0xbb with timestamp 130.532s (0x1FDE4 ms), logging uint16 variable with value 0xBABE:
 +CF -> Computer : 0x[5,2] 0xBB 0xE4 0xFD 0x01 0xBE 0xBA
 +</code>
 ==== Parameters ==== ==== Parameters ====
-The parameters system aims at making accessible all the gettable and settable parameters of the copter. In the copter a table of parameters is hold and can be retrieved. In this table every parameter name is associated with an ID and a group name. Three ID are used to acces the TOC and the paramareters:+The parameters system makes all the gettable and settable parameters of the copter accessibleThe copter holds a table of parameters which can be retrieved. In this table every parameter name is associated with an ID and a group name. Three IDs are used to acces the TOC and the parameters:
  
-| **Port**  | **Function**      | +Port      ^ Channel     Function          ^ 
-|  (0,9,0)  | TOC access        | +|  2        |           | TOC access        | 
-|  (0,9,1)  | Parameter read    | +|  2        |           | Parameter read    | 
-|  (0,9,2)  | Parameter write   |+|  2        | 2           | Parameter write   |
  
-=== TOC access === +=== TOC access (Port 2 channel 0) === 
-This ports permits to access the parameters table of content. The first byte of the message is a message ID, three messages ID are defined:+These messages permit to access the parameters table of content. The first byte of the message is a message ID, three messages ID are defined:
  
-| **Message ID** | **Meaning in upstream packets** | **Meaning in downstream packets** | +^  Message ID  ^  Meaning in upstream packets  ^  Meaning in downstream packets      ^ 
-|            0 | Reset TOC pointer             | Last TOC element                +|            0 | Reset TOC pointer             | Last TOC element                    
-|            1 | Get next TOC element          | TOC element                     +|            1 | Get next TOC element          | TOC element (ID, type, group, name) 
-|            3 | Get TOC CRC32                 | TOC CRC32                       |+|            3 | Get TOC CRC32                 Number of parameters, TOC CRC32     |
  
-The upstream ID are commands and are sent alone. The downstream have the following formats:+The upstream ID are commands and are sent alone. The downstream has the following formats:
  
 <code> <code>
Line 155: Line 355:
 </code> </code>
  
-The parameters are sequentially requested by the PC until the end. When the last parameter is reached it has the ID 0 'Last TOC element'. The reset command permits to reset the TOC pointers so that the next sent TOC element will be the first one. The Get CRC commands also returns the number of parameters.+The parameters are sequentially requested by the PC until the end. When the last parameter is reached it has the ID 0 'Last TOC element'. The reset command permits to reset the TOC pointers so that the next sent TOC element will be the first one. The "Get TOC CRC" command also returns the number of parameters.
  
-The CRC32 is a hash of the copter TOC. This is aim at implementing caching of the TOC in the PC Utils to avoid fetching the full TOC each time the copter is connected.+The CRC32 is a hash of the copter TOC. This is aimed at implementing caching of the TOC in the PC Utils to avoid fetching the full TOC each time the copter is connected.
  
-=== Parameter read === +=== Parameter read (Port 2 channel 1) and parameter write (Port 2 channel 2)=== 
-**TODO** + 
-=== Parameter write === +The following messages are sent from the client to the Crazyflie: 
-**TODO**+^  **Request for**   ^  **Byte**  ^  **Description**  ^ 
 +|  Parameter read    |          | The id of the parameter to read   | 
 +|  Parameter write   |          | The id of the parameter to write   | 
 +|   :::              |          | The value to set | 
 + 
 +The following messages are sent from the Crazyflie to the client: 
 +^  **Reply for**             **Byte**  ^  **Description** 
 +|  Parameter read & write            | Parameter id      | 
 +|   :::                         1-n    | Parameter value   |
  
 === Fight control ==== === Fight control ====
 **TODO** **TODO**
 +
 +=== Link layer ===
 +| 3:15:                                   | Echo (profiling)  |
 +| 3:15:                                   | Source (profiling)|
 +| 3:15:                                   | Sink (profiling)  |
 +| 3:15:                                   | NULL packet       |
projects/crazyflie/firmware/comm_protocol.1344361988.txt.gz · Last modified: 2015-07-15 16:30 (external edit)