2010-08-28 13:07:33 -03:00
|
|
|
/*
|
|
|
|
www.ArduCopter.com - www.DIYDrones.com
|
|
|
|
Copyright (c) 2010. All rights reserved.
|
|
|
|
An Open Source Arduino based multicopter.
|
|
|
|
|
|
|
|
File : Sensors.pde
|
|
|
|
Version : v1.0, Aug 27, 2010
|
|
|
|
Author(s): ArduCopter Team
|
|
|
|
Ted Carancho (aeroquad), Jose Julio, Jordi Muñoz,
|
|
|
|
Jani Hirvinen, Ken McEwans, Roberto Navoni,
|
|
|
|
Sandro Benigno, Chris Anderson
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2010-09-20 06:38:11 -03:00
|
|
|
* ************************************************************** */
|
2010-08-28 13:07:33 -03:00
|
|
|
|
2010-10-11 11:12:44 -03:00
|
|
|
/* ******* ADC functions ********************* */
|
|
|
|
// Read all the ADC channles
|
|
|
|
void Read_adc_raw(void)
|
|
|
|
{
|
|
|
|
//int temp;
|
|
|
|
|
|
|
|
for (int i=0;i<6;i++)
|
2010-11-27 00:56:31 -04:00
|
|
|
AN[i] = adc.Ch(sensors[i]);
|
2010-10-11 11:12:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns an analog value with the offset
|
|
|
|
int read_adc(int select)
|
|
|
|
{
|
|
|
|
if (SENSOR_SIGN[select]<0)
|
|
|
|
return (AN_OFFSET[select]-AN[select]);
|
|
|
|
else
|
|
|
|
return (AN[select]-AN_OFFSET[select]);
|
|
|
|
}
|
|
|
|
|
2010-09-20 06:38:11 -03:00
|
|
|
void calibrateSensors(void) {
|
2010-10-11 18:54:42 -03:00
|
|
|
int i;
|
2010-09-20 06:38:11 -03:00
|
|
|
int j = 0;
|
2010-10-11 18:54:42 -03:00
|
|
|
byte gyro;
|
|
|
|
float aux_float[3];
|
|
|
|
|
|
|
|
Read_adc_raw(); // Read sensors data
|
|
|
|
delay(5);
|
|
|
|
|
|
|
|
// Offset values for accels and gyros...
|
|
|
|
AN_OFFSET[3] = acc_offset_x; // Accel offset values are taken from external calibration (In Configurator)
|
|
|
|
AN_OFFSET[4] = acc_offset_y;
|
|
|
|
AN_OFFSET[5] = acc_offset_z;
|
|
|
|
aux_float[0] = gyro_offset_roll;
|
|
|
|
aux_float[1] = gyro_offset_pitch;
|
|
|
|
aux_float[2] = gyro_offset_yaw;
|
|
|
|
|
2010-09-20 06:38:11 -03:00
|
|
|
// Take the gyro offset values
|
2010-10-11 18:54:42 -03:00
|
|
|
for(i=0;i<600;i++)
|
|
|
|
{
|
|
|
|
Read_adc_raw(); // Read sensors
|
2010-10-30 11:38:52 -03:00
|
|
|
for(gyro = GYROZ; gyro <= GYROY; gyro++)
|
2010-10-30 05:30:46 -03:00
|
|
|
aux_float[gyro] = aux_float[gyro] * 0.8 + AN[gyro] * 0.2; // Filtering
|
|
|
|
Log_Write_Sensor(AN[0], AN[1], AN[2], AN[3], AN[4], AN[5], 0);
|
|
|
|
|
2010-09-20 06:38:11 -03:00
|
|
|
delay(5);
|
2010-10-30 05:30:46 -03:00
|
|
|
|
2010-10-11 18:54:42 -03:00
|
|
|
RunningLights(j); // (in Functions.pde)
|
2010-09-20 06:38:11 -03:00
|
|
|
// Runnings lights effect to let user know that we are taking mesurements
|
|
|
|
if((i % 5) == 0) j++;
|
|
|
|
if(j >= 3) j = 0;
|
|
|
|
}
|
2010-10-11 18:54:42 -03:00
|
|
|
|
|
|
|
// Switch off all ABC lights
|
|
|
|
LightsOff();
|
|
|
|
|
2010-10-30 05:30:46 -03:00
|
|
|
for(gyro = GYROZ; gyro <= GYROY; gyro++)
|
|
|
|
AN_OFFSET[gyro] = aux_float[gyro]; // Update sensor OFFSETs from values read
|
2010-10-13 13:48:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef UseBMP
|
|
|
|
void read_baro(void)
|
|
|
|
{
|
|
|
|
float tempPresAlt;
|
|
|
|
|
|
|
|
tempPresAlt = float(APM_BMP085.Press)/101325.0;
|
|
|
|
//tempPresAlt = pow(tempPresAlt, 0.190284);
|
|
|
|
//press_alt = (1.0 - tempPresAlt) * 145366.45;
|
|
|
|
tempPresAlt = pow(tempPresAlt, 0.190295);
|
2010-10-30 05:30:46 -03:00
|
|
|
if (press_alt == 0)
|
2010-10-13 13:48:09 -03:00
|
|
|
press_alt = (1.0 - tempPresAlt) * 4433000; // Altitude in cm
|
|
|
|
else
|
2010-11-21 08:56:25 -04:00
|
|
|
press_alt = press_alt * 0.75 + ((1.0 - tempPresAlt) * 4433000)*0.25; // Altitude in cm (filtered)
|
2010-10-13 13:48:09 -03:00
|
|
|
}
|
|
|
|
#endif
|
2010-12-22 09:38:54 -04:00
|
|
|
|
|
|
|
#ifdef IsRANGEFINDER
|
|
|
|
/* This function read IR data, convert to cm units and filter the data */
|
|
|
|
void read_RF_Sensors()
|
|
|
|
{
|
|
|
|
AP_RangeFinder_frontRight.read();
|
|
|
|
AP_RangeFinder_backRight.read();
|
|
|
|
AP_RangeFinder_backLeft.read();
|
|
|
|
AP_RangeFinder_frontLeft.read();
|
|
|
|
|
|
|
|
#ifdef LOG_RANGEFINDER
|
|
|
|
Log_Write_RangeFinder(AP_RangeFinder_frontRight.distance, AP_RangeFinder_backRight.distance, AP_RangeFinder_backLeft.distance,AP_RangeFinder_frontLeft.distance,0,0);
|
|
|
|
#endif // LOG_RANGEFINDER
|
|
|
|
}
|
|
|
|
#endif
|
2010-11-21 08:56:25 -04:00
|
|
|
|