More than just bricks... http://thinkbricks.net LEGO MINDSTORMS EV3 robots, LEGO projects, building instructions and programming cool robots. Wed, 21 Jan 2015 14:38:20 +0000 en-GB hourly 1 Book review: Lego Mindstorms EV3 Essentials eBook http://thinkbricks.net/book-review-lego-mindstorms-ev3-essentials-ebook/ http://thinkbricks.net/book-review-lego-mindstorms-ev3-essentials-ebook/#comments Wed, 21 Jan 2015 14:38:20 +0000 http://thinkbricks.net/?p=1176

I received a review electronic copy of Packt Publishing’s ‘Lego Mindstorms EV3 Essentials’ book. This book covers Java and leJOS programming on the EV3, so it differs from most other books on the market in that it choose a 3rd party environment rather than relying on the Lego-supplied one. It also requires a Linux environment as you will be typing a lot of commands in the terminal. The review copy I received is in electronic format so I can’t comment on the final print quality.

The challenge with writing a book based on an evolving open-source program is that you will be forever behind the curve. Despite being a relatively new publication this book is based on the 0.8.1 release of leJOS, which has been superseded by the 0.9.0 release. The leJOS team are constantly improving the platform and making it easier to install and program, so this book has already fallen out-of-date.

Chapter 2 of the ebook opens with coverage of the Lego Mindstorms EV3, and a survey of the sensors, motors and EV3 programmable brick. While it is welcome to have this in the book the content is nothing new; multiple pictures of the major pieces with some descriptive text. You can get this from Lego’s official website, and spending the first 40 pages of the book covering it again was not needed.

In Chapter 3 the on-brick programming functions of the EV3 brick area covered. Now this is where I started to get confused; wasn’t this book meant to be about leJOS programming and more advanced topics? Chapter 3 spends 30 pages covering on-brick programming, before finally we get to…

Chapter 4: leJOS – at last! This chapter introduces the leJOS Java environment for the EV3. Now the first problem is that the instructions given here are very Linux specific, so if you have a Windows or Mac then you are out of luck. Don’t worry though; Windows users have a graphical installer available to them from the official lejos.org website.

Chapter 5 discusses various ways to connect to the EV3. If you have a wifi dongle your EV3 can connect to your wifi hub and then you can simply ssh into it (or better yet, connect via the Eclipse plugin). If you don’t have a wifi dongle then you can use the USB cable and ssh to 10.0.1.1, which is the address the EV3 appears as to your PC or Mac.

This chapter spends a lot of time discussing how to configure the wifi password on the EV3 by editing the /etc/wpa_supplicant.conf file. Now this is certainly the hard-core Linux way to solve this problem, but by far the easier approach is to use the built-in wifi configuration screen on the EV3 to type in your wifi passphrase and connect automatically. This is covered in better detail at the leJOS website.

Chapter 6: this chapter covers programming the EV3 in Java, and it’s where I have a big issues with the book. It leaps into a discussion of the Gradle Java build environment. However all of the guidance on the lejos.org website is around using Eclipse to do your development, with ant build files. There’s no problem in choosing an alternate build environment but I think that an introductory book should have just stuck to the standard IDE suggested by the leJOS project. Another problem in the text is that once you have compiled your program the book suggests that you remove the SDcard from the EV3 and insert it into the computer to manually copy the .class files over. But we just spent the whole last chapter configuring our wifi and setting up ssh keys to avoid this! Very confusing.

Chapter 7 covers the basic operation of moving the motor and using the touch sensor. This is so basic that it really doesn’t require a whole chapter; one program to make the motor go forwards and another to wait for a press of the touch sensor.

Chapter 8, 9 and 10 discuss building a line-follower robot (your read that right; three chapters to cover one of the most basic robots). I was disappointed to find that the code in this chapter re-invented the wheel. It built a very simple differential-drive line follower robot that used simple rotations to try and find the line. The logic of the line-follower program uses a narrow sweep to find the line and if that fails it performs a wider sweep. This is the classic single sensor approach to line-following.

The author spends 60 pages over three chapters creating the line-follower code. This is the longest treatment of a line-follower robot I have ever read. I would suggest instead presenting the final program first and then explaining the parts step-by-step. I found it very difficult to follow all of the modifications made to the source code over three chapters.

Avoiding re-inventing code is critical to bug-free programming, but the sample line-follower robot reinvents the DifferentialPilot class from leJOS. There is little point in choosing the leJOS environment if you are not going to show users how to get the most advantage from using the class library!

