Step 3: Arduino and PC Programming

To program the Arduino, you'll obviously need the Arduino software and a USB cable.  You can also program the Arduino using a serial port and a TTL level converter if your PC has a serial port.  Note that the USB serial interface will not communicate with the Arduino's ATMega processor if there is a level converter connected to Arduino's serial pins (pins 0 and 1) so disconnect it before using USB.

On the Arduino we will need a serial interface that allows the PC to control the motors.  We will also need a PWM servo drive system to send the correct signals to the Vex motors and make sure they go in the correct directions when given the right values.  I also added some simple LED flashing, mainly for status indication but also because it looks cool.

On the PC we will need to open the serial port and send frames of data that the Arduino program will understand.  The PC also needs to come up with motor values.  An easy way to do this is to use a USB game pad or joystick, I'm using an Xbox 360 controller.  Another option is to use a networked computer (either a netbook or a small mini ITX board) on the robot itself to drive wirelessly.  With a netbook, you can even use the onboard webcam to stream back a video feed and drive your robot remotely.  I used the Linux sockets system to do network programming for my setup.  One program (the "joystick server") is run on a separate PC that has a controller plugged into it, and another program (the "client" ) is run on the netbook connected to the Arduino.  This links the two computers and sends joystick information to the netbook, which then sends out serial packets to the Arduino that drives the robot.

To connect to the Arduino using a Linux PC (in C++) you must first open the serial port at the correct baud rate and then send the values using a protocol that you have also used on the Arduino's code.  My serial format is simple and effective.  I use 4 bytes per "frame" to send the two motor speeds (each is a single byte).  The first and last bytes are hard-coded values that are used to keep the Arduino from sending the wrong byte to the PWM code and causing the motors to go crazy.  This is the primary purpose of the RGB LED, it flashes red when the serial frame was incomplete.  The 4 bytes are as follows:

255 (hard coded "start" byte), , , 200 (hard coded "end" byte)

To ensure reliable reception of the data, make sure you put enough delay between program loops.  If you run your PC code too fast, it will flood the port and the Arduino may start dropping or even mis-reading bytes.  Even if it doesn't drop information it can also overflow the Arduino's serial port buffer.

For the Vex motors, I used the Arduino Servo library.  Since Vex motors are just continuous rotation motors, they use the exact same signaling that servos use.  However, instead of 90 degrees being the center point, it is the stop point where the motor does not spin.  Lowering the "angle" causes the motor to start spinning in one direction, while raising the angle makes it spin in the other direction.  The farther away from the center point you are, the faster the motor will spin.  While it is not going to break anything if you send values greater than 180 degrees to the motors, I would advise limiting the values from 0 to 180 degrees (which in this case are speed increments).  Because I wanted more control and less out of control robot driving, I added a software "speed limit" to my program that doesn't allow the speed to increase above 30 "degrees" in either direction (range is 90 +/- 30).  I plan on adding a serial port command that changes the speed limit, so that the computer can remove the limit on the fly if you want to go fast (I've been testing in small rooms so I don't want it to speed up and crash into the wall, especially with a netbook on it).

For more information, download the attached code at the end of this Instructable.

Very nice! I've been tinkering with a VEX tank robot I converted to Arduino myself.. (Since (a) the VEX 1.0 programmer software went crassh with an old laptop, and (b) Innovation-FIRST doesn't seem to want to activate 1.0 anymore, and 2.0 costs $99 still.) Someone out there posted a hack of the VEX RC receiver, which shows the wave pattern, and how it can be decoded.. (timing, via a.. Yep! Arduino.) The VEX parts have a slightly different timing, compared to standard servos or constant-rotation servos, but the servo.h library is easily modifiable. (not too far off, but a servo.Write 90, seems to leave a little movement on the motor modules.) I powered my servos and motors through a 7805 regulator, from a +12V battery (which also powers the Arduino via the coaxial socket.. Word of the wise, NEVER draw +12V through the v-In pin of the arduino. a short circuit could fry the trace.) <br>
<p>Adding a new note.. The protocol used by the RF module, from the RC remote, is a serial PWM stream. Someone found that there is a 'Start' time, followed by 0-255 ms pulses for each of the joystick positions, and either a solid or no signal points for the buttons on the back.. Wish I could find the page, someone posted on how to decode with a Oscilloscope.</p>
You could use a USB hub, they are el chepo very most.
Very interesting. I might be able to get hold of an EeePC 701 cheap, which I think should be sufficient for this project. It has built in webcam, and runs linux. I would love to see an update to this project. I am a total noob though. Anyone got any suggestions on where I should go to read up on Arduinos?<br><br>PS - any chance of putting some code in there that causes the robot to stop when wifi drops out?
btw nice dr pepper can on step 4
great guide! Im gonna get an arduino for christmas probbably. I cant wait for that
Wow... Literally about 5 minutes ago I read your comment about that breadboard on SparkFun then stumbled upon this. I love crazy coincidences like this
When I started playing with Arduinos I did the same thing as you - I devised a control protocol for serial communication and then controlled everything from my PC. It worked well, but eventually someone told me about the Firmata protocol. A library for it ships with the the Arduino IDE. The Firmata protocol is intended to be a standard for this type of device-to-device communication. I found the Firmata to be a little difficult to use at first so ended up building a library of my own that implemented the PC-side of the protocol. I prefer to do my PC coding with C# so the library is Windows-only at the moment, but I'll create a Mono-compatible version soon so that it'll work on Linux as well. Check it out if you're interested: <a href="http://rhyduino.codeplex.com" rel="nofollow">http://rhyduino.codeplex.com</a>.
I've heard of Firmata before but it seems fairly complex for something this simple. It is designed to be able to control any part of the Arduino's I/O system from the PC and has lots of different commands. A simple protocol like this is easier to code for if you just need to drive motors. Plus, I don't know if Firmata supports configuring the Arduino's PWM outputs for servos. As for Linux programming, I like pure C++, it is a bit more work but doesn't rely on any proprietary systems and the code is fairly portable. I like GTK+ for graphical interfaces because it is cross-platform, it did not take long to port a graphical interface I wrote in Linux for my fan controller project to Windows and it worked great on both platforms.
&nbsp;Nice instructable &nbsp;! But I can't see the links for the PC program on step 3 :( &nbsp;<br /> Could you please re-upload it ? Thanks !
To be honest I&nbsp;never really finished this Instructable, it still needs some work and I never got around to uploading the code.&nbsp; I'll post a zip file with the code soon.&nbsp; There are a few things, there's the Arduino code, a program to drive it in Linux using a joystick, and a client/server set of applications (also for Linux) that let you drive the robot (controlled by a netbook)&nbsp;using another PC.&nbsp; Streaming a video feed isn't covered in my code, you just run a streaming program like VLC&nbsp;alongside the drive code to get video.<br />
Thanks !&nbsp;

About This Instructable



Bio: I finally graduated from Missouri University of Science and Technology (Missouri S&T, formerly University of Missouri Rolla) with a computer engineering degree. Originally from ... More »
More by CalcProgrammer1:Cheap and Easy Tachometer (RPM Sensor) for Brushed DC Motors Cheap Home Automation using Wireless Outlet Modules ATTiny USI I2C Introduction - A powerful, fast, and convenient communication interface for your ATTiny projects! 
Add instructable to: