Arduino CAN Bus Interface

I’ve been working on an implementation of CAN for use in aircraft.  The protocol is called CAN-FIX and is a part of the MakerPlane Open Source Airplane project.  You can get more information on CAN-FIX here.

The idea for this project is to build an Arduino shield that will let it communicate over the CAN bus.  I had some Microchip MCP2515 and MCP2551 (CAN Controller and CAN Transceiver, respectively) laying around so I decided to make my own little Arduino shield so that I could quickly try out some ideas.  I started with the little ProtoShield that Sparkfun sells.  I also had some header material laying around so just getting the PCB without the rest of the kit worked for me.  An easier answer may be to get the full ProtoShield Kit.  This will give you some buttons and LED’s to play with too.

My intention for this particular device is to use it as a generic USB<->CAN converter.  Once I have that working, I’ll have a CAN interface for a computer and could use the same code with an Arduino Mini, or something similar, to interface to a Beagleboard, Rasberry Pi or other SBC.

CAN Shield Schematic
CAN Shield Schematic

The circuit is pretty simple.  It’s just an SPI interface to the MCP2515 CAN Controller IC.  This IC handles all of the low level details of the CAN protocol.  Things like timing, arbitration, error detection etc are all offloaded to this IC.  I purposely made mine similar to the CAN Shield that Sparkfun sells.  The pins are the same but I choose to use a 20MHz ceramic resonator instead of the 16MHz oscillator that’s on the CAN Shield.  The only reason I did that was because I had some.  It might make it work better at higher bit rates too but I haven’t tested any of that to know.

There are a couple of CAN Shields out there and they all have basically the same interface to the Arduino so this means that we should all be able to use the same software with all of them.  After all an SPI interface to the MCP2515 seems to be the most popular way to do CAN communication anyway.

Arduino Leonardo
Arduino Leonardo

One thing about my shield that is a bit odd is how I actually tied in the MISO, MOSI and SCK lines.  Since I decided to try this with a new Arduino Leonardo which has some differences in where these signals are located.  They don’t bring them out to the headers any more, so I connected them to the six pin ISP programming header.  This should work regardless of which type of Arduino used but I haven’t tried it on another type.

Top View of the CAN Shield
Top View of the CAN Shield

In this shot you can see the MCP2515 to the right and the MCP2551 in the top center.  The DB-9 connector is just one end of a serial cable that I had lying around (You can’t see it in this picture, but it’s on the other end of that thick black cable).  I just cut one end off of it and found pin 2 and 7 for use here.  There is a pad on the proto board for a small pushbutton switch.  I used two of those holes (the ones tied to GND) as place to solder some wire and hold the serial cable.  There are probably better ways.  The transistor looking thing to the left is an LM34 temperature sensor.  I threw it on there so that I’d have something analog on the board to use when I’m playing around with the CAN-FIX software.

Bad photo of the solder side of the CAN Shield
Bad photo of the solder side of the CAN Shield

In the above image you can see the two rows of female headers that I used for the ISP interface.  I didn’t have any two row female headers so I just cut a couple of single row headers and jammed ’em together.  As you can see there really isn’t much to it.

The code to make this work can be had from GitHub.  It’s not very far along but it can be used for testing the circuit at this point.

Resources:

Schematic

MCP2515 Datasheet

MCP2551 Datasheet

Hangar Door Controls

I’ve been building a door for my hangar.  The door has been installed in the opening.  Now I have to finish installing the mechanical and electrical components that make it work.

The winch to the left is used to pull on the locking cable.  The motor and gearbox turn the main shaft.  The box to the right is the where the limit switches are located.
The winch to the left is used to pull on the locking cable. The motor and gearbox turn the main shaft. The box to the right is the where the limit switches are located.

The motor is a reversible 2HP 220VAC single phase motor.  It has a brake installed between it and the right angle gearbox.  The gearbox has two shafts.  I used both shafts and two ANSI 60 chains.  When I did the load calculations on the chain it came out to be right at the maximum for an ANSI 60 sized chain so I went ahead and used two.  If those chains break the door comes crashing down so I didn’t want to risk it.

