Progress

I managed to get a few more useful electronics bits working, which means the end is in sight for the initial electronics phase of the UAV project. I got the Atmega16 talking to a DS1820 temperature sensor over the i2c bus, which tells me that my computer cupboard is at 22 degrees C. I also figured out how to use the various modes of the inbuilt 16-bit timer, which allowed me to do PWM control of an LED (ie. the world’s most complicated light dimmer). Now I need to chop the cable of my one-and-only servo to hook it up and control its position using PWM. This will also let me use the other end of the cable to plug the RC receiver unit into my circuit as a PWM input, and experiment with sampling that. Eventually, the Atmega is going to sit inside in RC aeroplane, listening to my “suggestions” sent to it over the RC link, and deciding what actual control output to send to the servos.

So I’m getting pretty close to doing some UAV stuff rather than electronics dabbling. The main Flightgear developer has an interesting article about autopilots, although I’m still interested in finding more references. I was in Maplin today, and they sell IR thermopiles which will probably do for horizon-detecting (and therefore, attitude determination).

I’m still pondering what kind of software system to run on the plane. It’s going to have plenty of work to do – sampling RC PWM inputs, using the ADCs to measure attitude, talking to GPS over serial line, data logging, and producing PWM output for servos. It’s lots of stuff to juggle, and having some form of realtime task scheduler layer is starting to look attractive. I’ve seen other UAV systems use multiple microcontroller rather than trying to make one do everything. Kinda like communicating sequential processess …

UAV talks serial

I’ve managed to get the Atmega16 chip talking to my PC over an RS232 serial link. The Atmega has a built in UART, which makes serial comms easy. You just write the data you want to send into a register and the chip does the rest. A MAX232 chip converts from the 5v levels used by the atmega into +10/-10v used in RS232. I can leave my circuit plugged into the serial line and the in-circuit-programming cable and control everything from my PC. I had to work through a few problems with a logic probe to figure out what was going wrong, which made me think that an oscilloscope would be a Fine Idea. So, a quick trip to ebay resulted in a purchase of a basic but very useful oscilloscope, yay!

I’m trying to get Avrforth running at the moment. It runs okay, but whenever I try to store anything to memory, it hangs. I’m going to dig a bit more into this, but it’s not really directly relevant to the UAV project so I’m not going to spend ages on it.

Next task is to get to grips with timers and PWM input. This will let me use my ultrasonic range finder board, which indicates distance by varying the pulse-width. As ever, there is plenty of information out there about this. I also found an old i2c temperature sensor IC which I might try using too.

Different flavours of microcontrollers

Tim‘s friend Tony Cole wrote this list of various microcontrollers and what they’re suitable for. It’s really interesting .. much more useful than a dry list of technical specs. So, many thanks to Tony for his expertise and wisdom:

For very small tasks look at the MicroChip PIC, I think there is a GNU C
compiler for it now, but I use Hi-Tech C Compiler.
A very fast PIC copy is the Scenix.
Others: Zilog Z8 - I think this has free C Compiler and network stack.



For small tasks look at the Atmel AVR 8-bit micro controllers:
http://www.atmel.com/dyn/products/devices.asp?family_id=607


The one I use is the ATmega128 128-Kbyte self-programming Flash Program
Memory, 4-Kbyte SRAM, 4-Kbyte EEPROM, 8 Channel 10-bit A/D-converter.
JTAG interface for on-chip-debug. Up to 16 MIPS throughput at 16 MHz.
2.7 - 5.5 Volt operation. 

You can get a in-circuit programmers from Kanda
http://www.kanda.com/index.php3?cs=1&
or the chip manufacturer/distributors.

For RTOSes C Compilers and other tools for the AVR look at
www.avrfreaks.net


###########

For bigger/faster tasks the Atmel AT91 SAM 32-bit ARM processors are
very good, I've used the AT91SAM7S64 Atmel's AT91SAM7S64 is a member of
a series of low pincount Flash microcontrollers based on the 32-bit ARM
RISC processor. It features a 64 Kbyte high-speed Flash and an 16 Kbyte
SRAM, a large set of peripherals, including a USB 2.0 device, and a
complete set of system functions minimizing the number of external
components. The device is an ideal migration path for 8-bit
microcontroller users looking for additional performance and extended
memory.  