The code also failed to perform one of the most basic tasks of a line-following robot; calibrating the colour sensor to the line and non-line parts of the floor! It assumes that you can specify the colour of the line absolutely in the code, and there will be no interference from ambient light or noise. As anyone who has built a line-follower knows this is far from true!

So now we’ve built a basic line-follower robot and…the book is complete! Appendix A explains how to use Make in great detail (why?) and Appendix B gives two pages over to the various IDEs available for Java (again, why?)

This book does not contain building instructions, and it contains one programming project; a very simple line-following robot.

Conclusion: I’d avoid buying this book and instead use the tutorials available for free on the lejos.org website. It was very disappointing, spending more time on introductory concepts instead of creating advanced projects in Java and leJOS.

]]>
http://thinkbricks.net/book-review-lego-mindstorms-ev3-essentials-ebook/feed/ 0
Red Racer – a power functions racing car http://thinkbricks.net/red-racer-a-power-functions-racing-car/ http://thinkbricks.net/red-racer-a-power-functions-racing-car/#comments Sun, 21 Dec 2014 17:53:15 +0000 http://thinkbricks.net/?p=1139

Sometimes it’s fun to just build something different for a change. Red Racer is my version of Sunsky’s lovely Snow White Racing Machine  which I updated to use the new power functions servo for steering and medium motor for drive. I also styled it in red.

I came across the Show White racing machine a few years ago and had saved it for a future date to build. I really liked the clean and simple lines and compact design of the original, and decided to take advantage of the Christmas holiday to build my own version.

My first challenge was that I didn’t have any of the steering parts used in the original racer! Time for some thinking…if I was going to change how the steering worked I may as well upgrade the steering to use the new power functions servo motor.

Luckily Sunsky’s post on the original design had presented detailed disassembly instructions so I was able to build an initial version of the racer, and then modify it to use the new Power Functions motor and servo. I had to widen the chassis by one stud so that everything would fit, and increase the length too so the servo would fit.

The original car was styled in white, but I chose red as I did not have enough white pieces. One thing I learned from this build; I’ve a lot to learn about designing bodies for cars!

2014-12-20 23.39.36 HDR 2014-12-20 23.46.19 HDR 2014-12-20 23.46.08 HDR 2014-12-20 23.45.55 HDR 2014-12-20 23.45.35 HDR 2014-12-20 23.45.17 HDR 2014-12-20 23.45.05 HDR 2014-12-20 23.45.00 HDR 2014-12-20 23.44.43

]]>
http://thinkbricks.net/red-racer-a-power-functions-racing-car/feed/ 0
Review: The Lego Mindstorms EV3 Idea Book http://thinkbricks.net/review-the-lego-mindstorms-ev3-idea-book/ http://thinkbricks.net/review-the-lego-mindstorms-ev3-idea-book/#comments Sat, 13 Dec 2014 23:20:27 +0000 http://thinkbricks.net/?p=1067 EV3-IB_cover-front_newYoshihito Isogawa is well known for his excellent Technic Idea book series, and now he is back with an idea book targeted for the EV3. The ideas are as imaginative as ever, captured in Isogawa-san’s signature style of close-up images. Read on for my review of The Lego Mindstorms EV3 Idea Book from NoStarch Press…

The first thing to note about this book, unlike many other Mindstorms EV3 publications, is that it is not about building robots. In a way this makes Yoshihito’s book unique in that it focuses on techniques you can incorporate into your own creations instead of presenting you with amazing fully realised robots. The book is also not about EV3 programming, in fact most of the designs in the book require nothing more than a motor and an on-off switch!

Who is this book for?

It’s for anyone who wants to move beyond the models in the EV3 Home set and try to build their own robots. You may be frustrated at how complex it is to build using studless Lego beams. Or you may wonder how some of the advanced robots you see on other websites work. Those builders have mastered the art of taking simple building techniques and using them at scale. Once you learn the same basic building techniques you can apply them over and over again in your creations. This book will start you on the road to learning those building techniques.

Contents

So what is in the book? Well you are in for a treat; 181 different builds ranging from simple gear ratios to walking robots using only the parts in the retail Lego Mindstorms EV3 31313 set.

