Data Acquisition with Comedi
One example of an application for DAQ and Comedi is the Analytical Engineering, Inc. (AEI) airflow laboratory. In the AEI lab, airflow is generated by a fan and is forced through orifices of varying sizes. Using a custom-written software application, a technician can monitor the pressure buildup across the orifice. In turn, this pressure buildup can be used to calculate the approximate amount of air flowing across the orifice. This calculation is vital, because it allows a technician to determine whether various meter calibrations are correct.
However, the actual mass flow is more difficult to calculate completely. This number requires knowledge of two air pressures, three airflow temperatures, humidity, barometric pressure and altitude.
Off-the-shelf components exist for converting these measurements to voltage; one of the most popular interfaces is 5B. Using 5B modular blocks, it's possible to transform all of these measurements to voltages the DAQ card can read.
Using Comedi, reading these voltages becomes as trivial as using the comedi_data_read function. Calling this function and specifying a certain channel produces a resultant value, 3,421 for instance. But what does this number mean?
DAQ cards measure with a certain bit precision, 12 bits being the most common. They also specify a range or ranges of voltages over which they can be programmed to measure. Because a 12-bit number is represented from 0 to 4,095, it's easy to see that 3,421 is simply 3,421/4,095 * 100% of full scale (4,095). If the range of voltages is specified as [0, 5], then 3,421 would represent 4.177 volts.
Utilizing this information and knowing that the 5B block for temperature maps as [0 volts – 5 volts] → [0°C – 100°C], a small amount of programmatic math delivers a temperature of 83.56°C. Couple all of these measurements together, add a nice GUI interface and repeat the DAQ process every second.
More complex data acquisition can be performed as well. When acquiring data, it's important to make sure you sample fast enough so as not to miss any important information that occurs between samples. To support this, Comedi offers a command interface that can be used to set up synchronized sampling. Based on the sophistication of the DAQ card, timing can be handled by software interrupts or on-card interrupts.
Listing 1. Sample Program for Acquiring Voltage from One Channel
#include <stdio.h>
#include <comedilib.h>
const char *filename = "/dev/comedi0";
int main(int argc, char *argv[])
{
lsampl_t data;
int ret;
comedi_t *device;
/* Which device on the card do we want to use? */
int subdevice = 0;
/* Which channel to use */
int channel = 0;
/* Which of the available ranges to use */
int range = 0;
/* Measure with a ground reference */
int analogref = AREF_GROUND;
device = comedi_open(filename);
if(!device){
/* We couldn't open the device - error out */
comedi_perror(filename);
exit(0);
}
/* Read in a data value */
ret=comedi_data_read(device,subdevice,
channel,range,analogref,&data);
if(ret<0){
/* Some error happened */
comedi_perror(filename);
exit(0);
}
printf("Got a data value: %d\n", data);
return 0;
}
Comedi shines in most data acquisition applications. In fact, Comedi's limit generally resides in the hardware on which it's being run. Less expensive cards typically have a slower scan rate ability. For fast data acquisition, most of the higher priced cards come with onboard DMA, allowing an onboard processor to handle the acquisition and allowing Comedi simply to route the acquired buffered data.
Listing 2. Code Snippet Demonstrating More Advanced Scanning by Using Commands and Triggers
/* Goal: Set up Comedi to acquire 2 channels, and
scan each set twice. Perform the acquisition
after receiving a trigger signal on a digital
line.
*/
comedi_cmd c, *cmd=&c;
unsigned int chanlist[2];
/* CR_PACK is a special Comedi macro used to
setup a channel, a range, and a ground
reference
*/
chanlist[0] = CR_PACK(0,0,0);
chanlist[1] = CR_PACK(1,0,0);
/* Which subdevice should be used? */
/* Subdevice 0 is analog input on most boards */
cmd->subdev = 0;
cmd->chanlist = chanlist;
cmd->chanlist_len = n_chan;
/* Start command when an external digital line
is triggered. Use digital channel specified
in start_arg
*/
cmd->start_src = TRIG_EXT;
cmd->start_arg = 3;
/* begin scan immediately following trigger */
cmd->scan_begin_src = TRIG_FOLLOW;
cmd->scan_begin_arg = 0;
/* begin conversion immediately following scan */
cmd->convert_src = TRIG_NOW;
/* end scan after acquiring
scan_end_arg channels
*/
cmd->scan_end_src = TRIG_COUNT;
cmd->scan_end_arg = 2;
/* Stop the command after stop_arg scans */
cmd->stop_src = TRIG_COUNT;
cmd->stop_arg = 2;
/* Start the command */
comedi_cmd(device, cmd);
Fast scan rates don't translate to fast processing, however. Due to the non-deterministic nature of the stock Linux kernel, it's virtually impossible to handle acquisition and processing in real time—that is, to maintain strict scheduling requirements for a process. Help is available, however. The Linux Real-Time Application Interface (RTAI) and RTLinux are two of a small number of add-on packages that allow for better timing control in the kernel. Both packages provide interfaces to Comedi.
The basic idea behind these real-time interfaces is simple. Instead of running the kernel as the monolithic process, run it as a child of a small and efficient scheduler. This design prevents the kernel from blocking interrupts and allows it to be preempted. Then, any application that needs real-time control of the system can register itself with the scheduler and preempt the kernel as often as it needs to.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Developer Poll
- Dart: a New Web Programming Experience
- What's the tweeting protocol?
- Readers' Choice Awards
- Linux is good
1 hour 23 min ago - Reply to comment | Linux Journal
1 hour 40 min ago - Web Hosting IQ
2 hours 10 min ago - Web Hosting IQ
2 hours 11 min ago - Web Hosting IQ
2 hours 11 min ago - Reply to comment | Linux Journal
5 hours 12 min ago - play with linux? i think you mean work-around linux
13 hours 38 min ago - Where is Epistle?
13 hours 44 min ago - You forgot OwnCloud
14 hours 14 min ago - aplikasi free
17 hours 28 min ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.






Comments
Thanks
Thanks for your article.
I learned lots of things.
I want to use comedi and Qt to develop an DAQ Software for my undergraduate project, and this information was supportive one for me.