Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Analog-to-Digital Converter Code (http://www.chiefdelphi.com/forums/showthread.php?t=39536)

Joel J 23-01-2006 00:34

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.

Joe Ross 23-01-2006 11:59

Re: Analog-to-Digital Converter Code
 
Quote:

Originally Posted by Chriszuma
so then does it take values 4x faster? It seems like taking the average over 4 program cycles would make it less precise.

The following whitepaper goes much more in depth and may make more sense: http://www.chiefdelphi.com/forums/pa...le&paperid=234

mgurgol 23-01-2006 17:16

Re: Analog-to-Digital Converter Code
 
Quote:

Originally Posted by Kevin Watson
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

I have downloaded the FRC_GYRO code, compiled and linked it in MPLAB, and it controls the gyro great, thanks.

When I copy the FRC_Library into my 2006 project code (to update from the 8250 (2005) to the 8722 (2006) controller, I am getting a link error saying that symbol ifi_packet_num1 has multiple definitions. I was able to determine that there is a copy of ifi_utilities.o (where ifi_packet_num1 is defined) in the FRC_Library (that wasn't there in 2005). I was able to work around the problem by removing ifi_utilites.c from the project, and it linked fine.

My question is: Is there some setting I am missing in my project that would allow the linker to use the first instance of the object file it comes across versus including all versions. I see ifi_utilities.c in the frc_gyro (gyro.mcp) project, and the link works correctly. Since I don't modify ifi_utilities.c my work around shouldn't be a problem, but I would like to know why one project will link correctly, while another one won't.

Thanks, Mark

Chriszuma 23-01-2006 23:09

Re: Analog-to-Digital Converter Code
 
Quote:

Originally Posted by Joe Ross
The following whitepaper goes much more in depth and may make more sense: http://www.chiefdelphi.com/forums/pa...le&paperid=234

Wow, that really explained it well. Thanks.

Joel J 23-01-2006 23:22

Re: Analog-to-Digital Converter Code
 
Quote:

Originally Posted by Joel J.
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.

As I was waiting for your reply I took a look at the spec sheet for the PIC18f8722. It says there is a set delay of at least 2.5 microseconds that has to exist between the setting of an analog channel and the initiation of its conversion (to allow the sample and hold capacitor to start charging). Is this the reason for the seperation of the two actions?

I also see the following:
Quote:

Acquisition time may be set with the ACQT2:ACQT0 bits (ADCON2<5:3>) which provides a range of 2 to 20 TAD. When the GO/DONE bit is set, the A/D module continues to sample the input for the selected acquisition time, then automatically begins a conversion. Since the acquisition time is programmed, there may be no need to wait for an acquisition time between selecting a channel and setting the GO/DONE bit.
which makes it seem as though the PIC could implement a delay by itself. But taking another look at your code, I noticed that you enabled manual acquisition (ADCON2<5:3> = 0). Did you find the PIC to be unreliable in this area, or have I totally missed the point?

I'll wait for your feedback before continuing.

Kevin Watson 24-01-2006 00:22

Re: Analog-to-Digital Converter Code
 
Quote:

Originally Posted by Joel J.
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.



This is a great question. When I modified the code last Summer to separate the ADC and gyro functionality, I added the ability to sample multiple ADC channels using a simple round-robin algorithm. If you read the pic18f8520 data sheet you'll notice the second paragraph on page 217 which states:
"After the A/D module has been configured as desired, the selected channel must be acquired before the conversion is started...To determine acquisition time, see Section19.1 “A/D Acquisition Requirements”. After this acquisition time has elapsed, the A/D conversion can be started."

What this means is that you need to allow sufficient time for the sample-and-hold capacitor to charge between the time you change the channel and can actually start the conversion. Providing this delay within an ISR is, to say the least, un-cool. My solution was to start the conversion using the timer 2 ISR and manage data and change the ADC channel in the ADC ISR. The sample-and-hold capacitor then has ample time to charge between the ADC and timer 2 ISRs. This is how the currently posted code works.

The pic18f8722, used in the current robot controller, has added circuitry to automatically provide the acquisition delay before conversion begins (see page 277 of the p18f8722 data sheet). In the next revision of the ADC code I suspect that I'll be able to do away with the ADC interrupt and do everything in the timer 2 ISR. Perhaps this would be a good programming exercise for you?

-Kevin

Joel J 24-01-2006 00:34

Re: Analog-to-Digital Converter Code
 
ok, great!

I'll modify your code for use now, and then I'll swap in your next revision when you make it gold.

Thanks for the feedback.

McLOVIN 08-02-2008 19:32

Re: Analog-to-Digital Converter Code
 
For some reason our team is having great difficulty in getting the analog inputs to work, no matter what we plug into the inputs or print. We have initialized them and receive no errors yet no inputs are received. We have attempted testing a gyro, accelerometer, ultrasonic sensor, and variable resistor (potentiometer) but none have made a difference.

lukevanoort 08-02-2008 19:38

Re: Analog-to-Digital Converter Code
 
Quote:

Originally Posted by McLOVIN (Post 694837)
For some reason our team is having great difficulty in getting the analog inputs to work, no matter what we plug into the inputs or print. We have initialized them and receive no errors yet no inputs are received. We have attempted testing a gyro, accelerometer, ultrasonic sensor, and variable resistor (potentiometer) but none have made a difference.

There is a #define statement in adc.h that is something like NUMBER_OF_CHANNELS (I'm going from memory here, so I don't know exactly what the statement actually defines). Is that defined as 16? We had some analog input trouble last year when we only had 1 channel active and were trying to tune four PID loops using pots that were connected to analog ports that weren't enabled, which didn't work very well, but caused no compile-time errors.

McLOVIN 08-02-2008 19:44

Re: Analog-to-Digital Converter Code
 
Actually that wasn't the problem, but we seem to have fixed it. Our problem actually was that the interrupt Timer_4_ISR was defined in both adc.c, adc.h and timers.c and .h. No errors appeared but it didnt allow inputs.

Kevin Watson 08-02-2008 20:36

Re: Analog-to-Digital Converter Code
 
Quote:

Originally Posted by McLOVIN (Post 694845)
Actually that wasn't the problem, but we seem to have fixed it. Our problem actually was that the interrupt Timer_4_ISR was defined in both adc.c, adc.h and timers.c and .h. No errors appeared but it didnt allow inputs.

This is documented in the readme.txt file included with the software. Specifically, items 2 and 3:

Quote:

By default the analog to digital and gyro software is disabled.
Perform these steps to get your gyro working:
1) Enable the Initialize_ADC() and Initialize_Gyro() functions in
teleop.c/Initialization().
2) Enable the timer 4 interrupt service routine in ifi_frc.h
3) Make sure timer 4 is disabled at the top of timers.h.
4) Depending on the operating modes you will be using the gyro,
place calls to the Process_Gyro_Data() function in the disabled.c/
Disabled_Spin(), autonomous.c/Autonomous_Spin() and/or teleop.c/
Teleop_Spin() functions.
5) Add gyro bias calibration code that will execute before you
use your gyro. Example calibration code has been placed in
disabled.c/Disabled() that can be used for the competition (this
requires that you use a mode dongle to emulate the field
controller, which will put you in disabled mode for a period of
time before transitioning to autonomous mode). For testing
purposes you can also use the example code in teleop.c/Teleop()
to make sure your gyro is working.
6) Follow the instructions in adc_readme.txt, adc.h, gyro_readme.txt,
and gyro.h for information related to installation and calibration
of your gyro.
7) If building for the 2004 or 2005 PIC18F8520 based robot controller,
you will need to use the adc_8520.c source file instead of adc.c.
The header file adc.h will work with either version.
8) If building for the 2004 or 2005 PIC18F8520 based robot controller,
you will need to enable the ADC interrupt service routine in
ifi_frc.h
-Kevin