I couldn’t find any limit switches that I liked that didn’t cost $700.  I wanted to have two sets because if one set fails and the door continues to open, really bad things will happen.  The controls are set up so that one set of limit switches go into the PLC (Programmable Logic Controller) as inputs and are the primary way to stop the door at each end of it’s travel.  The other set of switches are set just further up and down than the first set and they are wired directly into the motor control circuit.  This way, if the PLC fails or one of these limit switches fail then the motor will still stop.

Limit switch enclosure
Limit switch enclosure

The limit switches work by turning a couple of Acme thread lead screws that have a two nuts on them.  I welded a washer on the nuts that I cut grooves into.  These grooves ride on the small aluminum angle pieces and this keeps them from turning.  I can make very fine adjustments to this by pulling the aluminum angles out and turning the nuts.  One of the nuts is the down limit the other is the up limit.  The turning of the screw causes the nuts to move back and forth and actuate the small lever switches on each side.

 

These are the chains and sprockets that drive the limit switch mechanism.
These are the chains and sprockets that drive the limit switch mechanism.

The limit switch lead screws rotate from sprockets that are attached to the main shaft.

The PLC is in the upper left, the main motor contactor is on the lower right.  Most of the relays are for lights.
The PLC is in the upper left, the main motor contactor is on the lower right. Most of the relays are for lights.

The main control panel controls the hangar lights and the door motor.  It uses a small Automation Direct PLC.  There are open/close buttons on the front of this panel and it also has the capability of interfacing with a keypad and a remote control.  I can open and close the door from the cockpit of my plane as I taxi up.  It’s pretty cool.  The PLC has a couple of communication ports on it for programming and for communication to other devices.  I used one to talk to a touchscreen that is near the walk in door of the hangar so that I can open and close the door from the entry door.  It’s a bit spoiled I know, but since this is the kind of thing that I do for a living I thought I should make the controls a little bit over the top.

I can control the door and the hangar lights from this touchscreen.
I can control the door and the hangar lights from this touchscreen.

I can also turn on the lights in the hangar from this touchscreen.  There are four banks of lights in the hangar.  I don’t always want all four banks on since I may only be working in one corner of the hangar so I can individually control them.  I had extra outputs on the PLC so I figured why not control the lights too.  This will give me the ability to control the lights from home at some point too.  Having the door computer controlled gives me lots of options on how to manage it.

This shows partially open.
This shows partially open.

Once I got everything installed I slowly opened the door a bit at a time and made sure that everything was working / holding.  It took a bit of fiddling to get the limit switches set and the tension on the straps set right.  Overall it works pretty well.

The open door.
The open door.

This turned out to be a much bigger project than I ever imagined.  It seems simple enough just looking at one but like most projects, the devil is in the details.  It’s nice to finally have my plane in a home of it’s own without a big hole in one wall.

 

 

Bifold Hangar Door

I’ve always wanted to own my own hangar.  I put it off for many years until I could afford to do what I wanted.  I didn’t want to settle on something that was too small, too far away or not built properly.  I was finally able to do that but I was not happy with any of the hangar door options that I had so I decided to build my own door.

Bottom panel of the door up on sawhorses for paint
Bottom panel of the door up on sawhorses for paint

The door is going be a bi-fold door.  This is the kind of hangar door that is suspended from the top and opens upward by bending outward in the middle.  The dimensions of the opening are 60′ wide by 16′ tall.  It’s made mostly of 2″ x 2″ square tubing.  There is some 1-1/2″ x 2″ and some 2″ x 3″ tubing as well.  I laid it all out on the floor of the hangar.  This was less than optimal since my airplane was stuck back in the corner of a hangar that had a very large hole in one end of it for several months.  I also elected to work on the door before doing the electrical work.  This was also a mistake since I did most of this work during the winter months and it get’s dark very early so I couldn’t do any work after work.  Had I done the lights in the hangar before building I would have been done sooner with both.  Once I got started on the door I couldn’t really do the lights because the door was in the way, so I was stuck.

Anyway, a 60′ x 16′ door turned out to be a much bigger challenge than I thought.  It didn’t look that big on paper.  It was a lot of welding.  The worst part was getting it up off the floor to paint.  I had to enlist the help of the neighbors several times to lift each panel and to turn it over.

Both door panels painted and ready to go up.
Both door panels painted and ready to go up.

