| Tekkotsu Homepage | Demos | Overview | Downloads | Dev. Resources | Reference | Credits |
WorldState.hGo to the documentation of this file.00001 //-*-c++-*- 00002 #ifndef INCLUDED_WorldState_h 00003 #define INCLUDED_WorldState_h 00004 00005 #ifdef PLATFORM_APERIOS 00006 #include "IPC/ProcessID.h" 00007 #include <OPENR/core_macro.h> 00008 #include <OPENR/ObjcommTypes.h> 00009 #include <OPENR/OPENR.h> 00010 #include <OPENR/OPENRAPI.h> 00011 #include <OPENR/OPENRMessages.h> 00012 #else 00013 class PostureEngine; 00014 #endif 00015 00016 #include "Shared/RobotInfo.h" 00017 #include "Events/EventBase.h" 00018 #include <math.h> 00019 #include <vector> 00020 00021 class EventRouter; 00022 class EventBase; 00023 00024 //The following SourceIDs are for events created by WorldState's event generators 00025 00026 //! holds source ID types for sensor events; see EventBase, see #SensorSourceID_t 00027 namespace SensorSourceID { 00028 //! holds source ID types for sensor events 00029 /*! May want to add a proximity alarm for IR distance? Probably 00030 * should do it from a separate generator to avoid screwing up 00031 * behaviors relying on the current setup 00032 */ 00033 enum SensorSourceID_t { 00034 UpdatedSID //!< sends status event as last event after processing a frame 00035 }; 00036 } 00037 00038 //! holds source ID types for power events; see EventBase, see #PowerSourceID_t 00039 namespace PowerSourceID { 00040 //! holds source ID types for power events 00041 /*! Also serve as offsets into WorldState::powerFlags[]. 00042 * 00043 * I've never seen a lot of these events thrown by the OS. 'NS' 00044 * means never-seen, which could simply be because i haven't put it 00045 * in that situation (don't have a station-type power charger) or 00046 * because the OS doesn't actually support sending that flag. 00047 * 00048 * Under normal conditions, you'll see MotorPowerSID, 00049 * BatteryConnectSID, DischargingSID, and PowerGoodSID always 00050 * active with occasional VibrationSID and UpdateSID. When the 00051 * chest button is pushed, PauseSID is activated and MotorPowerSID 00052 * is deactivated. 00053 * 00054 * The BatteryMonitorBehavior will give a warning once power begins 00055 * getting low. The OS won't boot off a battery with less than 15% 00056 * power remaining (which is when the LowPowerWarnSID is thrown) 00057 * 00058 * @note there's not a one-to-one correspondance of the events from 00059 * the OPENR power system... i map several OPENR events to fewer 00060 * Tekkotsu events, check the event's name if you want to know the 00061 * specific source (say if low battery is low current and/or low 00062 * voltage) Status ETIDS are only generated when one of a related 00063 * group goes on/off but others are still active 00064 */ 00065 enum PowerSourceID_t { 00066 PauseSID=0, //!< the chest button was pushed (this is not a normal button, it kills power to the motors in hardware) 00067 MotorPowerSID, //!< active while the motors have power 00068 VibrationSID, //!< triggered when the OS decides a large acceleration has occured, like falling down (or more specifically, hitting the ground afterward) 00069 BatteryEmptySID, //!< battery is dead 00070 LowPowerWarnSID, //!< triggered when sensors[PowerRemainOffset] <= 0.15 (PowerGoodSID stays on) 00071 BatteryFullSID, //!< battery is full 00072 ExternalPowerSID, //!< receiving power from an external source (such as AC cable, may or may not include the "station", see StationConnectSID) 00073 ExternalPortSID, //!< an external power source is plugged in (does not imply current is flowing however) 00074 BatteryConnectSID, //!< a battery is plugged in 00075 BatteryInitSID, //!< ? NS 00076 DischargingSID, //!< using power from the battery (although still stays on after hooked up to external power) 00077 ChargingSID, //!< you used to be able to charge while running, tho that has changed in more recent versions of OPEN-R. In any case, I never saw this even when it did work. 00078 OverheatingSID, //!< in case the robot starts getting too hot NS 00079 PowerGoodSID, //!< there is power, either from external or battery 00080 ChargerStatusSID, //!< ? NS 00081 SuspendedSID, //!< ? NS 00082 OverChargedSID, //!< in case the charger screws up somehow (?) NS 00083 TermDischargeSID, //!< end of battery (?) NS 00084 TermChargeSID, //!< end of charging (?) NS 00085 ErrorSID, //!< general power error NS 00086 StationConnectSID, //!< connected to a station NS 00087 BatteryOverCurrentSID, //!< similar to OverChargedSID (?) NS 00088 DataFromStationSID, //!< ? NS 00089 RegisterUpdateSID, //!< ? NS 00090 RTCSID, //!< ? NS 00091 SpecialModeSID, //!< ? NS 00092 BMNDebugModeSID, //!< ? NS 00093 PlungerSID, //!< I think this is in reference to having a memorystick (?) NS 00094 UpdatedSID, //!< sent as last event after processing a frame 00095 NumPowerSIDs 00096 }; 00097 } 00098 00099 class WorldState; 00100 extern WorldState * state; //!< the global state object, is a shared memory region, created by MainObject 00101 00102 //! The state of the robot and its environment 00103 /*! Contains sensor readings, current joint positions, etc. Notable members are: 00104 * - #outputs - joint positions and current LED values 00105 * - #buttons - current button values 00106 * - #sensors - values from other sensors (IR, acceleration, temperature, power levels) 00107 * - #pids - current PID settings for each joint (more specifically, each PID-controlled joint) 00108 * - #pidduties - how hard each of the PID joints is working to get to its target value 00109 * 00110 * Generally you will use enumerated values from a robot-specific namespace to 00111 * index into these arrays. Each of those members' documentation specifies where 00112 * to find the list of indices to use with them. 00113 * 00114 * This is a shared memory region between Main, Motion, and possibly others in the future. 00115 * Be very careful about including structures that use pointers in 00116 * this class... they will only be valid from the OObject that created 00117 * them, others may cause a crash if they try to access them. 00118 * 00119 * WorldState takes power and sensor updates from the system and 00120 * maintains the last known values in its member fields. It throws 00121 * events when some of these values change, listed in the 00122 * SensorSourceID, PowerSourceID namespaces, and the ButtonOffset_t 00123 * enumeration for your robot model's info 00124 * namespace. (e.g. ERS7Info::ButtonOffset_t) 00125 * 00126 * Status events for buttons only generated if the 00127 * WorldState::alwaysGenerateStatus flag is turned on. Otherwise, by 00128 * default, they are only generated when a value has changed. 00129 * (i.e. when the pressure sensitive buttons get a new pressure 00130 * reading) 00131 */ 00132 class WorldState { 00133 public: 00134 //! constructor - sets everything to zeros 00135 WorldState(); 00136 00137 bool alwaysGenerateStatus; //!< controls whether status events are generated for the boolean buttons 00138 00139 float outputs[NumOutputs]; //!< last sensed positions of joints, last commanded value of LEDs; indexes (aka offsets) are defined in the target model's namespace (e.g. "Output Offsets" section of ERS7Info) 00140 float buttons[NumButtons]; //!< magnitude is pressure for some, 0/1 for others; indexes are defined in the ButtonOffset_t of the target model's namespace (e.g. ERS7Info::ButtonOffset_t) 00141 float sensors[NumSensors]; //!< IR, Accel, Thermo, Power stuff; indexes are defined in SensorOffset_t of the target model's namespace (e.g. ERS7Info::SensorOffset_t) 00142 float pids[NumPIDJoints][3]; //!< current PID settings (same ordering as the #outputs), not sensed -- updated by MotionManager whenever it sends a PID setting to the system; note this is only valid for PID joints, has NumPIDJoint entries (as opposed to NumOutputs) 00143 float pidduties[NumPIDJoints]; //!< duty cycles - -1 means the motor is trying to move full power in negative direction, 1 means full power in positive direction, in practice, these values stay rather small - 0.15 is significant force. (same ordering as the #outputs); note this is only valid for PID joints, has NumPIDJoint entries (as opposed to NumOutputs) 00144 00145 float vel_x; //!< the current, egocentric rate of forward locomotion, mm/second 00146 float vel_y; //!< the current, egocentric rate of sideways (leftward is positive) locomotion, mm/second 00147 float vel_a; //!< the current, egocentric rate of rotational (counterclockwise is positive) locomotion, radian/second 00148 unsigned int vel_time; //!< the time at which we began moving along the current velocity vector 00149 00150 unsigned int robotStatus; //!< bitmask, see OPENR/OPower.h 00151 unsigned int batteryStatus; //!< bitmask, see OPENR/OPower.h 00152 unsigned int powerFlags[PowerSourceID::NumPowerSIDs]; //!< bitmasks of similarly-grouped items from previous two masks, corresponds to the PowerSourceID::PowerSourceID_t's 00153 00154 unsigned int button_times[NumButtons]; //!< value is time of current press, 0 if not down 00155 00156 unsigned int lastSensorUpdateTime; //!< primarily so calcDers can determine the time difference between updates, but others might want to know this too... 00157 unsigned int frameNumber; //!< the serial number of the currently available frame 00158 unsigned int framesProcessed; //!< the number of sensor updates which have been processed 00159 00160 static const double g; //!< the gravitational acceleration of objects on earth 00161 static const double IROORDist; //!< If IR returns this, we're out of range 00162 00163 #ifdef PLATFORM_APERIOS 00164 void read(OSensorFrameVectorData& sensor, WorldState* lastState, EventRouter* er); //!< will process a sensor reading as given by OPEN-R 00165 void read(const OPowerStatus& power, EventRouter* er); //!< will process a power status update as given by OPEN-R 00166 00167 //! returns the current WorldState instance for the running process, needed if your code may be in a shared memory region; otherwise you can directly access ::state 00168 /*! Generally you can access ::state directly, but if your code is running as a member of an object in a shared memory region, 00169 * this handles the shared object context switching problem on Aperios, and simply returns ::state on non-Aperios. */ 00170 static WorldState* getCurrent() { return stateLookupMap==NULL ? NULL : stateLookupMap[ProcessID::getID()]; } 00171 00172 // sets the current WorldState instance for the running process (handles the shared object context switching problem on Aperios, simply returns ::state on non-Aperios) 00173 // static void setCurrent(WorldState* st) { stateLookupMap[ProcessID::getID()]=st; } 00174 00175 //! sets the lookup table (held in a shared region, WorldStatePool::stateMap) used by getCurrent under Aperios 00176 static void setWorldStateLookup(WorldState * curMap[ProcessID::NumProcesses]) { stateLookupMap=curMap; } 00177 #else 00178 void read(const PostureEngine& pose, WorldState* lastState, EventRouter* er); //!< will process the posture's output positions as a new sensor reading 00179 00180 //! returns the current WorldState instance for the running process, needed if your code may be in a shared memory region; otherwise you can directly access ::state 00181 /*! Generally you can access ::state directly, but if your code is running as a member of an object in a shared memory region, 00182 * this handles the shared object context switching problem on Aperios, and simply returns ::state on non-Aperios. */ 00183 static WorldState* getCurrent() { return ::state; } 00184 #endif 00185 00186 //! bitmask corresponding to OPENR::GetRobotDesign() 00187 /*! This allows you to efficiently test different combinations, like 00188 * any 2x0 model vs. any 7 model or any 3xx model (when/if the 00189 * 3xx's are supported). 00190 * 00191 * Testing this will give more accurate feedback as to whether 00192 * features exist than checking RobotInfo values - to achieve dual 00193 * booting, RobotInfo may, for instance, tell you there are two 00194 * ears, but if you're running on a 220 the value you set them to 00195 * will be ignored */ 00196 unsigned int robotDesign; 00197 static const unsigned int ERS210Mask=1<<0; //!< use this to test for ERS-210 features 00198 static const unsigned int ERS220Mask=1<<1; //!< use this to test for ERS-220 features 00199 static const unsigned int ERS2xxMask = ERS210Mask | ERS220Mask; //!< use this to test for ERS-2xx features 00200 static const unsigned int ERS7Mask=1<<2; //!< use this to test for ERS-7 features 00201 00202 protected: 00203 unsigned int curtime; //!< set by read(OSensorFrameVectorData& sensor, EventRouter* er) for chkEvent() so each call doesn't have to 00204 00205 //! Tests to see if the button status has changed and post events as needed 00206 void chkEvent(std::vector<EventBase>& evtBuf, unsigned int off, float newval, const char* name); 00207 //! Tests to see if any button statuses have changed and post events as needed, bases comparison between already filled-in WorldStates 00208 void chkEvent(std::vector<EventBase>& evtBuf, WorldState* lastState); 00209 00210 //! sets the names of the flags that will be generating events 00211 /*! note that this function does not actually do the event posting, 00212 * unlike chkEvent() */ 00213 void chkPowerEvent(unsigned int sid, unsigned int cur, unsigned int mask, const char* name, 00214 std::string actname[PowerSourceID::NumPowerSIDs], 00215 std::string dename[PowerSourceID::NumPowerSIDs], 00216 unsigned int summask[PowerSourceID::NumPowerSIDs]) { 00217 if(cur&mask) { 00218 actname[sid]+=name; 00219 summask[sid]|=mask; 00220 } else if(powerFlags[sid]&mask) 00221 dename[sid]+=name; 00222 } 00223 00224 //! given the next value, calculates and stores the next current, the velocity, and the acceleration 00225 /*! @param next the new value that's about to be set 00226 * @param cur the previous value 00227 * @param vel the previous 1st derivative 00228 * @param acc the previous 2nd derivative 00229 * @param invtimediff @f$1/(curtime-prevtime)@f$ in seconds*/ 00230 inline void calcDers(double next, double& cur, double& vel, double& acc, double invtimediff) { 00231 double diff=next-cur; 00232 cur=next; 00233 next=diff*invtimediff;; 00234 diff=next-vel; 00235 vel=next; 00236 acc=diff*invtimediff; 00237 } 00238 00239 #ifdef PLATFORM_APERIOS 00240 static WorldState ** stateLookupMap; //!< array used by getCurrent()/setCurrent() to store the state in use by each process -- should be initialized by setWorldStateLookup() to point into a shared memory region 00241 #endif 00242 00243 private: 00244 WorldState(const WorldState&); //!< this shouldn't be called... 00245 WorldState& operator=(const WorldState&); //!< this shouldn't be called... 00246 }; 00247 00248 /*! @file 00249 * @brief Describes WorldState, maintains information about the robot's environment, namely sensors and power status 00250 * @author ejt (Creator) 00251 * 00252 * $Author: ejt $ 00253 * $Name: tekkotsu-3_0 $ 00254 * $Revision: 1.36 $ 00255 * $State: Exp $ 00256 * $Date: 2006/10/03 22:00:08 $ 00257 */ 00258 00259 #endif |
|
Tekkotsu v3.0 |
Generated Wed Oct 4 00:03:47 2006 by Doxygen 1.4.7 |