The book is broken into 6 broad topic areas:

  1. Basic mechanisms: gets you up and running with gear ratios, worm drives, changing the angle of rotation, reciprocating mechanisms and other techniques.
  2. Vehicles: now we start to move with various ways of building wheeled and tracked vehicles.
  3. Moving without wheels: ditch the wheel and walk using legs!
  4. Arms, Wing and other Movements: if you want to adorn your robot with droppers, flapping wings or interesting movements this section presents some ideas. I particularly like the ‘lifting things’ section that presents designs for simple forklift and scissor jack lifting mechanisms that could be built into any robot.
  5. Using sensors: this section is specific to the EV3 sensors and shows how to trigger them using different mechanisms. If you ever had the original Constructopedia from the first Mindstorms RIS set then this will bring back memories!
  6. Something extra: this section shows how to join beams at an angle using the Pythagorean theorem, and how to build a stand for an iPad.

Some sample images

These images give you a sense for the quality of the artwork in the book. Each of these models is constructed in multiple steps, but the book does not give traditional Lego step-by-step building instructions. You will need to do a little detective work to figure out how to build each model from the parts in the EV3 kit, but don’t worry, each model is shown from multiple angles and tricky steps are explained clearly.

EV3IB_sample-model_3_largeEV3IB_sample-model_2_largeEV3IB_sample-model_1_largeEV3IB_sample-model_5_large

The production quality of the book is very high. I really like the new wide-format and glossy colours that NoStarch are using for this book, and the previous two books from Sariel.

2014-12-07 10.55.40

Conclusion

This is a must-have book for any aspiring robot-builder to have on their bookshelf. You will find yourself returning to it over and again to look up building techniques. I have only had my copy for a week and it’s already very well thumbed!

]]>
http://thinkbricks.net/review-the-lego-mindstorms-ev3-idea-book/feed/ 0
‘Incredible Lego Technic’ book review http://thinkbricks.net/incredible-lego-technic-book-review/ http://thinkbricks.net/incredible-lego-technic-book-review/#comments Wed, 19 Nov 2014 16:07:46 +0000 http://thinkbricks.net/?p=1023

I was fortunate enough to receive a review copy of Sariel’s latest book titled Incredible Lego Technic and published by NoStarch Press. Unlike Sariel’s previous book this is a compendium of amazing and inspiring Lego Technic models by builders around the world. You won’t find detailed building instructions here, instead each model is presented in stunning colour with a description of the model and some history behind the original design.

One thing I really liked about this book was the lay-flat binding allowed the book to stay open on the desk as I read. This is a book where you can dip in and out and read about models that interest you, or pick a specific model and spend some time studying it. The production values are very high, with the technical advisor Eric Albrecht (aka Blackbird) ensuring that each model is well presented.

2014-11-15 14.33.06

Cover of book

Lay-flat binding makes reading easy

Lay-flat binding makes reading easy

Models

The models are split into sections by theme. There’s something here for everyone, whether you like earth movers or supercars!

  1. Agricultural – tractors and other farm equipment
  2. Aircraft
  3. Automotive
  4. Construction equipment
  5. Miscellaneous
  6. Motorbikes
  7. Supercars
  8. Tracked Vehicles
  9. Trucks
  10. Watercraft

One improvement I would suggest is to include a link to a webpage or Brickshelf gallery for each model so that you can read more once you find something that interests you.

High resolution images

Here are some samples from the book in high-resolution so you can see the details of each model.

]]>
http://thinkbricks.net/incredible-lego-technic-book-review/feed/ 0
Photos of EV3 Space Invaders model build http://thinkbricks.net/photos-of-space-invaders-model/ http://thinkbricks.net/photos-of-space-invaders-model/#comments Sun, 13 Jul 2014 15:49:21 +0000 http://thinkbricks.net/?p=1013

A blog reader asked if I had created building instructions for my Lego EV3 Space Invaders model. Unfortunately I never did create them before taking the model apart. But I did the next best thing; I took pictures as I took it apart! The Space Invaders model is broken down into 3 modules; the coin reader, the frame holding the EV3 and the joystick module. They snap together into the final assembly.

Take a look at these pictures and see what improvements you can come up with – I’m sure there are many!

Apologies for the pictures – I snapped these with my phone so the quality is so-so.

