Data between flex and the Linux serial port are exchanged
with a speed of 115200 baud in both directions. The stream
of data represents binary data packages or ascii information.
In the following we denote each byte by its hexadecimal
representation enclosed in quotes, e.g. "1B" for the
ascii escape. More formally
BYTE = "00" | "01" | .. | "FE" | "FF" .
BYTE2 = BYTE BYTE.
BYTE4 = BYTE2 BYTE2.

Binary packets:

PACKET   = "1B""02" HEADER CONTENTS CHECKSUM "1B" "03".
HEADER   = CLASS NUMBER SUBCLASS LENGTH.
CLASS    = BYTE.
NUMBER   = BYTE.
SUBCLASS = BYTE.
LENGTH   = BYTE.
CHECKSUM = BYTE.
CONTENTS = { BYTE}.


Byte stuffing
-------------

If HEADER, CONTENTS or CHECKSUM contain the special bytes
"1B" or "7C" they should be replaced by the following
sequences before sending the packet to the flex controller:
"1B" --> "1B" "00".
"7C" --> "7C" "1B" "01".

The inverse operation must unstuff the above mentioned
sequences if receiving data from flex.


Semantics of a packet
---------------------

The following semantics applies to every packet in the
unstuffed form:

LENGTH   = the number of bytes in CONTENTS
CHECKSUM = XOR of all bytes in CONTENTS
NUMBER   = some packets (e.g. motor data) have a running
           number for packets

CONTENTS (all CONTENTS is shown together with HEADER)

=================================================================
CLASS = "01"             -- packets related to the system status
  ---------------------------------------------------------------  
  SUBCLASS = "01"        -- battery and brake status

.............................................................
    Linux: 01 FF 01 00  -- request system status

.............................................................
    Flex : 01 FF 01 09
           BYTE4         -- timestamp
           BYTE4         -- battery voltage
           BYTE          -- brake status (0 = off, 1 = On)


=================================================================
CLASS = "02"             -- packets related to the motor system
  ---------------------------------------------------------------  
  SUBCLASS = "00"        -- ?

    .............................................................
    Linux: 02 51 00 01
           BYTE          -- ?

    Examples:
    Linux: 02 51 00 01 00
    Linux: 02 51 00 01 01


  ---------------------------------------------------------------  
  SUBCLASS = "07"        -- Linux: motor command
                         -- Flex: confirm motor command
   
    .............................................................
    Linux: 02 01 07 0E
           BYTE          -- axis (0 = linear, 1 = angular)
           BYTE4         -- velocity (positiv number)
           BYTE4         -- acceleration (positiv number)
           BYTE4         -- torque (positiv number)
           BYTE          -- direction (0 = backward/right, 
                         --            1 = forward/left)

    .............................................................
    Flex : 02 01 07 09 
           BYTE4         -- ?
           BYTE4         -- timestamp
           BYTE          -- axis

    Exmaple:
    Flex : 02 01 07 09 00 00 00 00 07 6F 50 10 00

  ---------------------------------------------------------------  
  SUBCLASS = "0A"        -- ?
 
    .............................................................
    Examples:
    Linux: 02 02 0A 00

  ---------------------------------------------------------------  
  SUBCLASS = "0C"        -- ?

    .............................................................
    Linux: 02 03 0C 00   -- ?

    Examples:
    Linux: 02 03 0C 00

    .............................................................
    Flex : 02 03 0C 08
           BYTE4
           BYTE4         -- timestamp

    Examples:    
    Flex :  02 03 0C 08 00 00 00 00 03 63 87 25

  ---------------------------------------------------------------  
  SUBCLASS = "21"        -- read motor
    Flex : 02 01 21 19
           BYTE4         -- ?
           BYTE4         -- timestamp
           BYTE          -- axis (0 = linear, 1 = angular)
           BYTE4         -- position (signed number)
           BYTE4         -- velocity (signed number)
           BYTE4         -- acceleration (signed number)
           BYTE4         -- torque (signed number)

  ---------------------------------------------------------------  
  SUBCLASS = "22"        -- start/stop sending motor packets (Linux)
                         -- confirm start/stop of motor (Flex)
    .............................................................
    Linux: 02 01 22 08
           BYTE4         -- timestamp
           BYTE4         -- start/stop info (0 = stop, 3 = start)

    Examples:
    Linux: 02 01 22 08 00 00 61 A8 00 00 00 03 -- start sending motor packets
    Linux: 02 01 22 08 00 00 61 A8 00 00 00 00 -- stop sending motor packets

    .............................................................
    Flex : 02 FF 22 08
           BYTE4         -- ?
           BYTE4         -- timestamp

    Flex : 02 FF 22 08 00 00 00 00 07 B5 36 38