It was too big of a door for me to install myself so I hired the same company that built the building to come back and hang it.

Lower panel of the door in place.
Lower panel of the door in place.

Once the door panels were up I had to hurry and get the straps and cables put in.  It worked out okay because the company that was putting it up for me had another job that had to be done so it gave me time to install the lift straps and the cables.  I could have done this after the sheet metal was put on but it was much easier to do without the sheet metal.

 

Door panels installed with the lifting straps attached.
Door panels installed with the lifting straps attached.
Another view of the door panels with straps
Another view of the door panels with straps

Once I was done with that the installers came back and put on the sheet metal.

Most of the sheet metal installed on the door.
Most of the sheet metal installed on the door.

 

Installation Done
Done

The door is installed in the building with the sheet metal and insulation complete.  Now it’s up to me to install the motor, shafting, electrical and the rest of the odd and end bits that make it move.  Stay tuned.

VMWare Tools in Kubuntu 9.04

I tried several different ways to install VMware Tools on Kubuntu 9.04 there are many different instructions out there and some worked and some did not. I was somewhat successful with these steps. Fundamentally all the information that you need is on this page, but some of it is in the comments. I’ve tried to make this more concise.

I am using VMware Fusion Version 2.04. I don’t know if this will work on any other versions as I don’t have any other versions to test it with. I also assume that it’ll work on Ubuntu 9.04 but I didn’t try that. This assumes that you already have Kubuntu installed and that you have root access.

I like to work at the command line so open a terminal session and go through these steps.

First we need to make sure that we have all the latest packages.


$ sudo apt-get update
$ sudo apt-get upgrade

Then from the “Virtual Machine” menu in VMWare select “Install VMWare Tools.” This will connect a virtual CD Rom to the guest OS that has the VMWare Tools on it. Copy the file VMwareTools-7.9.3.-xxx to your home directory and unarchive it with…


$ tar -xzvf VMwareTools-7.9.3*

Now we need to change one file in the distribution.


$ cd vmware-tools-distrib/lib/modules/source
$ tar -xvf vmhgfs.tar
$ cd vmhgfs-only
$ sudo kate page.c

Somewhere around line 867 you’ll find a line that looks like this…

page = __grab_cache_page(mapping, index);

…change it to look like this…

page = grab_cache_page_write_begin(mapping, index, flags);

Save the file and then go back to the Konsole and type this…

$ cd ..
$ rm vmhgfs.tar
$ tar -cvf vmhgfs.tar vmhgfs-only
$ cd ../../..
$ sudo ./vmware-install.pl

All of the questions can be answered by hitting the enter key except for the default resolution. At this point the mouse driver won’t work right so install the Xorg virtual mouse driver with this little command.

$ sudo apt-get install xserver-xorg-input-vmmouse

Now reboot and all should work.

You can use this command to bring up the settings editor for VMware Tools.

$ sudo /usr/bin/vmware-toolbox

Do It Yourself Outdoor Wireless Access Point

My wife and I live on 44 acres here in Texas and I needed to get my network distributed around the place. I needed to have access at the barn because my wife has a small office there and I wanted to have a camera at the entry gate. The barn is about 500 ft from the house and the gate is over 1500 ft away.

I built a couple of wireless outdoor access points using a Cisco-Linksys WRT54G Wireless-G Router, a PVC enclosure from the hardware store, a couple of outdoor antennas and some open source firmware for the WRT’s.

I started by installing an access point at the house and putting an antenna under the eve. Since I only need to be able to see this antenna from one side of the house this worked out pretty well, otherwise I would have had to put it on a mast.

Here is a picture of the access point at the gate. It just fits inside one of these 8″ CPVC boxes. I had to drill the hole in the bottom in just the right place since there is just not enough room inside to make a bend in that antenna cable.

I don’t have a picture of the access point at the barn but it looks just like this one except, I left one of the little rubber ducky antennas on it that came with it. If you put the WRT on it’s side and at a slight angle you can turn the one antenna up alongside the WRT and it all fits. Since the box is plastic there is little there to attenuate the signal. Since the barn is only 500 ft away it works just fine. You need to be sure and disable the other antenna in the setup program.

