Arduino and Kinect

Projects

Design, Build, Blow Their Minds

Enrique

Ramos

Melgar

Ciriaco Castro Diez with Przemek Jaworski

Apress*

Contents

Contents at

a

Glance

v

About the Authors

xvii

About the Technical Reviewer

xviii

Acknowledgments

xix

Introduction a

xx

Chapter 1: Arduino

Basics

1

What is Arduino?

2

A Brief

2

History of Arduino Installing Arduino

2

Installation

on

Mac OS X

2

Installation

on

Windows

3

Installation

on

Linux

3

Testing

the Arduino

3

Arduino Hardware Arduino

Input and Output Pins

Digital Pins Analog Input Pins Pull-Up Resistors Arduino Shields

Arduino IDE

7 ,

7 7 8

9 10

Serial Monitor Arduino

5

Language

11 12

ThesetupO Function

12

The

13

loopO Function

Variables

13

Variable Declaration and Initialization

13

Variable

14

Scope

vii

li

CONTENTS

15

Your First Arduino Project Breadboard

15

Building the Circuit Programming the Arduino

16

The

17 17

setupO Function

18

The loopO Function Circuit

18

Diagrams

Fritzing

18

Electronic Symbols

20

Electricity

20

AC/DC

21

Ohms Law

21

Joule's Law

21

Summary

22

m Chapter 2: Kinect Basics

23

A Brief History of the Kinect

25

Hacking the Kinect

25

Official Frameworks

25

The Kinect Sensor.....

.........

.—..

..

26

...

27

Positioning your Kinect Kinect RGB

Capabilities

27

Image

27

Image

28

Depth Map

28

IR

Hand and Skeleton

28

Tracking

Kinect Drivers and Frameworks

28

OpenKinect: Libfreenect Drivers

28

PrimeSense: OpenNI and NITE

29

OpenNI

29

NITE

30 30

Microsoft Kinect for Windows Kinect

Theory

Structured-Light 3D Scanning

viii

.

31 31

CONTENTS

Converting

the

Light Coding Image to

a

Depth Map

33

Kinect Alternative: ASUS Xtion PRO

33

Summary

34

Chapter 3: Processing.... Processing Language Installing Processing

35

Processing IDE

37

A First Processing Sketch

38

Processing Variables Variable Scope Structure of a Processing Sketch setupO Function drawO Function Processing Libraries Simple-OpenNI

38

M

Installing Simple-OpenNI Installation on Windows Installation in Mac OS X Installation on Linux

Accessing the Depth Map and RGB Image.—.—.

36

36

38 38 38

39 40 41 41

42 42 43

.—.......................44

The Third Dimension

46

Processing in 3D

46

Matrix Transforms

47

Camera Control Libraries

50

KinectOrbit Example

50

Kinect

Space

Linear and Two-Dimensional Arrays

52

54

Coloring the Point Cloud

55

NITE Functions

56

Hand Tracking Skeleton

Summary

Tracking

57 58

60

CONTENTS

m Chapter 4: Arduino and Kinect: "Hello World"

61

Parts List for Hello World

62

Serial Communication

62

Serial-Controlled LED

63 65

Pulse Width Modulation PWM-Controlled LED

Writing Your

65

Own Communication Protocol

Serial-Controlled LEDs

67

Kinect-Controlled LEDs

69

Feedback from Arduino

72

Summary

76

Chapter 5: Kinect Remote Control

77

Parts List for Kinect Remote Control

77

Hacking a Remote Control Connecting the Remote to Arduino

78

H

Assembling the Prototype

80 83

Shield

86

Testing the Circuit Kinect Hand

Tracking and

Gesture

Recognition

89

Libraries and Setup

89

NITE Callbacks

91

Draw

Loop and Other Functions

Connecting the Processing Sketch to Arduino Summary m Chapter 6: Kinect Networked Puppet The

X

67

Puppet

93 97 97 99

100

Servos

100

Building the Stage

103

Building the Puppet

105

Building the Circuit

108

Testing the Servos

113

Setting the Servos to the Starting Position

115

CONTENTS

H

Skeleton

118

Tracking On-Screen

119

Simple-OpenNI Events Angle Calculation

121

Network Communication

124

Communicating Within a Local Network Communicating over the Internet Server Applet: Sending the Angles over Client Applet: Controlling the Puppet

124 125 a

125

Network

127

130

Final Arduino Code

Summary



132

Chapter 7: Mood Lamps RGB Color Space

133

Arduino Nano

135

Building the

134

136

Circuit

139

Resistors

