CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

ADC -- Potentiometer that measures tilt angle

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Bill S.
Guest







ADC -- Potentiometer that measures tilt angle
PostPosted: Fri Nov 11, 2005 4:37 pm     Reply with quote

Hello,

Was wondering if you guys can help me out.


I have a potentiometer on a pendulum pole (placed straight above the car's roof) that will detect my R/C car's tilt angle. Input voltage to the 10K linear potentiometer is 5V. At rest the voltage is at 2.5V. My range will be from 1.5V - 3.5 V depending oh how far my pendulum swings.

When the voltage changes I would like to figure out the angle of the car based on the voltage change (eg. 10 degrees left or right ). Is there a way to create a "lookup" table to quickly determine the angle (put into a variable called angle) when a certain voltage is applied?


So far I got this: (Used http://mdubuc.freeshell.org/Common/gp2d12.c as an example...... unless there is another way for a lookup table.

I will manually figure out the angles (left and right) based on different voltages I apply )




Code:
/*Tilt angle interface */
uint8_t get_tiltangle(uint8_t tiltangle);
void monitor_tiltangle_sensor(void);


/* Variable definitions */
???
???
static const uint8_t tilt_calib_data[NUM_TILT_CALIB_DATA] = {
  124, 107, 93, 83, 75, 67, 62, 57, 53, 49, 47, 44, 42, 39, 38, 36,
   34,  33, 32, 30, 29, 28, 27, 26, 25, 25, 24, 23, 23, 22, 21 }
???
??? 


};


/****************************************************/
void main (void) {

  for ( ; ; ) {
     monitor_tiltangle_sensor();
     display_tiltangle();
  }

}
/****************************************************/

/****************************************************/
uint8_t   get_tiltangle(uint8_t tiltreading) {
  int i;

??
??
??
??


} /* get_tiltangle */
/****************************************************/


/****************************************************/
void monitor_tiltangle_sensor(void) {

   setup_adc_ports(A_ANALOG);
   setup_adc(ADC_CLOCK_DIV_8);

   set_adc_channel(ANGLE_ANCH);
   value = Read_ADC();
   setup_adc(ADC_OFF);
   angle = get_tiltangle(value);

}
/****************************************************/



What I'm basically doing is if the angle is above a certain threshold say 20 degrees...I will slow down the RC vehicle so it will not rollover (pole is approx 3m high , so it will flip over if the car is travelling fast).

Also, would I just be needing one input port on the PIC controller to determine the voltages of the poteniometer?? eg. RA2.



THank you,
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Fri Nov 11, 2005 9:58 pm     Reply with quote

You are correct - you need only one analog input on the pic to read the pot. The resistance of your pot is too high though. The PIC's datasheet asks for the output impedance less then 2.5k. With 10k pot the input impedance will be in the neiborhoud of 5k. I would recommend getting a pot with R < 2.5k or buffering your signal with an op-amp.

I think you have a good example of a lookup table. However, I don't think you need a lookup table, because you don't need to use trigonometric functions.

Here's the schematic, which shows your pot as 2 resistors:

Code:

Vcc
 |
 \
 /
 \ R1 = a * r
 /
 \
 /    wiper of your pot
 |<------------------------------->>V  to the analog input of the PIC
 \
 /
 \ R2 = (p - a) * r = R - R1
 /
 \
 /
 |
///
GND

Where:
V                  voltage observed by the A/D [V] edit: or [a/d counts]
Vcc              PIC's supply voltage [V] or 255 A/D counts or 1024 A/D counts
p                  total angular swing of your pot [deg]
R = R1 + R2        total resistance of your pot [ohm]
a                  angle of your pendulum meaured from the extreme position of the pot [deg]
r = R/p            is resistance of your pot per degree [ohm/deg]


For a resistive divider we can write:

V/Vcc = R2/(R1+R2) = a*r/R

Trivially, the angle is

a = (v/Vcc)(R/r)
Bill S.
Guest







PostPosted: Mon Nov 14, 2005 6:21 pm     Reply with quote

Thank you for the reply. I did the following:

Code:
void
monitor_angle_sensor(void) {
   /* Read ADC */
  setup_adc_ports(ALL_ANALOG);
  setup_adc(ADC_CLOCK_INTERNAL);

  set_adc_channel(ANGLE_CHANNEL); /* Read Port A1/AN1 */

  delay_us(ADC_DELAY); 
  angle = Read_ADC();

}



This gave me a number from 0 (V) - 1024 (5V)
At rest, my potentiometer ouptuts around 2.5V so I got a number of 508.. when I swing either left or right.. i get a range from 430 - 540.
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Mon Nov 14, 2005 6:44 pm     Reply with quote

Bill S. wrote:

Code:

  setup_adc_ports(ALL_ANALOG);
  setup_adc(ADC_CLOCK_INTERNAL);



Actually, you don't need to call these 2 functions every time you make an A/D conversion. You can call them just once in the main(), unless the ADC ports and clock are changing.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group