Friday, January 7, 2011

Arduino Takes Control (of RC Truck)

Recap: I'm entering the Sparkfun Autonomous Vehicle Competition (AVC) for 2011 (if they have it again), I bought an RC truck for the robot platform.  After playing with the truck a little, it was time to hand the controls over to an Arduino (aka ATmega328P on a breadboard)

This ElectrixRC stadium truck will soon become a robot...

How do you control an RC car with a microcontroller?  It's easy. The steering servo is controlled with servo pulses (surprising, no?) and so is the Electronic Speed Controller (ESC) aka throttle. The Arduino servo library makes coding a breeze.

Image from coastalplanes.com

Wiring is easy too.  The ESC and Servo both use standard 3-wire RC servo connectors.  The ESC has a 5V regulator (Battery Elimination Circuit or BEC). Wire the BEC's 5V and GND to power the Arduino, and wire the signal line to one of the digital pins. Then wire the steering servo's signal wire to another digital pin and connect the power and ground wires so the servo gets power.

After converting my Pololu SPI programmer to serial mode (Pokey and my FTDI programmer were on loan for the Thompson Robotics Expo) I modded some code and... voila:



Here's the code to control the car. Easy peasy. I used digital pins 9 and 10. Getting the range and center set correctly on the servo and throttle took quite a bit of trial and error.  You don't want to force the steering servo to full lock as that is hard on the servo.

// Steering/Throttle test - Michael Shimniok
// Based on Sweep by BARRAGAN  

#include <servo.h> 
 
// create servo object to control a servo 
// a maximum of eight servo objects can be created 
Servo steering;  
Servo throttle;

int ledPin = 13;
int pos = 0;    // variable to store the servo position 

#define CENTER 95
#define LEFT 55
#define RIGHT 130
 
void setup() 
{ 
  steering.attach(9);
  throttle.attach(10);  // attaches the servo on Arduino pin 10 
  
  steering.write(CENTER);
  throttle.write(90); // set mid throttle
} 
 
 
void loop() 
{
  throttle.write(85);
  digitalWrite(ledPin, HIGH);
  delay(250);
  throttle.write(90);
  digitalWrite(ledPin, LOW);
  delay(500);
  digitalWrite(ledPin, HIGH);
  delay(100);
  digitalWrite(ledPin, LOW);
  delay(100);
  sweep(CENTER, RIGHT, 1);
  sweep(RIGHT, CENTER, -1);
  sweep(CENTER, LEFT, -1);
  sweep(LEFT, CENTER, 1);
  digitalWrite(ledPin, HIGH);
  delay(100);
  digitalWrite(ledPin, LOW);
  delay(100);
}  

void sweep(unsigned char start, unsigned char stop, char inc)
{
  unsigned char pos;
  char i;

  for(pos = start; pos+inc != stop; pos += inc)
  { 
    steering.write(pos);             // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  }
} 

That's one small step of progress with a massive, overwhelming list of things left to do. But hey, every journey, however long and arduous, starts with a single step, right?

Friday, December 31, 2010

Connecting mbed to Wiimote IR Camera

Background

I got some very interesting and wonderful presents this Christmas (thanks Mom, Dad, and Father-in-Law!). So... what can you make with a microcontroller, a Wiimote, and a laser?  More on that another time, if I can pull it off. But here's how you can run a Wiimote IR camera with an mbed and track a laser dot (or other light sources) on your Windows, Mac, or Linux host.

mbed Microcontroller
Meanwhile, my parents very generously gave me an mbed microcontroller -- a powerful ARM Cortex M3 on a 40-dip board. Thanks Mom and Dad!! You can see it hooked up in its first, post-"hello world" experiment.


Wiimote
The Wiimote I planned to hack was purchased with a most generous gift card from my father in law (thanks, Mike!) Inside the Wiimote is an infrared camera that tracks infrared light point sources and reports their coordinates over I²C.

Credit

I'm following in the footsteps of great hackers, here. Here are the sources for the Wiimote IR Camera hacks:
Extracting the IR Camera

After disassembling the Wiimote, I desoldered the camera using solder braid with some extra flux paste. I soldered the camera's 8 thin leads onto a 2x4 pin header and soldered that onto a perfboard with 1x4 headers on either side so I could easily breadboard the thing.



Wiring the Camera to the mbed

Using the mbed, the interfacing is super simple.

The LPC1768 is a 3.3V device, eliminating all the voltage level translation hassle that the 5V Arduino owners had to endure.

And, since it's running at 96MHz, programmed one of the mbed's PWM signals to output an ~20MHz clock signal for the camera.  No crystal oscillator necessary.

The camera pinout is shown in the image below.


Here's a quick and dirty schematic. Note the pullup resistors on SCL, SDA, and RESET. In kako's and others' schematics, there's a 0.1uF capacitor between RESET and GND, probably to filter glitches.  So far I've gotten away with omitting the cap.

Wiimote to mbed wiring schematic. Click for large version.
Software

Microcontroller Software
I converted kako's source code to mbed C++ and added the ~20MHz clock signal on p21, and setup I2C communication on p9/p10.  The source code listing can be found here.

Here some sample output over the USB serial device while the camera tracks a laser. The camera tracks four light sources and provides x and y coordinates. The output from the microcontroller is a comma-separated list of x-y pairs: x1,y1,x2,y2,x3,y3,x4,y4

239,272,360,299,1023,1023,1023,1023
235,267,353,292,1023,1023,1023,1023
237,274,359,300,1023,1023,1023,1023
251,284,368,305,1023,1023,1023,1023
253,292,375,319,1023,1023,1023,1023
253,298,374,322,1023,1023,1023,1023
256,299,376,322,1023,1023,1023,1023
258,299,376,319,1023,1023,1023,1023
...

PC GUI Client Software
I wanted to visually display the camera's tracking data, so I wrote a Java Swing equivalent of the CC2's ATM 18 Project BlobTracker Front End software, since I couldn't get it to work. Well, the Java app works pretty well. Here is the source code for the Java app.



I mentioned the laser and used it here, but I have other plans.  I'm still working on it.  If I pull it off I'll be sure to post.

Happy New Year!

Sunday, December 26, 2010

Renaming an Applescript Xcode Project

To rename an Xcode AppleScript project, follow the steps below, based on my experience using Aleksandar Vacić's article. I needed to rename my serial communications AppleScript project. I simplified and clarified some of the steps in Aleksandar's article and made them specific to an AppleScript project.
  • Close the project in Xcode. 
  • Copy/rename the folder into new name
  • Open the new folder and rename the .applescript* and .xcodeproj files to the new name
  • Delete the build folder
  • Right click .xcodeproj and select Show package contents, which will reveal several files
  • Edit each of these files with a text editor (TextEdit, vi, Vim, Aquamacs, etc.) and replace all instances of the old name with the new name. In vi (or vim), type :.,$s/oldname/newname/g
  • Load the project file in Xcode, select  Clean all targets
  • Now build the project
_______
* You'll need to rename whatever your source code file is, like .sch as in Aleksandar's article.