Irobot Create 2 Tethered Driving Using Serial Communication and Python Nick Saxton April 2, 2015
Total Page:16
File Type:pdf, Size:1020Kb
iRobot Create 2 Tethered Driving Using Serial Communication and Python Nick Saxton April 2, 2015 Executive Summary: This application note will detail the necessary steps to write a simple Python script to control an iRobot Create 2 through serial port communication. Following an introduction to the topic and the technology used, a detailed walkthrough will be provided showing the reader how to write a user controlled tethered driving script. This walkthrough will start with a blank file and show all of the required steps to reach the final script. Additionally, automated driving code will be discussed, including comments on how it is used in the Automated 3D Environment Modeling project sponsored by Dr. Daniel Morris of the Michigan State University 3D Vision Lab1. Keywords: iRobot Create Tethered Driving Serial Communication2 Python Programming 1 Introduction: This section will introduce the technology and concepts used in this application note. First, an overview of the iRobot Create 2 Programmable Robot will be provided. Then, the Python programming language will be examined, including a brief history of the language and some of its key features. Finally, serial port communication will be discussed. Following this introduction, the objective of this application note will be looked at. The iRobot Create 2 Programmable Robot is a fully programmable version of iRobot’s popular Roomba vacuum cleaning robot. It weighs under 8 lbs, measures a little over 13 inches in diameter and approximately 3.5 inches tall, and has a serial port on its top side, accessible after removing the top cover. An image of the Create 2 can be seen below in Figure 1. Figure 1. iRobot Create 2 Programmable Robot The Create 2 is powered by a rechargeable battery. The battery can be charged from dead to full in approximately 3 hours. Two methods of charging are available, either the robot can be docked to the provided charging station or it can be charged in a more traditional way by plugging a power adapter into it. The battery powers multiple actuators on the robot, including wheel motors, a speaker, and LED lights. The most compelling feature of the Create 2 is the ability to communicate with it through the aforementioned serial port. A computer can be connected to the robot using a provided USB to serial port cable, providing two-way communication at TTL (0-5V) levels. Commands can be sent through this connection in compliance with the iRobot Roomba Open Interface. Certain serial port settings must be used in order to successfully communicate with the Create 2. First, the robot can communicate at two possible baud rates: 115200 or 19200. Additionally, each communication contains 8 data bits, 1 stop bit, and has no flow control or parity. 2 The Open Interface (OI) has four operating modes: off, passive, safe, and full. By default, the OI is in off mode. Therefore, whenever the robot has finished charging or the on button has been pressed, it enters this mode. If the Spot, Clean, or Seek Dock buttons are pressed, the robot enters passive mode. Passive mode allows for sensor data to be requested through the serial port, but no commands can be sent that changes the actuator parameters, such as wheel speed. On the other hand, safe mode provides full control of the robot through the serial port with a few exceptions. In safe mode, the forward cliff detection and wheel drop sensors are active, meaning that the robot will stop in situations that trigger those sensors, regardless of what commands have been provided to it. Additionally, the robot cannot be charged while in safe mode. If any of the previously mentioned exceptions occurs while in safe mode, the robot reverts to passive mode. The final mode of operation is full mode. Full mode works similarly to safe mode but with the previously mentioned exceptions deactivated. The OI provides the programmer with a multitude of commands that can be sent to the Create 2. These commands can be seen in detail by looking at the iRobot Create 2 Open Interface (OI) Specification sheet which is available on the iRobot website (link provided in the References section)2. Several of these commands will be used in the Procedure section of this document and will be given brief explanations at that time. These commands can be sent through the serial port in a few ways, most notably by either using a serial terminal such as Realterm (link in the References section)3 or by writing a program in a variety of languages. For the purposes of this application note, serial port communication is done using the Python programming language. The Python programming language is a general-purpose, high-level language that aims to be highly readable and simpler to use than other high-level languages such as C++ and Java. It is an interpreted language meaning that it can execute instructions directly as they are type line-by-line. It first appeared in 1991 and has grown to have a large community of developers in various fields of computing. Currently, the Python community is split between two major versions of the language: Python 2 and Python 3. Python 3 aimed to strip out many redundant ways of doing the same thing. An example of the classic “Hello, world!” program is shown below in Figure 2 in both Python 2 and Python 3. Figure 2. “Hello, world!” in Python 2 & 3 3 There are several key features that make Python a desirable language to use for our purposes. First, as previously mentioned, Python is a very readable language. There is minimal use of brackets, braces, and parentheses in Python. Additionally, indentation plays a big role in the language, which forces you into writing nicer looking code, while in a language like C++ you could technically write an entire program on one very long line. An example of the simplicity and readability can be seen below in Figure 3, which shows off how functions and for loops are written in Python. Another great feature of Python is its large collection of built-in libraries and functions. A link to the Python documentation pages can be found in the References section of this document4. The other feature that makes Python the perfect language for this application is the previously mentioned large community of developers. Because of this, there is plenty of great documentation out there offering code examples and tips on how to solve all kinds of problems with Python. Additionally, many easy to install libraries can be found including the library used in the Procedure section below, pySerial. Figure 3. Simple program in Python showing functions and for loops Before discussing the objective of this application note and walking through the procedure to achieve that objective, one more concept has to be examined: serial port communication. Serial communication is a commonly used was of sending data from one place to another whether over a bus or a USB-to-serial cable as is used in this application. In serial communication, as the name suggests, bits are sent one at a time sequentially. The other commonly used communication type is parallel in which multiple bits are sent simultaneously over multiple channels. While it may seem less intuitive to use serial instead of parallel communication, serial has several advantages over parallel. For one, serial links can be clocked at significantly higher speeds than parallel links, allowing for a higher data rate. This is due to a few factors, namely because serial links consist of only one channel there is no risk of clock skew. Additionally, again because of the single channel of serial, there is less chance of interference from surrounding components. Another major advantage of serial communication is that its infrastructure tends to be quite a bit cheaper than that required for parallel communication. There are a few important settings when dealing with serial port communication. First, the speed of the port can be changed to accommodate specific needs. As mentioned above, the iRobot serial port can handle both 115200 bits/sec and 19200 bits/sec (referred to as baud). Another setting is the number of data bits per communication. In this application, that number is set at 8 bits (or a byte) as is common in most modern serial communication applications. Additionally, one stop bit is used in the Create 2’s port to signal the end of a 4 single communication to or from the port. The last two important settings for our needs are parity and flow control. Parity allows for error detection and is not used in this application. Flow control is also not used in this application since no situations will arise in which communication would have to be paused and restarted. Objective: Now that the important concepts utilized in this application note have been introduced and discussed, the objective (or the final outcome of the Procedure section) can be detailed. Upon completion of the following walkthrough, you should have a Python program that provides manual, tethered control of an iRobot Create 2 robot. Commands that will be available to be sent to the robot are as follows: ● passive - causes Create 2 to enter passive mode ● safe - causes Create 2 to enter safe mode ● full - causes Create 2 to enter full mode ● clean - causes Create 2 to begin cleaning the area ● dock - causes Create 2 to find its charging dock ● reset - resets the Create 2, which changes the mode to off ● beep - causes Create 2 to produce beep sound from its speaker ● up/down - causes Create 2 to drive forward/backward ● left/right - causes Create 2 to rotate counterclockwise/clockwise The final program will run in a window, displaying the commands in the format that the Create 2 accepts.