Sunday, October 16, 2016

SeanBot 2.0 - smartphone web interface for lego tank robots

SeanBot

Two years ago I did the SeanBot robot which was the EV3D4 build but with ev3dev running under the covers and a basic web interface for driving.  The implementation was a bit clunky though as it used an apache web server to serve the web pages, images, etc but used a second web server for the REST API.  Despite being a bit clunky to get going I've seen several projects use the seanbot code to provide a web interface for their robot. pandaeatsbamboo was the most recent project to do so, they have a nice blog post on their build.

Since there were several projects using the seanbot code and since many of the basic lego mindstorms builds are tank style robots (EV3D4, TRACK3R, GRIPP3R, EV3RSTORM, ete are all tank robots) I decided to revamp the seanbot code to make it a little easier to use.  Here it is in action, details after the video.


Goals


  • Only run one web server
  • Implement the web server in python
  • Take advantage of the Tank class in ev3dev-lang-python's helper.py
  • Provide a desktop interface and a touch based smartphone interface
  • Make it easy for someone to modify the existing code for their own robot

Web Interface

The web server listens on port 8000 so go to http://x.x.x.x:8000/ where x.x.x.x is the IP address of your EV3.  The main interface will present you with a choice between a desktop interface or a mobile interface.


Desktop Interface

The desktop interface looks very similar to the original seanbot interface.  There are two sliders, one to control the speed of the medium motor and one to control the speed of the tank's movements.  This page supports touch events so you can use the desktop interface from your smartphone if you like.


Mobile Interface

The mobile interface uses a virtual joystick, if you drag the black dot towards the top of the gray circle the robot will move forward.  Drag it to the right to turn clockwise, drag it to the left to turn counter clockwise, etc.  

The closer the black dot is to the edge of the gray circle the faster the robot moves.


The Code


The code is available at ev3dev-lang-python in the demo/EV3D4 directory, just run EV3D4WebControl and off you go (I am assuming that you are running python-ev3dev 0.7.0 or later).  WebControlledTank lives in ev3dev/helper.py so take a look at that if you want to see the details on what is going on behind the scenes.

If you want your own tank class robot with a web interface you could do something similar to this:

#!/usr/bin/env python3

import logging
from ev3dev.auto import OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D
from ev3dev.helper import WebControlledTank, MediumMotor

log = logging.getLogger(__name__)

class MyTankRobot(WebControlledTank):

    def __init__(self, 
                 medium_motor=OUTPUT_A, 
                 left_motor=OUTPUT_C,
                 right_motor=OUTPUT_B):
        WebControlledTank.__init__(self, left_motor, right_motor)

        self.medium_motor = MediumMotor(medium_motor)
        self.medium_motor.reset()
        # code specific to your robot (sensors, etc) goes here

if __name__ == '__main__':
    mybot = MyTankRobot()
    mybot.main()




6 comments:

  1. Hi Daniel, thank you for sharing! It's really simple yet so useful for me as a beginner. It works on my PC browsers. However, I could not open the website on my iPhone. It always says "connection time out", do you know why? I have tried and googled everything but I still could not load the web.

    ReplyDelete
  2. Is your iPhone on the same network as your EV3 and PC?

    ReplyDelete
    Replies
    1. yes, it is on the same one. I used Chrome on my macbook and on my PC, both are working fine. I am wondering if it's the html that doesn't allow it, or the the server doesn't accept or open the port for the phone.

      Delete
  3. What if you use chrome on your iPhone? It shouldn’t make any difference though.

    ReplyDelete
  4. Finally I got it working on my phone. So, if I run the script from pc, my phone could not connect to the server (brick). But if I run the script from the brick, it turns out okaay. I thought they should work either way. But anyway, thank you for responses, pardon this newb. :p

    ReplyDelete