2014-06-13 20.02.29 HDR 2014-06-13 20.02.19 HDR 2014-06-13 20.02.10 HDR 2014-06-13 20.01.59 HDR 2014-06-13 20.01.53 HDR 2014-06-13 20.01.52 2014-06-13 20.01.44 HDR 2014-06-13 20.01.32 HDR 2014-06-13 20.01.31 2014-06-13 20.01.19 HDR 2014-06-13 20.01.07 2014-06-13 20.00.55 HDR 2014-06-13 20.00.38 HDR 2014-06-13 20.00.14 2014-06-13 20.00.04 2014-06-13 19.59.51 2014-06-13 20.00.29 HDR
]]>
http://thinkbricks.net/photos-of-space-invaders-model/feed/ 0
Review: Lego Mindstorms EV3 discovery book http://thinkbricks.net/review-lego-mindstorms-ev3-discovery-book/ http://thinkbricks.net/review-lego-mindstorms-ev3-discovery-book/#comments Mon, 07 Jul 2014 19:40:10 +0000 http://thinkbricks.net/?p=972

I was very excited to receive a copy of Laurens Valk’s latest book; The Lego Mindstorms EV3 Discovery Book published by NoStarch Press. You may know Laurens from his previous Lego Mindstorms titles, and if you own any of his previous works you’ll know he puts considerable time and attention into his books . This edition is targeted at the new Mindstorms EV3 kit, and is of the same high quality.

I decided to jump in at the end and build one of the more advanced models; an autonomous roving grabber arm. The lift and grab mechanism on this robot has to be seen in action to be believed – a single motor closes the grabber and raises the arm in one motion. Laurens has done a good job describing how the mechanism works as it is complex. The building instructions, like the rest of the book, and printed in high-quality 4-colour and are some of the best I’ve seen in a book that did not come from Lego.

2014-06-14 11.37.50 2014-06-14 11.37.28 EV3DB_070-071_web EV3DB_146-147_web

What else is in the book? Laurens covers the basics of the EV3 set, and focuses on programming in the graphical language provided by Lego. Along the way he makes the concepts introduced more concrete by demonstrating how they make different robots work. I like how at the end of each chapter there are suggestions for ways to try more advance approaches to make your robot do more.

What about the robots? I’m delighted to see that Laurens has provided building instructions for six (yes 6!) new robots that haven’t been seen before. These are:

  1. The EXPLOR3R, a wheeled vehicle that uses sensors to navigate around a room and follow lines
  2. The FORMULA EV3 RACE CAR, a streamlined remote-controlled race car
  3. ANTY, a six-legged walking creature that adapts its behavior to its surroundings
  4. SK3TCHBOT, a robot that lets you play games on the EV3 screen
  5. The SNATCH3R, a robotic arm that can autonomously find, grab, lift, and move the infrared beacon
  6. LAVA R3X, a humanoid robot that walks and talks

This is a must-have book if you want to learn more about building and programming EV3 robots. Nice work Laurens!

]]>
http://thinkbricks.net/review-lego-mindstorms-ev3-discovery-book/feed/ 0
Space Invaders written in Java for the LEGO Mindstorms EV3 http://thinkbricks.net/space-invaders-written-in-java-for-the-lego-mindstorms-ev3/ http://thinkbricks.net/space-invaders-written-in-java-for-the-lego-mindstorms-ev3/#comments Tue, 27 May 2014 02:05:32 +0000 http://thinkbricks.net/?p=935

Space Invaders! The classic arcade game is back, and this time built using a single Lego Mindstorms EV3 kit.

I decide to write this game in leJOS using the 0.8.1-beta release (see the leJOS wiki for more on getting it installed). I contemplated writing it in the EV3 graphical language but the thought of coding the invader movements made me cry, so Java it is!

What’s does this little game have in it? A coin reader that waits for you to put a coin in the slot before you can play the game (just like in the arcade), a theme tune playing in the background, realistic space invader movements, a joystick using an EV3 large servo motor and invaders that fire at you as you shot at them.

Screenshots

Invaders Insert Coininvaders2

Graphics and Sounds

leJOS supports simple graphics using the Image class. An Image is a series of bytes that describe a graphics object on the screen. Unfortunately you can’t take a standard graphics format such as PNG or JPG and display it on the EV3 screen. The EV3 uses a LEGO image format and the leJOS code expects the image to display to be in this format. However the EV3Control tool provided with leJOS 0.8.1-beta allows you to convert between a bitmap file and the EV3 image format.

I used Paintbrush to create the original graphics for my Space Invaders. Paintbrush is a basic image drawing program for Mac OS, which suits my needs perfectly. Each invader is a 20×20 pixel bitmap which I draw by hand. The player’s ship is also a 20×20 pixel bitmap file. I then used EV3Control to convert the bitmaps into EV3 images. These files are available in this ZIP file: ImageFiles

  • Invader bitmap files. Two versions for each move of the invader: one and two.
  • Ship bitmap file
  • Explosion bitmap.
  • Invader EV3 image file.
  • Ship EV3 image file.
  • Explosion image file.