=================================================================
CLASS = "03"             -- packets related to joystick
  ---------------------------------------------------------------  
  SUBCLASS = "00"        --

    .............................................................
    Linux: 03 65 00 00   -- request one packet with joystick data

    Examples:
    Linux: 03 65 00 00
    Linux: 03 51 00 00

    .............................................................
    Flex: 03 65 00 0D
           BYTE4         -- timestamp
           BYTE4         -- horizental move of joystick
           BYTE4         -- vertical position of joystick
           BYTE          -- buttons (1 - left button, 2 top button)

    Examples:
    Flex : 03 65 00 0D 0B A4 1B 05 FF FF FF FA 00 00 00 1A 00
    Flex : 03 65 00 0D 00 01 B5 6E 00 00 00 00 00 00 00 63 00 
    Flex : 03 65 00 0D 00 01 B5 E8 00 00 00 3A 00 00 00 63 00 

=================================================================
CLASS = "04"             -- packets related to the sonar system
  Sonars fire in fire groups. This groups are arranged in such a way
  that the sonars do not interfere with each other while firing.
  Therefore the cluster numbers on the ATRVJr are arranged in the
  following way
                ------- front ------
                   10 11 12 13 14
                05                25
                04                24
                03                23
                02                22
                01                21
                   00          20
                ------- rear -------

  ---------------------------------------------------------------  
  SUBCLASS = "00"        -- start/stop sending sonar packets (Linux)
                         -- confirm start/stop sonar packets (Flex)

    .............................................................
    Linux: 04 04 00 0D
           BYTE4         -- sonar frequency
           BYTE4         -- ?
           BYTE          -- ?
           BYTE4         -- start/stop info (0 = stop, 2 = start)

    Examples:
    Linux: 04 04 00 0D 00 00 75 30 00 00 00 00 00 00 00 00 02
           -- start sending sonar packets

    Linux: 04 04 00 0D 00 00 75 30 00 00 00 00 00 00 00 00 00
           -- stop sending sonar packets

    .............................................................
    Flex : 04 04 00 0D
           BYTE4         -- ?
           BYTE4         -- timestamp

    Examples:
    Flex : 04 FF 00 08 00 00 00 00 01 F1 F4 12
  ---------------------------------------------------------------  
  SUBCLASS = "02"

    .............................................................
    Linux: 04 NUMMER 02 LENGTH
           00 00 00 02
           BYTE4         -- timestamp
           { BYTE        -- cluster number
             BYTE2       -- sonar data for this sensor
                            if 7F FE then invalid senor readings
           }

    Examples:
    Flex : 04 6B 02 3B 00 00 00 02 0A 4D 9E C0 
           00 7F FE 01 7F FE 02 7F FE 03 7F FE 04 7F FE 05 7F FE 
           10 7F FE 11 7F FE 12 7F FE 13 7F FE 14 7F FE 
           20 7F FE 21 7F FE 22 7F FE 23 7F FE 24 7F FE 25 7F FE

    Flex:  04 ED 02 11 00 00 00 02 01 07 05 D6 
           03 04 EB 11 0A C8 23 01 A9