Understanding

140

Resistor Color Codes

142

Testing the Circuit XBee Wireless Module Arduino The

.145

.'.

149

Programming.

Lamp Class

Object-Oriented Programming

,

150 150

Class Declaration

151

Field

151

Description

Constructor.

152

Methods

152

User Control Sketch

154

Variable Declaration

154

SetupO Function

154

DrawO Function

155

User Control

157

Lamp Control Lamp Creation Data

Storage and Retrieval

157 159

160

xi

m CONTENTS

Serial Communication

161

Display Functions Simple-OpenNI Callbacks Summary

161 163 164

Chapter 8: Kinect-Driven Drawing Robot Building the Robot II

170

Parti

170

Part 2

171

Part 3

172

The Base

174

Building the Circuit

175

Testing the Circuit

176

Firmata and the Arduino

Library for Processing

Servo Test Robot Simulation

176 176

177

Angle Measuring Program

179

Robot Simulation Program

180

Driving the Physical Robot Kinect: Tangible Table Interface Calibrating the Point Cloud Rotation Equations Rotating the Point Cloud Point Cloud Filtering Finding the Finger Position

183

185 187

188 189 194 195

Virtual Robot Model

198

Polishing the Input The Drawing Robot in Action Summary

201

Chapter 9: Kinect Remote-Controlled Vehicles Electrical Motors and the H-Bridge Hacking a Car Building the Circuit M

xii

167

204 206 207

208 210

212

m CONTENTS

Testing the Circuit

216

Setup Function

218

Draw Function

218 220

DrawVector Function Car Function SendSerial Function Arduino

220 221

Testing Sketch Setup Function

222

Loop Function

222

Turning Functions

223

Move Functions

224

Enable/Disable Functions

Proximity Setup Function Loop Function Sensors

Move Functions

222

224 224 229 229

230

XBee and Wireless

230

Kinect RC Interface

232

Setup Function

234

Draw Function

235

Other Functions

236 241

Summary M

Chapter 10: Biometric Station

Hacking

a

Bathroom Scale

Seven-Segment LCD Hacking the LCD Acquiring the LCD Signal Reading the LCD Signal Sending the Signal to Processing

Decoding the LCD Signal Using the Weight Data Implementing the Biometric Recognition Imports and Variable Declaration Setup Function

243 244

245 246 249 250

254 256

259 261 261 262

xiii

*S

CONTENTS

User Interface

263

Draw Function

265

Additional Functions

266

New User

266

Updating User Data Updating User Height

267

Saving the User User Recognition

270

Reading and Drawing the Chart Graphic Output

273

Serial Event

276 276

268 270 275

Simple-OpenNI User Events

277

Summary m

Chapter 11:3D Modeling Interface

The Interface

280

Arduino LilyPad

282

Flex Sensors

.

283

Sewing the Circuit

284

Testing the Circuit

..287

Arduino Serial Sender

287

Processing Glove Data Receiver

287

Going Wireless

289

LilyPad XBee

289

SoftwareSerial Library

290

Wireless Arduino Code

290

Implementing the Modeling Interface The Glovelnterface Class

Setter Functions Functions

Display Calibrating

the Interface

Geometric Classes

xiv

279

292 292 293

294 295 296

Point Class

296

Line Class

298

Shape Class

298

IB

Modeling Interface Main Program Imports and Fields Setup Function

CONTENTS

299

299 300

Draw Function

301

Additional Functions

303

Processing Callbacks Simple-OpenNI Callbacks Summary

305

Chapter 12: Turntable The Theory Building a Turntable M

Scanner

Connecting the Gears Building the Circuit ArduinoCode

Processing Code

306 307 309 310 312 314 319 ...321 324

Variable Declaration

326

Setup and Draw Functions

327

Additional Functions

328

Exporting the Point Cloud The

exportPly Function

335 337

Surface Reconstruction in Meshlab

338

Summary

342

H

343

Chapter 13: Kinect-Controlled Delta Robot

About This Project

344

The Delta Robot

345

Building The

a

Delta Robot

345

Legs

345

The Base

348

The Effector

350

The

350

Gripper

Hanging the Robot

353

Building the Circuit

355

XV

CONTENTS

Delta Robot Simulation Software Inverse Kinematics

361

DeltaRobot Class

363

deltaLeg Class

368

Driving the Delta Robot Simulation with the Mouse Kinect-Controlled Delta Robot

xvi

361

371 372

Gripper Control

377

Sending the

380

Data to Arduino

Arduino Code

381

Summary

384

Index

385