Lab 3.1: Control wifibot via Keyboard


The goal of this page is to understand the protocol that control the wifibot and using keyboard to send the command to maneuver wifibot to move around.

Serial communication

The control commands are sent via serial port (default: ttyS0) to the wifibot control board. We can write the script in c++ to send the command to certain serial port we want.

Following code is the basic structure for serial port writing and reading in c++:


wifibot Protocol

The wifibot receives the command via serial port and processes the command. You have to send 9 bytes command via serial port ttyS0 at 19200 baud rate. Following explanation is the detail for command:

Char 1 is 255
Char2 is size (here is 0x07)
Char 3-4 is the left speed 0 -> 240 tics max
Char 5-6 is the right speed 0 -> 240 tics max
Char 7 is the Left / Right speed command flag : Forward / Backward and speed control left & right ON / OFF.

Char 7 is decomposed as follow (1 byte char -> 8 bits):
(128) Bit 7 Left Side Closed Loop Speed control :: 1 -> ON / 0 -> OFF
(64) Bit 6 Left Side Forward / Backward speed flag :: 1 -> Forward / 0 -> Reverse
(32) Bit 5 Right Side Closed Loop Speed control :: 1 -> ON / 0 -> OFF
(16) Bit 4 Right Side Forward / Backward speed flag :: 1 -> Forward / 0 -> Reverse
(8) Bit 3 Relay 4 On/Off (DSUB15 POWER Pin 13 and 14)
(4) Bit 2 Relay 3 On/Off (DSUB15 POWER Pin 11 and 12)
(2) Bit 1 Relay 2 On/Off (DSUB15 POWER Pin 4 and 5)
(1) Bit 0 Relay 1 for Sensors. On/Off: 0 is OFF 1 is ON (DSUB15 POWER Pin 3)

Now we provide the more elicit explanation:

Char1 is 255
Char2 is 0x07
Char3 is 120  <—speed for left
Char4 is 00
Char5 is 120  <—speed for right
Char6 is 0
Char7 is 80(0+64+0+16)
Char8 – Char9 = CRC16(data)

The detail for Char7 is explained as follows:

  • Ex1: If we want car to move forward, we have to set both side forward, therefore, we have to set bit 6 and 4 as value 1 which result in Char7 as 01010000(binary)–>80(decimal). That is why Char7 is 80 in this case.
  • Ex2: If we want car to turn left, we have to set right side forward and left side backward, therefore we have to set bit 6 zero and bit 4 as one which result in Char7 as 00010000(binary)–>16(decimal). So you have to send Char7 as 16 is this case.
  • Ex3: If we want the car move forward and turn Relay1 (Hokuyo power) on, we can use: 01010001(binary)–>81(decimal).

Given the above example, We believe that you now understand the control command for wifibot. Below script provide the serial port communication and also CRC encryption function.

We are going to write a subscribe node to get the data from keyboard and then transfer the command via serial port to move wifibot. Before we implement all these things, we have to understand what topic the keyboard node send.

Install Keyboard Teleoperation Package

First install the keyboard teleoperation package in Ubuntu using following command:

We can use the package using following command, make sure you are running roscore.

Now lets look at the topic that is published by this teleop node:

Go back to the terminal window of teller node, then you can see the topic printed as following:


For more detail, lets look at the type of the topic using command:

Where it will return:

Therefore, we now understand that the type is geometry_msgs/Twist, we can write a subscribe node that query Twist type message.

Put serial communication, wifibot protocol and teleop together

Combining with previous serial function, wifibot protocol and subscribe node of Twist type topic. We can implement the function that using the keyboard send the command to control the wifibot in ROS.

Open a new terminal to create a new .cpp script for compiling the node:

Then past the following code in the script:

Do not forget to add following line in CMakeLists.txt:

And also do not forget to add dependency for geometry_msgs in manifest.xml:

Then compile the package using:

In the above script, the Crc16 function is CRC encryption which is necessary for wifibot protocol. The CRC encrypts the command from byte[1] to byte[6] (total 6 bytes excluding byte[0]) into 16 bits, then it is split into two bytes to be sent.

The cmdRCCallback function is the subscriber for Twist message, we get the values from keyboard turning them to four basic motions (go forward, go backward, turn left and turn right) to control the wifibot.

Play wifibot with keyboard

Now let us teleoperate the wifibot using keyboard:

In the first terminal run the roscore:

In second terminal:

In third terminal:

Now you can move the wifibot using your keyboard:

  • i: move forward
  • u: turn left
  • o:turn right
  • k:stop
  • <: move backward