00001 #ifndef MRP2_SERIAL_H
00002 #define MRP2_SERIAL_H
00003
00004 #include "serial_comm.h"
00005 #include "usb_comm.h"
00006 #include "time.h"
00007 #include <sys/time.h>
00008 #include <string>
00009 #include <stdint.h>
00010 #include <unistd.h>
00011 #include <vector>
00012
00013 using std::string;
00014
00015 typedef enum {
00016 DIAG_CLEAR_CMD = 0,
00017 DIAG_MOTOR_STALL_L = 1,
00018 DIAG_MOTOR_STALL_R = 2,
00019 DIAG_BATT_LOW = 3,
00020 DIAG_BATT_HIGH = 4,
00021 DIAG_MOTOR_DRVR_ERR = 5,
00022 DIAG_AUX_LIGHTS_ERR = 6
00023 }diag_t;
00024
00025 class MRP2_Serial
00026 {
00027 public:
00028 MRP2_Serial(std::string port_name, uint32_t baudrate = 38400, std::string mode = "8N1", bool simple = true);
00029 MRP2_Serial(uint16_t vendor_id, uint16_t product_id, int ep_in_addr, int ep_out_addr, bool simple = true);
00030 virtual ~MRP2_Serial ();
00031 void set_speeds(int32_t left_speed, int32_t right_speed);
00032 void set_speed_l(int32_t left_speed);
00033 void set_speed_r(int32_t right_speed);
00034 void set_param_pid(char side, char param, float value);
00035 void set_param_imax(char side, uint32_t value);
00036 void set_maxspeed_fwd(uint32_t value);
00037 void set_maxspeed_rev(uint32_t value);
00038 void set_max_accel(uint32_t value);
00039
00040 void set_estop(bool value);
00041 void clear_diag(int diag);
00042 std::vector<int> get_speeds(bool update=false);
00043 int get_speed_l(bool update=false);
00044 int get_speed_r(bool update=false);
00045 float get_param_pid(char side, char param, bool update=false);
00046 std::vector<int> get_param_imax(char side, bool update=false);
00047 int get_maxspeed_fwd(bool update=false);
00048 int get_maxspeed_rev(bool update=false);
00049 int get_maxaccel(bool update=false);
00050
00051 int get_batt_volt(bool update=false);
00052 int get_batt_current(bool update=false);
00053 int get_batt_soc(bool update=false);
00054 std::vector<int> get_positions(bool update=false);
00055 int get_position_l(bool update=false);
00056 int get_position_r(bool update=false);
00057 std::vector<int> get_bumpers(bool update=false);
00058 void reset_positions();
00059 void reset_position_l();
00060 void reset_position_r();
00061 bool get_estop(bool update=false);
00062 bool get_diag(int diag);
00063 void update_diag();
00064 int get_batt_cell_capacity(bool update=false);
00065 void set_bumper_estop(bool value);
00066 bool get_bumper_estop(bool update=false);
00067 bool get_estop_button(bool update=false);
00068 std::vector<int> get_sonars(bool update=false);
00069 bool is_available();
00070 void set_read_timeout(double timeout);
00071 double get_read_timeout(void);
00072
00073 void update();
00074
00075 typedef enum {
00076 setSPEEDS = 1,
00077 setSPEED_L = 2,
00078 setSPEED_R = 3,
00079 setPARAM_KP_L = 4,
00080 setPARAM_KP_R = 5,
00081 setPARAM_KI_L = 6,
00082 setPARAM_KI_R = 7,
00083 setPARAM_KD_L = 8,
00084 setPARAM_KD_R = 9,
00085 setPARAM_IMAX_L = 10,
00086 setPARAM_IMAX_R = 11,
00087 setMAXSPEED_FWD = 12,
00088 setMAXSPEED_REV = 13,
00089 setMAXACCEL = 14,
00090 setBATT_CELL_V = 15,
00091 setBATT_PARALLEL_COUNT = 16,
00092 setBATT_SERIES_COUNT = 17,
00093 setBATT_CELL_NOMINAL_V = 18,
00094 setESTOP = 19,
00095 clearDIAG = 20,
00096 getSPEEDS = 21,
00097 getSPEED_L = 22,
00098 getSPEED_R = 23,
00099 getPARAM_KP_L = 24,
00100 getPARAM_KP_R = 25,
00101 getPARAM_KI_L = 26,
00102 getPARAM_KI_R = 27,
00103 getPARAM_KD_L = 28,
00104 getPARAM_KD_R = 29,
00105 getPARAM_IMAX_L = 30,
00106 getPARAM_IMAX_R = 31,
00107 getMAXSPEED_FWD = 32,
00108 getMAXSPEED_REV = 33,
00109 getMAXACCEL = 34,
00110 getBATT_CELL_V = 35,
00111 getBATT_PARALLEL_COUNT = 37,
00112 getBATT_SERIES_COUNT = 38,
00113 getBATT_CELL_NOMINAL_V = 39,
00114 getBATT_VOLT = 40,
00115 getBATT_CURRENT = 41,
00116 getBATT_SOC = 42,
00117 getPOSITIONS = 43,
00118 getPOSITION_L = 44,
00119 getPOSITION_R = 45,
00120 getBUMPERS = 46,
00121 resetPOSITIONS = 47,
00122 resetPOSITION_L = 48,
00123 resetPOSITION_R = 49,
00124 getESTOP = 50,
00125 getDIAG = 51,
00126 ACK = 52,
00127 getBATT_CELL_CAPACITY=53,
00128 setBUMPER_ESTOP=54,
00129 getBUMPER_ESTOP=55,
00130 getESTOP_BTN=56,
00131 getSONARS=83
00132 }serial_t;
00133
00134 private:
00135 void array_chopper(uint8_t *buf, int start, int end);
00136 unsigned char checksum(int size);
00137 bool checksum_match(uint8_t *buf, int size);
00138 unsigned char checksum_check_array(uint8_t *arr, int size);
00139 int first_validator(uint8_t *buf);
00140 int second_validator(uint8_t *buf, int data_len);
00141 int find_message_start(uint8_t *buf, int lastIndex);
00142 int execute_command(uint8_t *buf);
00143 void print_array(uint8_t *buf, int length);
00144 int send_and_get_reply(uint8_t _command, uint8_t *send_array, int send_size, bool is_ack);
00145 int read_serial(uint8_t _command_to_read);
00146 int process(uint8_t *inData, int recievedData, uint8_t _command_to_read);
00147 int process_simple (uint8_t *inData, int recievedData, uint8_t _command_to_read);
00148 bool _get_ack(serial_t command);
00149
00150 int _speed_l, _speed_r, _imax_l, _imax_r, _maxspeed_fwd, _maxspeed_rev, _maxaccel, _batt_volt, _batt_current, _batt_soc, _batt_cell_capacity, _bumper_estop, _estop_btn;
00151 bool _estop, _diag_motor_stall_l, _diag_motor_stall_r, _diag_batt_low, _diag_batt_high, _diag_motor_drvr_err, _diag_aux_lights_err;
00152 double _Kp_l, _Ki_l, _Kd_l, _Kp_r, _Ki_r, _Kd_r;
00153 int _position_l, _position_r;
00154 std::vector<int> _bumpers, _positions, _speeds, _imax, _sonars;
00155
00156 int speeds[2];
00157 char sendArray[20];
00158 bool e_stop;
00159 bool dir_left;
00160 bool dir_right;
00161
00162 int _port_nr;
00163 int _baudrate;
00164 std::string _port_name, _mode;
00165
00166
00167 uint8_t tempData[10000];
00168 uint8_t tempDataIndex;
00169
00170 bool seekForChar;
00171 char startChar;
00172 uint8_t _ack_data;
00173
00174 double Kp,Ki,Kd,Kol;
00175 double read_timeout_;
00176
00177 milvus::SerialComm serial_port;
00178 milvus::UsbComm usb_port;
00179
00180 uint16_t vendor_id_, product_id_;
00181 int ep_in_addr_, ep_out_addr_;
00182
00183 bool use_usb_;
00184
00185 bool line_ok_;
00186
00187 bool simple_;
00188 };
00189
00190 #endif