windell747 14-02-2008 22:11

Re: Analog-to-Digital Converter Code
 
Hi Kevin, We have recently come to realize that the p1_y and p1_x ADCs are somewhat noisy. The output value varies randomly maybe about +/-10 of 127 when the joystick is connected, but not touched. Does your code address this problem? We were trying to work around the problem using a comb digital filter.

Thanks,
Windell

Kevin Watson 14-02-2008 22:26

Re: Analog-to-Digital Converter Code
 
Quote:

Originally Posted by windell747 (Post 699147)
Hi Kevin, We have recently come to realize that the p1_y and p1_x ADCs are somewhat noisy. The output value varies randomly maybe about +/-10 of 127 when the joystick is connected, but not touched. Does your code address this problem? We were trying to work around the problem using a comb digital filter.

Thanks,
Windell

No, that code runs on the PIC inside the operator interface, which I didn't write. I would try using a running average of 2-4 samples to see if it quiets down.

-Kevin

srinivas 16-02-2008 19:52

Re: Analog-to-Digital Converter Code
 
Quote:

Originally Posted by Kevin Watson (Post 439136)
This is a great question. When I modified the code last Summer to separate the ADC and gyro functionality, I added the ability to sample multiple ADC channels using a simple round-robin algorithm. If you read the pic18f8520 data sheet you'll notice the second paragraph on page 217 which states:
"After the A/D module has been configured as desired, the selected channel must be acquired before the conversion is started...To determine acquisition time, see Section19.1 “A/D Acquisition Requirements”. After this acquisition time has elapsed, the A/D conversion can be started."

What this means is that you need to allow sufficient time for the sample-and-hold capacitor to charge between the time you change the channel and can actually start the conversion. Providing this delay within an ISR is, to say the least, un-cool. My solution was to start the conversion using the timer 2 ISR and manage data and change the ADC channel in the ADC ISR. The sample-and-hold capacitor then has ample time to charge between the ADC and timer 2 ISRs. This is how the currently posted code works.

The pic18f8722, used in the current robot controller, has added circuitry to automatically provide the acquisition delay before conversion begins (see page 277 of the p18f8722 data sheet). In the next revision of the ADC code I suspect that I'll be able to do away with the ADC interrupt and do everything in the timer 2 ISR. Perhaps this would be a good programming exercise for you?

-Kevin

Hello Kevin
When our team is using your FRC gyro code. there seems to be a problem. The motors are twitching. That is the arm and shoulder motors are coming back but are not staying in there.
Can u please tell us what to do about that. We caliberated the victors and checked the victors and checked the voltages. But when we use the default code without gyros the code works fine.

Please let us know what is happenning.

Thank you
Srinivas

Joe Ross 17-02-2008 13:07

Re: Analog-to-Digital Converter Code
 
Quote:

Originally Posted by srinivas (Post 700423)
Hello Kevin
When our team is using your FRC gyro code. there seems to be a problem. The motors are twitching. That is the arm and shoulder motors are coming back but are not staying in there.
Can u please tell us what to do about that. We caliberated the victors and checked the victors and checked the voltages. But when we use the default code without gyros the code works fine.

Please let us know what is happenning.

Thank you
Srinivas

What PWMs are your victors connected to? PWMs 13-16 are controlled by the user processor and subject to glitches under high load when using IFI's Generate_PWMs function. If you move those motors to another PWM or use Kevin's replacement PWM code, it should work better.


All times are GMT -5. The time now is 08:25.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi