i DEVELOPING APPLICATIONS FOR LEGO MINDSTORMS NXT ROBOTS IN ANDROID A SUMMER INTERNSHIP REPORT Submitted by SOUVIK DAS DEPARTMENT OF ELECTRONICS AND COMUNICATION HERITAGE INSTITUTE OF TECHNOLOGY, KOLKATA-700107 Under the guidance of Dr. C S Kumar DEPARTMENT OF MECHANICAL ENGINEERING INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR JULY 2014
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
i
DEVELOPING APPLICATIONS FOR LEGO MINDSTORMS NXT ROBOTS
IN ANDROID
A SUMMER INTERNSHIP REPORT
Submitted by
SOUVIK DAS
DEPARTMENT OF ELECTRONICS AND COMUNICATION HERITAGE INSTITUTE OF TECHNOLOGY,
KOLKATA-700107
Under the guidance of
Dr. C S Kumar DEPARTMENT OF MECHANICAL ENGINEERING
INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
JULY 2014
ii
iii
ACKNOWLEDGEMENT I take great delight in expressing my deep felt gratitude to Prof. C S Kumar sir under whose guidance, I completed my summer internship. It was an honor and pleasure to work under him. I thank him for being patient and taking good care to see that I think and act in the right direction. I wholeheartedly thank Research Scholars-Roshan Kumar Hota, Deboshree, Nava Raja Poonam Anthony and Neha Jain for helping me throughout the project and making my summer internship a cakewalk. The kindness and concern they showed towards me is unforgettable. I would like to thank the Lab in charge – Kouhik Nag for their help and generosity. I thank my co-interns - R.Vishnu Vardhan, Teja Krishna, G.Vinod, Sunil Kumar Dutta and Sai Kaushik for being with me on and off times and making my intern days delightful. Finally, I thank Robotics and Intelligence Lab and CAD/CAM Lab, IIT Kharagpur for providing me with this oppurtunity to work and avail the facilities. I would also like to thank my parents for believing in me and have patience. I am highly indebted to my dear friends, Debanjan Lahiri and Samujjal Das for encouraging me and for letting me have this golden opportunity. SOUVIK DAS Department of Electronics and Communication Engineering, Heritage Institute of Technology, Kolkata-700107 [email protected]
iv
ABSTRACT
The report presents the work that has been carried out during the internship in
IIT Kharagpur. The study on using the android applications to control the robots is
done and previous works, different robotic platforms and available hardware and soft
wares are discussed briefly.
LABVIEW 2012 SP1 with MINDSTORMS NXT/EV3 extension is used to test
the sensors and actuators of NXT and the simple algorithms are implemented to make
some applications like Line Follower and Intelligent Vehicle for testing the NXT brick
and its functionality.
Finally, the attention is shifted to App Inventor 2 and its communication with
NXT Brick's Bluetooth module. A decent study is made on how the App Inventor sends
signals to the NXT over Bluetooth and started with developing simple applications to
communicate with NXT Bluetooth. Then, focused on using the sensors like
accelerometer, gyroscope and touch sensor that there in the android device. The
graphs of the sensor reading are generated. Finally, PID algorithm is implemented
using App Inventor 2 to build application like self-orientation robot and self-
balancing robot. These applications are tested on LEGO MINDSTORMS NXT and
results are documented.
Keywords: Android, LEGO MINDSTORMS NXT, App Inventor2, LabView, Control
systems
v
CONTENTS
Title Page i
Certificate ii
Acknowledgment iii
Abstract iv
Contents vi
List of Figures vii
List of Photographs viii
List of Abbreviations viii
CHAPTER 1: INTRODUCTION 1
CHAPTER 2: PROBLEM DESCRIPTION 1
CHAPTER 3: LITERATURE REVIEW 2
3.1 Related Previous Works
3.2 IOIO – An Alternative
3.2.1 Control Flow:
3.2.2 Modes of Communication
3.3 Lego Mindstorms NXT
3.3.1 PROGRAMMING LEGO MINDSTORMS NXT:
3.3.2 Connectivity and communication:
3.4 Lejos Mindstorms: NXJ Technology
3.5 App Inventor:
3.5.1 BUILDING THE USER INTERFACE
3.5.2 PROGRAMMING THE BEHAVIOUR
CHAPTER 4: ANDROID BASED ROBOTICS 10
4.1 Labview and Lego Mindstorms NXT
4.1.1 LINEFOLLOWER ROBOT
vi
4.1.2 INTELLIGENT VEHICLE
4.2 Android and Lego Mindstorms NXT
4.2.1 CONNECTION
4.2.2 TOUCH CONTROL
4.2.3 ACCELEROMETER CONTROLLED NXT
4.2.4 SELF-ORIENTATION ROBOT
4.2.5 SELF-BALANCING ROBOT
CHAPTER 5: METHODOLOGY 17
5.1 Programming the NXT with LabView
5.2 App Inventor 2 and Control Systems
CHAPTER 6: RESULTS AND DISCUSSION 25
CHAPTER 7: CONCLUSION AND FUTURE SCOPE 26
REFERENCES 27
APPENDIX I 28
OVERVIEW OF DIFFERENT SENSORS
Touch Sensor
Accelerometer
Gyro/Tilt Sensor
Light Sensor
Ultrasonic Sensor
APPENDIX II 31
LEGO COMMUNICATION PROTOCOL
APPENDIX III 33
PID Controller
APPENDIX IV 34
LABVIEW CODES
vii
Line Follower Robot
Intelligent Vehicle
APP INVENTOR 2
Bluetooth Connection
Lego Remote
Lego Accelerometer Remote
Self-Orientation Robot
Self-Balancing Robot
Pseudo Codes
LIST OF FIGURES
1. Fig 3.3 Block diagram
2. Fig 3.4 Modes of communication with remote server
3. Fig 3.6 Communication Block Diagram
4. Fig 4.3 AI2 Companion
5. Fig 4.4 Interface (a) Bluetooth is connected (b) Bluetooth is not connected
First, a provision for Bluetooth connection(a Bluetooth Client is provided for
the Android Device) is made. Then using App Inventor 2 Block Programming
interface the actual program is built. The outline of the algorithm followed is given
below:
- Establish Bluetooth connection
- Read the button
- Execute corresponding action
- Repeat the steps
The above algorithm is followed and implemented in App Inventor2(the programs are
given in Appendix IV Section). The Application is then tested on the Robot and is
concluded to be working fine.
5.2.2 Accelerometer Controlled NXT
The application uses the accelerometer of the Android Device to navigate the
robot. The Accelerometer Reading in Z-direction and Y-direction is used for giving
power to motors and enable steering. When the Tab is being held in landscape
orientation, inclination of the top of the Tab in downward sense gives forward motion
and in upward sense gives backward motion. If the Tab is inclined in left sense then it
moves the Robot in Anti-clockwise direction and vice-versa. A Samsung Galaxy Tab
is used to run the application.
20
The outline of the algorithm which is followed is given below:
- Read Y and Z values from the accelerometer
- Use Z value for powering the motors
- Use Y value for steering
- Set left motor power to the difference of Z and Y values
- Set right motor power to the sum of Z and Y values
- Repeat all steps
5.2.3 LEGO Orientation
In this application, the android device is a part of the robot.As has been mentioned earlier, this experiment uses the Gyro Sensor to orient itself to the given user-specified initial value. For this experiment Samsung Galaxy Tab 10.1 is used. The Tab is attached on the NXT Robot in Crab Chimera formation. The Application extracts raw data from the Orientation Sensor of the Tab and implements a P-Controller Algorithm to generate a power control. The error is calculated by the following formula:
error = final_pos - initial_pos ;
Wherefinal_pos is the current position of robot and initial_pos is the position specified by the user (the position at which the Robot will orient itself ultimately). The outline of the algorithm that is followed to implement the P-controller is given below:
- Get the azimuth value (which corresponds to direction) from user - Set it as the initial position - Read gyro sensor - Subtract initial value from reading to get the error - Multiply the error with proportional constant of P-Controller (P-gain) - Set the value to power - if the error is negative power the left motor, else power the right motor - Repeat all the steps
5.2.4 Two Wheeled Balancing(Segway) Robot using Android Device
The mobile is fixed to the robot, like has been done for the self-orientation
robot. The model (shown in the figure) has been chosen to make the Segway more
21
Fig 5.5Self-balancing Robot
stable. The main objective of choosing this model is to keep the center of mass just
above the axis of the wheel so that the net torque on the robot can be minimized.
Moreover the Android phone is attached with the robot in the front so the robot has a
natural tendency to fall in the forward direction. To cancel the effect certain changes
have been introduced in the algorithm.
PID Controller is implemented in App Inventor 2 which directly balances the
Segway. The phone is used as both the sensor and the processor to balance the Robot.
The accelerometer senses only the linear movements in three directions and gives
data. Thisdataisused to measure the roll and pitch angle. At this point the X, Y, and Z
readingsare pretty noisy so a Low-pass filter is applied to remove the short-term
fluctuations. The formula for a low-pass filter is pretty easy and it uses the previous
read values. The low pass filter implemented is given below,
𝑋𝑛 = 𝛼𝑋𝑛−1 + (1 − 𝛼)𝑋𝑛 (1)
After obtaining the raw values with less noise, roll and pitch values can be
computed. The formulae to calculate roll and pitch from raw accelerometer values
and derivation of the formulas are given below.
22
PITCH AND ROLL ESTIMATION
The orientation of the smartphone can be defined by its roll, pitch and yaw
rotationsfrom an initial position. The roll, pitch and yaw rotation matrices, which
transform a vector (such as the earth's gravitational field vector g) under a rotation of
the coordinate system of Figure by angles φin roll, θ in pitch and ψ in yaw about the
x, yand zaxes respectively, are:
Fig. 5.6 Coordinate system and rotation axes [16]
The raw values need to be converted to the g unit (1g = 9.8 m/s²), before applying the corresponding equations. The process of obtaining and converting the accelerometer readings depends on the accelerometer
𝐺𝑎𝑐𝑐 = 𝑅𝑎𝑤𝑎𝑐𝑐 × 𝑅𝑎𝑛𝑔𝑒2𝑟𝑒𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛−1
(2)
Now the sensor readings (converted to g unit) are,
𝐺𝑝 = �𝐺𝑝𝑥𝐺𝑝𝑦𝐺𝑝𝑧
� = 𝑅𝑔 = 𝑅 �001� (3)
R is the rotation matrix describing the orientation and ‘g’ is the earth’s gravitational
field matrix.
23
𝑅𝑥(𝜑) = �1 0 00 cos (𝜑) sin (𝜑)0 −sin (𝜑) cos (𝜑)
� (4)
𝑅𝑦(𝜃) = �cos (𝜃) 0 −sin (𝜃)
0 1 0sin (𝜃) 0 cos (𝜃)
� (5)
𝑅𝑧(ψ) = �cos (ψ) sin (ψ) 0−sin (ψ) cos (ψ) 0
0 0 1� (6)
There are six possible orderings of these three rotation matrices and, in
principle,all are equally valid. The rotation matrices do not, however, commute
meaning that the composite rotation matrix Rdepends on the order in which the roll,
pitch and yaw rotations are applied. It is instructive to compute the values of the six
possible composite rotation matrices Rand to determine their effect on the earth's
gravitational field of 1g initially aligned downwards along the z-axis
𝑅𝑥𝑦𝑧 �001� = 𝑅𝑥(𝜑)𝑅𝑦(𝜃)𝑅𝑧(ψ)�
001� (7)
After substituting (3), (4), (5) in (6), we get
𝑅𝑥𝑦𝑧 �001� = �
−sin (𝜃)cos (𝜃)sin (𝜑)cos (𝜃)cos (𝜑)
� (8)
Above equation can be written in the form of (7)
𝐺𝑝||𝐺𝑝||
= �−sin (θ)
cos (𝜃)sin (𝜑)cos (𝜃)cos (𝜑)
� (9)
1
�𝐺𝑝𝑥2+𝐺𝑝𝑦2+𝐺𝑝𝑧2�𝐺𝑝𝑥𝐺𝑝𝑦𝐺𝑝𝑧
� = �−sin (𝜃)
cos (𝜃)sin (𝜑)cos (𝜃)cos (𝜑)
� (10)
24
By solving equations (8) and (9) for pitch and roll, we get,
𝜑𝑥𝑦𝑧 = �−𝐺𝑝𝑦𝐺𝑝𝑧
� (11)
𝜃𝑥𝑦𝑧 = � 𝐺𝑝𝑥
�𝐺𝑝𝑦2+ 𝐺𝑝𝑧2� (12)
The mobile is fixed on the robot in landscape orientation. The error calculated
using the change in roll angle. This error is fed to the PID controller which sends the
control signals for the NXT brick to drive the motors in order to balance the robot.
The outline of the algorithm that is followed to implement the PID-controller is given below:
- Calibrate the sensor in stable position
- Read accelerometer and calculate pitch
- Calculate error by subtracting the calibrated value from the current value
- Calculate the difference term by subtracting the current error from previous
error
- Calculate the sum term by adding the current error previous sum
- Add all the three terms after multiplying error with P-gain, difference with
D-gain, sum with I-gain to get the power
- Repeat all the steps
The pseudo codes for all algorithms are given in appendix IV.
25
CHAPTER 6: RESULTS AND DISCUSSIONS
Android apps are developed using App Inventor to control LEGO MINDSTORMS
NXT robots. Two kinds of applications are made to control the robots:
1) Applications which control the robot remotely
2) The applications which need the mobile to be attached to the phone.
The robot is made to be remotely controlled by android applications viz. LEGO
Touch Control and LEGO Accelerometer Control successfully. The P controller is
successfully implemented in the android application for self-orientation robot. The
algorithm, to calculate the roll and pitch from raw values using rotation matrices and
low pass filter to reduce the noise, and PID controller are successfully implemented in
the android application. But it is partially successful when implemented in hardware.
The issues observed are noise in sensor readings, delay caused by the motors and
communication delay.
26
CHAPTER 7: CONCLUSION AND FUTURE WORK
All the android applications developed, are successfully implemented on
LEGO MINDSTORMS NXT platform. As mentioned, the self-balancing robot is not
successful on hardware. The noise in the sensor readings is reduced up to some extent
by implementing simple low pass filter. But, that did not suffice mobile application to
give stable control signals for the robot.
In future, Kalman filter will be implemented to get the accurate readings from the
sensor. As App Inventor 2 is drag and drop kind of programming, it does not
facilitates to code the advanced algorithms. In this context, LeJos would better serve
the purpose than App Inventor. In LeJos, as Java is used, many control algorithms can
effectively be implemented to make the systems robust.
The Bluetooth capabilities of the NXT brick have been used by connecting it with
PC or by connecting it to an Android Device. But the idea of Android Based Robotics
does not limit itself to establish a connection between the NXT platform and Android
Device. This can be extended and further carried out to establish a connection
between all Robotic Platforms available commercially and Android phone/tab.
27
REFERENCES
[1] Hans Peter Moravec: Robot definition http://www.britannica.com/EBchecked/topic/505818/robot
[2] Best of LEGO NXT Projects over Timehttp://www.intorobotics.com/tag/mindstorms-nxt/
[3] Oros N., Krichmar J.L. 2013. "Smartphone Based Robotics: Powerful, Flexible and Inexpensive Robots for Hobbyists, Educators, Students and Researchers." CECS Technical Report 13-16. November 26, 2013. Center for Embedded Computer Systems, University of California, Irvine.
[8] Appendix 1-LEGO MINDSTORMS NXT Communication protocol
[9] S. Goebel, et al., "Using the Android Platform to control Robots," in Proceedings of 2nd International Conference on Robotics in Education (RiE 2011), 2011, pp. INNOC - Austrian Society for Innovative Computer Sciences--142.
[10] http://www.lejos.org/nxj.php
[11] Sergio Sandoval-Reyes, Pedro Galicia-Galicia and Ivan Gutierrez-Sanchez “Visual Learning Environments for Computer Programming” -- 2011 Electronics, Robotics and Automotive Mechanics Conference
[12] Jesús M. Gómez-de-Gabriel, Anthony Mandow Jesús Fernández-Lozano, and Alfonso J. García-Cerezo -- “Using LEGO NXT Mobile Robots With LabVIEW for Undergraduate Courses on Mechatronics” , IEEE International Conference on Social Computing / IEEE International Conference on Privacy, Security, Risk and Trus