Wednesday, July 28, 2010

Materials-The electronics

As noted in the initial post, I already have some (most) of the electronics required. First of all, some basic ideas of how the project is going to be built.

First of all, this is going to be a three dimensional device, and the cutting head will be able to move on at least 3 axis. This means there will need to be at least 3 motors operating to position it. Below is a small summary of what seem to be the four main ways to move something using a small controller:
DC motor: While speed/torque can be controlled somewhat by PWM (Pulse Width Modulation, essentially outputting a fractional power, which is close enough to a lower power), they aren't necessarily exacting. They use 1 H-bridge to control. They use a 2 wire connection (Motor input A and Motor input B), from the H-bridge, one of which is providing voltage and the other not. They can be reversed by making the other high. Note that they will never both be high. (Well they shouldn't be anyway.) The example at the left is what you might find in a simple toy, especially like a battery powered fan.


Servo motor: (commonly just Servos) These have positional control, which is easily programmed, however, almost all servos that you can easily find (trust me, I looked) have a range of motion that's usually either 180 or 270 degrees. While this can be translated into larger linear motions, with a large connector, it means it's harder to handle. Internally, they have a DC motor, but also have gears and electronics to have it move/hold position based upon a PWM signal. Requires 1 PWM output to control. They use a 3 pin (Signal (PWM), Vin and GND) connection. The example at the right is what seems to be called a hobby servo.

Continuous Servo Motors: These are servo motors which have been modified, so that the speed is what is controlled. This means that they don't control position. Making them very similar to straight DC motors, but often with higher torque, due to gearing, and finer control. Requires 1 PWM output to control. Like Servos, they use a 3 pin (ground, Vin and GND) connection. I don't have one, so no pictures, but the one above could be modified for continuous rotation, and look exactly the same.

Stepper Motors: These motors are very useful for this sort of thing, as instead of controlling speed, they (somewhat like Servos) control position, but don't have a limitation on movement, so they mostly combine the advantages of all of the above, having control over position, having some control over speed, and not being limited in range of motion. How they achieve this is via having (generally) two coils, which alternate which is powered to move, and just keep one on to keep the motor in position. However, they have the drawback in that they require two H-bridges to control. They may use a variety of wires to interface, 4-6 being the most common. In those 4 of them are as far as I can think of always Motor control 1A, 1B (for one coil), Motor control 2A, 2B (for the other). Extra wires are often ground wires. The example on the left is a 7.5 deg/step (48 steps per rotation) from adafruit, which can operate as either unipolar or bipolar (the two types don't make a difference in code, just how they are wired.) The example on the right is a 1.8 deg/step (200 steps per rotation) pulled from an Epson Stylus 777 printer, and will probably be what is used for at least 2 axis of the machine.

There are some variants of the 4, such as linear servos, which control a linear position with the same method a servo maintains it's angle, however, most of them I've come across that are potentially useful for this project, are $100 each, which for my purposes, puts it out of consideration.

Because of the advantages, I decided to go with steppers, I have two 200 steps/rotation (1.8 deg/step) motors I salvaged out of a Epson 777 printer that wasn't working and had been replaced. I also have two 48 step (7.5 degree) steppers, which I will probably end up using for the other axis.

For Arduino, what seems to be the best motor shield is the Adafruit Motor Shield. However, it is insufficient for my choice of stepper motors. It can control 2 steppers (via 2 dual H-bridges), which is very close to what is needed, I was considering using a continuous rotation servo. This left me needing another pair of h-bridges, and considering designs for them. I noticed while looking around, the SOC Cricket, which has 2 H-bridges, and runs Arduino. If you've read the other entries, you'll know I got that. That gives me a total of 6 H-bridges, enough to run 3 stepper motors. (I also have the option to use a few servos as well.)

So electronically, we have the Arduino Duemilanove, Adafruit Motor Shield (hooked to 2 steppers), talking to the Cricket, which will be connected to 1 stepper. (Another post will cover the communication needs of the project). For power, neither the Duemilanove/Motor Shield or the Cricket can use their normal power (5V and 3.3V respectively) to power the motors, so we have to hook up motor power for both of those, separately (though we can probably use the same power source for both motor powers). As far as for the devices themselves, they don't draw much power, so we will be powering the Duemilanove off USB, and the Cricket from the Duemilanove's 3.3V power.


Monday, July 26, 2010

Setting up the Arduino Software to program the Cricket

To the left we see the simple circuit to program the Cricket. This is a second breadboarding based upon my last post to make sure it's correct. One thing the shown circuit lacks is the LEDs (and resistors, don't forget them!) hooked up to pins 7-9 to see it the Cricket being programmed by the Duemilanove successfully, but the circuit works. The resistors in the upper right, are to disable auto-reset.

That brings us to Step 2: Getting the Arduino software to work with the Cricket. For this I originally thought that it required modifying a line in the Arduino software itself, fortunately this is not the case, because when tracking the code backwards, I discovered the upload.using parameter of boards.txt, something which controls the list of boards you see in the Arduino program under the Tools menu.

If you open the Arduino software (I assume you know how to install it for your OS, I found it best to install the packages for Ubuntu, then download the standalone (I actually used svn, as I was looking to patch it for a while.) because we also have to install the Sanguino extension to Arduino. Sanguino is another hardware board similar to the official Arduinos, such as Duemilanove or earlier models, but using the Atmega644P, as opposed to the Atmega8/168/368 series (used in official Arduinos). This is almost what we want, as the differences between the 644 and 644P appear to be negligible, aside from the addition of a second USART in the 644P, and a few power saving changes. Fortunately, even though the Sanguino project uses the 644P, the software extension to Arduino is compatible with the 644. (Provided no bugs exist, and there are always bugs.) The instructions to install Sanguino are on the front page, I'm assuming you can copy a directory, into another.

Now, my image shows an entry for the Cricket. We really want to have that entry, so in the 'Sanguino' directory you just copied, locate the boards.txt file. We need to tell it about the Cricket, this means telling the Arduino software what to use to program it, what speeds, and what fuse settings (those may not be absolutely necessary, but I have not tried them without it.) Add the following to the end of boards.txt, which should have very similar entries, but for Sanguino.whatever. Note, this is for a 10MHz one, for other speeds, you will have to adjust the build.f_cpu frequency setting, and possibly the fuses (these settings come in the download below)
cricket10.name=Cricket 10 MHz (Atmel 644 @ 10 MHz)
cricket10.upload.protocol=arduino cricket10.upload.maximum_size=63488
cricket10.upload.speed=19200 cricket10.upload.using=arduinoisp
cricket10.bootloader.low_fuses=0xEE cricket10.bootloader.high_fuses=0xDC
cricket10.bootloader.extended_fuses=0xFF cricket10.bootloader.path=atmega644
cricket10.bootloader.file=BootLoader_Cricket_10MHZ.hex
cricket10.bootloader.unlock_bits=0x3F cricket10.bootloader.lock_bits=0xCF
cricket10.build.mcu=atmega644 cricket10.build.f_cpu=10000000L
cricket10.build.core=arduino
Then copy the bootloader from the download of Soc-robotic's Chinook firmware bootloaders/atmega644/BootLoader_Cricket_10MHZ.hex (Some of this comes from me looking at this blog entry which focuses on the Wasp, but helped me figure this out for the Cricket. Though, I think the only setting which is the same is the two atmega644 settings.)

We should be all set, on the desktop. There's one more piece of software to load, this one is the programmer on the Duemilanove. Start the Arduino software, and check that everying prior works, by going to the Tools->Board menu which looks like the one pictured above, and have entries for "Sanguino", and "Cricket 10MHz (Atmel 644 @ 10 MHz)". If so, you are good to this point. From here, select your Arduino's port and then from the Tools menu, your port. (On Linux this is commonly /dev/ttyUSB0 or /dev/ttyUSB1.)

Now we need to open the (provided) sketch for using the Arduino as an ISP, so go to File->Examples->ArduinoISP. Now, you need to possibly compile and upload to your Duemilanove. First, from the Sketch menu, click Verify/Compile. In the lower blue bar, it should say "Compiling..." then switch to "Done Compiling." For now, disconnect a pin from the resistors which disable the auto reset. After it does, (if there are no errors, which would be in the form of red text inside the black box) you need to upload it, via File->Upload to I/O board. The blue bar should say "Uploading to I/O board", then "Done Uploading". If it shows no errors, you are ready to move on to uploading to the Cricket. If you connected LEDs to pins 7-9 (don't forget the resistors!), then they should light up then go out in the order 7, 8, 9, with 9 staying lit but sort of pulsing, to show it has uploaded. After this is done, you can reconnect the resistors to disable the autoreset.

Now, go back to the board menu, and select the Cricket entry. For this, we will use the Blink example, and then modify it to make sure it's working (and for a little example of programming.) Go to the File->Examples menu again, this time instead of ArduinoISP, open the Digital folder and find the 'Blink' example. (The example should be at the top.). Now, Repeat the Compile/Upload process (the shortcuts are Ctrl-R for compile, and Ctrl-U for upload). If you connected LEDs, you should see the one connected to 9 remain on (as usual), but also the one connected to pin 7 (programming) light up for a bit. If pin 8's lights up, that indicates an error. (I haven't seen this happen, so my advice would be double check your circuit, and try once again. After that, start googling for the problem, or asking for help.)

Once the programming led goes out, and/or the software says Done uploading, one of the two LEDs should start to blink every second. That's great, but I've noticed the cricket sometimes blinks with whatever bootloader it had. So to make sure we know that it is right, we shall use both LEDs. On the row after to where it says int LedPin = 13;, add the line "int LedPin2 = 12", and replicate each line with LedPin in it, on the line below it, with LedPin2 substituted for LedPin. Then in the loop() function, on the LedPin2 lines, change LOW to HIGH and HIGH to LOW, this will cause the LEDs to alternate, every second. Compile, then upload it again. and the LEDs should start a slow alternation every second. You can also adjust how long each one is on, by changing the number of microseconds in the delay functions, say 100. So change both occurances of 1000 to 100, and Compile/Upload, and the two LEDs should fairly rapidly alternate.

That should be all for now, next post will talk about controlling a DC motor and possibly stepper motors.

Saturday, July 24, 2010

Using Duemilanove to Program the Cricket


As promised, this will have pictures, though not necessarily great ones.

After finding a what seemed like a great deal on ebay, An arduino compatible When I got it I realied that the header to turn the top left port into a 10-pin standard ISP header wasn't included (as my plan was to order a serial port ISP10 programmer), I really wanted to figure out how to make the board useful.

To the left is the Cricket with labels on it, note that my particular one has a Serial flash and a 10MHz crystal. First of all, a general description, I consider this board to be turned 90 degrees clockways from normal orientation. (This was done so most pins can be labeled.) The pins are labeled via Arduino(/Sanguino, I'll get to that later) pinout. For right now, what we want to do is program the chip. The pins we need are marked in the upper left of the image.


Atmega processors are programmed using In-system programming, which normally uses a 6 or 10 pin header carrying the required information. (Though the 10-pin doesn't use all of them.) If you look at the picture of the Arduino Duemilanove, you will see a 6-pin header on the right, this is for ISP programming of the chip. The pin out has a pin for Reset, Serial Clock (SCK), Master to Slave Communication (MOSI), Slave to Master (MISO), a 5V and obviously Ground. The 10-pin adds an LED indicator, and more Ground connections.

Fortunately, Arduino is popular enough that, someone has already written an implementation of ISP programming, which is distributed in the example sketches. Called ArduinoISP, this is usually used to allow uploading just the bootloader to another Arduino, but in this case, due to the lack of USB, and my desire not to splice serial cables, I will be using this to upload sketches as well. You will find that it uses pin 13 for sck, 12 for MISO, 11 for MOSI, and 10 for reset. As well as using 9, 8 and 7 for status LEDs. (Check the file)

On the cricket, all of these functions are brought out nicely to a header, and the cricket is easily inserted into a breadboard (It's not possible to see, but the cricket is essentially stackable). I use this to make it easy to adjust which pins go where, and to make it easy for future reprogramming, I set it up on a breadboard, such that it is simple to plug the cricket back in. If you look at the first picture, you'll see there are 7 pins contained, in addition to the 4 labeled, they are 2 for power (Vcc and Ground, which in my case, must be a 3.3V, fortunately the Arduino supplies this), and one for SS, which is Slave Select. This needs to be held low, which I noticed, conveniently happens on RESET, as I read the ArduinoISP code, so I'm going to have it also go to SS, as well as reset.

So, the 7 pins, need to be connected to pins 10-13 and power on the Arduino, this is one to one, except for pin 10 to SS as I've used it, SS can also be connected to ground. (That's probably a better solution anyhow. EDIT: Not really, I just tried it and it failed to work, so just use pin 10) On the breadboard, I've connected them to a few rows on the opposite side, matching the Arduino pins, so that I can easily plug wires from the Arduino into the matching number. (I may make a diagram later, as it is counting from the top/left of the cricket's J2 connector, Arduino pin -> Cricket pin: 3.3V -> Vcc; 11 -> 5/MOSI; 13 -> 7/SCK; 10 OR GND -> 4/SS; 12 -> 6/MISO; 10 -> RESET; GND -> Ground)

Now, there is one other issue: Automatic reset. As far as I can tell, this needs to be disabled while using the Arduino as an ISP programmer. There are two ways to disable it. One is to cut a trace, which will disable it until that trace is bridged. Which seems like an annoying case (and if you want to disable it again, cut, etc.) Apparently earlier Arduinos provided a jumper. Unfortunately my Duemilanove doesn't. However, there is a much more elegant solution, which is to temporarily disable it. This is accomplished via a 110 ohm resister between reset and 5v. (In my picture, that's the two resistors in the lower left.)

Now you can try to upload the Cricket bootloader with the avrdude command: avrdude -b 19200 -c arduino -p m644 -U flash:w:BootLoader_Cricket_10MHZ.hex -P /dev/ttyUSB0 if the bootloader (obtainable from soc-robotics) is in the directory. You may also have to adjust the port (-P option). This should upload the bootloader. I believe The LED will slowly flash.

Next post will describe how to upload programs(sketches) to the Cricket and how to set up the Arduino software to handle it, due to the chip being an Atmega 644 and not a normal Arduino chip, and the difficulty of using an ISP programmer for sketches as opposed to using the bootloader. (It might be two parts.)

Oh, and just so it's clear: images in this post Copyright 2010 by James L.

Friday, July 23, 2010

An Introduction to this

I decided to on the project I'm working on, start a blog for once. The reason being that, in other situations, I've lost documentation on projects. For example, in high school, I was working on an ebook reader for the TI-83 in Z80 assembly, and recently found just the code which was nearly done, but not the documentation that was on paper, so unless I wanted to have the fun of figuring out what I did, and why I did it 10 years later, I figured I would write a blog, theoretically which should be around for a long time, which will also allow others to benefit from my knowledge.

This project is to build a CnC milling machine (and possibly a laser cutter), hopefully improving it to the point where it is suitable for *very* precise work. If you haven't guessed, the electronic interface to this will be via Arduino & Arduino-compatible boards controlling stepper motors, and interfaced via the Arduino's onboard USB-serial connector. The non-electronic physical hardware will for at least the first version, be done by hand, with hopefully good results.

As it exists now, one axis control is built, but the motor is not permanently attached. A motorshield from Adafruit Industries has been assembled, which is not that hard, even though it'd been a long time since I'd soldered most anything. (and I did make a mistake, when de-soldering in the terminals I'd put in backward. Ooops. It's functional though.)

Hardware wise, In addition to the Arduino Duemilanove (with an Atmega 328 chips) I will be using one or two Crickets from SOC-Robotics (with Atmega 644 chips), which were purchased on ebay. This required me to figure out how to program the Cricket via the Duemilanove, and connect them to the Duemilanove, which while a less powerful chip, will be the primary controller (due to it's USB connectivity.)

Someday I'll be able to spell Duemilanove without reference. The next post will describe how to program the Cricket, with pictures.