/*AP_RangeFinder_SRF04::AP_RangeFinder_SRF04(const AP_HAL::GPIO *gpio):_gpio(gpio) { }*/ AP_RangeFinder_SRF04::AP_RangeFinder_SRF04(AP_HAL::DigitalSource *trigger,AP_HAL::DigitalSource *echo, AP_HAL::Scheduler* sched): _trigger(trigger),_echo(echo),_sched(sched) { max_distance = 3000; min_distance = 10; setAveraging(10); begin(); }
/// Begin variables /// - int _distancePin: number indicating the distance to an object: ANALOG IN /// When you use begin() without parameters standard values are loaded: A0 void AnalogDistanceSensor::begin(int distancePin) { pinMode(distancePin, INPUT); _distancePin=distancePin; setAveraging(1); //1: all samples passed to higher level setARefVoltage(5); // 5: default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards) //setARefVoltage(3); // external analog reference: for 3.3V: put a wire between the AREF pin and the 3.3V VCC pin. //This increases accuracy (and uses a different LUT) }
/// <summary> /// Begin variables /// - int _distancePin: number indicating the distance to an object: ANALOG IN /// - int _transferFunctionLUT3V: Transfer function Lookup Table (for 3.3V reference value) /// - int _transferFunctionLUT5V: Transfer function Lookup Table (for 5V reference value) /// When you use begin() without variables standard values are loaded: A0 /// </summary> void DistanceGP2Y0A21YK::begin(int distancePin) { pinMode(distancePin, INPUT); _distancePin=distancePin; setAveraging(100); //1: all samples passed to higher level setARefVoltage(5); // 5 default situation //setARefVoltage(3); // 3.3V: put a wire between the AREF pin and the 3.3V VCC pin. //This increases accuracy (and uses a different LUT) }
/// <summary> /// Begin variables /// - int trigPin: pin used to activate the sensor /// - int echoPin: pin used to read the reflection /// </summary> void AP_RangeFinder_SRF04::begin(int echoPin, int trigPin) { //_gpio->channel(trigPin+54); // _gpio->chanel(echoPin+54); _trigPin=trigPin; _echoPin=echoPin; //pinMode(_trigPin, OUTPUT); //pinMode(_echoPin, INPUT); setAveraging(1); //1: all samples passed to higher level }
/* Initialize stuff: * - Start Vref module * - Clear all fail flags * - Internal reference (default: external vcc) * - Mux between a and b channels (b channels) * - Calibrate with 32 averages and low speed * - When first calibration is done it sets: * - Resolution (default: 10 bits) * - Conversion speed and sampling time (both set to medium speed) * - Averaging (set to 4) */ void ADC_Module::analog_init() { // default settings: /* - 10 bits resolution - 4 averages - vcc reference - no interrupts - pga gain=1 - conversion speed = medium - sampling speed = medium initiate to 0 (or 1) so the corresponding functions change it to the correct value */ analog_res_bits = 0; analog_max_val = 0; analog_num_average = 0; analog_reference_internal = 1; var_enableInterrupts = 0; pga_value = 1; conversion_speed = 0; sampling_speed = 0; // the first calibration will use 32 averages and lowest speed, // when this calibration is over the averages and speed will be set to default by wait_for_cal and init_calib will be cleared. init_calib = 1; fail_flag = ADC_ERROR_CLEAR; // clear all errors // Internal reference initialization VREF_TRM = VREF_TRM_CHOPEN | 0x20; // enable module and set the trimmer to medium (max=0x3F=63) VREF_SC = VREF_SC_VREFEN | VREF_SC_REGEN | VREF_SC_ICOMPEN | VREF_SC_MODE_LV(1); // (=0xE1) enable 1.2 volt ref with all compensations // select b channels *ADC_CFG2_muxsel = 1; // set reference to vcc/external setReference(ADC_REF_EXTERNAL); // set resolution to 10 setResolution(10); // calibrate at low speed and max repetitions setAveraging(32); setConversionSpeed(ADC_LOW_SPEED); setSamplingSpeed(ADC_LOW_SPEED); // begin init calibration calibrate(); }
/* Initialize stuff: * - Start Vref module * - Clear all fail flags * - Internal reference (default: external vcc) * - Mux between a and b channels (b channels) * - Calibrate with 32 averages and low speed * - When first calibration is done it sets: * - Resolution (default: 10 bits) * - Conversion speed and sampling time (both set to medium speed) * - Averaging (set to 4) */ void ADC_Module::analog_init() { // default settings: /* - 10 bits resolution - 4 averages - vcc reference - no interrupts - pga gain=1 - conversion speed = medium - sampling speed = medium initiate to 0 (or 1) so the corresponding functions change it to the correct value */ analog_res_bits = 0; analog_max_val = 0; analog_num_average = 0; analog_reference_internal = 2; var_enableInterrupts = 0; pga_value = 1; conversion_speed = 0; sampling_speed = 0; calibrating = 0; fail_flag = ADC_ERROR_CLEAR; // clear all errors num_measurements = 0; // select b channels // *ADC_CFG2_muxsel = 1; setBit(ADC_CFG2, ADC_CFG2_MUXSEL_BIT); // set reference to vcc setReference(ADC_REF_3V3); // set resolution to 10 setResolution(10); // the first calibration will use 32 averages and lowest speed, // when this calibration is over the averages and speed will be set to default by wait_for_cal and init_calib will be cleared. init_calib = 1; setAveraging(32); setConversionSpeed(ADC_LOW_SPEED); setSamplingSpeed(ADC_LOW_SPEED); // begin init calibration calibrate(); }
/* Waits until calibration is finished and writes the corresponding registers * */ void ADC_Module::wait_for_cal(void) { uint16_t sum; while(*ADC_SC3_cal) { // Bit ADC_SC3_CAL in register ADC0_SC3 cleared when calib. finishes. } if(*ADC_SC3_calf) { // calibration failed fail_flag |= ADC_ERROR_CALIB; // the user should know and recalibrate manually } __disable_irq(); if (calibrating) { sum = *ADC_CLPS + *ADC_CLP4 + *ADC_CLP3 + *ADC_CLP2 + *ADC_CLP1 + *ADC_CLP0; sum = (sum / 2) | 0x8000; *ADC_PG = sum; sum = *ADC_CLMS + *ADC_CLM4 + *ADC_CLM3 + *ADC_CLM2 + *ADC_CLM1 + *ADC_CLM0; sum = (sum / 2) | 0x8000; *ADC_MG = sum; calibrating = 0; } __enable_irq(); // the first calibration uses 32 averages and lowest speed, // when this calibration is over, set the averages and speed to default. if(init_calib) { // set conversion speed to medium setConversionSpeed(ADC_MED_SPEED); // set sampling speed to medium setSamplingSpeed(ADC_MED_SPEED); // number of averages to 4 setAveraging(4); init_calib = 0; // clear } }
/* Sets up all initial configurations and starts calibration * */ void ADC::analog_init(uint32_t config) { VREF_TRM = 0x60; VREF_SC = 0xE1; // enable 1.2 volt ref if (analog_reference_internal) { ADC0_SC2 |= ADC_SC2_REFSEL(1); // 1.2V ref } else { ADC0_SC2 |= ADC_SC2_REFSEL(0); // vcc/ext ref } // set resolution setResolution(10); // number of averages setAveraging(analog_num_average); // begin calibration calibrate(); }
/// begin variables /// - int sleepPin: number indicating to which pin the sleep port is attached. DIGITAL OUT /// - int selfTestPin: number indicating to which pin the selftest port is attached. DIGITAL OUT /// - int zeroGPin: number indicating to which pin the ZeroGpin is connected to. DIGITAL IN /// - int gSelectPin: number indication to which pin the Gselect is connected to. DIGITAL OUT /// - int xPin: number indicating to which pin the x-axis pin is connected to. ANALOG IN /// - int yPin: number indicating to which pin the y-axis pin is connected to. ANALOG IN /// - int zPin: number indicating to which pin the z-axis pin is connected to. ANALOG IN /// - int offset: array indicating the G offset on the x,y and z-axis /// When you use begin() without variables standard values are loaded: A0,A1,A2 as input for X,Y,Z and digital pins 13,12,11,10 for sleep, selftest, zeroG and gSelect void AcceleroMMA7361::begin(int sleepPin, int selfTestPin, int zeroGPin, int gSelectPin, int xPin, int yPin, int zPin) { pinMode(sleepPin, OUTPUT); pinMode(selfTestPin, OUTPUT); pinMode(zeroGPin, INPUT); pinMode(gSelectPin, OUTPUT); pinMode(xPin, INPUT); pinMode(yPin, INPUT); pinMode(zPin, INPUT); digitalWrite(sleepPin,HIGH); digitalWrite(selfTestPin,LOW); _sleepPin = sleepPin; _selfTestPin = selfTestPin; _zeroGPin = zeroGPin; _gSelectPin = gSelectPin; _xPin = xPin; _yPin = yPin; _zPin = zPin; _sleep = false; setOffSets(0,0,0); setARefVoltage(5); setAveraging(10); setSensitivity(HIGH); }