I made a little theme tune to play while the game waits for you to enter a coin. I composed this using Garage Band on my iPad and then downsampled it into an 8-bit WAV file to play on the EV3. Sorry about the quality. Download it here: Theme2

You will need to copy the EV3 image files and the sound file into the same directory holding the Space Invaders jar file on the EV3 using scp. This would require you to use the Terminal and type the following commands. I assume you have your EV3 connected via a USB cable, if you use wifi then replace 10.0.1.1 with the IP address of your EV3 shown on the display. The root password on the EV3 is r00tme.

scp Theme2.m4a root@10.0.1.1:/home/lejos/programs
scp invader1.lni root@10.0.1.1:/home/lejos/programs
scp invader2.lni root@10.0.1.1:/home/lejos/programs
scp ship.lni root@10.0.1.1:/home/lejos/programs
scp explosion.lni root@10.0.1.1:/home/lejos/programs

Program Structure

The code is broken down into a number of class each of which implement a basic function. I make heavy use of Java threads to keep everything moving in parallel.

  • Sprite: the Sprite class represents an image on the screen at an x,y position. Each Sprite class holds a single image and can render it onto the screen, erase it or move it. The Sprite class is used by the Ship and Invader classes to represent the graphics on the screen.
  • Score: the Score class represents the player’s score, number of lives left and ammo remaining. The score class runs as a thread to update the score on the screen.
  • Invader: represents a single space invader on the screen. An invader has an x,y position and can move sideways and down. The invader understands that when it reaches the edge of the screen it has to change directions and move down a line.
  • Invaders: this class represents all of the invaders currently left in the game. The invaders are stored in an ArrayList of Invader class objects. Each invader is placed along a grid at its starting position when the constructor is called. Each time the invaders move I iterate over this list and tell each invader to move itself on the screen. If an invader is shot down then I simply delete it from the ArrayList and it disappears off the screen. The invaders run on their own thread so that they keep moving in the background.
  • Ship: the player’s ship. This class handles reading from the joystick to control the ship movement and firing missiles at the invaders. It also runs as a thread so the player can always move the ship.
  • Missile: a single missile fired by either a player or an invader. The direction of travel is controlled by the movingUp boolean variable set when the missile is created. A missile is just a 3×3 rectangle on the screen as it is faster to draw than an image. A missile runs as a thread so that it continues to move until it either goes off the edge of the screen or intersects with another sprite.
  • Missiles: represents all of the missiles currently fired in the game. The key feature of this class is to run a thread to periodically check which missiles have hit which objects. If a missile has hit an invader then the player scores a point. If the missile has hit the ship then the player loses a life.
  • SpaceInvaders: the main class that starts everything running.

How to play

Here’s how to play the Space Invaders game:

  1. Install leJOS for the EV3 on an SD card and boot your EV3.
  2. Copy the class files into a new project, or just grab the SpaceInvaders.jar.
  3. Copy the image files into /home/lejos/programs
  4. Run Space Invaders
  5. Insert a coin!
  6. Move the joystick left/right to move the ship and the touch sensor fires missiles.
  7. You start with 50 ammo so use it wisely!
  8. The invaders will be shooting at you so dodge their bullets while firing your missiles.
  9. Remember the lower down the screen the invaders get the faster they move!

Downloads

  • All of the image files as a zip: ImageFiles
  • The compiled Space Invaders executable jar file ready to run on the EV3. Compressed as a ZIP file. SpaceInvaders.jar
  • The Space invaders theme tune in wav format compressed to 8 bit for the EV3. Groovy! Theme2
  • If you prefer you can download and import the SpaceInvaders Eclipse project and import it into your workspace in Eclipse. SpaceInvaders

Images

2014-05-26 14.58.472014-05-26 14.59.10 HDR 2014-05-26 14.58.57 2014-05-26 15.01.19 2014-05-26 15.00.46 2014-05-26 15.00.38 2014-05-26 15.00.26 2014-05-26 15.00.19 2014-05-26 14.59.43 2014-05-26 14.59.10

]]>
http://thinkbricks.net/space-invaders-written-in-java-for-the-lego-mindstorms-ev3/feed/ 9
Chopp3r – a Lego Mindstorms EV3 helicopter http://thinkbricks.net/chopp3r-a-lego-mindstorms-ev3-helicopter/ http://thinkbricks.net/chopp3r-a-lego-mindstorms-ev3-helicopter/#comments Thu, 20 Mar 2014 23:12:08 +0000 http://thinkbricks.net/?p=887

