er.h

00001 /*
00002  *  Player - One Hell of a Robot Server
00003  *  Copyright (C) 2000-2003
00004  *     David Feil-Seifer
00005  *                      
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  */
00022 
00023 /*
00024  *
00025  * Relevant constants for the "ER" robots, made by Evolution Robotics.    
00026  *
00027  * Some of this code is borrowed and/or adapted from the player
00028  * module of trogdor; thanks to the author of that module.
00029  */
00030 
00031 #ifndef _ER1_DRIVER_H_
00032 #define _ER1_DRIVER_H_
00033 
00034 #define ER_DEFAULT_PORT "/dev/usb/tts/0"
00035 #define ER_DEFAULT_LEFT_MOTOR 0
00036 #define ER_DELAY_US 10000
00037 
00038 
00039 /* ER1 Commands */
00040 
00041 #define NOOP                0x00
00042 #define SETVEL              0x11
00043 #define UPDATE              0x1A
00044 #define GETCMDPOS           0x1D
00045 #define GETEVENTSTATUS      0x31
00046 #define RESETEVENTSTATUS    0x34
00047 #define RESET               0x39
00048 #define SETMOTORCMD         0x77
00049 #define SETLIMITSWITCHMODE  0x80
00050 #define GETVERSION          0x8F
00051 #define SETACCEL            0x90
00052 #define SETDECEL            0x91
00053 #define SETPROFILEMODE      0xA0
00054 #define SETSTOPMODE         0xD0
00055 #define READANALOG          0xEF
00056 
00057 #define MOTOR_0             0x00
00058 #define MOTOR_1             0x01
00059 
00060 /* robot-specific info */
00061 #define ER_DEFAULT_MOTOR_0_DIR          -1
00062 #define ER_DEFAULT_MOTOR_1_DIR          1
00063 #define ER_DEFAULT_AXLE_LENGTH          .38
00064 
00065 #define ER_MAX_TICKS 3000
00066 #define ER_WHEEL_RADIUS         .055
00067 #define ER_WHEEL_CIRC           .345575197
00068 #define ER_WHEEL_STEP           .45
00069 #define ER_M_PER_TICK           .0000058351 //JM 12/11/06
00070 //#define ER_M_PER_TICK         .00000602836879 // NdT 1/4/06
00071 //#define ER_M_PER_TICK           .000005700      // THC 1/23/06
00072 
00073 /* for safety */
00074 #define ER_MAX_WHEELSPEED       .500
00075 #define ER_MPS_PER_TICK         1
00076 
00077 #define FULL_STOP       0
00078 #define STOP            1
00079 
00080 
00081 #include <libplayercore/player.h>
00082 #include <libplayercore/driver.h>
00083 #include <libplayercore/drivertable.h>
00084 
00085 
00086 typedef struct
00087 {
00088   player_position2d_data_t position;
00089 } __attribute__ ((packed)) player_er1_data_t;
00090 
00091 
00092 
00093 class ER : public Driver 
00094 {
00095   private:
00096     player_er1_data_t er1_data;
00097     
00098     player_devaddr_t position_id;
00099     int position_subscriptions;
00100 
00101   public:
00102     ER(ConfigFile* cf, int section);
00103 
00104     // public, so that it can be called from pthread cleanup function
00105     int SetVelocity(double lvel, double rvel);
00106     void Stop( int StopMode );
00107     virtual void Main();
00108     virtual int Setup();
00109     virtual int Shutdown();
00110     //void HandleConfig(void);
00111     //void GetCommand(void);
00112     //void PutData(void);
00113     
00114     // MessageHandler
00115     virtual int ProcessMessage(QueuePointer & resp_queue,
00116                                player_msghdr * hdr,
00117                                void * data);
00118     //void HandlePositionCommand(player_position2d_cmd_t position_cmd);
00119     void Test();
00120         
00121   private:
00122     // this function will be run in a separate thread
00123     int InitOdom();
00124     int InitRobot();
00125     
00126     //serial connection
00127     int *_tc_num;
00128     int _fd; // device file descriptor
00129     const char* _serial_port; // name of dev file
00130     bool _fd_blocking;
00131     
00132     // methods for internal use
00133     int WriteBuf(unsigned char* s, size_t len);
00134     int ReadBuf(unsigned char* s, size_t len);
00135     int SendCommand(unsigned char * cmd, int cmd_len, unsigned char * ret_val, int ret_len);
00136     int checksum_ok (unsigned char *buf, int len);
00137 
00138     int ComputeTickDiff(int from, int to);
00139     int ChangeMotorState(int state);
00140     int BytesToInt32(unsigned char *ptr);
00141     float BytesToFloat(unsigned char *ptr);
00142     void UpdateOdom(int ltics, int rtics);
00143     void SpeedCommand( unsigned char address, double speed, int dir );
00144     void SetOdometry (long,long,long);
00145     void ResetOdometry();
00146     //periodic functions
00147     int GetOdom(int *ltics, int *rtics);
00148     int GetBatteryVoltage(int* voltage);
00149     int GetRangeSensor( int s, float * val );
00150     void MotorSpeed();
00151 
00152     //er values
00153     double _axle_length;
00154     int _motor_0_dir;
00155     int _motor_1_dir;
00156 
00157     //internal info
00158     bool _debug;
00159     bool _need_to_set_speed;
00160     bool _odom_initialized;
00161     bool _stopped;
00162     int _last_ltics, _last_rtics;
00163     double _px, _py, _pa;  // integrated odometric pose (m,m,rad)
00164     int _powered, _resting; // If _powered is false, no constant updates. _resting means the last update was a 0,0 one.
00165     
00166     int send_command( unsigned char address, unsigned char c, int ret_num, unsigned char * ret );
00167     int send_command_2_arg( unsigned char address, unsigned char c, int arg, int ret_num, unsigned char * ret );
00168     int send_command_4_arg( unsigned char address, unsigned char c, int arg, int ret_num, unsigned char * ret );
00169 
00170 };
00171 #endif
00172 

Last updated 12 September 2005 21:38:45