|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
Analog-to-Digital Converter Code
For those who might be interested, I've just posted new analog-to-digital converter interface code for IFIs robot controllers. A few advantages of using this software include parallel, non-blocking operation of the ADC hardware, precise control over the sampling period, which is important for signal processing and control applications and the ability to make measurements with greater precision using a technique called oversampling. The code can be found here: http://kevin.org/frc. As always, if you find a bug in the code or a problem with the documentation, please let me know.
-Kevin Here's the readme file: Code:
The source code in adc.c/.h contains software to setup and run your robot controllers analog-to-digital converter in the background. A few advantages of using this software include parallel, non-blocking operation of the ADC hardware, precise control over the sampling period, which is important for signal processing and control applications and the ability to make more precise measurements using oversampling. Number of channels, sample rate and oversampling parameters can be changed by editing adc.h and re-compiling. This software makes the assumption that when it's running, it solely controls the analog to digital conversion hardware. WARNING: The higher sampling rates can place quite a load on the robot controllers CPU. If you get the red-light-of-death or notice any general wackiness in the operation of your robot, first try a lower sampling rate to determine if this is the problem. In general, you should select the lowest sampling rate that meets your performance criteria. This source code will work with the Robovation (A/K/A EDU-RC) robot controller and the FIRST Robotics robot controller. The included project files were built with MPLAB version 7.20. If your version of MPLAB complains about the project version, the best thing to do is just create a new project with MPLAB's project wizard. Include every file except: FRC_alltimers.lib and ifi_alltimers.lib and you should be able to build the code. **************************************************************** Eight things must be done before this software will work correctly with your robot controller: 1) A #include statement for the adc.h header file must be included at the beginning of each source file that calls the functions in adc.c. The statement should look like this: #include "adc.h". 2) Initialize_ADC() must be called from user_routines.c/ User_Initialization(). 3) On the EDU-RC, all analog inputs must be configured as INPUTs in user_routines.c/User_Initialization(). 4) The call to Set_Number_of_Analog_Channels() must be removed or commented out in user_routines.c/User_Initialization(). 5) The timer 2 interrupt handler, Timer_2_Int_Handler(), and ADC interrupt handler, ADC_Int_Handler(), must be installed in User_Routines_Fast.c/InterruptHandlerLow(). See the included copy of User_Routines_Fast.c to see how this is done. 6) Define the number of analog channels that you'd like this software to track by opening adc.h and following the embedded instructions above #define NUM_ADC_CHANNELS. 7) For advanced users, analog channels can be oversampled to decrease noise and gain resolution in your analog measurements. The oversampling ratio can be changed from the default x16 by commenting-out the line #define ADC_SAMPLES_PER_UPDATE_16 found in adc.h and then removing the // from in front of one of the other options. Measurement range and resolution can be determined from this table: Last edited by Kevin Watson : 05-09-2005 at 14:22. Reason: Added adc_readme.txt text |
|
#2
|
|||||
|
|||||
|
Re: Analog-to-Digital Converter Code
wow thanks allot!
|
|
#3
|
||||
|
||||
|
Re: Analog-to-Digital Converter Code
I've updated the ADC code to version 0.3. The new version uses the "special event trigger" functionality of the CCP2 module to automatically start an analog to digital conversion without software intervention. This new version generates half of the interrupts/sec of version 0.2. As always, the code can be found here: http://kevin.org/frc
-Kevin Last edited by Kevin Watson : 16-12-2005 at 02:18. Reason: Added link to the code. |
|
#4
|
||||
|
||||
|
Re: Analog-to-Digital Converter Code
Well, I finally posted the ADC and Gyro code for the 2006 robot controller. Sorry about the delay, but I ran into a nasty bug that caused me to spend a few days in the fourth and fifth levels of programmer hell. The bug was in the design of the PIC18F8722 (yes, a bug in the sillicon) and I had to figure out what was going on. Anyway, the gyro code works really, really well with the gyro in the kit of parts. The accelerometers also work really well, but I need to do more testing before I unleash that code.
-Kevin |
|
#5
|
||||
|
||||
|
Re: Analog-to-Digital Converter Code
Let's suppose there are four analog signals being sampled and "buffered" by your code. The gyro is running on ADC_CH0 and the other three channels are for other devices. How do we know when the gyro specifically has new data available? I saw the ~update_count~ variable, but it seemed to just indicate when any new sample is available, not when the sample for a specific channel is. Am I going to have to modify your code (maybe add an array of update_counts, with each index correspondent with the channel's freshness of data?) to allow this functionality, or is it already there and I'm just not seeing it?
|
|
#6
|
||||
|
||||
|
Re: Analog-to-Digital Converter Code
Quote:
-Kevin |
|
#7
|
|||||
|
|||||
|
Re: Analog-to-Digital Converter Code
Hi Kevin...
Excellent code. I'm trying to get some early Auto-Heading code testing done using an existing Robot. I was hoping I could use the team's little 2003 EDU robot as a test platform. I'm an experienced embedded programmer, but new to the IFI hardware. I tried deploying your gyro code on the small EDU controller. I discovered that when I included the new ADC module I got a program run error light, so I dropped back to the standard iti_utilities code for the ADC conversion. (I assumed a basic hardware incompatibility....) My only problem is that now I'm not sure where to put the call to Process_Gyro_Data(). I'd like to put it in User_Routines_Fast.c but I don't have your ADC code installed so I don't know when the converters have got new values. D'oh. Am I flogging a dead horse here, or is there a simple way to use the standard IFI analog utilities to drive your Gyro code? I'm only using a single analog channel (for the Gyro) so that may simplify things. I don't need fully calibrated data for my initial testing... I just need it to basically run. If I can get the calls IN, I can get the rest to work. Thanks for your time. Phil. Quote:
|
|
#8
|
||||
|
||||
|
Re: Analog-to-Digital Converter Code
Quote:
Gyro code for the EDU-RC can be found on my website. -Kevin |
|
#9
|
|||||
|
|||||
|
Re: Analog-to-Digital Converter Code
Oops.
I'm in "drinking from a fire-hose" mode and never seem to look in the right place for information. Just when I think I've downloaded enough... I find more.. After posting, I discovered your adc_readme file which let me go through and add your stuff to our teem's prior EDU-RC code. Now I don't get an error & the gyro seems to be working as with the 2006RC. Now with this new information I can double check against your webiste archives if need be. Thanks. I'll look harder next time. Phil Quote:
|
|
#10
|
|||
|
|||
|
Re: Analog-to-Digital Converter Code
Quote:
The adc_readme.txt says to remove the call to Set_Number_of_Analog_Channels() in user_routines.c. But the user_routines.c file included in the same zip (frc_adc.zip) still has it in. Same situation in the user_routines.c file in the frc_gyro.zip. So, should the call remain as is, or be commented out? Thanks |
|
#11
|
||||
|
||||
|
Re: Analog-to-Digital Converter Code
Quote:
-Kevin |
|
#12
|
||||
|
||||
|
Re: Analog-to-Digital Converter Code
Okay, I know this is getting kind of abstract, and not truly necessary to a "black box" understanding of this code, but how exactly does oversampling work? I looked it up on wikipedia, and all i got was more confused. I understand that is is supposed to give more precision, but I really don't understand how.
Thanks. |
|
#13
|
|||||
|
|||||
|
Re: Analog-to-Digital Converter Code
Oversampling is essentially taking a number of samples and averaging them. If you average four samples, you get the equivalent of two more bits of resolution, but it takes four times as long to get the value. It trades time for precision.
|
|
#14
|
||||
|
||||
|
Re: Analog-to-Digital Converter Code
so then does it take values 4x faster? It seems like taking the average over 4 program cycles would make it less precise.
|
|
#15
|
||||
|
||||
|
Re: Analog-to-Digital Converter Code
Yes, it samples faster than the rate the data is being sent.
I have another question for Kevin (of course, I'm trying to optimize everything, but I don't want to make a bad move while doing this): Is there a specific reason for switching from doing both the initiation of and saving of an ADC sample in the timer interrupt? I remember in last year's gyro code that both functions were done in the same place, whereas now you are using both the timer interrupt, and the ADC interrupt to take the samples. Is it safe to kinda shift the ADC functionality to the timer interrupt and reduce the extra overhead by removing the ADC interrupt altogether? Or do the two of them together execute faster, somehow, than the single timer? Before you: - Started the initial A/D conversion in Initialize_XXXX() - Saved the value at the start of the timer2 interrupt - Did the if/else/etc to put the value in the right place - Started another A/D conversion by setting the GO bit. - Exited the timer2 interrupt. And if I get the go ahead, I'll just do this again. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Out of the Box Camera Code | russell | Programming | 9 | 21-10-2009 05:28 |
| Updated: Example Gyro Interface Code | Kevin Watson | Programming | 2 | 28-03-2005 04:36 |
| Example gyro code released. | Kevin Watson | Programming | 60 | 17-03-2005 18:32 |
| Team THRUST - Kevin's Code and Camera Code Combine | Chris_Elston | Programming | 3 | 31-01-2005 22:28 |
| heres the code. y this not working | omega | Programming | 16 | 31-03-2004 15:18 |