I bought this antenna from RadioLabs. It’s a 16 Element 15 dbi Yagi Antenna. Here is a link to the antenna. The connector on the back of the WRT is a Female Reverse Polarity TNC (or RP-TNC). My antenna came with an N type connector and I simply ordered a cable that had the correct connectors on each end. The key is to remember that you need a Male RP-TNC on the cable or antenna to connect to the router.

The whole thing is powered off of the same solar panel that powers the gate controller.

I used the DD-WRT firmware for both of these. Both are used in Client Mode. Client mode is a way to have the WRT54g router act as the client end of the wireless connection instead of the access point end. This basically creates a ‘virtual wire’ type of connection. It’s as if I ran an EtherNet cable all the way out to the gate from the switch in the house. I can go out to the gate, plug my laptop into one of the ports of the router and surf the internet.

There is a lot of documentation on how to use DD-WRT in Client Mode, but there is one ‘gotcha’ that had me stumped for a while. You need to use MAC address filtering in the access point and put all of the client’s MAC addresses in the list. This may have changed in more recent versions of DD-WRT but MAC address filtering would be a good idea anyway, if for no other reason, than to help keep the neighbors from using your connection.

AVR Dragon

I have been using an AVRISP mkII for programming my AVR microcontrollers since I started playing with them a year or so ago. I really like the AVRISP because it’s inexpensive, it works well and it’s small enough to carry around. I recently made a mistake on a little test chip that I was using and programmed the RSTDISBL fuse because I wanted to use the reset pin as a port. Well that made it impossible for my AVRISP to reprogram the chip. It seemed obvious after I did it. You see the chip needs to be held in reset for the in circuit serial programming to work. Since I disabled the reset pin it couldn’t do this anymore. Well I decided to bite the bullet and buy an AVR Dragon, since I kinda wanted one for its debugging capabilities anyway. Now I had an excuse, I needed the high voltage programming to fix my screw up.

This little programmer/debugger is pretty cool. It has the ability to program all the modes of most (if not all) of the AVR line of microcontrollers. It also talks to AVR Studio and can be used as an in circuit emulator. Actually it’s a little bit better than an emulator because it doesn’t emulate, it actually executes the code on the target microcontroller. It uses either the JTAG or DebugWire interface on the microcontroller to control the chip. It can do this on the prototype area on the Dragon board or in the target circuit.

I had a little bit of trouble with mine at first. I use a Mac and have Windows running in VMWare Fusion. This has usually worked really well for me but for some reason I was unable to update the firmware in the AVR Dragon. I had an old laptop at work with XP installed on it, so I put AVR Studio on that machine and used it to update the firmware. It works fine. I hope that Atmel gets that figured out before too long. Surely I’m not the only one having that problem. Once the firmware is updated, it works fine from VMWare but there was something about the firmware update.

The documentation for the Dragon is pretty good in some areas and not so good in others. It’s great for wiring but it took a little digging and Googling for me to figure out that the Dragon doesn’t really do any emulation. It simply uses the debugging interfaces on the target chip. This is actually a better deal than emulation since it runs on the actual hardware but it means that you do have to have the target chip to plug into the Dragon.

The software interface to AVR Studio is simple and easy. You just select AVR Dragon as the programming interface if you want to use it for programming. You can also select it as the debugging device instead of the simulator and use the emulation capabilities. The programming interface is pretty intuitive but it can be a little tedious. The Dragon doesn’t actually connect the debug/programming interface to the prototype area on the board. The High Voltage programming port, the ISP port and the JTAG port are all put on dual row headers (one you have to install yourself) and then there is another 40 pin header that corresponds to the pins on the microcontroller socket (that you also have to install yourself). I found that it was very helpful to have some little jumper wires with female header connectors on them so that it’ll be easy to wire up from one AVR to another.

AVR Studio has all the documentation for wiring in the AVR Tools Help menu. The wiring for each of the AVR line is laid our really well and in color which makes it quite easy to hook up.

I also found it helpful to install a 40 pin ZIF socket on the prototype area. This lets me put any AVR that the Dragon supports (well any that are PDIPs) in this socket very easily. Here is a picture of my Dragon with the jumper wires set up to do high voltage programming on an ATtiny84.

