/* sharp.c */ /* es - Input/Output general purpose board. {{{ * * Copyright (C) 2006 Dufour Jérémy * * Robot APB Team/Efrei 2004. * Web: http://assos.efrei.fr/robot/ * Email: robot AT efrei DOT fr * * 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 2 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, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * }}} */ #include "sharp.h" #include "modules/adc/adc.h" #include "modules/utils/utils.h" /** Array of sharp values. */ uint16_t sharp_values[SHARP_NUMBER]; /** Thresholds */ uint16_t sharp_threshold_high_[SHARP_NUMBER], sharp_threshold_low_[SHARP_NUMBER]; uint8_t sharp_old_state[SHARP_NUMBER]; /** Init this module. */ void sharp_init (void) { uint8_t compt; /* Init ADC module */ adc_init (); /* All pins are on the right direction, nothing to do */ for (compt = 0; compt < SHARP_NUMBER; compt++) { sharp_threshold_high_[compt] = 5000; sharp_threshold_low_[compt] = 4000; } } /** Read data from sharp sensors. */ void sharp_update_values (uint8_t sharp_num) { /* Check sharp exists (prevent kernel panic) */ if (sharp_num < SHARP_NUMBER) { /* Start capture */ adc_start (sharp_num + 1); /* Wait ADC to finish the sensor */ while (!adc_checkf ()) ; /* Wait a little */ utils_delay_us (250); /* Get values */ sharp_values[sharp_num] = adc_read (); } } /** Analyse a sharp value. */ int8_t sharp_analyse_values (uint8_t sharp_num) { /* Check sharp exists (prevent kernel panic) */ if (sharp_num < SHARP_NUMBER) { // XXX Check more often first if (sharp_values[sharp_num] < sharp_threshold_low_[sharp_num]) { sharp_old_state[sharp_num] = SHARP_CLEAR; return SHARP_CLEAR; } if (sharp_values[sharp_num] > sharp_threshold_high_[sharp_num]) { sharp_old_state[sharp_num] = SHARP_OBSTACLE; return SHARP_OBSTACLE; } /* If we are here, we are inside the threshold, look at previous value */ return sharp_old_state[sharp_num]; } return -1; } /** Configure sharp threshold. */ void sharp_config_threshold (uint8_t sharp_num, uint16_t high, uint16_t low) { /* Check sharp exists (prevent kernel panic) */ if (sharp_num < SHARP_NUMBER) { sharp_threshold_high_[sharp_num] = high; sharp_threshold_low_[sharp_num] = low; } }