AXIS: EMC2 Blog|
Blog
About AXIS Get EMC2 Documentation Translations ETCH CNC Developer Homepages: Chris Radek Jeff Epler Related sites: linuxcnc.org linuxcnc wiki |
I recently got an arduino board. The AVR
microcontroller on it has 6 10-bit ADCs. The version I got has a USB
connection, so I decided to write an analog input firmware and associated
userspace HAL driver in python. It requires pyserial, available on ubuntu
with 'sudo apt-get install python-serial.
The "packet" structure is very simple: each ADC reading consists of two bytes with the following structure:
Communication takes place at 9600 baud, and it takes 120 bits (including start and stop bits) to update all analog inputs. This makes the maximum update rate 80Hz. However, some additional time is required for the analogRead() function to execute, and there are no timing guarantees for the HAL userspace component running on the PC. The program consists of two parts. First, the part that runs on the AVR, "adc6.pde" (see Listing 1). Second, the part that runs on the PC, "arduino-adc6.py" (see Listing 2). I don't have any analog sources handy, but instead I hooked the several supply voltages available up to the first three analog inputs. Here's a sample session: The nominal voltages are 0V, 5V, and 3.3V. It looks like the AVR's "sample and hold" circuitry tends to read near the previous value when the input is actually floating.$ halrun halcmd: loadusr -Wn arduino-adc6 python ./arduino-adc6.py halcmd: show pin Component Pins: Owner Type Dir Value Name 28114 float OUT 0 arduino-adc6.analog-in-0 28114 float OUT 5 arduino-adc6.analog-in-1 28114 float OUT 3.377322 arduino-adc6.analog-in-2 28114 float OUT 3.333333 arduino-adc6.analog-in-3 28114 float OUT 3.29912 arduino-adc6.analog-in-4 28114 float OUT 3.294233 arduino-adc6.analog-in-5 There are some obvious next steps: First, there are 6 bidirectional digital I/O pins available. It would be nice to read these. Second, it would be nice to use some of them as outputs, and also use the 6 8-bit PWM generators that are available. Besides the caveat that this driver is not realtime, it is also important to note that neither USB nor RS232 serial provide isolation, and that only voltages in the range 0..5V may be applied to the AVR's ADC pins. Listing 1: adc6.pde
Listing 2: arduino-adc6.py
|
| [æ] |