|
|
|
@ -1,61 +1,61 @@
@@ -1,61 +1,61 @@
|
|
|
|
|
/*
|
|
|
|
|
AP_ADC_ADS7844.cpp - ADC ADS7844 Library for Ardupilot Mega |
|
|
|
|
Code by Jordi Mu<EFBFBD>oz and Jose Julio. DIYDrones.com |
|
|
|
|
|
|
|
|
|
Modified by John Ihlein 6 / 19 / 2010 to: |
|
|
|
|
1)Prevent overflow of adc_counter when more than 8 samples collected between reads. Probably |
|
|
|
|
only an issue on initial read of ADC at program start. |
|
|
|
|
2)Reorder analog read order as follows: |
|
|
|
|
p, q, r, ax, ay, az |
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and / or |
|
|
|
|
modify it under the terms of the GNU Lesser General Public |
|
|
|
|
License as published by the Free Software Foundation; either |
|
|
|
|
version 2.1 of the License, or (at your option) any later version. |
|
|
|
|
|
|
|
|
|
External ADC ADS7844 is connected via Serial port 2 (in SPI mode) |
|
|
|
|
TXD2 = MOSI = pin PH1 |
|
|
|
|
RXD2 = MISO = pin PH0 |
|
|
|
|
XCK2 = SCK = pin PH2 |
|
|
|
|
Chip Select pin is PC4 (33) [PH6 (9)] |
|
|
|
|
We are using the 16 clocks per conversion timming to increase efficiency (fast) |
|
|
|
|
/*
|
|
|
|
|
AP_ADC_ADS7844.cpp - ADC ADS7844 Library for Ardupilot Mega |
|
|
|
|
Code by Jordi Mu<EFBFBD>oz and Jose Julio. DIYDrones.com |
|
|
|
|
|
|
|
|
|
Modified by John Ihlein 6 / 19 / 2010 to: |
|
|
|
|
1)Prevent overflow of adc_counter when more than 8 samples collected between reads. Probably |
|
|
|
|
only an issue on initial read of ADC at program start. |
|
|
|
|
2)Reorder analog read order as follows: |
|
|
|
|
p, q, r, ax, ay, az |
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and / or |
|
|
|
|
modify it under the terms of the GNU Lesser General Public |
|
|
|
|
License as published by the Free Software Foundation; either |
|
|
|
|
version 2.1 of the License, or (at your option) any later version. |
|
|
|
|
|
|
|
|
|
External ADC ADS7844 is connected via Serial port 2 (in SPI mode) |
|
|
|
|
TXD2 = MOSI = pin PH1 |
|
|
|
|
RXD2 = MISO = pin PH0 |
|
|
|
|
XCK2 = SCK = pin PH2 |
|
|
|
|
Chip Select pin is PC4 (33) [PH6 (9)] |
|
|
|
|
We are using the 16 clocks per conversion timming to increase efficiency (fast) |
|
|
|
|
|
|
|
|
|
The sampling frequency is 1kHz (Timer2 overflow interrupt) |
|
|
|
|
|
|
|
|
|
So if our loop is at 50Hz, our needed sampling freq should be 100Hz, so |
|
|
|
|
So if our loop is at 50Hz, our needed sampling freq should be 100Hz, so |
|
|
|
|
we have an 10x oversampling and averaging. |
|
|
|
|
|
|
|
|
|
Methods: |
|
|
|
|
Init() : Initialization of interrupts an Timers (Timer2 overflow interrupt) |
|
|
|
|
Ch(ch_num) : Return the ADC channel value |
|
|
|
|
|
|
|
|
|
// HJI - Input definitions. USB connector assumed to be on the left, Rx and servo
|
|
|
|
|
// connector pins to the rear. IMU shield components facing up. These are board
|
|
|
|
|
// referenced sensor inputs, not device referenced.
|
|
|
|
|
On Ardupilot Mega Hardware, oriented as described above: |
|
|
|
|
Chennel 0 : yaw rate, r |
|
|
|
|
Channel 1 : roll rate, p |
|
|
|
|
Channel 2 : pitch rate, q |
|
|
|
|
Channel 3 : x / y gyro temperature |
|
|
|
|
Channel 4 : x acceleration, aX |
|
|
|
|
Channel 5 : y acceleration, aY |
|
|
|
|
Channel 6 : z acceleration, aZ |
|
|
|
|
Channel 7 : Differential pressure sensor port |
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
|
extern "C" { |
|
|
|
|
// AVR LibC Includes
|
|
|
|
|
#include <inttypes.h> |
|
|
|
|
|
|
|
|
|
Methods: |
|
|
|
|
Init() : Initialization of interrupts an Timers (Timer2 overflow interrupt) |
|
|
|
|
Ch(ch_num) : Return the ADC channel value |
|
|
|
|
|
|
|
|
|
// HJI - Input definitions. USB connector assumed to be on the left, Rx and servo
|
|
|
|
|
// connector pins to the rear. IMU shield components facing up. These are board
|
|
|
|
|
// referenced sensor inputs, not device referenced.
|
|
|
|
|
On Ardupilot Mega Hardware, oriented as described above: |
|
|
|
|
Chennel 0 : yaw rate, r |
|
|
|
|
Channel 1 : roll rate, p |
|
|
|
|
Channel 2 : pitch rate, q |
|
|
|
|
Channel 3 : x / y gyro temperature |
|
|
|
|
Channel 4 : x acceleration, aX |
|
|
|
|
Channel 5 : y acceleration, aY |
|
|
|
|
Channel 6 : z acceleration, aZ |
|
|
|
|
Channel 7 : Differential pressure sensor port |
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
|
extern "C" { |
|
|
|
|
// AVR LibC Includes
|
|
|
|
|
#include <inttypes.h> |
|
|
|
|
#include <stdint.h> |
|
|
|
|
#include <avr/interrupt.h> |
|
|
|
|
#include "WConstants.h" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#include "AP_ADC_ADS7844.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Commands for reading ADC channels on ADS7844
|
|
|
|
|
static const unsigned char adc_cmd[9] = { 0x87, 0xC7, 0x97, 0xD7, 0xA7, 0xE7, 0xB7, 0xF7, 0x00 }; |
|
|
|
|
#include <avr/interrupt.h> |
|
|
|
|
#include "WConstants.h" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#include "AP_ADC_ADS7844.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Commands for reading ADC channels on ADS7844
|
|
|
|
|
static const unsigned char adc_cmd[9] = { 0x87, 0xC7, 0x97, 0xD7, 0xA7, 0xE7, 0xB7, 0xF7, 0x00 }; |
|
|
|
|
|
|
|
|
|
// the sum of the values since last read
|
|
|
|
|
static volatile uint32_t _sum[8]; |
|
|
|
@ -78,12 +78,12 @@ static inline unsigned char ADC_SPI_transfer(unsigned char data)
@@ -78,12 +78,12 @@ static inline unsigned char ADC_SPI_transfer(unsigned char data)
|
|
|
|
|
/* Put data into buffer, sends the data */ |
|
|
|
|
UDR2 = data; |
|
|
|
|
/* Wait for data to be received */ |
|
|
|
|
while ( !(UCSR2A & (1 << RXC2)) ); |
|
|
|
|
/* Get and return received data from buffer */ |
|
|
|
|
return UDR2; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while ( !(UCSR2A & (1 << RXC2)) ); |
|
|
|
|
/* Get and return received data from buffer */ |
|
|
|
|
return UDR2; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ISR (TIMER2_OVF_vect) |
|
|
|
|
{ |
|
|
|
|
uint8_t ch; |
|
|
|
@ -127,13 +127,13 @@ ISR (TIMER2_OVF_vect)
@@ -127,13 +127,13 @@ ISR (TIMER2_OVF_vect)
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Constructors ////////////////////////////////////////////////////////////////
|
|
|
|
|
AP_ADC_ADS7844::AP_ADC_ADS7844() |
|
|
|
|
{ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
|
|
|
|
void AP_ADC_ADS7844::Init(void) |
|
|
|
|
// Constructors ////////////////////////////////////////////////////////////////
|
|
|
|
|
AP_ADC_ADS7844::AP_ADC_ADS7844() |
|
|
|
|
{ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
|
|
|
|
void AP_ADC_ADS7844::Init(void) |
|
|
|
|
{ |
|
|
|
|
pinMode(ADC_CHIP_SELECT, OUTPUT); |
|
|
|
|
|
|
|
|
@ -172,15 +172,15 @@ void AP_ADC_ADS7844::Init(void)
@@ -172,15 +172,15 @@ void AP_ADC_ADS7844::Init(void)
|
|
|
|
|
|
|
|
|
|
// Read one channel value
|
|
|
|
|
uint16_t AP_ADC_ADS7844::Ch(uint8_t ch_num) |
|
|
|
|
{ |
|
|
|
|
{ |
|
|
|
|
uint16_t count; |
|
|
|
|
uint32_t sum; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ensure we have at least one value
|
|
|
|
|
while (_count[ch_num] == 0) /* noop */ ; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// grab the value with interrupts disabled, and clear the count
|
|
|
|
|
cli(); |
|
|
|
|
cli(); |
|
|
|
|
count = _count[ch_num]; |
|
|
|
|
sum = _sum[ch_num]; |
|
|
|
|
_count[ch_num] = 0; |
|
|
|
@ -204,8 +204,8 @@ uint32_t AP_ADC_ADS7844::Ch6(const uint8_t *channel_numbers, uint16_t *result)
@@ -204,8 +204,8 @@ uint32_t AP_ADC_ADS7844::Ch6(const uint8_t *channel_numbers, uint16_t *result)
|
|
|
|
|
// ensure we have at least one value
|
|
|
|
|
for (i=0; i<6; i++) { |
|
|
|
|
while (_count[channel_numbers[i]] == 0) /* noop */; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// grab the values with interrupts disabled, and clear the counts
|
|
|
|
|
cli(); |
|
|
|
|
for (i=0; i<6; i++) { |
|
|
|
@ -214,8 +214,8 @@ uint32_t AP_ADC_ADS7844::Ch6(const uint8_t *channel_numbers, uint16_t *result)
@@ -214,8 +214,8 @@ uint32_t AP_ADC_ADS7844::Ch6(const uint8_t *channel_numbers, uint16_t *result)
|
|
|
|
|
_count[channel_numbers[i]] = 0; |
|
|
|
|
_sum[channel_numbers[i]] = 0; |
|
|
|
|
} |
|
|
|
|
sei(); |
|
|
|
|
|
|
|
|
|
sei(); |
|
|
|
|
|
|
|
|
|
// calculate averages. We keep this out of the cli region
|
|
|
|
|
// to prevent us stalling the ISR while doing the
|
|
|
|
|
// division. That costs us 36 bytes of stack, but I think its
|
|
|
|
|