CHOPP3R is a search-and-rescue helicopter made from a single 31313 Lego Mindstorms EV3 set. I built this as a potential model for a book, but as the book has not materialised I decided to publish it instead! CHOPP3R has working top and rear rotors driven my a medium motor. It searches for a liferaft containing the beacon using the IR sensor mounted under the cockpit. There is a winch mounted on the side to lower a rescue rope that is controlled using the up/down buttons on the EV3. I’m particularly pleased with this model as it looks realistic and yet only uses a single 31313 EV3 set to build. It just shows you that a single set can give you a lot of flexibility.

The Chopp3r.ev3 control program for CHOPP3R is broken into four parts.

  1. Monitor the touch sensor. If it is pressed then toggle the boolean variable flying
  2. If flying is true then turn on the rotor motor, otherwise turn it off.
  3. If flying is true then get the distance reading from the IR sensor and use that to compute the delay between beeps.
  4. Monitor the up/down buttons and power the winch motor.

To use this model all you need to do is:

  1. Press the top button on the IR beacon to put it into beacon mode; the green LED in the IR beacon should illuminate.
  2. Download and run the program on the EV3.
  3. Press the touch sensor to start the rotors.
  4. As the chopper gets closer to the IR beacon the beeps will get progressively quicker.

I made building instructions for this model available as a RescueChopper.pdf. Be warned; it’s a 21MB file so best to right-click and Save As… before viewing.

ERRATA: There is a mistake in the building instruction PDF at step 25. Two parts are missing to hold the EV3 onto the Chopper body. You need to attach a Technic Beam 3 x 3.8 x 7 Liftarm Bent 45 Double (part 32009) to both sides of the EV3. See the pictures in the gallery below for details.

CABLES: Connect the cables as follows:

  • Motor A: small motor driving the rotors
  • Motor D: winch motor
  • Port 1: Ultrasonic sensor
  • Port 4: Touch sensor

 

]]>
http://thinkbricks.net/chopp3r-a-lego-mindstorms-ev3-helicopter/feed/ 18
Control your EV3 robot with a web browser in lejos 0.5.0 http://thinkbricks.net/control-your-robot-with-a-web-browser-in-lejos-0-5-0-alpha/ http://thinkbricks.net/control-your-robot-with-a-web-browser-in-lejos-0-5-0-alpha/#comments Sun, 05 Jan 2014 17:05:01 +0000 http://thinkbricks.net/?p=870

This program that allows you to control a robot over the internet (or at least your home wifi) from a web browser. The EV3 leJOS 0.5.0 release makes it trivial to write internet-aware programs on the EV3 as it supports the full Java networking library. I used the NanoHTTPD Java class which packages a simple web server in one Java file. Read on!

An EV3 with a wifi adapter brings the power of internet-accessible robots to your Lego set. Once the EV3 is connected to your wifi network you can write leJOS programs that use the Java networking libraries to communicate with any host. One practical application is to run a web server on your EV3 brick and link it to a leJOS program to control your robot. Now you can control your robot from any internet-connected device; a phone, a tablet or your PC.

Writing a web server is not a trivial task; while the HTTP protocol is very simple, but there are a lot of corner-cases to consider to get a web server that will work with every browser. Luckily there are many freely available Java implementations of web servers available. I chose the NanoHTTPD implementation; it packages a fully functional web-server in a single Java class file which is perfect to incorporate into your leJOS programs. I modified the web server code to intercept button presses for a simple web page GUI to control the direction and movement of the robot.

ASIDE: The Apache web server is a very powerful freely available package for Linux computers, and powers many of the web sites on the Internet. But Apache is overkill for a home-robotics web server. NanoHTTPD is far better suited to running on a device like the EV3.

NewImage

What’s involved?