The AT91SAM7S256 has 256 Kbyte high-speed Flash and a 64 Kbyte SRAM:

http://www.atmel.com/dyn/products/devices.asp?family_id=605


Look at:
The AT91SAM7S64-IAR bundles the AT91SAM7S-EK evaluation board, a USB
JTAG ICE and a 32 KB limited IAR KickStart toolchain. It enables the
evaluation of and code development for applications running on an
AT91SAM7Sxx device. 

This only cost about £100 and is a good starting point.


ECOS RTOS might be worth looking at: http://ecos.sourceware.org/ there
is also a book on this ROTS.

Or search the web for a free RTOS: e.g. http://www.freertos.org/



###########

Unfortunately, all the above will not realistically run interpreted
languages, such as python, java, scheme, lisp - but you could search the
web, someone might have done it!. These sorts of languages require an OS
like Linux.



If you want to run Linux look at the many embedded PCs available, or the
Atmel AT91RM9200 microcontroller (ARM9 CPU with MMU). The AT91RM9200-EK
Evaluation Kit supports the AT91RM9200 ARM9-based 32-bit RISC
microcontroller and enables real-time code development and evaluation.
http://www.atmel.com/products/AT91/

It has 32MBytes of SDRAM and runs at 200MHz:
The AT91RM9200-EK Evaluation Board
The board consists of an AT91RM9200 together with the following:
! 8 Mbytes of parallel Flash memory
! Four banks of 2M x 32-bit SDRAM
! DataFlash® or SD/MMC memory expansion socket
! Additional DataFlash memory expansion socket
! Digital-to-Analog Converter (DAC) for a stereo audio signal
! Four communication ports (USB host and device, Ethernet, serial and
DBGU)
! Graphic controller with output to a standard VGA monitor
! JTAG/ICE, ETM and code test port interface
! Expansion connector
! Onboard prototype area

I have a working Linux tree and GNU C Compilers for the AT91RM9200-EK,
you can do a lot on this baby. It is well supported see
http://www.arm.linux.org.uk/

Hello UAV world

My UAV project has taken its first steps. I got the Atmega16 microcontroller last week, and set about doing the hardware equivalent of “hello, world”, which involves flashing an LED off and on. I breadboarded a circuit with 5v and an LED on the first output line. I had expected that the chips memory was blank and would require some initial programming, and so was pleasantly surprised when I powered it up and saw the LED blinking all by itself. The chip must come with a rather useful default program! A good start.

Mental note: Must buy a bench power supply. I’m fed up building LM7805-based battery-driven power supplies every time I do anything electronic.

Further note: LM7805’s don’t immediately blow up if you plug them into back-to-front.

Final note: Neither do electrolytic capacitors, as far as I can make them, despite dire warnings about their explosive tendencies. They mostly die quietly.

The Atmega16 chip is supported by gcc, and Gentoo linux makes it easy to get this all set up. You just “emerge crossdev” then run “crossdev -target avr”. This produces avr-gcc (and libs/headers in /usr/avr). Finally, you run avr-gcc -mmcu=atmega16 and it generates appropriate code for the chip. The avr-objcopy converts from ELF format to HEX format used by most programmers.

Next, I need to get programmer software working so I could download my program onto the chip. I tried PonyProg first. It could read the chip memory fine, but failed to write. I spent hours trying different delay values, checking and rechecking the connections to no avail. Next, I tried avrdude which is much better (more configurable and better error messages) but still had no success. I kept getting “Verify error – unable to read hfuse properly” errors, which suggested that the cable from the PC to the Atmega board was flaky and unreliable. After many more frustrating hours, I tried using a avrdude on a different PC and it worked first time. Perhaps I cooked the parallel port on my desktop last time I did hardware …

Next step is to get my PC and the Atmega chip talking over a serial link, which just requires a MAX232 chip to convert the voltage levels – the Atmega16 has a builtin USART. Then I can see about getting a forth interpreter running on the chip to allow me to do interactive experiments. I’m not into the whole “compiler, burn, test” cycle … ocaml/ruby/lisp has spoiled me too much.