=================================================================
CLASS = "05"        -- packets related to the digital I/O system


  ---------------------------------------------------------------  
  SUBCLASS = "00"        -- initialyze digital I/O system

    .............................................................
    Examples:
    Linux: 05 05 00 04 00 00 27 10

  ---------------------------------------------------------------  
  SUBCLASS = "01"        -- flex sends changed bumpers

    .............................................................
    Examples:
    Flex : 05 BD 01 07 05 FC EA 92 40 00 00
    Flex : 05 BD 01 07 05 FC EA 92 40 00 01

  ---------------------------------------------------------------  
  SUBCLASS = "02"        -- check if I/O changed

    .............................................................
    Linux: 05 FF 02 04 
           BYTE4          -- timestamp

    Examples:
    Linux: 05 FF 02 04 08 93 67 60 -- request for update
    if nothing changed, number of packages (including this) is 1
    Flex:  05 FF 02 0C 00 00 00 00 08 98 CD 8F 00 00 00 01
                        ?  ?  ?  ?
                          timestamp *  *  *  *
                                          count *  *  *  *
  ---------------------------------------------------------------  
  SUBCLASS = "03"
    
    .............................................................
    Flex:  05 FF 03 07
           BYTE4          -- timestamp
           BYTE           -- position of byte which changed
           BYTE2          -- new value of changed byte (why 16 bit?)
          
    Examples:
    Flex:  05 FF 03 07 08 99 59 2E 40 00 01 -- Byte 40 changed to new value
                     number of byte *
                                value  *  * -- 01 is the front bumber, 02 the rear


=================================================================
Examples of startup and shutdown

Startup atrvjrserver
--------------------
Linux: 02 01 22 08 00 00 61 A8 00 00 00 03 -- start sending motor packets
Linux: 02 02 0A 00                         -- ?
Linux: 02 03 0C 00                         -- ?
Linux: 04 04 00 0D 00 00 75 30 00 00 00 00 00 00 00 00 02 -- start sending sonar packets
Linux: 05 05 00 04 00 00 27 10             -- start sending IO packets
Flex:  02 01 22 08 00 00 00 00 03 63 87 23 -- confirm: start sending motor packets
Flex:  02 03 0C 08 00 00 00 00 03 63 87 25 -- ?
Flex:  05 05 00 08 00 00 00 00 03 63 87 27 -- confirm: start sending IO packets

Shutdown atrvjrserver
---------------------
Linux: 04 04 00 0D 00 00 75 30 00 00 00 00 00 00 00 00 00 -- stop sending sonar packets
Linux: 02 01 22 08 00 00 61 A8 00 00 00 00 -- stop sending motor packets
Linux: 05 01 00 04 00 00 00 00             -- stop sending IO packets
Flex:  02 01 22 08 00 00 00 00 03 63 10 AB -- confirm: stop sending motor packets
Flex:  05 01 00 08 00 00 00 00 03 63 10 AC -- confirm: stop sending IO packets

=================================================================
Examples if accessed from mobility SW

setMotor(0.1, 0.0)
Linux: 02 01 07 0E 00 00 00 22 2A 00 03 0D 40 00 00 75 30 01
Linux: 02 01 07 0E 01 00 00 00 00 00 03 0D 40 00 00 9C 40 01
                       *  *  *  *  speed
                    *  which axis
                                                direction  *
setMotor(0.2, 0.0)
Linux: 02 01 07 0E 00 00 00 44 54 00 03 0D 40 00 00 75 30 01
Linux: 02 01 07 0E 01 00 00 00 00 00 03 0D 40 00 00 9C 40 01

setMotor(-0.1, 0.0)
Linux: 02 01 07 0E 00 00 00 22 2A 00 03 0D 40 00 00 75 30 00
Linux: 02 01 07 0E 01 00 00 00 00 00 03 0D 40 00 00 9C 40 01

setMotor(-1.0, -1.0)
Linux: 02 01 07 0E 00 00 01 55 A6 00 03 0D 40 00 00 75 30 00
Linux: 02 01 07 0E 01 00 00 8D B5 00 03 0D 40 00 00 9C 40 00