Arduino Vader “Force Grip” remote car (a.k.a. smartphone virtual wheel)

The 3-wheel car is built with the Makeblock Robot Starter Kit. (Plus some Legos for style.) It has a Bluetooth module. It is controlled by the phone’s gyros via Bluetooth. You drive it like a steering wheel with two hands, or like Darth Vader with one hand.

1. Code for the smartphone below. I saved this as gyroCar.py and transferred it to my phone (LG Optimus Dynamic). Then I execute/run it using SL4A from the phone itself:

If you use the code above, the part that you’ll need to tweak for your own build is the DroidGyroJoystick class. Particularly, the pitch/roll numbers my phone gives lying on one edge may be different from yours. The other things you might want to change are the tuning parameters. For example, I configured a “deadzone” with accelZero and turnZero so that small motions near “centered” position won’t count as joystick inputs.

(If you’re new to SL4A, I show how to get set up, as well as where the android.py in “import android” above comes from, here.)

2. Arduino sketch for the robot car below. Sorry it’s unnecessarily complicated. It’s because it’s code for a 2-in-1 IR or Bluetooth + obstacle-avoidance car. If you connect only an IR sensor to Port 6, it will be IR-controlled. If you connect only a Bluetooth module to Port 7, it will be Bluetooth-controlled. Why did I do such a thing? Because I wanted to be able to switch remotes for my kids just by swapping modules, without uploading sketches.

Anyway, what’s important here is that in Bluetooth mode, it recognizes my silly custom command set (fwd, left, right, bwd, etc). These command strings are how the SL4A Python script on the phone instructs the car what to do, over Bluetooth. The other useful thing is that the Bluetooth commands let me specify the speed, in addition to direction. So the more you push your joystick forward, the faster it’ll go.

P.S. I just installed the WordPress “Crayon Syntax Highlighter” plugin. It prettifies blocks of code within ‹pre› tags, like above. I like it so far.

Teaser: Makeblock Robot Starter Kit Cellbot + Sensors + shxtty Path Tracking

Well this was fun. Ingredients:

  • Makeblock Bluetooth module
    To execute instructions from smartphone.

  • LG Optimus Dynamic
    Strapped on robot for sensors. WiFi to talk with Mission Control. Bluetooth to control robot.

  • Tornado server
    Runs on PC. Connects to phone via SL4A. Talks with web UI using websockets.

  • Websockets
    To stream sensor data from phone to web UI. Also relays commands from web UI to robot.

  • Web UI
    Basic webpage with Javascript to handle keyboard events and draw things

  • D3.js
    For rendering the accelerometer G-meter (not shown in video)

  • HTML Canvas
    I just used a 2-D grid and rectangles (fillRect) to draw the robot’s snail trail.

  • Physics
    v = d/t; d = v*t
    Estimate distance traveled by robot using motor “velocity” and time.

  • Ye Olde Trigonometry
    To convert bearing + distance traveled to x-y position in room.

You know, this could make a good school project. Lots of applied basic science, math and programming.

I promise to put up details and code in a later post 🙂

(If I forget, please email aaron@secretsciencelab.com and I’ll share everything with you.)

How to build a Makeblock Bluetooth/WiFi Cellbot car

Ingredients:
– LG Optimus Dynamic
– PC game controller (joystick)
– pygame for joystick input
– python script runs on PC to translate joystick input to movement commands
– SL4A to send movement commands from PC to smartphone over WiFi
– smartphone sends movement commands to Arduino via Bluetooth (also SL4A)
– Arduino translates commands to movement

First, we taught Bao to listen with Bluetooth

That was using pyserial, connecting directly from PC to Arduino via COM17.

I used that to build a basic instruction set to control movement. E.g.,:
!fwd 10
!bwd 5
!left 3
!right 10
!fwdleft 10 5
!bwdright 6 6
!stop

Those instructions are parsed and executed on the Arduino. The numbers tell how much the joystick was deflected.

But it wasn’t very interesting to connect directly from PC to robot via Bluetooth. I wanted to strap my smartphone on to add that extra layer of sensors and computing power. So my scheme was for my PC to talk to my smartphone and for the smartphone to be like the Jewel Wasp, controlling the robot via Bluetooth.

This was the main Python code running on my PC:

And this was what I had on the Arduino:

The main changes were to handle commands sent via Bluetooth. Bytes were read one at a time and accumulated into a buffer. The buffer was parsed for commands and the commands were executed. I had to tune the vectors a little to take advantage of the analog joystick input and to smooth out transitions to/from any direction.

I rigged the smartphone onto the robot with Legos. Then I downloaded and streamed the robot’s POV using the “IP Webcam” app because why not?

It’s ALIVE! Robot POV

Pilot POV

It worked better than I expected. The controller lag was not too bad, even with the inefficient command string processing scheme.

Tee hee

When I was done playing I restored it back to the old IR + Ultrasonic setup. I still haven’t figured out what’s the best next step from IR to Bluetooth for a kid.