00001 #ifndef RFlex_H
00002 #define RFlex_H
00003
00004
00005 #include <string>
00006 #include <pthread.h>
00007 #include <SerialPort.H>
00008
00009 #include <Angle.H>
00010
00011 #define MAX_SONAR 18
00012 #define RFLEX_B21R 0
00013 #define RFLEX_ATRVJR 1
00014
00015 class RFlex {
00016 public:
00017 RFlex(const std::string &filename, const std::string &robotType);
00018 ~RFlex();
00019
00020
00021 bool systemUpdate();
00022 bool systemLCDDump();
00023
00024
00025 bool motionSetDefaults();
00026
00027
00028 bool setBrake();
00029 bool releaseBrake();
00030
00031
00032 void motorSetTranslateVelocity(const double velocity);
00033 void motorSetRotateVelocity(const double velocity);
00034 void motorSetVelocity(const double translate, const double rotate);
00035
00036
00037 bool odometryOn(const int period = 100);
00038 bool odometryOff();
00039
00040
00041 bool digitalIOOn(const int period = 100);
00042 bool digitalIOOff();
00043
00044
00045 bool joystickReport();
00046
00047
00048 bool sonarOn();
00049 bool sonarOff();
00050 int getNumSonars(){ return m_numSonar;}
00051 double getSonar(int i) { if(i<m_numSonar){ return m_sonars[i]; } else { return 0; }}
00052
00053
00054 float batteryVoltage() {return m_battery_voltage;}
00055
00056
00057 bool brakeOn() {return m_brake;}
00058 bool brakeOff() {return !brakeOn();}
00059
00060
00061 double x() {return lockedValue(m_x, m_motor_mutex);}
00062 double y() {return lockedValue(m_y, m_motor_mutex);}
00063 double heading() {return lockedValue(m_theta, m_motor_mutex);}
00064
00065
00066 double translateVelocity() {return lockedValue(m_trans_velocity, m_motor_mutex);}
00067 double translateAcceleration() {return lockedValue(m_trans_acceleration, m_motor_mutex);}
00068 double translateTorque() {return lockedValue(m_trans_torque, m_motor_mutex);}
00069
00070 double rotateVelocity() {return lockedValue(m_rot_velocity, m_motor_mutex);}
00071 double rotateAcceleration() {return lockedValue(m_rot_acceleration, m_motor_mutex);}
00072 double rotateTorque() {return lockedValue(m_rot_torque, m_motor_mutex);}
00073
00074 void zero(){m_x=0; m_y=0; m_theta = 0; m_last_distance_set=false; m_last_angdistance_set=false;}
00075
00076 private:
00077 SerialPort m_port;
00078
00082 unsigned char m_type;
00083
00084
00085 double m_magicDistance;
00086 double m_magicAngle;
00087 double m_magicTorque;
00088
00089 double m_sonars[MAX_SONAR];
00090 int m_numSonar;
00091
00092
00093 static const int s_buffer_size = 1024;
00094 bool m_found;
00095 int m_offset;
00096 unsigned char m_read_buffer[s_buffer_size];
00097 unsigned char m_write_buffer[s_buffer_size];
00098
00099
00100 pthread_mutex_t m_write_mutex;
00101 pthread_mutex_t m_motor_mutex;
00102 pthread_mutex_t m_command_mutex;
00103
00104
00105 pthread_t m_read_thread;
00106 pthread_t m_periodic_thread;
00107
00108
00109 double m_translate_command;
00110 double m_rotate_command;
00111
00112
00113 float m_battery_voltage;
00114 bool m_brake;
00115
00116
00117 double m_x;
00118 double m_y;
00119 Angle m_theta;
00120
00121 double m_trans_velocity;
00122 double m_trans_acceleration;
00123 double m_trans_torque;
00124
00125 double m_rot_velocity;
00126 double m_rot_acceleration;
00127 double m_rot_torque;
00128
00129 long m_last_distance;
00130 long m_last_angdistance;
00131 bool m_last_distance_set;
00132 bool m_last_angdistance_set;
00133
00134 bool motorSendTranslateVelocity(const double velocity);
00135 bool motorSendRotateVelocity(const double velocity);
00136
00137 bool sendCommand(const unsigned char port, const unsigned char id, const unsigned char opcode, const int length);
00138 bool writePacket(const int size) const;
00139
00140 void readPacket();
00141 int readData();
00142
00143 void parseSystemReport();
00144 void parseMotorReport();
00145 void parseJoystickReport();
00146 void parseSonarReport();
00147 void parseDigitalIOReport();
00148 void parseIRReport();
00149
00150 unsigned char convertChar(const int offset) const;
00151 unsigned short convertShort(const int offset) const;
00152 unsigned long convertLong(const int offset) const;
00153 void insertChar(const unsigned char value, const int offset);
00154 void insertLong(const long value, const int offset);
00155
00156 static unsigned char computeCRC(const unsigned char *buffer, const int n);
00157
00158 static void *rFlexReadThread(void *ptr);
00159 static void *rFlexPeriodicThread(void *ptr);
00160
00161 template <typename T> T lockedValue(T &value, pthread_mutex_t &mutex) {
00162 pthread_mutex_lock(&mutex);
00163 T retval = value;
00164 pthread_mutex_unlock(&mutex);
00165 return retval;
00166 }
00167
00168
00169 RFlex(const RFlex &rflex);
00170 RFlex &operator=(const RFlex &rflex);
00171 };
00172
00173
00174 #endif