00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef _CLODBUSTERDEVICE_H
00032 #define _CLODBUSTERDEVICE_H
00033
00034 #include <pthread.h>
00035 #include <sys/time.h>
00036
00037 #include <driver.h>
00038 #include <drivertable.h>
00039 #include <packet.h>
00040 #include <player.h>
00041
00042
00043
00044
00045
00046
00047
00048
00049 #define CLODBUSTER_CYCLETIME_USEC 50000
00050
00051
00052 #define SYNC 255
00053
00054 #define SET_SERVO_THROTTLE 0
00055 #define SET_SERVO_FRONTSTEER 1
00056 #define SET_SERVO_BACKSTEER 2
00057 #define SET_SERVO_PAN 3
00058
00059 #define ECHO_SERVO_VALUES 64 // 0x40
00060 #define ECHO_MAX_SERVO_LIMITS 65 //0x41
00061 #define ECHO_MIN_SERVO_LIMITS 66 //0x42
00062 #define ECHO_CEN_SERVO_LIMITS 67 //0x43
00063
00064 #define ECHO_ENCODER_COUNTS 112 // 0x70
00065 #define ECHO_ENCODER_COUNTS_TS 113 // 0x71
00066 #define ECHO_ADC 128 //0x80
00067 #define READ_ID 97
00068
00069 #define SET_SLEEP_MODE 144 // 0x90
00070 #define ECHO_SLEEP_MODE 145 // 0x91
00071
00072 #define SLEEP_MODE_ON 1
00073 #define SLEEP_MODE_OFF 0
00074
00075 #define SERVO_CHANNELS 8
00076
00077
00078
00079 #define ARGINT 0x3B // Positive int (LSB, MSB)
00080 #define ARGNINT 0x1B // Negative int (LSB, MSB)
00081 #define ARGSTR 0x2B // String (Note: 1st byte is length!!)
00082
00083 #define CLODBUSTER_CONFIG_BUFFER_SIZE 256
00084
00085 #define DEFAULT_CLODBUSTER_PORT "/dev/ttyUSB0" // This has to be USB - serial port.
00086
00087 typedef struct clodbuster_encoder_data
00088 {
00089 uint32_t time_count;
00090 int32_t left,right;
00091 } __attribute__ ((packed)) clodbuster_encoder_data_t;
00092
00093 class PIDGains
00094 {
00095 private:
00096 float kp, ki, kd, freq, k1, k2, k3;
00097 void findK()
00098 {
00099 float T=1.0/freq;
00100 k1 = kp + .5*T*ki + kd/T;
00101 k2 = -kp - 2.0*kd/T + .5*ki*T;
00102 k3 = kd/T;
00103 printf("Gain constants set to K1 = %f, K2 = %f, K3 = %f\n",k1,k2,k3);
00104 };
00105 public:
00106 PIDGains(float kp_, float ki_, float kd_, float freq_)
00107 :kp(kp_), ki(ki_), kd(kd_),freq(freq_)
00108 {
00109 findK();
00110 };
00111
00112 void SetKp(float k)
00113 {
00114 kp=k;
00115 findK();
00116 };
00117 void SetKi(float k)
00118 {
00119 ki=k;
00120 findK();
00121 };
00122 void SetKd(float k)
00123 {
00124 kd=k;
00125 findK();
00126 };
00127 void SetFreq(float f)
00128 {
00129 freq=f;
00130 findK();
00131 };
00132 float K1(){return(k1);};
00133 float K2(){return(k2);};
00134 float K3(){return(k3);};
00135 };
00136
00137 class ClodBuster:public Driver
00138 {
00139 private:
00140 player_position_data_t position_data;
00141 void ResetRawPositions();
00142 clodbuster_encoder_data_t ReadEncoders();
00143
00144 int clodbuster_fd;
00145
00146
00147 char clodbuster_serial_port[MAX_FILENAME_SIZE];
00148
00149 int kp,ki,kd;
00150
00151
00152 bool initdone;
00153 clodbuster_encoder_data_t encoder_offset;
00154 clodbuster_encoder_data_t encoder_measurement;
00155 clodbuster_encoder_data_t old_encoder_measurement;
00156 float EncV, EncOmega, EncVleft, EncVright;
00157
00158 bool direct_command_control;
00159 unsigned char max_limits[SERVO_CHANNELS],center_limits[SERVO_CHANNELS],min_limits[SERVO_CHANNELS];
00160 void GetGraspBoardParams();
00161
00162
00163 float WheelRadius;
00164 float WheelBase;
00165 float WheelSeparation;
00166 unsigned int CountsPerRev;
00167 float Kenc;
00168
00169
00170 float LoopFreq;
00171 PIDGains *Kv, *Kw;
00172
00173 void IntegrateEncoders();
00174 void DifferenceEncoders();
00175
00176 protected:
00177
00178
00179 int motor_max_speed;
00180 int motor_max_turnspeed;
00181
00182
00183 bool use_vel_band;
00184
00185
00186 public:
00187
00188 ClodBuster( ConfigFile* cf, int section);
00189 virtual ~ClodBuster();
00190
00191
00192 virtual void Main();
00193
00194 virtual int Setup();
00195 virtual int Shutdown();
00196
00197 unsigned char SetServo(unsigned char chan, int value);
00198 void SetServo(unsigned char chan, unsigned char cmd);
00199
00200
00201
00202
00203
00204
00205
00206 };
00207
00208
00209 #endif