The plane that flies itself

I’ve decided to dabble in hardware again. The grand plan is to turn an RC aeroplane into a semi-autonomous UAV. I’ve used PIC microcontrollers before, but this time I thought I’d switch and try one of the Atmel microcontrollers instead. Microcontrollers have got a bit nicer since the last time I used them. For example, generating PWM signals for servos used to require software bit-banging but now there is hardware support for it.

I settled on the Atmega16L which runs at 8MHz, has 16k flash ram for programs, and 1k static ram for calculations. It has onboard analogue-digital converters, which is perfect for plugging in “what angle is the plane at” sensors. It has 3 PWM outputs, which’ll be perfect for controlling the servos which move the rudder/elevator plus the electronic speed controller. Plus, it supports in-circuit programming which means you don’t have to keep dragging the chip out of the circuit and plopping it in a hardware programmer.

At first, I’m going to for something simple as proof-of-concept – automatic landing lights. An ultrasonic range sensor will point downward from the plane and switch on some super-bright LEDs when the height is below two meters. It’s nothing complicated, but it’s a good starting point to allow me to figure out how this stuff will fit into an RC plane, and how to make it robust enough to survive the inevitable crash landings.

Beyond that, I’m going to use a spare RC channels to allow me to switch an ‘autopilot mode’ on and off. After all, I want to be able to get manual control back if the software crashes! When autopilot is on, the microcontroller will take input from a range of sensors (IR horizon finding or inclinometer/gyro-combo for attitude, pressure sensor for altitude, GPS for position). It’ll figure out what it wants to do next, and move the control surfaces appropriately. Things like automatic landing modes have been done before and sound pretty possible. There are also light-weight wireless video systems which people have put into planes. And you can buy fairly cheap 433Mhz radio link chips which’d allow telemetry to be sent from the plane to a ground station.

What’s the point in this? It’s a blend between three of my interests. I’m interested in the physics/engineering aspect of flight and aerodynamic design. I enjoy doing simple hardware systems, and nowadays simple hardware systems can do lots of cool stuff. Finally, I like working on reliable/critical software systems. So, dealing with the realtime aspects of this project will be new and fun. As an aside, I’ve been reading about RTLinux and RTAI which are pretty cool. It reminds me of first time I saw the SoftICE debugger. There’s something remarkable about being able to pause an entire operating system!

On the software side, I’d be happier in a high-level language. But embedded systems are traditionally done in C, Forth or assembly in order to meet hard realtime demands. If you used a garbage collected language, a GC pause could mean that you miss a critical event. There’s been research into incremental collectors with low overhead for embedded systems though. And, typically, you have less need for dynamic memory allocation in an embedded system. But people have tried targetting high level language to embedded platform. Someone had a go at running nhc98 haskell runtime on a palm pda, and someone else did scheme on a PIC (PDF). If you were to use a high-level language in an embedded system, you’d want to have some guarantees about its time and space performance, and this is what the Hume project at St Andrews uni is looking at. And if you think that HLL can’t live on the bare metal, take a look at this Haskell OS project.

Finally, the “testing” side of this project is pretty interesting. I can obviously unit test the software before it goes onboard the plane. But how do you do integration testing? One answer is to place your flight-control software into a virtual world and make it think that it is actually flying. To achieve this, you can take advantage of the fact that Flightgear can send information about the plane (position, inclination, speed etc) out on a network port. You can grab this information, repackage it and send it to the flight-control software running inside a simulator (no hardware involved). The flight control software ultimately sends signals to servo, so you need to read these signal, map them back into flightgear-speak and push them across the network to flightgear. This way, you can see how the flight-control software behaves in high winds without risking the model plane itself!

Ah, that’s all for now. I’m well aware that I often start projects with grand plans and then get distracted by Other Things. This project is pretty amenable to the old ‘put it on the shelf for a few months’ treatment. It’s not “all or nothing” like some of my previous projects. So I am pretty hopeful of building something k3wl over the next few months and having a semi-autonomous plane buzzing around the skies.