Oh the story on my little ATtiny that I disabled reset on ends well. I put it in the Dragon, unprogrammed the fuse, put it back in my target circuit and it works just fine. It’s short one I/O pin (that I didn’t really need anyway) but I can program it with my AVRISP mkII again.

ATX Power Supply Conversion

Commercial lab power supplies can be pretty expensive. For most of what I do in my little lab I don’t really need that level of sophistication. ATX computer power supplies are cheap and plentiful. I had an old one laying around and I decided to turn it into something that I could use in the lab.

In the old days of the AT power supplies it was easy to turn them on and take power from them. I used a few of these for powering prototypes years ago but the ATX power supplies are a little more complicated and I never got around to figuring out a way to turn them into something useful.

One of the extra features of the ATX power supplies over the old AT versions is that the ATX power supply can be turned on and off by the computer itself instead of a big giant switch that turns the AC power to the supply on and off. This is done by simply grounding one of the wires in the main connector. It’s the green wire. The ATX supplies have a pretty high current capacity for 3.3V 5V and 12V outputs. There are also -5V and -12V outputs, but these aren’t capable of delivering the same current.

The one that I modified had the following specs…

Total Power Output 230 Watts
5V 18 Amps
3.3V 10 Amps
12V 7 Amps
-12V 0.8 Amps

It also has a +5V standby of 2A that is on all the time whether the supply is powered up or not. I had no use for this one so I clipped it off.

I took the power supply apart to figure out where to put the binding posts and the power indicator LED. It made sense to use the same side where the wires come out of the circuit board. The problem with this for me was that this side of the box is on the part of the enclosure that comes off from the rest of it. It worked okay but the soldering was tricky. Other supplies will have different geometry and may be easier. This one is physically very small so I struggled with it a bit.

Radio Shack didn’t have enough of the big binding posts that I wanted so I bought a couple of little ones. I figured that since the negative voltage outputs won’t supply much current that they could stand to be on the smaller ones. It would have also been nice to have a different colored one for each voltage but RS only supplies red and black so I went with those. I didn’t want to spend forever on this little project.

The color code for all the wires…

Green Power Supply On
White -5 V
Blue -12 V
Red 5 V
Orange 3.3 V
Yellow 12 V
Black Ground
Purple 5 V Standby (Not Used)
Gray LED Power Indication

After I laid out and drilled the holes for the binding posts I started cutting and soldering the wires. The first thing I did was solder the green ON wire to one of the black ground wires. You could wire this to a small switch if you want to but the way mine turned out I have pretty good access to the main switch that came on the power supply. If you want to use the standby 5V power then you’ll have to put a switch between the green wire and ground for powering the supply on and off. You could also build a fancy little circuit that could power the supply off after some time. The sky’s the limit with this.

I put a 220Ω resistor in series with a small LED (check the polarity) and connected it to one of the red 5V wires and one of the smaller black ground wires. This gives me a power on indication. After I finished I discovered that the gray wire is designed to be used for this purpose. Oh well, I’ll remember that next time. Using one of the red wires works just fine.

After that I started cutting and bundling the different colors of wires together. I tried to tie as many of each color to it’s respective binding post as I could but there was no way to get all of those black wires on one post. Since I had extra black binding posts I used a couple for the ground. I tend to have banana plugs hanging 6 inches off of my power supply grounds so this will give me more options anyway.

Once I got them all soldered together I put the power supply back together and turned it on. It just blinked and went back off. It didn’t take me long to figure out what I had missed. This particular supply has a couple of small sense wires coming off of the circuit board. One was orange and the other was black. I took the thing back apart and connected the orange one to the 3.3V post and the black one to ground. When I turned it on this time it stayed one. Success!

In my research I found that there are several different types of power supplies and some have a sense wire that has to be connected and some do not. On some of them it is a different color. It may take a little playing around with whatever power supply you have. There is plenty of information on the internet too.

There also seem to be some power supplies that won’t stay on if there is no load. In this case you will need to get a 10 or 20Ω – 10W resistor and put it between one of the red wires and ground. This will give the supply enough load to stay on. Mine worked without it.

The last thing, was to make some labels with my label maker.

These ATX computer power supplies are actually pretty good switched mode power supplies, and with a little hacking they are easy to convert to bench use. This whole project can be done for less than $15 if you have an old power supply laying around. If not they are easy to find.