00001 /** 00002 \file Robots/LoBot/irccm/LoBumps.c 00003 \brief Low-level reactions for bump sensors. 00004 00005 This file defines the functions that implement the bump sensor action 00006 and pending API for the low-level Robolocust control program meant to 00007 be run on the iRobot Create's Command Module. 00008 */ 00009 00010 /* 00011 ************************************************************************ 00012 * The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 * 00013 * by the University of Southern California (USC) and the iLab at USC. * 00014 * See http://iLab.usc.edu for information about this project. * 00015 * * 00016 * Major portions of the iLab Neuromorphic Vision Toolkit are protected * 00017 * under the U.S. patent ``Computation of Intrinsic Perceptual Saliency * 00018 * in Visual Environments, and Applications'' by Christof Koch and * 00019 * Laurent Itti, California Institute of Technology, 2001 (patent * 00020 * pending; application number 09/912,225 filed July 23, 2001; see * 00021 * http://pair.uspto.gov/cgi-bin/final/home.pl for current status). * 00022 ************************************************************************ 00023 * This file is part of the iLab Neuromorphic Vision C++ Toolkit. * 00024 * * 00025 * The iLab Neuromorphic Vision C++ Toolkit is free software; you can * 00026 * redistribute it and/or modify it under the terms of the GNU General * 00027 * Public License as published by the Free Software Foundation; either * 00028 * version 2 of the License, or (at your option) any later version. * 00029 * * 00030 * The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope * 00031 * that it will be useful, but WITHOUT ANY WARRANTY; without even the * 00032 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR * 00033 * PURPOSE. See the GNU General Public License for more details. * 00034 * * 00035 * You should have received a copy of the GNU General Public License * 00036 * along with the iLab Neuromorphic Vision C++ Toolkit; if not, write * 00037 * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * 00038 * Boston, MA 02111-1307 USA. * 00039 ************************************************************************ 00040 */ 00041 00042 /* 00043 Primary maintainer for this file: mviswana usc edu 00044 $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/LoBot/irccm/LoBumps.c $ 00045 $Id: LoBumps.c 13768 2010-08-07 23:41:41Z mviswana $ 00046 */ 00047 00048 /*------------------------------ HEADERS ------------------------------*/ 00049 00050 // lobot headers 00051 #include "LoBumps.h" 00052 #include "LoDrive.h" 00053 #include "LoSensors.h" 00054 #include "LoIO.h" 00055 #include "LoUtils.h" 00056 #include "LoCMInterface.h" 00057 #include "LoOpenInterface.h" 00058 00059 /*------------------------------ GLOBALS ------------------------------*/ 00060 00061 // The data that accompanies an ACK_BUMPS message is stored in this 00062 // array. 00063 // 00064 // DEVNOTE: Instead of using a separate variable to indicate that an 00065 // ACK_BUMPS is pending, we simply use the bit flags for the bump sensors 00066 // (stored in the first element of this array) as the pending flag. If 00067 // none of the bump sensors was active, it means no action was taken and, 00068 // hence, no acknowledgement is pending. 00069 static char g_bumps[LOBOT_BUMPS_SIZE] ; 00070 00071 /*--------------------- REACTING TO BUMP SENSORS ----------------------*/ 00072 00073 // This function retrieves the bump sensor state from the LoSensors 00074 // module and takes appropriate action. 00075 // 00076 // DEVNOTE: This function does not check to see if sensor data is 00077 // available before retrieving the bump sensor state. The main program 00078 // should take care of that, i.e., check that sensor data is actually 00079 // available before calling this function. We do it like this because 00080 // there are other low-level sensor reaction modules and all of them 00081 // would have to keep checking the same flag over and over again. Much 00082 // nicer if the main program checks the sensor data availability flag 00083 // once and then calls all the sensor reaction functions one-by-one. 00084 void lo_bumps(void) 00085 { 00086 char bumps = lo_get_sensor(LOBOT_SENSORS_BUMPS) ; 00087 g_bumps[LOBOT_BUMPS_FLAGS] = 00088 (bumps & LOBOT_OI_BUMP_BOTH) | // front bumpers 00089 ((bumps & LOBOT_BUMP_REAR_BOTH) >> 3) ; // rear bumpers 00090 00091 bumps = g_bumps[LOBOT_BUMPS_FLAGS] ; 00092 00093 int backup = 0, spin = 0 ; 00094 if ((bumps & LOBOT_OI_BUMP_BOTH) == LOBOT_OI_BUMP_BOTH) 00095 { 00096 backup = -300 ; 00097 spin = -30 ; 00098 } 00099 else if (bumps & LOBOT_OI_BUMP_LEFT) 00100 { 00101 backup = -150 ; 00102 spin = -15 ; 00103 } 00104 else if (bumps & LOBOT_OI_BUMP_RIGHT) 00105 { 00106 backup = -150 ; 00107 spin = 15 ; 00108 } 00109 else if ((bumps & LOBOT_BUMP_ACK_REAR_BOTH) == LOBOT_BUMP_ACK_REAR_BOTH) 00110 { 00111 backup = 25 ; // NOTE: +ve backup for rear bumper ==> move forward 00112 spin = 20 ; 00113 } 00114 else if (bumps & LOBOT_BUMP_ACK_REAR_LEFT) 00115 { 00116 backup = 75 ; // NOTE: +ve backup for rear bumper ==> move forward 00117 spin = 10 ; 00118 } 00119 else if (bumps & LOBOT_BUMP_ACK_REAR_RIGHT) 00120 { 00121 backup = 75 ; // NOTE: +ve backup for rear bumper ==> move forward 00122 spin = -10 ; 00123 } 00124 00125 if (bumps & LOBOT_OI_BUMP_EITHER) // front bump ==> first backup, then spin 00126 { 00127 backup = lo_backup(-225, backup) ; 00128 spin = lo_spin(150, spin) ; 00129 lo_update_odometry(backup, spin) ; 00130 } 00131 else if (bumps & LOBOT_BUMP_ACK_REAR_EITHER) // rear bump 00132 { 00133 if (lo_rear_bumps_spin()) // configured for spinning and moving up 00134 { 00135 spin = lo_spin(150, spin) ; 00136 backup = lo_backup(225, backup) ; 00137 lo_update_odometry(backup, spin) ; 00138 } 00139 else // rear bump configured only to stop 00140 lo_stop_immediate() ; 00141 } 00142 00143 g_bumps[LOBOT_BUMPS_DISTANCE_HI] = lo_hibyte(backup) ; 00144 g_bumps[LOBOT_BUMPS_DISTANCE_LO] = lo_lobyte(backup) ; 00145 g_bumps[LOBOT_BUMPS_ANGLE_HI] = lo_hibyte(spin) ; 00146 g_bumps[LOBOT_BUMPS_ANGLE_LO] = lo_lobyte(spin) ; 00147 } 00148 00149 /*------------------- BUMP SENSOR ACKNOWLEDGEMENTS --------------------*/ 00150 00151 // Check if bump sensor acknowledgement is pending so that main loop can 00152 // send out the ACK the next time it switches to talking to the high 00153 // level via the Command Module's USB port. 00154 char lo_bumps_pending(void) 00155 { 00156 return g_bumps[LOBOT_BUMPS_FLAGS] ; 00157 } 00158 00159 // Send pending bump sensor ACK to the high level 00160 void lo_send_bumps(void) 00161 { 00162 lo_tx(LOBOT_ACK_BUMPS) ; 00163 for (unsigned char i = 0; i < LOBOT_BUMPS_SIZE; ++i) 00164 lo_tx(g_bumps[i]) ; 00165 00166 // Sensor data is no longer pending after it has been sent to the high 00167 // level. 00168 g_bumps[LOBOT_BUMPS_FLAGS] = 0 ; 00169 }