There are a few steps involved to getting the web server code set up on the EV3, but once you’ve done this once you can skip these steps and jump straight to step 12 to start the web server each time.

  1. Insert a wifi adapter in your EV3.
  2. Power up the EV3 and make note of the IP address assigned (let’s call it YOURIPADDRESS)
  3. Import the NanoWebserver project into Eclipse.
  4. Modify the IP address in the web server_build.xml file ev3.host property to the IP address from step 2.
  5. Build a differential-drive robot with the motors on ports A and D of the EV3.
  6. Right-click on the web server_build.xml file, select Run As… Ant Build.
  7. The code should build and upload to your EV3.
  8. Download the www.tar.gz file below.
  9. Open your Terminal (on a Mac) or use a file transfer program on a PC to copy the www.tar.gz file to the EV3 in the directory /home/lejos/programs. For example, on a Mac you would type scp www.tar.gz root@YOURIPADDRESS:/home/lejos/programs
  10. Log into the EV3 using ssh and cd to /home/lejos/programs
  11. Extract the www.tar.gz file: tar xzvf www.tar.gz. This should create a www directory containing the index.html and png image files for the buttons on the web page.
  12. In a terminal window on your EV3 type: cd /home/lejos/programs; jrun -jar SimpleWebServer.jar -h YOURIPADDRESS -d /home/lejos/programs/www
  13. You will see the web server starting up; in takes about 20 second to initialise.
  14. Go to a web browser on your PC and in the address field enter YOURIPADDRESS. You should see the web page displayed. If not check the debug output on the command terminal from the EV3.
  15. Click the buttons to make your robot move!

Setting up a web server on your EV3

The NanoHTTPD server comes with a sample web server program that can run unmodified on the EV3. You will need an EV3 with a wifi adapter – be aware that the IP address of the wifi adapter will likely change each time you restart your EV3.

Web server Eclipse Project

I packaged my Eclipse project in a zip file (NanoWebserver.zip) which is broken down into these class files:

  • DebugServer.java (optional) – a simple web server that returns a web page containing the header and parameter details of the HTTP request. Very handy for debugging.
  • HelloServer.java (optional) – a Hello World server that generates the HTML code and a form in Java.
  • InternalRewrite.java (required) – rewriting rules used by the NanoHTTPD.
  • NanoHTTPD.java (required) – the core class for handling the HTTP protocol.
  • ServerRunner.java (required) – used by NanoHTTPD.
  • SimpleWebServer (required) – the main web server class. This is the program you will run on the EV3. I added the handleParameters() method to control the robot (see below for details).
  • WebServerPlugin.java (required) – used by NanoHTTPD.
  • WebServerPluginInfo.java (required) – used by NanoHTTPD.

I created three build files as follows:

  • debugserver_build.xml – builds the DebugServer.java to a jar file named DebugServer.jar and copies it onto the EV3 in the directory /home/lejos/programs. It will show up in the Programs menu in leJOS on your EV3, or you can run it from the command line.
  • helloserver_build.xml – builds the HelloServer.java class to a jar file named HelloServer.jar.
  • webserver_build.xml – builds the SimpleWebServer.java class to a jar file named SimpleWebServer.jar. This is the file we will use to control the robot.

You can download the NanoWebserver.zip file above and then unzip it and import it into Eclipse to get started.

Creating a web page

I created a simple web page to display the button icons. It is named index.html and looks as follows:

<html>
<head><title>LEGO EV3 Robot Control</title>
<script> 
function action(type) {
var xmlhttp;
if (window.XMLHttpRequest) {
 // code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 } else {
 // code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
xmlhttp.open("GET","index.html?action="+type,true);
xmlhttp.send();
}
</script>
<noscript>Sorry, your browser does not support JavaScript!</noscript>
</head>
<body> 
<div style="text-align:center"> 
<h1>Lego EV3 Robot Control</h1> 
<button type="button" onclick="action('up')"><img src="up.png"></button>
<br>
<button type="button" onclick="action('left')"><img src="left.png"></button>
<button type="button" onclick="action('stop')"><img src="close.png"></button>
<button type="button" onclick="action('right')"><img src="right.png"></button>
<br>
<button type="button" onclick="action('back')"><img src="down.png"></button>
<br>
<button type="button" onclick="action('plus')"><img src="plus.png"></button>
<button type="button" onclick="action('minus')"><img src="minus.png"></button>
</div>
<hr>
2014 Mark Crosbie <a href="http://thinkbricks.net">http://thinkbricks.net</a>
</body>
</html>

The web page refers to the png files which I downloaded from IconFinder.com. The web contents will live in /home/lejos/programs/www on the EV3 (though you can move it to another location). I’ve packaged the www directory as a gzip’d tar file: www.tar.gz. Copy this file onto your EV3 in /home/lejos/programs and untar it there.

Handling the web requests

The modification I made to the SimpleWebServer.java class is to add a handleParameters() method that intercepts the HTTP request and extracts two parameters; action and speed. The action parameter corresponds to the name of the button pressed (e.g. “up” or “plus”) and the speed parameter is used to set the motor speed.

// Handle the parameters passed to the request to see if we control the robot
void handleParameters(IHTTPSession session, Map<String, String> header, Map<String, String> parms) {

   String uri = session.getUri();
   System.out.println(session.getMethod() + " '" + uri + "' ");

   if(parms.containsKey("speed")) {
       speed = Integer.parseInt(parms.get("speed"));
       System.out.println("Speed set to " + speed);
   }

   // see if the action and speed parameters are given
   if(parms.containsKey("action")) {
      String action = parms.get("action");
      System.out.println("Action = " + action);

      leftMotor.setSpeed(speed);
      rightMotor.setSpeed(speed);

      switch(action) {
        case "up":
        leftMotor.forward();
        rightMotor.forward();
        break;

        case "back":
        leftMotor.backward();
        rightMotor.backward();
        break;

        case "left":
        leftMotor.backward();
        rightMotor.forward();
        break;

        case "right":
        leftMotor.forward();
        rightMotor.backward();
        break;

        case "stop":
        leftMotor.stop();
        rightMotor.stop();
        break;

        case "plus":
        speed += 60;
        if(speed > 1440)
        speed = 1440;

        leftMotor.setSpeed(speed);
        rightMotor.setSpeed(speed);
        System.out.println("Speed is now " + speed);
        break;

        case "minus":
        speed -= 60;
        if(speed <= 60)
          speed = 60;

        leftMotor.setSpeed(speed);
        rightMotor.setSpeed(speed);
        System.out.println("Speed is now " + speed);
        break;

        default: System.out.println("Unknown");
      }
   }
}

The code is not very complicated – it takes the header and parameters from the HTTP session and looks for the value of the “action” parameter if it is set.

]]>
http://thinkbricks.net/control-your-robot-with-a-web-browser-in-lejos-0-5-0-alpha/feed/ 6
Mindsensors PFMate and SumoEyes with EV3 in leJOS http://thinkbricks.net/mindsensors-pfmate-and-sumoeyes-with-ev3-in-lejos/ http://thinkbricks.net/mindsensors-pfmate-and-sumoeyes-with-ev3-in-lejos/#comments Wed, 01 Jan 2014 23:00:03 +0000 http://thinkbricks.net/?p=860 This fun little robot is a test bed to see if the Mindsensors PFMate and SumoEyes will work with leJOS on the EV3. Good news is that both sensors work perfectly with the current 0.5.0-alpha build of leJOS!

2014 01 01 13 48 58

The Mindsensors PFMate allows you to control Lego Technic Power Functions Motors by sending IR commands to the power functions receiver. Each receiver has two motor output ports (labeled A and B in the code) and a receiver can be listening on one of four channels (1-4). Thus you can independently control up to 8 Power Functions motors from a single PFMate which should accommodate up to the largest robots. LeJOS for the NXT provided a PFMate class that allowed you to communicate with the PFMate in Java. I tested this class in the 0.5.0-alpha build of leJOS for EV3 and it works perfectly.

NOTE: Despite what is shown on the Mindsensors website the PFMate will work with leJOS!

I also tested the Mindsensors SumoEyes on the EV3. The SumoEyes is a three zone obstacle detector that tells you if an obstacle was detected to the left, right or centre of the centre. The detection is not very accurate, but its handy to have a single sensor that takes the place of two or more Ultrasonic sensors on a single port. leJOS provides the SumoEyesSensor class to interface with the SumoEyes. I discovered that on the EV3 the value returned from the SumoEyes for “No Obstacle” is 1022 and not 1023 as in the class code. 

2014 01 01 13 49 27

The code is presented as four class files in this handy zip FirstRover.zip which you can unzip and import directly into Eclipse. I used a subsumption class which allows me to build incremental behaviours in layers in the robot code. It’s  good way to incrementally test and develop your code. 

  • PFRover.java – the main rover Java class file that starts the subsumption arbitrator running.
  • Driving.java – the subsumption behaviour for driving the robot.
  • CollisionDetection.java – the subsumption behaviour for detecting collisions using the SumoEyes. 
  • ButtonWatch.java – a subsumption behaviour to detect collisions using the SumoEyes.
]]>
http://thinkbricks.net/mindsensors-pfmate-and-sumoeyes-with-ev3-in-lejos/feed/ 0